EMMA Coverage Report (generated Mon Mar 20 21:27:43 EST 2006)
[all classes][com.webhydra.slug]

COVERAGE SUMMARY FOR SOURCE FILE [SlugConfig.java]

nameclass, %method, %block, %line, %
SlugConfig.java100% (1/1)100% (16/16)87%  (260/300)92%  (59/64)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class SlugConfig100% (1/1)100% (16/16)87%  (260/300)92%  (59/64)
putInService (String, Map, InstanceDef []): void 100% (1/1)57%  (53/93)67%  (10/15)
<static initializer> 100% (1/1)100% (5/5)100% (1/1)
SlugConfig (): void 100% (1/1)100% (18/18)100% (5/5)
clear (): void 100% (1/1)100% (37/37)100% (8/8)
getDefaultName (String): String 100% (1/1)100% (24/24)100% (4/4)
getDefaultRunnerName (): String 100% (1/1)100% (4/4)100% (1/1)
getDefaultTranslatorName (): String 100% (1/1)100% (4/4)100% (1/1)
getInstance (): SlugConfig 100% (1/1)100% (2/2)100% (1/1)
getMapKeys (Map): String [] 100% (1/1)100% (26/26)100% (6/6)
getRunner (String): ISlugRunner 100% (1/1)100% (6/6)100% (1/1)
getRunnerNames (): String [] 100% (1/1)100% (5/5)100% (1/1)
getTranslator (String): IProcessStatusTranslator 100% (1/1)100% (6/6)100% (1/1)
getTranslatorNames (): String [] 100% (1/1)100% (5/5)100% (1/1)
init (String, InputStream): SlugConfig 100% (1/1)100% (51/51)100% (14/14)
putRunnersInService (InstanceDef []): void 100% (1/1)100% (7/7)100% (2/2)
putTranslatorsInService (InstanceDef []): void 100% (1/1)100% (7/7)100% (2/2)

