Coverage details for com.martiansoftware.nailgun.AliasManager

LineHitsSource
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      */
432    public AliasManager() {
442        aliases = new java.util.HashMap();
45         
46         try {
472            Properties props = new Properties();
482            props.load(getClass().getClassLoader().getResourceAsStream("com/martiansoftware/nailgun/builtins/builtins.properties"));
492            loadFromProperties(props);
500        } catch (java.io.IOException e) {
510            System.err.println("Unable to load builtins.properties: " + e.getMessage());
522        }
532    }
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) {
722        for (Iterator i = properties.keySet().iterator(); i.hasNext();) {
7320            String key = (String) i.next();
7420            if (!key.endsWith(".desc")) {
75                 try {
7610                    Class clazz = Class.forName(properties.getProperty(key));
7710                    String desc = properties.getProperty(key + ".desc", "");
7810                    addAlias(new Alias(key, desc, clazz));
790                } catch (ClassNotFoundException e) {
800                    System.err.println("Unable to locate class " + properties.getProperty(key));
8130                }
82             }
83         }
842    }
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) {
9210        synchronized (aliases) {
9310            aliases.put(alias.getName(), alias);
9410        }
9510    }
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() {
1042        Set result = new java.util.TreeSet();
1052        synchronized(aliases) {
1062            result.addAll(aliases.values());
1072        }
1082        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) {
1175        synchronized (aliases) {
1185            aliases.remove(aliasName);
1195        }
1205    }
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) {
12910        return ((Alias) aliases.get(aliasName));
130     }
131  
132 }

this report was generated by version 1.0.5 of jcoverage.
visit www.jcoverage.com for updates.

copyright © 2003, jcoverage ltd. all rights reserved.
Java is a trademark of Sun Microsystems, Inc. in the United States and other countries.