PageRenderTime 61ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/ojc-core/jdbcbc/jdbcbcimpl/src/com/sun/jbi/jdbcbc/model/runtime/DataBases.java

https://bitbucket.org/pymma/openesb-components
Java | 301 lines | 203 code | 25 blank | 73 comment | 30 complexity | 55e054bffba95234ed3de6ed44d4ec81 MD5 | raw file
  1. /*
  2. * BEGIN_HEADER - DO NOT EDIT
  3. *
  4. * The contents of this file are subject to the terms
  5. * of the Common Development and Distribution License
  6. * (the "License"). You may not use this file except
  7. * in compliance with the License.
  8. *
  9. * You can obtain a copy of the license at
  10. * https://open-jbi-components.dev.java.net/public/CDDLv1.0.html.
  11. * See the License for the specific language governing
  12. * permissions and limitations under the License.
  13. *
  14. * When distributing Covered Code, include this CDDL
  15. * HEADER in each file and include the License file at
  16. * https://open-jbi-components.dev.java.net/public/CDDLv1.0.html.
  17. * If applicable add the following below this CDDL HEADER,
  18. * with the fields enclosed by brackets "[]" replaced with
  19. * your own identifying information: Portions Copyright
  20. * [year] [name of copyright owner]
  21. */
  22. /*
  23. * @(#)DataBases.java
  24. *
  25. * Copyright 2004-2007 Sun Microsystems, Inc. All Rights Reserved.
  26. *
  27. * END_HEADER - DO NOT EDIT
  28. */
  29. package com.sun.jbi.jdbcbc.model.runtime;
  30. import com.sun.jbi.internationalization.Messages;
  31. import java.lang.reflect.InvocationTargetException;
  32. import java.util.logging.Level;
  33. import java.util.logging.Logger;
  34. import javax.jbi.component.ComponentContext;
  35. import javax.naming.InitialContext;
  36. import javax.naming.NamingException;
  37. import javax.sql.*;
  38. import javax.sql.XADataSource;
  39. public enum DataBases {
  40. DERBY,ORACLE,DB2,SQLSERVER,VSAM,MYSQL;
  41. DBConnectionInfo mdbConnectionInfo = null;
  42. private static final Logger mLogger = Logger.getLogger(DataBases.class.getName());
  43. private static final Messages mMessages = Messages.getMessages(DataBases.class);
  44. public int dbType() {
  45. return ordinal();
  46. }
  47. @Override
  48. public String toString() {
  49. switch (this) {
  50. case DERBY:
  51. return "DERBY";
  52. case ORACLE:
  53. return "ORACLE";
  54. case DB2:
  55. return "DB2";
  56. case SQLSERVER:
  57. return "SQLSERVER";
  58. case VSAM:
  59. return "VSAM";
  60. case MYSQL:
  61. return "MYSQL";
  62. }
  63. return null;
  64. }
  65. public Class getXADataSourceClass() throws ClassNotFoundException {
  66. switch (this) {
  67. case DERBY:
  68. return Class.forName("org.apache.derby.jdbc.ClientXADataSource");
  69. case ORACLE:
  70. return Class.forName("oracle.jdbc.xa.client.OracleXADataSource");
  71. case DB2:
  72. return Class.forName("com.ibm.db2.jdbc.DB2XADataSource");
  73. case SQLSERVER:
  74. return Class.forName("com.ddtek.jdbcx.sqlserver.SQLServerDataSource");
  75. }
  76. return null;
  77. }
  78. public Class getDataSourceClass() throws ClassNotFoundException {
  79. switch (this) {
  80. case DERBY:
  81. return Class.forName("org.apache.derby.jdbc.ClientDataSource");
  82. case ORACLE:
  83. return Class.forName("oracle.jdbc.pool.OracleDataSource");
  84. case DB2:
  85. return Class.forName("com.SeeBeyond.db2.jdbcx.db2.DB2DataSource");
  86. case SQLSERVER:
  87. return Class.forName("com.SeeBeyond.sqlserver.jdbcx.sqlserver.SQLServerDataSource");
  88. }
  89. return null;
  90. }
  91. public Object buildDataSource() throws ClassNotFoundException , Exception{
  92. Class dsClass = null;
  93. if (mdbConnectionInfo.getXAEnabled().equalsIgnoreCase("XATransaction"))
  94. dsClass = getXADataSourceClass();
  95. else
  96. dsClass = getDataSourceClass();
  97. return buildDataSource(dsClass);
  98. }
  99. public Object buildDataSource(final Class dsclass) throws ClassNotFoundException, Exception {
  100. try {
  101. final Object dataSourceInstance = dsclass.newInstance();
  102. final Class[] stringParam = new Class[] {String.class};
  103. final Class[] intParam = new Class[]{int.class};
  104. if (this == DERBY) {
  105. dsclass.getMethod("setServerName", stringParam)
  106. .invoke(dataSourceInstance,
  107. new Object[] { mdbConnectionInfo.getServerName() });
  108. dsclass
  109. .getMethod("setPortNumber", new Class[] { int.class })
  110. .invoke(dataSourceInstance,
  111. new Object[] { mdbConnectionInfo.getPortNumber() });
  112. dsclass.getMethod("setDatabaseName", stringParam)
  113. .invoke(dataSourceInstance,
  114. new Object[] { mdbConnectionInfo.getDatabaseOrSchemaName() });
  115. dsclass.getMethod("setUser", stringParam)
  116. .invoke(dataSourceInstance,
  117. new Object[] { mdbConnectionInfo.getUsername() });
  118. dsclass.getMethod("setPassword", stringParam)
  119. .invoke(dataSourceInstance,
  120. new Object[] { mdbConnectionInfo.getPassword() });
  121. } else if (this == ORACLE) {
  122. dsclass.getMethod("setURL", stringParam)
  123. .invoke(dataSourceInstance,
  124. new Object[] { mdbConnectionInfo.getDbUrl()});
  125. dsclass.getMethod("setUser", stringParam)
  126. .invoke(dataSourceInstance,
  127. new Object[] { mdbConnectionInfo.getUsername() });
  128. dsclass.getMethod("setPassword", stringParam)
  129. .invoke(dataSourceInstance,
  130. new Object[] { mdbConnectionInfo.getPassword() });
  131. } else if (this == DB2) {
  132. dsclass.getMethod("setServerName", stringParam)
  133. .invoke(dataSourceInstance,
  134. new Object[] { mdbConnectionInfo.getServerName()});
  135. dsclass.getMethod("setPortNumber", intParam)
  136. .invoke(dataSourceInstance,
  137. new Object[] { mdbConnectionInfo.getPortNumber()});
  138. dsclass.getMethod("setDatabaseName", stringParam)
  139. .invoke(dataSourceInstance,
  140. new Object[] { mdbConnectionInfo.getDatabaseOrSchemaName()});
  141. dsclass.getMethod("setUser", stringParam)
  142. .invoke(dataSourceInstance,
  143. new Object[] { mdbConnectionInfo.getUsername() });
  144. dsclass.getMethod("setPassword", stringParam)
  145. .invoke(dataSourceInstance,
  146. new Object[] { mdbConnectionInfo.getPassword() });
  147. } else if (this == SQLSERVER) {
  148. dsclass.getMethod("setServerName", stringParam)
  149. .invoke(dataSourceInstance,
  150. new Object[] { mdbConnectionInfo.getServerName()});
  151. dsclass.getMethod("setPortNumber", intParam)
  152. .invoke(dataSourceInstance,
  153. new Object[] { mdbConnectionInfo.getPortNumber()});
  154. dsclass.getMethod("setDatabaseName", stringParam)
  155. .invoke(dataSourceInstance,
  156. new Object[] { mdbConnectionInfo.getDatabaseOrSchemaName()});
  157. dsclass.getMethod("setUser", stringParam)
  158. .invoke(dataSourceInstance,
  159. new Object[] { mdbConnectionInfo.getUsername() });
  160. dsclass.getMethod("setPassword", stringParam)
  161. .invoke(dataSourceInstance,
  162. new Object[] { mdbConnectionInfo.getPassword() });
  163. }
  164. return dataSourceInstance;
  165. } catch (final IllegalAccessException iae) {
  166. mLogger.log(Level.SEVERE,"Illegal Access Exception While Dealing with Driver class",iae);
  167. } catch (final NoSuchMethodException nsme) {
  168. mLogger.log(Level.SEVERE,"No Such Method Exception While Dealing with Driver class",nsme);
  169. } catch (final InvocationTargetException ite) {
  170. mLogger.log(Level.SEVERE,"Invocation Target Exception While Dealing with Driver class",ite);
  171. } catch (final InstantiationException ie) {
  172. mLogger.log(Level.SEVERE," Exception While Instantiating the Driver class",ie);
  173. }
  174. return null;
  175. }
  176. /*public Object buildXADataSource() {
  177. try {
  178. final Class dsclass = getXADataSourceClass();
  179. final Object dataSourceInstance = dsclass.newInstance();
  180. final Class[] stringParam = new Class[] { String.class };
  181. final Class[] stringParams = new Class[] {String.class,String.class};
  182. if (this == DERBY) {
  183. dsclass.getMethod("setServerName", stringParam)
  184. .invoke(dataSourceInstance,
  185. new Object[] { dbConnectionInfo.getServerName() });
  186. dsclass.getMethod("setPortNumber", new Class[] { int.class })
  187. .invoke(dataSourceInstance,
  188. new Object[] { dbConnectionInfo.getPortNumber() });
  189. dsclass.getMethod("setDatabaseName", stringParam)
  190. .invoke(dataSourceInstance,
  191. new Object[] { dbConnectionInfo.getDatabaseOrSchemaName() });
  192. dsclass.getMethod("setUser", stringParam)
  193. .invoke(dataSourceInstance,
  194. new Object[] { dbConnectionInfo.getUsername() });
  195. dsclass.getMethod("setPassword", stringParam)
  196. .invoke(dataSourceInstance,
  197. new Object[] { dbConnectionInfo.getPassword() });
  198. } else if (this == ORACLE) {
  199. dsclass.getMethod("setURL", stringParam).invoke(dataSourceInstance,
  200. new Object[] { dbConnectionInfo.getDbUrl()});
  201. dsclass.getMethod("setUser", stringParam)
  202. .invoke(dataSourceInstance,
  203. new Object[] { dbConnectionInfo.getUsername() });
  204. dsclass.getMethod("setPassword", stringParam)
  205. .invoke(dataSourceInstance,
  206. new Object[] { dbConnectionInfo.getPassword() });
  207. }
  208. return dataSourceInstance;
  209. } catch (final ClassNotFoundException cnfe) {
  210. cnfe.getMessage();
  211. } catch (final IllegalAccessException iae) {
  212. } catch (final NoSuchMethodException nsme) {
  213. } catch (final InvocationTargetException ite) {
  214. } catch (final InstantiationException ie) {
  215. }
  216. return null;
  217. }
  218. */
  219. public void setDBConnectionInfo(final DBConnectionInfo dbConnectionInfo) {
  220. this.mdbConnectionInfo = dbConnectionInfo;
  221. }
  222. public DataBases getValue(final String value) {
  223. if (value.equalsIgnoreCase("DERBY")) {
  224. return DERBY;
  225. } else if (value.equalsIgnoreCase("ORACLE")) {
  226. return ORACLE;
  227. } else if (value.equalsIgnoreCase("VSAM")) {
  228. return VSAM;
  229. } else if (value.equalsIgnoreCase("DB2")) {
  230. return DB2;
  231. } else if (value.equalsIgnoreCase("SQLSERVER")) {
  232. return SQLSERVER;
  233. } else if (value.equalsIgnoreCase("MYSQL")) {
  234. return MYSQL;
  235. } else {
  236. return null;
  237. }
  238. }
  239. public DatabaseModel getDatabaseModel() {
  240. switch (this) {
  241. case DERBY:
  242. return DerbyDataAccess.getInstance();
  243. case ORACLE:
  244. return OracleDataAccess.getInstance();
  245. case DB2:
  246. return Db2DataAccess.getInstance();
  247. case SQLSERVER:
  248. return SqlServerDataAccess.getInstance();
  249. }
  250. return DatabaseModelImpl.getInstance();
  251. }
  252. public String createAndBindJNDIDS(final ComponentContext componentContext,
  253. Object datasource, final String jndiname, final String xaEnabled)
  254. throws NamingException {
  255. final InitialContext namingContext = componentContext.getNamingContext();
  256. if(mdbConnectionInfo.getXAEnabled().equalsIgnoreCase("XATransaction")){
  257. datasource =(XADataSource)datasource;
  258. }else{
  259. datasource =(DataSource)datasource;
  260. }
  261. if(lookup(jndiname,namingContext)){
  262. namingContext.bind(jndiname, datasource);
  263. }
  264. return jndiname;
  265. }
  266. public boolean lookup(final String jndiname,final InitialContext namingContext) throws NamingException{
  267. try{
  268. namingContext.lookup(jndiname);
  269. }catch(javax.naming.NameNotFoundException e){
  270. return true;
  271. }
  272. return false;
  273. }
  274. }