1 5 package org.exoplatform.services.database.impl; 6 7 import java.sql.Connection ; 8 import java.sql.DatabaseMetaData ; 9 import java.util.*; 10 import javax.naming.Context ; 11 import javax.naming.InitialContext ; 12 import javax.sql.DataSource ; 13 import org.exoplatform.services.database.DatabaseService; 14 import org.picocontainer.Startable; 15 import org.exoplatform.commons.Environment; 16 import org.exoplatform.container.configuration.ConfigurationManager; 17 import org.exoplatform.container.configuration.ServiceConfiguration; 18 19 23 public class DatabaseServiceImpl implements DatabaseService, Startable { 24 25 private DataSource defaultDS_; 26 private Map datasources_; 27 28 public DatabaseServiceImpl(ConfigurationManager confService) throws Exception { 29 datasources_ = new HashMap(); 30 ServiceConfiguration sconf = confService.getServiceConfiguration(DatabaseService.class); 31 DatabaseServiceConfig config = 32 (DatabaseServiceConfig)sconf.getObjectParam("database.service.config").getObject() ; 33 List dsConfigs = config.getDatasource() ; 34 DataSource datasource = null; 35 for (int i = 0; i < dsConfigs.size(); i++) { 36 DatabaseServiceConfig.DataSourceConfig dsconfig = 37 (DatabaseServiceConfig.DataSourceConfig) dsConfigs.get(i); 38 datasource = findDatasource(dsconfig) ; 39 datasources_.put(dsconfig.getName(), datasource); 40 if(defaultDS_ == null) defaultDS_ = datasource ; 41 } 42 } 43 44 public DataSource getDefaultDataSource() { return defaultDS_; } 45 46 public DataSource getDataSource(String name) { 47 return (DataSource ) datasources_.get(name); 48 } 49 50 public String getDatabaseType(String dsName) { 51 DataSource ds = getDataSource(dsName); 52 return getDatabaseType(ds) ; 53 } 54 55 public String getDatabaseType(DataSource ds) { 56 String dbType = null; 57 Connection conn = null; 58 try { 59 conn = ds.getConnection(); 60 DatabaseMetaData data = conn.getMetaData(); 61 String pname = data.getDatabaseProductName().toLowerCase(); 62 if (pname.indexOf(DatabaseService.HSQL) >= 0) { 63 dbType = DatabaseService.HSQL; 64 } else if (pname.indexOf(DatabaseService.MYSQL) >= 0) { 65 dbType = DatabaseService.MYSQL; 66 } else if (pname.indexOf(DatabaseService.DB2) >= 0) { 67 dbType = DatabaseService.DB2; 68 } else if (pname.indexOf(DatabaseService.ORACLE) >= 0) { 69 dbType = DatabaseService.ORACLE; 70 } else if (pname.indexOf(DatabaseService.POSTGRESQL) >= 0) { 71 dbType = DatabaseService.POSTGRESQL; 72 } else if (pname.indexOf(DatabaseService.SQL_SERVER) >= 0) { 73 dbType = DatabaseService.SQL_SERVER; 74 } else { 75 dbType = DatabaseService.UNKNOWN; 76 } 77 } catch (Exception ex) { 78 ex.printStackTrace(); 79 } finally { 80 if (conn != null) { 81 try { 82 conn.close(); 83 } catch (Exception ex) { } 84 } 85 } 86 return dbType; 87 } 88 89 private DataSource findDatasource(DatabaseServiceConfig.DataSourceConfig dsconfig) throws Exception { 90 DataSource datasource = null; 91 Context ctx = null; 92 int platform = Environment.getInstance().getPlatform(); 93 if (platform == Environment.TOMCAT_PLATFORM) { 94 ctx = new InitialContext (); 95 datasource = (javax.sql.DataSource ) ctx.lookup("java:comp/env/" + dsconfig.getJndi()); 96 } else if (platform == Environment.JETTY_PLATFORM) { 97 ctx = new InitialContext (); 98 datasource = (javax.sql.DataSource ) ctx.lookup(dsconfig.getJndi()); 99 } else if (platform == Environment.WEBSHPERE_PLATFORM) { 100 ctx = new InitialContext (); 101 datasource = (javax.sql.DataSource ) ctx.lookup(dsconfig.getJndi()); 102 } else if (platform == Environment.WEBLOGIC_PLATFORM) { 103 ctx = new InitialContext (); 104 datasource = (javax.sql.DataSource ) ctx.lookup(dsconfig.getJndi()); 105 } else if (platform == Environment.JBOSS_PLATFORM) { 106 Properties props = new Properties(); 107 props.put(Context.URL_PKG_PREFIXES, "org.jboss.naming:org.jnp.interfaces"); 108 props.put(Context.PROVIDER_URL, "jnp://localhost:1099"); 109 props.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory"); 110 ctx = new InitialContext (props); 111 datasource = (javax.sql.DataSource ) ctx.lookup("java:" +dsconfig.getJndi()); 112 } else { 113 ctx = new InitialContext (); 114 datasource = (javax.sql.DataSource ) ctx.lookup("java:comp/env/" + dsconfig.getJndi()); 115 } 116 if(ctx != null) ctx.close() ; 117 return datasource ; 118 } 119 120 public void start() { } 121 public void stop() { } 122 } 123 | Popular Tags |