KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > exoplatform > services > database > impl > DatabaseServiceImpl


1 /***************************************************************************
2  * Copyright 2001-2003 The eXo Platform SARL All rights reserved. *
3  * Please look at license.txt in info directory for more license detail. *
4  **************************************************************************/

5 package org.exoplatform.services.database.impl;
6
7 import java.sql.Connection JavaDoc;
8 import java.sql.DatabaseMetaData JavaDoc;
9 import java.util.*;
10 import javax.naming.Context JavaDoc;
11 import javax.naming.InitialContext JavaDoc;
12 import javax.sql.DataSource JavaDoc;
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 /**
20  * Created by The eXo Platform SARL . Author : Tuan Nguyen
21  * tuan08@users.sourceforge.net Date: Jun 14, 2003 Time: 1:12:22 PM
22  */

23 public class DatabaseServiceImpl implements DatabaseService, Startable {
24   
25   private DataSource JavaDoc defaultDS_;
26   private Map datasources_;
27
28   public DatabaseServiceImpl(ConfigurationManager confService) throws Exception JavaDoc {
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 JavaDoc 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 JavaDoc getDefaultDataSource() { return defaultDS_; }
45
46   public DataSource JavaDoc getDataSource(String JavaDoc name) {
47     return (DataSource JavaDoc) datasources_.get(name);
48   }
49
50   public String JavaDoc getDatabaseType(String JavaDoc dsName) {
51     DataSource JavaDoc ds = getDataSource(dsName);
52     return getDatabaseType(ds) ;
53   }
54   
55   public String JavaDoc getDatabaseType(DataSource JavaDoc ds) {
56     String JavaDoc dbType = null;
57     Connection JavaDoc conn = null;
58     try {
59       conn = ds.getConnection();
60       DatabaseMetaData JavaDoc data = conn.getMetaData();
61       String JavaDoc 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 JavaDoc ex) {
78       ex.printStackTrace();
79     } finally {
80       if (conn != null) {
81         try {
82           conn.close();
83         } catch (Exception JavaDoc ex) { }
84       }
85     }
86     return dbType;
87   }
88   
89   private DataSource JavaDoc findDatasource(DatabaseServiceConfig.DataSourceConfig dsconfig) throws Exception JavaDoc {
90     DataSource JavaDoc datasource = null;
91     Context JavaDoc ctx = null;
92     int platform = Environment.getInstance().getPlatform();
93     if (platform == Environment.TOMCAT_PLATFORM) {
94       ctx = new InitialContext JavaDoc();
95       datasource = (javax.sql.DataSource JavaDoc) ctx.lookup("java:comp/env/" + dsconfig.getJndi());
96     } else if (platform == Environment.JETTY_PLATFORM) {
97       ctx = new InitialContext JavaDoc();
98       datasource = (javax.sql.DataSource JavaDoc) ctx.lookup(dsconfig.getJndi());
99     } else if (platform == Environment.WEBSHPERE_PLATFORM) {
100       ctx = new InitialContext JavaDoc();
101       datasource = (javax.sql.DataSource JavaDoc) ctx.lookup(dsconfig.getJndi());
102     } else if (platform == Environment.WEBLOGIC_PLATFORM) {
103       ctx = new InitialContext JavaDoc();
104       datasource = (javax.sql.DataSource JavaDoc) 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 JavaDoc(props);
111       datasource = (javax.sql.DataSource JavaDoc) ctx.lookup("java:" +dsconfig.getJndi());
112     } else {
113       ctx = new InitialContext JavaDoc();
114       datasource = (javax.sql.DataSource JavaDoc) 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