001: /*
002:
003: Copyright 2004, Martian Software, Inc.
004:
005: Licensed under the Apache License, Version 2.0 (the "License");
006: you may not use this file except in compliance with the License.
007: You may obtain a copy of the License at
008:
009: http://www.apache.org/licenses/LICENSE-2.0
010:
011: Unless required by applicable law or agreed to in writing, software
012: distributed under the License is distributed on an "AS IS" BASIS,
013: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: See the License for the specific language governing permissions and
015: limitations under the License.
016:
017: */
018:
019: package com.martiansoftware.nailgun;
020:
021: import java.util.Iterator;
022: import java.util.Map;
023: import java.util.Properties;
024: import java.util.Set;
025:
026: /**
027: * An AliasManager is used to store and lookup command Aliases by name.
028: * See <a href="Alias.html">Alias</a> for more details.
029: *
030: * @author <a href="http://www.martiansoftware.com/contact.html">Marty Lamb</a>
031: */
032: public class AliasManager {
033:
034: /**
035: * actual alias storage
036: */
037: private Map aliases;
038:
039: /**
040: * Creates a new AliasManager, populating it with
041: * default Aliases.
042: */
043: public AliasManager() {
044: aliases = new java.util.HashMap();
045:
046: try {
047: Properties props = new Properties();
048: props
049: .load(getClass()
050: .getClassLoader()
051: .getResourceAsStream(
052: "com/martiansoftware/nailgun/builtins/builtins.properties"));
053: loadFromProperties(props);
054: } catch (java.io.IOException e) {
055: System.err.println("Unable to load builtins.properties: "
056: + e.getMessage());
057: }
058: }
059:
060: /**
061: * Loads Aliases from a java.util.Properties file located at the
062: * specified URL. The properties must be of the form:
063: * <pre><code>[alias name]=[fully qualified classname]</code></pre>
064: * each of which may have an optional
065: * <pre><code>[alias name].desc=[alias description]</code></pre>
066: *
067: * For example, to create an alias called "<code>myprog</code>" for
068: * class <code>com.mydomain.myapp.MyProg</code>, the following properties
069: * would be defined:
070: *
071: * <pre><code>myprog=com.mydomain.myapp.MyProg
072: *myprog.desc=Runs my program.
073: * </code></pre>
074: * @param properties the Properties to load.
075: */
076: public void loadFromProperties(java.util.Properties properties) {
077: for (Iterator i = properties.keySet().iterator(); i.hasNext();) {
078: String key = (String) i.next();
079: if (!key.endsWith(".desc")) {
080: try {
081: Class clazz = Class.forName(properties
082: .getProperty(key));
083: String desc = properties.getProperty(key + ".desc",
084: "");
085: addAlias(new Alias(key, desc, clazz));
086: } catch (ClassNotFoundException e) {
087: System.err.println("Unable to locate class "
088: + properties.getProperty(key));
089: }
090: }
091: }
092: }
093:
094: /**
095: * Adds an Alias, replacing any previous entries with the
096: * same name.
097: * @param alias the Alias to add
098: */
099: public void addAlias(Alias alias) {
100: synchronized (aliases) {
101: aliases.put(alias.getName(), alias);
102: }
103: }
104:
105: /**
106: * Returns a Set that is a snapshot of the Alias list.
107: * Modifications to this Set will not impact the AliasManager
108: * in any way.
109: * @return a Set that is a snapshot of the Alias list.
110: */
111: public Set getAliases() {
112: Set result = new java.util.TreeSet();
113: synchronized (aliases) {
114: result.addAll(aliases.values());
115: }
116: return (result);
117: }
118:
119: /**
120: * Removes the Alias with the specified name from the AliasManager.
121: * If no such Alias exists in this AliasManager, this method has no effect.
122: * @param aliasName the name of the Alias to remove
123: */
124: public void removeAlias(String aliasName) {
125: synchronized (aliases) {
126: aliases.remove(aliasName);
127: }
128: }
129:
130: /**
131: * Returns the Alias with the specified name
132: * @param aliasName the name of the Alias to retrieve
133: * @return the requested Alias, or null if no such Alias
134: * is defined in this AliasManager.
135: */
136: public Alias getAlias(String aliasName) {
137: return ((Alias) aliases.get(aliasName));
138: }
139:
140: }
|