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

COVERAGE SUMMARY FOR SOURCE FILE [PropertiesConfigParser.java]

nameclass, %method, %block, %line, %
PropertiesConfigParser.java100% (1/1)100% (7/7)100% (209/209)100% (42/42)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class PropertiesConfigParser100% (1/1)100% (7/7)100% (209/209)100% (42/42)
PropertiesConfigParser (): void 100% (1/1)100% (8/8)100% (3/3)
extractParamName (String, String): String 100% (1/1)100% (11/11)100% (2/2)
getProperties (String): InstanceDef [] 100% (1/1)100% (142/142)100% (22/22)
getRunnerDefs (): InstanceDef [] 100% (1/1)100% (4/4)100% (1/1)
getTranslatorDefs (): InstanceDef [] 100% (1/1)100% (4/4)100% (1/1)
load (InputStream): void 100% (1/1)100% (26/26)100% (9/9)
load (Properties): void 100% (1/1)100% (14/14)100% (4/4)

1/*
2 * @(#) $Id: PropertiesConfigParser.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.SlugInitializationException;
35import java.io.IOException;
36import java.io.InputStream;
37import java.util.HashMap;
38import java.util.Iterator;
39import java.util.List;
40import java.util.LinkedList;
41import java.util.Map;
42import java.util.Properties;
43import java.util.StringTokenizer;
44 
45/**
46 * Property configuration parser.
47 *
48 * Loads and instantiates all <code>ISlugRunner</code> runners from a properties
49 * file.
50 * <dl>The file format is as follows:
51 * <dt>slug.runners</dt> <dd>property defining a comma separated list of runner
52 * names. Each <code>runner's name</code> is a <code>token</code> composed by
53 * alphanumeric characters</dd>
54 * <dt>slug.runner[<code>runner's name</code>].impl</dt> <dd>defiens the
55 * <code>ISlugRunner</code> implementation class name for a <code>runner's name</code>
56 * defined in <code>slug.runners</code> property</dd>
57 * <dt>slug.runner[<code>runner's name</code>].param[<code>param name</code>]</dt>
58 * <dd>defines name/value pair parameter to the <code>ISlugRunner</code> implementation
59 * initialization. The name is used as provided by the <code>param name</code> and
60 * property's value is passed as a value for it. Zero or many parameters may be
61 * defined for each <code>ISlugRunner</code> implementation instance</dd>
62 * <dt>slug.translatorsr</dt> <dd>property defining a comma separated list of
63 * translator names. Each <code>runner's name</code> is a <code>token</code> composed by
64 * alphanumeric characters</dd>
65 * <dt>slug.translator[<code>translator's name</code>].impl</dt> <dd>defiens the
66 * <code>IProcessStatusTranslator</code> implementation class name for a
67 * <code>translator's name</code> defined in <code>slug.translators</code> property</dd>
68 * <dt>slug.translator[<code>translator's name</code>].param[<code>param name</code>]</dt>
69 * <dd>defines name/value pair parameter to the <code>IProcessStatusTranslator</code>
70 * implementation initialization. The name is used as provided by the <code>param name</code>
71 * and property's value is passed as a value for it. Zero or many parameters may be
72 * defined for each <code>IProcessStatusTranslator</code> implementation instance</dd>
73* </dl>
74 * Example properties file<br>
75 * <pre>
76 * slug.runners=good,param_test
77 * slug.runner[good].runnerimpl=com.webhydra.slug.process.UnlimitedProcessRunner
78 * slug.runner[param_test].runnerimpl=com.webhydra.slug.SlugRunnerParamTest
79 * slug.runner[param_test].param[p1]=Test Patameter 1
80 * slug.runner[param_test].param[p2]=Test Parameter 2
81 * slug.translators=xml
82 * slug.translator[xml].impl=com.webhydra.slug.process.xml.XmlXsdProcessStatusTranslator
83 * </pre>
84 *
85 * @author rossen
86 */
87public class PropertiesConfigParser implements ISlugConfigParser {
88 
89    /** Properties holder */
90    Map props;
91 
92    /** Creates a new instance of PropertiesConfigParser */
93    public PropertiesConfigParser()
94    {
95        this.props = new HashMap();
96    }
97 
98    /**
99     * Initializes class instance.
100     * @param is Slug's configuration properties input stream.
101     * @exception SlugInitializationException if provided configurataion file is not
102     * accessible or is invald.
103     */
104    public void load(InputStream is) throws SlugInitializationException
105    {
106        if (is == null)
107        {
108            throw new SlugInitializationException("Null input stream supplied!");
109        }
110        Properties props = new Properties();
111        try
112        {
113            props.load(is);
114        }
115        catch (IOException ioe)
116        {
117            throw new SlugInitializationException("Error loading configuration properties:", ioe);
118        }
119        load(props);
120    }
121 
122    /**
123     * Initialize new PropertiesConfigParser instance.
124     * @param props Slug's configuration properties.
125     * @exception  SlugInitializationException if provided properties are null or empty.
126     */
127    protected void load(Properties props) throws SlugInitializationException
128    {
129        if ((props == null) || props.isEmpty())
130        {
131            throw new SlugInitializationException("Null or empty properties provided!");
132        }
133        this.props = props;
134    }
135 
136    /**
137     * Gets all <code>ISlugRunner</code> implementation definitions
138     */
139    public InstanceDef[] getRunnerDefs()
140    {
141        return getProperties("runner");
142    }
143 
144    public InstanceDef[] getTranslatorDefs()
145    {
146        return getProperties("translator");
147    }
148 
149    /**
150     * Gets all <code>IProcessStatusTranslator</code> implementation definitions
151     */
152    private InstanceDef[] getProperties(String type)
153    {
154        List result = new LinkedList();
155        if ((props != null) && !props.isEmpty())
156        {
157            String prefix = "slug." + type;
158            String listKey = prefix + "s";
159            String listVal = (String) props.get(listKey);
160            if ((listVal != null) && (listVal.trim().length() > 0))
161            {
162                StringTokenizer nameTokens = new StringTokenizer(listVal, ", ;", false);
163                while (nameTokens.hasMoreTokens())
164                {
165                    InstanceDef def = new InstanceDef();
166                    def.name = nameTokens.nextToken();
167                    def.className = (String) props.get(prefix + "[" + def.name + "].impl");
168                    String paramPrefix = prefix + "[" + def.name + "].param[";
169                    for (Iterator i = props.keySet().iterator(); i.hasNext();)
170                    {
171                        String key = (String) i.next();
172                        if (key.startsWith(paramPrefix) && key.endsWith("]"))
173                        {
174                            String k = extractParamName(key, paramPrefix);
175                            String v = (String) props.get(key);
176                            def.params.put(k, v);
177                        }
178                    }
179                    result.add(def);
180                }
181            }
182        }
183        return (InstanceDef []) result.toArray(new InstanceDef[result.size()]);
184    }
185 
186    /**
187     * Extract parameter name from an indexed parameters property.
188     * @param key property key
189     * @param preffix runner property name prefix.
190     */
191    protected static String extractParamName(String key, String preffix)
192    {
193        String paramName = key.substring(preffix.length(), key.length() - 1);
194        return paramName;
195    }
196 
197}

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