KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > cjdbc > requestplayer > RequestPlayerProperties


1 /**
2  * C-JDBC: Clustered JDBC.
3  * Copyright (C) 2002-2004 French National Institute For Research In Computer
4  * Science And Control (INRIA).
5  * Contact: c-jdbc@objectweb.org
6  *
7  * This library is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU Lesser General Public License as published by the
9  * Free Software Foundation; either version 2.1 of the License, or any later
10  * version.
11  *
12  * This library is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
15  * for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with this library; if not, write to the Free Software Foundation,
19  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
20  *
21  * Initial developer(s): Emmanuel Cecchet.
22  * Contributor(s): Julie Marguerite, Mathieu Peltier.
23  */

24
25 package org.objectweb.cjdbc.requestplayer;
26
27 import java.io.File JavaDoc;
28 import java.io.FileInputStream JavaDoc;
29 import java.io.IOException JavaDoc;
30 import java.io.InputStream JavaDoc;
31 import java.util.Properties JavaDoc;
32
33 /**
34  * Checks and gets all properties needed by the request player tools.
35  *
36  * @author <a HREF="mailto:Emmanuel.Cecchet@inria.fr">Emmanuel Cecchet</a>
37  * @author <a HREF="mailto:julie.marguerite@inria.fr">Julie Marguerite</a>
38  * @author <a HREF="mailto:mathieu.peltier@inrialpes.fr">Mathieu Peltier</a>
39  * @version 1.0
40  */

