1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 package stats;
28 
29 import share.LogWriter;
30 import java.util.Hashtable;
31 import util.DynamicClassLoader;
32 
33 /**
34  * A factory class for creating out producers.
35  */
36 public class OutProducerFactory {
37 
38     /**
39      * Create an out producer. The type that is created depends on the
40      * parameters given. These are:
41      * <ul>
42      *   <li>DataBaseOut - If set to true, a database outproducer is created.
43      *   <li>OutProducer - The value of this parameter names the class that is created.
44      * </ul>
45      * @param Parameters of the test.
46      * @return The created out producer.
47      */
48     public static LogWriter createOutProducer(Hashtable param) {
49         LogWriter dbOut = null;
50         boolean getDatabase = convertToBool(param.get("DataBaseOut"));
51         if (getDatabase) {
52             dbOut = createDataBaseOutProducer(param);
53         }
54         if (dbOut == null) {
55             DynamicClassLoader dcl = new DynamicClassLoader();
56             String outProducerName = (String)param.get("OutProducer");
57             if (outProducerName != null) {
58                 try {
59                     dbOut = (LogWriter)dcl.getInstance(outProducerName);
60                 }
61                 catch(IllegalArgumentException e) {
62                     e.printStackTrace();
63                 }
64             }
65         }
66         if (dbOut == null) {
67             dbOut = createSimpleOutProducer();
68         }
69         return dbOut;
70     }
71 
72     /**
73      * Create a databbase out producer.
74      * @param The test parameters
75      * @return The database out producer, or null if it couldn't be created.
76      */
77     public static LogWriter createDataBaseOutProducer(Hashtable param) {
78         String dataProducerName = (String)param.get("DataBaseOutProducer");
79         if (dataProducerName == null) {
80             String testBaseName = (String)param.get("TestBase");
81             dataProducerName = testBaseName.substring(testBaseName.indexOf("_")+1);
82             dataProducerName = "stats." + makeFirstCharUpperCase(dataProducerName)
83                             + "DataBaseOutProducer";
84         }
85         DynamicClassLoader dcl = new DynamicClassLoader();
86         LogWriter dbOut = null;
87         try {
88             dbOut = (LogWriter)dcl.getInstance(dataProducerName,
89                 new Class[]{new Hashtable().getClass()}, new Object[]{param});
90         }
91         catch(IllegalArgumentException e) {
92             e.printStackTrace();
93         }
94         return dbOut;
95     }
96 
97     /**
98      * As a fallback, create a simple out producer, if all else failed.
99      * @return A simple out producer, writing to the screen.
100      */
101     public static LogWriter createSimpleOutProducer() {
102         return new SimpleOutProducer();
103     }
104 
105     private static boolean convertToBool(Object val) {
106         if(val != null) {
107             if ( val instanceof String ) {
108                 String sVal = (String)val;
109                 if ( sVal.equalsIgnoreCase("true") || sVal.equalsIgnoreCase("yes") ) {
110                     return true;
111                 }
112 
113             }
114             else if (val instanceof Boolean) {
115                 return ((Boolean)val).booleanValue();
116             }
117         }
118         return false;
119     }
120 
121     /**
122      * Make the first character to an upper case char.
123      * @param name The String to change
124      * @return The String with an upper case first char.
125      */
126     private static String makeFirstCharUpperCase(String name) {
127         return name.substring(0,1).toUpperCase() + name.substring(1);
128     }
129 
130 /*    public static void main(String[] args) {
131         Hashtable p = new Hashtable();
132         p.put("DataBaseOut", "yes");
133         p.put("TestBase", "java_complex");
134         p.put("Version", "srx645gggg");
135         createDataBaseOutProducer(p);
136     } */
137 }
138