1ef39d40dSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3ef39d40dSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4ef39d40dSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5ef39d40dSAndrew Rist  * distributed with this work for additional information
6ef39d40dSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7ef39d40dSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8ef39d40dSAndrew Rist  * "License"); you may not use this file except in compliance
9ef39d40dSAndrew Rist  * with the License.  You may obtain a copy of the License at
10ef39d40dSAndrew Rist  *
11ef39d40dSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12ef39d40dSAndrew Rist  *
13ef39d40dSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14ef39d40dSAndrew Rist  * software distributed under the License is distributed on an
15ef39d40dSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16ef39d40dSAndrew Rist  * KIND, either express or implied.  See the License for the
17ef39d40dSAndrew Rist  * specific language governing permissions and limitations
18ef39d40dSAndrew Rist  * under the License.
19ef39d40dSAndrew Rist  *
20ef39d40dSAndrew Rist  *************************************************************/
21ef39d40dSAndrew Rist 
22ef39d40dSAndrew Rist 
23cdf0e10cSrcweir package stats;
24cdf0e10cSrcweir 
25cdf0e10cSrcweir import share.LogWriter;
26cdf0e10cSrcweir import java.util.Hashtable;
27cdf0e10cSrcweir import util.DynamicClassLoader;
28cdf0e10cSrcweir 
29cdf0e10cSrcweir /**
30cdf0e10cSrcweir  * A factory class for creating out producers.
31cdf0e10cSrcweir  */
32cdf0e10cSrcweir public class OutProducerFactory {
33cdf0e10cSrcweir 
34cdf0e10cSrcweir     /**
35cdf0e10cSrcweir      * Create an out producer. The type that is created depends on the
36cdf0e10cSrcweir      * parameters given. These are:
37cdf0e10cSrcweir      * <ul>
38cdf0e10cSrcweir      *   <li>DataBaseOut - If set to true, a database outproducer is created.
39cdf0e10cSrcweir      *   <li>OutProducer - The value of this parameter names the class that is created.
40cdf0e10cSrcweir      * </ul>
41*e6b649b5SPedro Giffuni      * @param param Parameters of the test.
42cdf0e10cSrcweir      * @return The created out producer.
43cdf0e10cSrcweir      */
createOutProducer(Hashtable param)44cdf0e10cSrcweir     public static LogWriter createOutProducer(Hashtable param) {
45cdf0e10cSrcweir         LogWriter dbOut = null;
46cdf0e10cSrcweir         boolean getDatabase = convertToBool(param.get("DataBaseOut"));
47cdf0e10cSrcweir         if (getDatabase) {
48cdf0e10cSrcweir             dbOut = createDataBaseOutProducer(param);
49cdf0e10cSrcweir         }
50cdf0e10cSrcweir         if (dbOut == null) {
51cdf0e10cSrcweir             DynamicClassLoader dcl = new DynamicClassLoader();
52cdf0e10cSrcweir             String outProducerName = (String)param.get("OutProducer");
53cdf0e10cSrcweir             if (outProducerName != null) {
54cdf0e10cSrcweir                 try {
55cdf0e10cSrcweir                     dbOut = (LogWriter)dcl.getInstance(outProducerName);
56cdf0e10cSrcweir                 }
57cdf0e10cSrcweir                 catch(IllegalArgumentException e) {
58cdf0e10cSrcweir                     e.printStackTrace();
59cdf0e10cSrcweir                 }
60cdf0e10cSrcweir             }
61cdf0e10cSrcweir         }
62cdf0e10cSrcweir         if (dbOut == null) {
63cdf0e10cSrcweir             dbOut = createSimpleOutProducer();
64cdf0e10cSrcweir         }
65cdf0e10cSrcweir         return dbOut;
66cdf0e10cSrcweir     }
67cdf0e10cSrcweir 
68cdf0e10cSrcweir     /**
69cdf0e10cSrcweir      * Create a databbase out producer.
70cdf0e10cSrcweir      * @param The test parameters
71cdf0e10cSrcweir      * @return The database out producer, or null if it couldn't be created.
72cdf0e10cSrcweir      */
createDataBaseOutProducer(Hashtable param)73cdf0e10cSrcweir     public static LogWriter createDataBaseOutProducer(Hashtable param) {
74cdf0e10cSrcweir         String dataProducerName = (String)param.get("DataBaseOutProducer");
75cdf0e10cSrcweir         if (dataProducerName == null) {
76cdf0e10cSrcweir             String testBaseName = (String)param.get("TestBase");
77cdf0e10cSrcweir             dataProducerName = testBaseName.substring(testBaseName.indexOf("_")+1);
78cdf0e10cSrcweir             dataProducerName = "stats." + makeFirstCharUpperCase(dataProducerName)
79cdf0e10cSrcweir                             + "DataBaseOutProducer";
80cdf0e10cSrcweir         }
81cdf0e10cSrcweir         DynamicClassLoader dcl = new DynamicClassLoader();
82cdf0e10cSrcweir         LogWriter dbOut = null;
83cdf0e10cSrcweir         try {
84cdf0e10cSrcweir             dbOut = (LogWriter)dcl.getInstance(dataProducerName,
85cdf0e10cSrcweir                 new Class[]{new Hashtable().getClass()}, new Object[]{param});
86cdf0e10cSrcweir         }
87cdf0e10cSrcweir         catch(IllegalArgumentException e) {
88cdf0e10cSrcweir             e.printStackTrace();
89cdf0e10cSrcweir         }
90cdf0e10cSrcweir         return dbOut;
91cdf0e10cSrcweir     }
92cdf0e10cSrcweir 
93cdf0e10cSrcweir     /**
94cdf0e10cSrcweir      * As a fallback, create a simple out producer, if all else failed.
95cdf0e10cSrcweir      * @return A simple out producer, writing to the screen.
96cdf0e10cSrcweir      */
createSimpleOutProducer()97cdf0e10cSrcweir     public static LogWriter createSimpleOutProducer() {
98cdf0e10cSrcweir         return new SimpleOutProducer();
99cdf0e10cSrcweir     }
100cdf0e10cSrcweir 
convertToBool(Object val)101cdf0e10cSrcweir     private static boolean convertToBool(Object val) {
102cdf0e10cSrcweir         if(val != null) {
103cdf0e10cSrcweir             if ( val instanceof String ) {
104cdf0e10cSrcweir                 String sVal = (String)val;
105cdf0e10cSrcweir                 if ( sVal.equalsIgnoreCase("true") || sVal.equalsIgnoreCase("yes") ) {
106cdf0e10cSrcweir                     return true;
107cdf0e10cSrcweir                 }
108cdf0e10cSrcweir 
109cdf0e10cSrcweir             }
110cdf0e10cSrcweir             else if (val instanceof Boolean) {
111cdf0e10cSrcweir                 return ((Boolean)val).booleanValue();
112cdf0e10cSrcweir             }
113cdf0e10cSrcweir         }
114cdf0e10cSrcweir         return false;
115cdf0e10cSrcweir     }
116cdf0e10cSrcweir 
117cdf0e10cSrcweir     /**
118cdf0e10cSrcweir      * Make the first character to an upper case char.
119cdf0e10cSrcweir      * @param name The String to change
120cdf0e10cSrcweir      * @return The String with an upper case first char.
121cdf0e10cSrcweir      */
makeFirstCharUpperCase(String name)122cdf0e10cSrcweir     private static String makeFirstCharUpperCase(String name) {
123cdf0e10cSrcweir         return name.substring(0,1).toUpperCase() + name.substring(1);
124cdf0e10cSrcweir     }
125cdf0e10cSrcweir 
126cdf0e10cSrcweir /*    public static void main(String[] args) {
127cdf0e10cSrcweir         Hashtable p = new Hashtable();
128cdf0e10cSrcweir         p.put("DataBaseOut", "yes");
129cdf0e10cSrcweir         p.put("TestBase", "java_complex");
130cdf0e10cSrcweir         p.put("Version", "srx645gggg");
131cdf0e10cSrcweir         createDataBaseOutProducer(p);
132cdf0e10cSrcweir     } */
133cdf0e10cSrcweir }
134