1 23 package org.objectweb.clif.isac.plugins.jdbcsimple10; 24 25 import java.sql.Connection ; 26 import java.sql.DriverManager ; 27 import java.sql.SQLException ; 28 import java.util.Hashtable ; 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 42 public class SessionObject implements SampleAction, SessionObjectAction { 43 public final static int SQL_REQUEST = 0; 45 46 public static final String BASE = "base_name"; 48 49 public static final String SERVER_TYPE = "server_type"; 50 51 public static final String HOST = "host"; 52 53 public static final String PORT = "port"; 54 55 public static final String LOGIN = "login"; 56 57 public static final String PASSWD = "passwd"; 58 59 public static final String POSTGRESQL = "postgresql"; 61 62 public static final String MYSQL = "mysql"; 63 64 private final static String POSTGRESQL_URL = "jdbc:postgresql:"; 66 67 private final static String MYSQL_URL = "jdbc:mysql:"; 68 69 private final static String POSTGRESQL_DRIVER = "org.postgresql.Driver"; 71 72 private final static String MYSQL_DRIVER = "org.mysql.Driver"; 73 74 private String login; 76 77 private String passwd; 78 79 private String serverType; 80 81 private String baseName; 82 83 private String host; 84 85 private String port; 86 87 private Connection connection; 88 89 95 public SessionObject(Hashtable params) { 96 this.serverType = (String ) params.get(SERVER_TYPE); 98 this.baseName = (String ) params.get(BASE); 99 this.host = (String ) params.get(HOST); 100 this.port = (String ) params.get(PORT); 101 this.login = (String ) params.get(LOGIN); 102 this.passwd = (String ) params.get(PASSWD); 103 104 this.initJDBCConnection(); 106 } 107 108 114 private SessionObject(SessionObject toClone) { 115 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 this.initJDBCConnection(); 125 } 126 127 131 134 private void initJDBCConnection() { 135 try { 137 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 cnfe) { 147 throw new IsacRuntimeException("Unable to find the driver !"); 148 } 149 String baseUrl ; 150 if (this.serverType.equals(POSTGRESQL)) { 152 baseUrl = POSTGRESQL_URL + "//" + this.host + ":" + this.port 154 + "/" + this.baseName; 155 } else if (this.serverType.equals(MYSQL)) { 156 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 try { 166 connection = DriverManager.getConnection(baseUrl, login, passwd); 167 } catch (SQLException se) { 168 throw new IsacRuntimeException( 169 "Unable to create connection on server : " + se); 170 } 171 } 172 173 176 public void closeConnection() { 177 try { 178 connection.close(); 179 } catch (SQLException se) { 180 throw new IsacRuntimeException( 181 "Unable to close the jdbc connection",se); 182 } 183 } 184 185 189 193 public ActionEvent doSample(int number, Hashtable params, ActionEvent report) { 194 switch (number) { 195 case SQL_REQUEST: 196 return JDBCSample.sqlRequest(this, params, report); 197 default: 198 throw new RuntimeException ("Unable to find the sample : " + number); 199 } 200 } 201 202 206 209 public Object createNewSessionObject() { 210 return new SessionObject(this); 211 } 212 213 216 public void close() { 217 this.closeConnection() ; 219 } 220 221 224 public void reset() { 225 } 227 228 232 235 public String getLogin() { 236 return login; 237 } 238 239 242 public String getPasswd() { 243 return passwd; 244 } 245 246 249 public String getBaseName() { 250 return baseName; 251 } 252 253 256 public Connection getConnection() { 257 return connection; 258 } 259 260 263 public String getHost() { 264 return host; 265 } 266 267 270 public String getPort() { 271 return port; 272 } 273 274 277 public String getServerType() { 278 return serverType; 279 } 280 } | Popular Tags |