KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > clif > isac > plugins > jdbcsimple10 > SessionObject


1 /*
2  * CLIF is a Load Injection Framework
3  * Copyright (C) 2004 France Telecom R&D
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  *
19  * CLIF
20  *
21  * Contact: clif@objectweb.org
22  */

23 package org.objectweb.clif.isac.plugins.jdbcsimple10;
24
25 import java.sql.Connection JavaDoc;
26 import java.sql.DriverManager JavaDoc;
27 import java.sql.SQLException JavaDoc;
28 import java.util.Hashtable JavaDoc;
29
30 import org.objectweb.clif.isac.plugins.jdbcsimple10.actions.JDBCSample;
31 import org.objectweb.clif.scenario.util.isac.exception.IsacRuntimeException;
32 import org.objectweb.clif.scenario.util.isac.plugin.SampleAction;
33 import org.objectweb.clif.scenario.util.isac.util.SessionObjectAction;
34 import org.objectweb.clif.storage.api.ActionEvent;
35
36 /**
37  * This class is the implementation of the isac jdbc session object
38  *
39  * @author JC Meillaud
40  * @author A Peyrard
41  */

42 public class SessionObject implements SampleAction, SessionObjectAction {
43     //samples constants
44
public final static int SQL_REQUEST = 0;
45
46     // name of the params
47
public static final String JavaDoc BASE = "base_name";
48
49     public static final String JavaDoc SERVER_TYPE = "server_type";
50
51     public static final String JavaDoc HOST = "host";
52
53     public static final String JavaDoc PORT = "port";
54
55     public static final String JavaDoc LOGIN = "login";
56
57     public static final String JavaDoc PASSWD = "passwd";
58
59     // server type
60
public static final String JavaDoc POSTGRESQL = "postgresql";
61
62     public static final String JavaDoc MYSQL = "mysql";
63
64     // base url constants
65
private final static String JavaDoc POSTGRESQL_URL = "jdbc:postgresql:";
66
67     private final static String JavaDoc MYSQL_URL = "jdbc:mysql:";
68
69     // drivers
70
private final static String JavaDoc POSTGRESQL_DRIVER = "org.postgresql.Driver";
71
72     private final static String JavaDoc MYSQL_DRIVER = "org.mysql.Driver";
73
74     // attributes
75
private String JavaDoc login;
76
77     private String JavaDoc passwd;
78
79     private String JavaDoc serverType;
80
81     private String JavaDoc baseName;
82
83     private String JavaDoc host;
84
85     private String JavaDoc port;
86
87     private Connection JavaDoc connection;
88
89     /**
90      * Build a new SessionObject for this plugin
91      *
92      * @param params
93      * The table containing all the so params
94      */

95     public SessionObject(Hashtable JavaDoc params) {
96         // set the attributes
97
this.serverType = (String JavaDoc) params.get(SERVER_TYPE);
98         this.baseName = (String JavaDoc) params.get(BASE);
99         this.host = (String JavaDoc) params.get(HOST);
100         this.port = (String JavaDoc) params.get(PORT);
101         this.login = (String JavaDoc) params.get(LOGIN);
102         this.passwd = (String JavaDoc) params.get(PASSWD);
103
104         // init the connection
105
this.initJDBCConnection();
106     }
107
108     /**
109      * This constructor is used to clone the specified session object
110      *
111      * @param toClone
112      * The session object to clone
113      */

114     private SessionObject(SessionObject toClone) {
115         // copy references of the attributes values
116
this.serverType = toClone.getServerType();
117         this.baseName = toClone.getBaseName();
118         this.host = toClone.getHost();
119         this.port = toClone.getPort();
120         this.login = toClone.getLogin();
121         this.passwd = toClone.getPasswd();
122
123         // init the connections
124
this.initJDBCConnection();
125     }
126
127     //////////////////////////////////
128
// Methods
129
/////////////////////////////////
130

131     /**
132      * Init a new jdbc connection to the base
133      */

134     private void initJDBCConnection() {
135         // check if the driver class is reachable
136
try {
137             // switch between the different known server types
138
if (this.serverType.equals(POSTGRESQL)) {
139                 Class.forName(POSTGRESQL_DRIVER);
140             } else if (this.serverType.equals(MYSQL)) {
141                 Class.forName(MYSQL_DRIVER);
142             }
143             else {
144                 throw new IsacRuntimeException("Unable to find the driver for this type of server : "+serverType);
145             }
146         } catch (ClassNotFoundException JavaDoc cnfe) {
147             throw new IsacRuntimeException("Unable to find the driver !");
148         }
149         String JavaDoc baseUrl ;
150         // switch between the different known server types
151
if (this.serverType.equals(POSTGRESQL)) {
152             // create the base name url
153
baseUrl = POSTGRESQL_URL + "//" + this.host + ":" + this.port
154                     + "/" + this.baseName;
155         } else if (this.serverType.equals(MYSQL)) {
156             // create the base name url
157
baseUrl = MYSQL_URL + "//" + this.host + ":" + this.port
158                     + "/" + this.baseName;
159         }
160         else {
161             throw new IsacRuntimeException("Unable to find the driver for this type of server : "+serverType);
162         }
163
164         // create the connection on this base
165
try {
166             connection = DriverManager.getConnection(baseUrl, login, passwd);
167         } catch (SQLException JavaDoc se) {
168             throw new IsacRuntimeException(
169                     "Unable to create connection on server : " + se);
170         }
171     }
172
173     /**
174      * This method permit to close the sql connection
175      */

176     public void closeConnection() {
177         try {
178             connection.close();
179         } catch (SQLException JavaDoc se) {
180             throw new IsacRuntimeException(
181                     "Unable to close the jdbc connection",se);
182         }
183     }
184
185     ///////////////////////////////////
186
// SampleAction implementation
187
///////////////////////////////////
188

189     /**
190      * @see org.objectweb.clif.scenario.util.isac.plugin.SampleAction#doSample(int,
191      * java.util.Hashtable, org.objectweb.clif.storage.api.ActionEvent)
192      */

193     public ActionEvent doSample(int number, Hashtable JavaDoc params, ActionEvent report) {
194         switch (number) {
195         case SQL_REQUEST:
196             return JDBCSample.sqlRequest(this, params, report);
197         default:
198             throw new RuntimeException JavaDoc("Unable to find the sample : " + number);
199         }
200     }
201
202     //////////////////////////////////
203
// SessionObjectAction implementation
204
//////////////////////////////////
205

206     /**
207      * @see org.objectweb.clif.scenario.util.isac.util.SessionObjectAction#createNewSessionObject()
208      */

209     public Object JavaDoc createNewSessionObject() {
210         return new SessionObject(this);
211     }
212
213     /**
214      * @see org.objectweb.clif.scenario.util.isac.util.SessionObjectAction#close()
215      */

216     public void close() {
217         // close the jdbc connection
218
this.closeConnection() ;
219     }
220
221     /**
222      * @see org.objectweb.clif.scenario.util.isac.util.SessionObjectAction#reset()
223      */

224     public void reset() {
225         // nothing to reset
226
}
227
228     ///////////////////////////////
229
// Attributes getters
230
///////////////////////////////
231

232     /**
233      * @return Returns the login.
234      */

235     public String JavaDoc getLogin() {
236         return login;
237     }
238
239     /**
240      * @return Returns the passwd.
241      */

242     public String JavaDoc getPasswd() {
243         return passwd;
244     }
245
246     /**
247      * @return Returns the baseName.
248      */

249     public String JavaDoc getBaseName() {
250         return baseName;
251     }
252
253     /**
254      * @return Returns the connection.
255      */

256     public Connection JavaDoc getConnection() {
257         return connection;
258     }
259
260     /**
261      * @return Returns the host.
262      */

263     public String JavaDoc getHost() {
264         return host;
265     }
266
267     /**
268      * @return Returns the port.
269      */

270     public String JavaDoc getPort() {
271         return port;
272     }
273
274     /**
275      * @return Returns the serverType.
276      */

277     public String JavaDoc getServerType() {
278         return serverType;
279     }
280 }
Popular Tags