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 share.DescEntry;
27 
28 import java.util.Hashtable;
29 
30 /**
31  *
32  * @author  sg128468
33  */
34 public abstract class DataBaseOutProducer implements LogWriter {
35     protected Hashtable mSqlInput = null;
36     protected Hashtable mSqlOutput = null;
37     protected String[] mWriteableEntryTypes = null;
38     protected SQLExecution mSqlExec;
39     protected boolean m_bDebug = false;
40 
41 
42     /** Creates a new instance of DataBaseOutProducer
43      * @param param The Hashtable with test parameters
44      */
DataBaseOutProducer(Hashtable param)45     public DataBaseOutProducer(Hashtable param) {
46         mSqlInput = new Hashtable();
47         mSqlInput.putAll(param);
48 
49         Object o = param.get("DebugIsActive");
50         String debug = null;
51         if (o instanceof String)
52             debug = (String)o;
53         else
54             debug = o.toString();
55         if (debug != null && (debug.equalsIgnoreCase("true") || debug.equalsIgnoreCase("yes"))) {
56             m_bDebug = true;
57         }
58         // set default for writeable entries: method
59         setWriteableEntryTypes(new String[]{"method"});
60     }
61 
62     /** initialization
63      *
64      */
initialize(DescEntry entry, boolean active)65     public boolean initialize(DescEntry entry, boolean active) {
66         if (entry.UserDefinedParams != null)
67             mSqlInput.putAll(entry.UserDefinedParams);
68 
69         String jdbcClass = (String)mSqlInput.get("JDBC");
70         if (jdbcClass == null)
71             jdbcClass = "org.gjt.mm.mysql.Driver";
72         String dbURL = (String)mSqlInput.get("DataBaseURL");
73         String user = (String)mSqlInput.get("User");
74         String password = (String)mSqlInput.get("Password");
75         if (user == null)
76             user = (String)mSqlInput.get("OperatingSystem");
77         if (password == null)
78             password = user;
79 
80         mSqlExec = new SQLExecution(jdbcClass, dbURL, user, password, m_bDebug);
81         mSqlExec.openConnection();
82         prepareDataBase(entry.Logger);
83         return true;
84     }
85 
86     /**
87      *
88      *
89      */
summary(DescEntry entry)90     public boolean summary(DescEntry entry) {
91         mSqlExec.openConnection();
92         findTypeInEntryTree(entry, entry.Logger);
93 //        checkDataBase(entry.Logger);
94         mSqlExec.closeConnection();
95         return true;
96     }
97 
98     /**
99      * Step recursively through the entry tree: write all entries of the
100      * defined types to the database.
101      * @param entry The description entry that is take as root
102      * @param log The log writer
103      */
findTypeInEntryTree(DescEntry entry, LogWriter log)104     protected boolean findTypeInEntryTree(DescEntry entry, LogWriter log) {
105         boolean returnVal = true;
106         if (isWriteableEntryType(entry)) {
107             returnVal &= insertEntry(entry, log);
108         }
109 
110         if (entry.SubEntryCount >0) {
111             for (int i=0; i<entry.SubEntryCount; i++) {
112                 returnVal &= findTypeInEntryTree(entry.SubEntries[i], log);
113             }
114         }
115         // if we are not on method leaf, exit here
116         // insert one method result into database
117         return returnVal;
118     }
119 
120     /**
121      * Insert this entrry to the database.
122      * @param entry The entry to write.
123      * @param log The log writer.
124      */
insertEntry(DescEntry entry, LogWriter log)125     protected boolean insertEntry(DescEntry entry, LogWriter log) {
126         // copy the swlInput Hashtable, so it can be reset easily for the next run
127         Hashtable copySqlInput = new Hashtable();
128         copySqlInput.putAll(mSqlInput);
129         // put some stuff from entry in the Hashtable
130         mSqlInput.put("EntryLongName", entry.longName);
131         mSqlInput.put("EntryName", entry.entryName);
132         mSqlInput.put("EntryState", entry.State);
133         mSqlInput.put("EntryType", entry.EntryType);
134         boolean result = insertEntry(log);
135         // reset the Hashtable
136         mSqlInput = copySqlInput;
137         return result;
138 
139     }
140 
141     /**
142      * Set the writeable entry types: for example "method", "interface", etc.
143      * All these entries are written to the database.
144      * @param types A String array with all types that have to be written.
145      */
setWriteableEntryTypes(String[] types)146     public void setWriteableEntryTypes(String[] types) {
147         mWriteableEntryTypes = types;
148     }
149 
150     /**
151      * Is the entry of the writeable entry type?
152      * @param entry The entry that is checked
153      * @return True, if it is indeed a writeable entry.
154      */
isWriteableEntryType(DescEntry entry)155     protected boolean isWriteableEntryType(DescEntry entry) {
156         boolean result = false;
157         for (int i=0; i<mWriteableEntryTypes.length; i++) {
158             if (entry.EntryType.equals(mWriteableEntryTypes[i])) {
159                 result = true;
160                 break;
161             }
162         }
163         return result;
164     }
165 
166     /**
167      * Wrap the command of SQLExecution class for transparency.
168      */
executeSQLCommand(String command, boolean mergeOutput)169     protected boolean executeSQLCommand(String command, boolean mergeOutput) {
170         return mSqlExec.executeSQLCommand(command, mSqlInput, mSqlOutput, mergeOutput);
171     }
172 
173     /**
174      * Wrap the command of SQLExecution class for transparency.
175      */
executeSQLCommand(String command)176     protected boolean executeSQLCommand(String command) {
177         return mSqlExec.executeSQLCommand(command, mSqlInput, mSqlOutput);
178     }
179 
180     /**
181      * Method to print: empty here
182      */
println(String msg)183     public void println(String msg) {
184     }
185 
186     /**
187      * Prepare the database: executed once at the beginning.
188      * Abstract method, so derived classes have to overwrite it.
189      */
prepareDataBase(LogWriter log)190     protected abstract boolean prepareDataBase(LogWriter log);
191 
192     /**
193      * Insert one entr into the database.
194      * Abstract method, so derived classes have to overwrite it.
195      */
insertEntry(LogWriter log)196     protected abstract boolean insertEntry(LogWriter log);
197 
198     /**
199      *
200     protected abstract boolean checkDataBase(LogWriter log);
201      */
202 
203 }
204