1/*
2 * @(#) $Id: SlugConfig.java,v 1.1.1.1 2006/03/19 06:09:42 rossen Exp $
3 *
4 * Copyright (c) 2006, WebHydra.com
5 * All rights reserved.
6 * 
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are met:
9 * 
10 *     * Redistributions of source code must retain the above copyright notice,
11 *       this list of conditions and the following disclaimer.
12 *     * Redistributions in binary form must reproduce the above copyright
13 *       notice, this list of conditions and the following disclaimer in the
14 *       documentation and/or other materials provided with the distribution.
15 *     * Neither the name of the WebHydra.com nor the names of its contributors
16 *       may be used to endorse or promote products derived from this software
17 *       without specific prior written permission.
18 * 
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
23 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31 
32package com.webhydra.slug;
33 
34import com.webhydra.slug.process.IProcessStatusTranslator;
35import com.webhydra.slug.process.ISlugService;
36import com.webhydra.slug.SlugInitializationException;
37import com.webhydra.slug.process.ISlugRunner;
38import com.webhydra.slug.process.SluggishProcess;
39import java.io.IOException;
40import java.io.InputStream;
41import java.io.Serializable;
42import java.lang.reflect.Method;
43import java.util.ArrayList;
44import java.util.Enumeration;
45import java.util.HashMap;
46import java.util.Iterator;
47import java.util.LinkedList;
48import java.util.List;
49import java.util.Map;
50import java.util.Properties;
51import java.util.Set;
52import java.util.StringTokenizer;
53import javax.servlet.ServletContext;
54 
55/**
56 * Slug configuration file.
57 * Holds all <code>ISlugRunner</code>s and <code>IProcessStatusTranslator</code>s
58 * and provide access to them.
59 *
60 * @author rossen
61 */
62class SlugConfig implements Serializable
63{
64 
65    private static final long serialVersionUID = 1L;
66 
67    /** Singleton nistnace of SlugConfig */
68    private static SlugConfig inst = new SlugConfig();
69 
70    /** Holds all translators */
71    private Map translators;
72 
73    /**
74     * Holds all ISlugRunner runners
75     */
76    private Map runners;
77 
78    /** Holds the name of the first runner added as a default one */
79    private Map defaultInstances;
80 
81    /** Key for the default runner implementation */
82    private static final String RUNNER = "runner";
83 
84    /** Key for the default translator implementation */
85    private static final String TRANSLATOR = "translator";
86 
87    /** Creates a new instance of SlugConfig
88     */
89    private SlugConfig() {
90        defaultInstances = new HashMap();
91        runners = new HashMap();
92        translators = new HashMap();
93    }
94 
95    /**
96     * Initialize SlugConfig's singleton instance.
97     * This method should be called only once during the <code>Slug</code> servlet initialization.
98     * @param impl ISlugConfigParser implementation class name
99     * @param is Slug's configuration properties input stream.
100     * @exception SlugInitializationException if provided configurataion file is not
101     * accessible or is invald, or if provided implementation cannot be instantiated.
102     * @exception IllegalArgumentException in case of null parameter or if provided
103     * implementation does not implement ISlugConfigParser.
104     */
105    static SlugConfig init(String impl, InputStream is) throws SlugInitializationException, IllegalArgumentException
106    {
107        if (is == null)
108        {
109            throw new IllegalArgumentException("Null input stream supplied!");
110        }
111        if ((impl == null) || (impl.trim().length() == 0))
112        {
113                throw new IllegalArgumentException("ISlugConfigParser implementation null or empty!");
114        }
115        try
116        {
117            // instantiate the ISlugConfigParser implementation
118            Class implCls = Class.forName(impl);
119            ISlugConfigParser cfgParser = (ISlugConfigParser) implCls.newInstance();
120            cfgParser.load(is);
121 
122            // load all runners and translators
123            inst.putRunnersInService(cfgParser.getRunnerDefs());
124            inst.putTranslatorsInService(cfgParser.getTranslatorDefs());
125            return inst;
126        }
127        catch (ClassCastException cce)
128        {
129            throw new IllegalArgumentException(
130                    "Invalid ISlugConfigParser implementation!");
131        }
132        catch (Throwable t)
133        {
134            throw new SlugInitializationException(
135                    "ISlugConfigParser implementation istantiation failed!", t);
136        }
137    }
138 
139    /** Obtain current instance
140     * @return SlugConfig instance.
141     */
142    static SlugConfig getInstance()
143    {
144        return inst;
145    }
146 
147    /**
148     * Initialize and put in service provided <code>ISlugRuner</code> definitions.
149     * This is a simple wrapper arround putInService(String, Map, InstanceDef[])
150     * @param defs instance definitiosns to be installed.
151     * @exception SlugInitializationException in case of instantiation problem.
152     * @see #putInService(String, Map, InstanceDef[])
153     */
154    private void putRunnersInService(InstanceDef[] defs)  throws SlugInitializationException
155    {
156        putInService(RUNNER, runners, defs);
157    }
158 
159    /**
160     * Initialize and put in service provided <code>IProcessStatusTranslator</code> definitions.
161     * This is a simple wrapper arround putInService(String, Map, InstanceDef[])
162     * @param defs instance definitiosns to be installed.
163     * @exception SlugInitializationException in case of instantiation problem.
164     * @see #putInService(String, Map, InstanceDef[])
165     */
166    private void putTranslatorsInService(InstanceDef[] defs)  throws SlugInitializationException
167    {
168        putInService(TRANSLATOR, translators, defs);
169    }
170 
171    /**
172     * Initialize and put in service provided <code>ISlugService</code> definitions.
173     * @param type name of the ISlugService implementations.
174     * @param m container for instantiated instances.
175     * @param defs instance definitiosns to be installed.
176     * @exception SlugInitializationException in case of instantiation problem.
177     */
178    private void putInService(String type, Map m, InstanceDef[] defs) throws SlugInitializationException
179    {
180        if ((defs == null) || (defs.length == 0))
181        {
182            throw new SlugInitializationException("Missing " + type + " definitions!");
183        }
184        for (int i = 0; i < defs.length; i++)
185        {
186            try
187            {
188                ISlugService svc = (ISlugService) Class.forName(defs[i].className).newInstance();
189                svc.init(defs[i].params);
190                svc.prepare();
191                m.put(defs[i].name, svc);
192                if (!defaultInstances.containsKey(type))
193                {
194                    defaultInstances.put(type, defs[i].name);
195                }
196            }
197            catch (SlugInitializationException sie)
198            {
199                throw sie;
200            }
201            catch (Throwable t)
202            {
203                throw new SlugInitializationException("Error instantiationg " +
204                        type + " \"" + defs[i].name + "\": ", t);
205            }
206        }
207    }
208 
209    /**
210     * Retrieve named <code>ISlugRunner</code>
211     *
212     * @param rName name of a runner.
213     * @return <code>ISlugRunner</code> for provided name if instantiated, othewise null.
214     */
215    ISlugRunner getRunner(String rName)
216    {
217        return (ISlugRunner) runners.get(rName);
218    }
219 
220    /**
221     * Retrieve named <code>IProcessStatusTranslator</code>
222     *
223     * @param tName name of a translator.
224     * @return <code>IProcessStatusTranslator</code> for provided name if
225     * instantiated, othewise null.
226     */
227    IProcessStatusTranslator getTranslator(String tName)
228    {
229        return (IProcessStatusTranslator) translators.get(tName);
230    }
231 
232    /**
233     * Obtain default instance name for specified type.
234     * @return the name of the default instance for specified type.
235     */
236    private String getDefaultName(String type) throws SlugInitializationException
237    {
238        String name = (String) defaultInstances.get(type);
239        if (name == null)
240            throw new SlugInitializationException("There are not any " + type +
241                    "s in service yet!");
242        return name;
243    }
244 
245    /**
246     * Getter for the defaultRunnerName property.
247     *
248     * @return the defaultRunnerName property value.
249     * @exception SlugInitializationException if no <code>ISlugRunner/code>s
250     * are put in service yet.
251     */
252    String getDefaultRunnerName() throws SlugInitializationException
253    {
254        return getDefaultName(RUNNER);
255    }
256 
257    /**
258     * Getter for the defaultRunnerName property.
259     *
260     * @return the defaultTranslatorName property value.
261     * @exception SlugInitializationException if no <code>IProcessStatusTranslator/code>s
262     * are put in service yet.
263     */
264    String getDefaultTranslatorName() throws SlugInitializationException
265    {
266        return getDefaultName(TRANSLATOR);
267    }
268 
269    /** Returns an array of all keys for the map.
270     * @param map source Map.
271     * @return String array of map's keys. If provided map is null or empty
272     * array with length 0 is returned.
273     */
274    private String[] getMapKeys(Map m)
275    {
276        String [] result = new String[0];
277        if ((m != null) && !m.isEmpty())
278        {
279            Set keyNames = m.keySet();
280            if ((keyNames != null) && !keyNames.isEmpty())
281            {
282                result = (String []) keyNames.toArray(new String[keyNames.size()]);
283            }
284        }
285        return result;
286    }
287 
288    /**
289     * Retrieve a list of runner names.
290     *
291     * @return a list with the names of all instantiated <code>ISlugRunner</code>s.
292     */
293    String [] getRunnerNames()
294    {
295        return getMapKeys(runners);
296    }
297 
298    /**
299     * Retrieve a list of translator aleiases.
300     *
301     * @return a list with the aliases of all instantiated <code>IProcessStatusTranslator</code>s.
302     */
303    String [] getTranslatorNames()
304    {
305        return getMapKeys(translators);
306    }
307 
308    /** Clear all instantiated runners
309     */
310    synchronized void clear()
311    {
312        // clear runners
313        String [] runnerNames = getRunnerNames();
314        for (int i = 0; i < runnerNames.length; i++)
315        {
316            ISlugRunner r = getRunner(runnerNames[i]);
317            r.clear();
318            runners.remove(runnerNames[i]);
319        }
320        // clear translators
321        translators = new HashMap();
322        // clear the default instances
323        defaultInstances = new HashMap();
324    }
325 
326}

[all classes][com.webhydra.slug]
EMMA 2.0.5312 (C) Vladimir Roubtsov