KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > martiansoftware > nailgun > AliasManager


1 /*
2
3   Copyright 2004, Martian Software, Inc.
4
5   Licensed under the Apache License, Version 2.0 (the "License");
6   you may not use this file except in compliance with the License.
7   You may obtain a copy of the License at
8
9   http://www.apache.org/licenses/LICENSE-2.0
10
11   Unless required by applicable law or agreed to in writing, software
12   distributed under the License is distributed on an "AS IS" BASIS,
13   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   See the License for the specific language governing permissions and
15   limitations under the License.
16
17 */

18
19 package com.martiansoftware.nailgun;
20
21 import java.util.Iterator JavaDoc;
22 import java.util.Map JavaDoc;
23 import java.util.Properties JavaDoc;
24 import java.util.Set JavaDoc;
25
26 /**
27  * An AliasManager is used to store and lookup command Aliases by name.
28  * See <a HREF="Alias.html">Alias</a> for more details.
29  *
30  * @author <a HREF="http://www.martiansoftware.com/contact.html">Marty Lamb</a>
31  */

32 public class AliasManager {
33     
34     /**
35      * actual alias storage
36      */

37     private Map JavaDoc aliases;
38     
39     /**
40      * Creates a new AliasManager, populating it with
41      * default Aliases.
42      */

43     public AliasManager() {
44         aliases = new java.util.HashMap JavaDoc();
45         
46         try {
47             Properties JavaDoc props = new Properties JavaDoc();
48             props.load(getClass().getClassLoader().getResourceAsStream("com/martiansoftware/nailgun/builtins/builtins.properties"));
49             loadFromProperties(props);
50         } catch (java.io.IOException JavaDoc e) {
51             System.err.println("Unable to load builtins.properties: " + e.getMessage());
52         }
53     }
54     
55     /**
56      * Loads Aliases from a java.util.Properties file located at the
57      * specified URL. The properties must be of the form:
58      * <pre><code>[alias name]=[fully qualified classname]</code></pre>
59      * each of which may have an optional
60      * <pre><code>[alias name].desc=[alias description]</code></pre>
61      *
62      * For example, to create an alias called "<code>myprog</code>" for
63      * class <code>com.mydomain.myapp.MyProg</code>, the following properties
64      * would be defined:
65      *
66      * <pre><code>myprog=com.mydomain.myapp.MyProg
67      *myprog.desc=Runs my program.
68      * </code></pre>
69      * @param properties the Properties to load.
70      */

71     public void loadFromProperties(java.util.Properties JavaDoc properties) {
72         for (Iterator JavaDoc i = properties.keySet().iterator(); i.hasNext();) {
73             String JavaDoc key = (String JavaDoc) i.next();
74             if (!key.endsWith(".desc")) {
75                 try {
76                     Class JavaDoc clazz = Class.forName(properties.getProperty(key));
77                     String JavaDoc desc = properties.getProperty(key + ".desc", "");
78                     addAlias(new Alias(key, desc, clazz));
79                 } catch (ClassNotFoundException JavaDoc e) {
80                     System.err.println("Unable to locate class " + properties.getProperty(key));
81                 }
82             }
83         }
84     }
85     
86     /**
87      * Adds an Alias, replacing any previous entries with the
88      * same name.
89      * @param alias the Alias to add
90      */

91     public void addAlias(Alias alias) {
92         synchronized (aliases) {
93             aliases.put(alias.getName(), alias);
94         }
95     }
96     
97     /**
98      * Returns a Set that is a snapshot of the Alias list.
99      * Modifications to this Set will not impact the AliasManager
100      * in any way.
101      * @return a Set that is a snapshot of the Alias list.
102      */

103     public Set JavaDoc getAliases() {
104         Set JavaDoc result = new java.util.TreeSet JavaDoc();
105         synchronized(aliases) {
106             result.addAll(aliases.values());
107         }
108         return (result);
109     }
110
111     /**
112      * Removes the Alias with the specified name from the AliasManager.
113      * If no such Alias exists in this AliasManager, this method has no effect.
114      * @param aliasName the name of the Alias to remove
115      */

116     public void removeAlias(String JavaDoc aliasName) {
117         synchronized (aliases) {
118             aliases.remove(aliasName);
119         }
120     }
121
122     /**
123      * Returns the Alias with the specified name
124      * @param aliasName the name of the Alias to retrieve
125      * @return the requested Alias, or null if no such Alias
126      * is defined in this AliasManager.
127      */

128     public Alias getAlias(String JavaDoc aliasName) {
129         return ((Alias) aliases.get(aliasName));
130     }
131
132 }
133
Popular Tags