Line | Hits | Source |
---|---|---|
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; | |
22 | import java.util.Map; | |
23 | import java.util.Properties; | |
24 | import java.util.Set; | |
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 aliases; | |
38 | ||
39 | /** | |
40 | * Creates a new AliasManager, populating it with | |
41 | * default Aliases. | |
42 | */ | |
43 | 2 | public AliasManager() { |
44 | 2 | aliases = new java.util.HashMap(); |
45 | ||
46 | try { | |
47 | 2 | Properties props = new Properties(); |
48 | 2 | props.load(getClass().getClassLoader().getResourceAsStream("com/martiansoftware/nailgun/builtins/builtins.properties")); |
49 | 2 | loadFromProperties(props); |
50 | 0 | } catch (java.io.IOException e) { |
51 | 0 | System.err.println("Unable to load builtins.properties: " + e.getMessage()); |
52 | 2 | } |
53 | 2 | } |
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 properties) { | |
72 | 2 | for (Iterator i = properties.keySet().iterator(); i.hasNext();) { |
73 | 20 | String key = (String) i.next(); |
74 | 20 | if (!key.endsWith(".desc")) { |
75 | try { | |
76 | 10 | Class clazz = Class.forName(properties.getProperty(key)); |
77 | 10 | String desc = properties.getProperty(key + ".desc", ""); |
78 | 10 | addAlias(new Alias(key, desc, clazz)); |
79 | 0 | } catch (ClassNotFoundException e) { |
80 | 0 | System.err.println("Unable to locate class " + properties.getProperty(key)); |
81 | 30 | } |
82 | } | |
83 | } | |
84 | 2 | } |
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 | 10 | synchronized (aliases) { |
93 | 10 | aliases.put(alias.getName(), alias); |
94 | 10 | } |
95 | 10 | } |
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 getAliases() { | |
104 | 2 | Set result = new java.util.TreeSet(); |
105 | 2 | synchronized(aliases) { |
106 | 2 | result.addAll(aliases.values()); |
107 | 2 | } |
108 | 2 | 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 aliasName) { | |
117 | 5 | synchronized (aliases) { |
118 | 5 | aliases.remove(aliasName); |
119 | 5 | } |
120 | 5 | } |
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 aliasName) { | |
129 | 10 | return ((Alias) aliases.get(aliasName)); |
130 | } | |
131 | ||
132 | } |
this report was generated by version 1.0.5 of jcoverage. |
copyright © 2003, jcoverage ltd. all rights reserved. |