41 public class RequestPlayerProperties extends Properties JavaDoc
42 {
43   /** Default Request player configuration file. */
44   public static final String JavaDoc DEFAULT_CONFIG_FILE = "requestplayer.properties";
45
46   /** Standard connection management type. */
47   public static final int STANDARD_CONNECTION = 0;
48
49   /** Optimized connection management type. */
50   public static final int FIXED_CONNECTION = 1;
51
52   /** Pooling connection management type. */
53   public static final int POOLING_CONNECTION = 2;
54
55   /** Configuration file. Default value is {@link #DEFAULT_CONFIG_FILE}. */
56   private File JavaDoc configFile;
57
58   /** Trace file. */
59   private String JavaDoc traceFile;
60
61   /**
62    * How many requests from the trace file should be executed (0 means the
63    * whole trace is executed).
64    */

65   private int nbRequests = 0;
66
67   /** Number of clients to run in parallel to issue the requests. */
68   private int nbClients = 0;
69
70   /** Request timeout in seconds (0 means no timeout). */
71   private int timeout = 0;
72
73   /** Database driver. */
74   private String JavaDoc databaseDriver;
75
76   /** Database URL. */
77   private String JavaDoc databaseUrl;
78
79   /** Database login. */
80   private String JavaDoc databaseLogin;
81
82   /** Database password. */
83   private String JavaDoc databasePassword;
84
85   /**
86    * Connection management type used in client emulator: standard, fixed or
87    * pooling.
88    */

89   private int connectionType;
90
91   /**
92    * Connection pool size. Must be greater than 0 if connection type is
93    * pooling.
94    */

95   private int poolSize = 0;
96
97   /**
98    * Creates a new <code>RequestPlayerProperties</code> instance. If the
99    * given configuration file cannot be read or if the
100    * {@link #DEFAULT_CONFIG_FILE}file is not found in the classpath, the
101    * current thread is killed.
102    *
103    * @param configFileString configuration file or <code>null</code> if the
104    * default file must be used.
105    */

106   public RequestPlayerProperties(String JavaDoc configFileString)
107   {
108     if (configFileString == null)
109     {
110       configFile = new File JavaDoc(DEFAULT_CONFIG_FILE);
111     }
112     else
113     {
114       configFile = new File JavaDoc(configFileString);
115
116       // Check that the configuration file exists
117
if (!configFile.isFile() || !configFile.exists())
118       {
119         System.err.println(
120           "Cannot read the request player configuration file '"
121             + configFile.toString()
122             + "'");
123         Runtime.getRuntime().exit(1);
124       }
125     }
126
127     // Load properties from configuration file
128
try
129     {
130       InputStream JavaDoc stream = null;
131       if (configFileString == null)
132       {
133
134         stream =
135           RequestPlayerProperties.class.getResourceAsStream(
136             "/" + DEFAULT_CONFIG_FILE);
137         if (stream == null)
138         {
139           System.err.println(
140             "Cannot find request player properties file '"
141               + DEFAULT_CONFIG_FILE
142               + "' in classpath");
143         }
144       }
145       else
146       {
147         stream = new FileInputStream JavaDoc(configFile);
148       }
149
150       super.load(stream);
151       stream.close();
152     }
153     catch (IOException JavaDoc e)
154     {
155       System.err.println(
156         "An error occured when reading the request player properties file '"
157           + configFile.toString()
158           + "'");
159       Runtime.getRuntime().exit(1);
160     }
161   }
162
163   /**
164    * Checks for all needed fields in <code>requestplayer.properties</code>
165    * and initialize corresponding values.
166    *
167    * @return <code>true</code> if so
168    */

169   public boolean checkPropertiesFile()
170   {
171     try
172     {
173       System.out.println();
174       System.out.println("### Database information ###");
175       databaseUrl = getProperty("db_url");
176       System.out.println("Database url : " + databaseUrl);
177       databaseDriver = getProperty("db_driver");
178       System.out.println("Database driver : " + databaseDriver);
179       databaseLogin = getProperty("db_username");
180       System.out.println("Username : " + databaseLogin);
181       databasePassword = getProperty("db_password");
182       System.out.println("Password : " + databasePassword);
183
184       System.out.println();
185       System.out.println("### General information ###");
186       traceFile = getProperty("trace_file");
187       System.out.println("Trace file : " + traceFile);
188       nbRequests = new Integer JavaDoc(getProperty("nb_requests")).intValue();
189       System.out.println("Number of requests : " + nbRequests);
190       timeout = new Integer JavaDoc(getProperty("timeout")).intValue();
191       System.out.println("Timeout on requests : " + timeout + " seconds");
192       nbClients = new Integer JavaDoc(getProperty("nb_clients")).intValue();
193
194       System.out.println("Number of clients : " + nbClients);
195       String JavaDoc connType = getProperty("connection_type");
196       System.out.println("Connection type : " + connType);
197       if (connType.equalsIgnoreCase("fixed"))
198         connectionType = FIXED_CONNECTION;
199       else if (connType.equalsIgnoreCase("standard"))
200         connectionType = STANDARD_CONNECTION;
201       else if (connType.equalsIgnoreCase("pooling"))
202       {
203         connectionType = POOLING_CONNECTION;
204         poolSize = new Integer JavaDoc(getProperty("poolsize")).intValue();
205         System.out.println("Connection pool size: " + poolSize);
206       }
207
208       return true;
209     }
210     catch (Exception JavaDoc e)
211     {
212       System.err.println(
213         "Error while checking request player properties file '"
214           + configFile.toString()
215           + "': "
216           + e.getMessage());
217       return false;
218     }
219   }
220
221   /**
222    * Gets the trace file.
223    *
224    * @return name of the trace file
225    */

226   public String JavaDoc getTraceFile()
227   {
228     return traceFile;
229   }
230
231   /**
232    * Gets the number of requests to execute from the trace file (0 means the
233    * whole trace is executed).
234    *
235    * @return number of requests
236    */

237   public int getNbRequests()
238   {
239     return nbRequests;
240   }
241
242   /**
243    * Gets the database URL for the SQL database.
244    *
245    * @return the database url
246    */

247   public String JavaDoc getDatabaseURL()
248   {
249     return databaseUrl;
250   }
251
252   /**
253    * Gets the database driver class name for the SQL database.
254    *
255    * @return database driver class name
256    */

257   public String JavaDoc getDatabaseDriver()
258   {
259     return databaseDriver;
260   }
261
262   /**
263    * Gets the login for the SQL database.
264    *
265    * @return username
266    */

267   public String JavaDoc getDatabaseUsername()
268   {
269     return databaseLogin;
270   }
271
272   /**
273    * Gets the password for the SQL database
274    *
275    * @return password
276    */

277   public String JavaDoc getDatabasePassword()
278   {
279     return databasePassword;
280   }
281
282   /**
283    * Returns the number of clients to run in parallel to issue the requests.
284    *
285    * @return the number of clients
286    */

287   public int getNbClients()
288   {
289     return nbClients;
290   }
291
292   /**
293    * Returns the connection type.
294    *
295    * @return the connection type
296    */

297   public int getConnectionType()
298   {
299     return connectionType;
300   }
301
302   /**
303    * Returns the connection pool size.
304    *
305    * @return the pool size
306    */

307   public int getPoolSize()
308   {
309     return poolSize;
310   }
311
312   /**
313    * Returns the request timeout in seconds (0 means no timeout).
314    *
315    * @return an <code>int</code> value
316    */

317   public int getTimeout()
318   {
319     return timeout;
320   }
321 }
322
Popular Tags