KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > sql > DatabaseManager


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  *
23  * Free Software Foundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Scott Ferguson
28  */

29
30 package com.caucho.sql;
31
32 import com.caucho.loader.EnvironmentLocal;
33 import com.caucho.util.L10N;
34
35 import javax.sql.DataSource JavaDoc;
36 import java.util.HashMap JavaDoc;
37 import java.util.logging.Logger JavaDoc;
38
39 /**
40  * Manages databases in a local environment, e.g. for PHP dynamic
41  * database lookup.
42  */

43 public class DatabaseManager {
44   protected static final Logger JavaDoc log
45     = Logger.getLogger(DatabaseManager.class.getName());
46   private static final L10N L = new L10N(DatabaseManager.class);
47
48   private static final EnvironmentLocal<DatabaseManager> _localManager
49     = new EnvironmentLocal<DatabaseManager>();
50
51   private final HashMap JavaDoc<String JavaDoc,DBPool> _databaseMap
52     = new HashMap JavaDoc<String JavaDoc,DBPool>();
53
54   /**
55    * The manager is never instantiated.
56    */

57   private DatabaseManager()
58   {
59   }
60
61   /**
62    * Returns the database manager for the local environment.
63    */

64   private static DatabaseManager getLocalManager()
65   {
66     synchronized (_localManager) {
67       DatabaseManager manager = _localManager.getLevel();
68
69       if (manager == null) {
70     manager = new DatabaseManager();
71
72     _localManager.set(manager);
73       }
74
75       return manager;
76     }
77   }
78
79   /**
80    * Returns a matching dbpool.
81    */

82   public static DataSource JavaDoc findDatabase(String JavaDoc driver, String JavaDoc url)
83     throws Exception JavaDoc
84   {
85     return getLocalManager().findDatabaseImpl(driver, url);
86   }
87
88   /**
89    * Looks up the local database, creating if necessary.
90    */

91   private DataSource JavaDoc findDatabaseImpl(String JavaDoc driverName, String JavaDoc url)
92     throws Exception JavaDoc
93   {
94     synchronized (_databaseMap) {
95       DBPool db = _databaseMap.get(url);
96
97       if (db == null) {
98     db = new DBPool();
99     
100     DriverConfig driver = db.createDriver();
101
102     ClassLoader JavaDoc loader = Thread.currentThread().getContextClassLoader();
103     
104     Class JavaDoc driverClass = Class.forName(driverName, false, loader);
105
106     driver.setType(driverClass);
107     driver.setURL(url);
108
109     db.init();
110
111     _databaseMap.put(url, db);
112       }
113
114       return db;
115     }
116   }
117 }
118
119
Popular Tags