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