KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas > dbm > DataBaseServiceImpl


1 /**
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 1999-2004 Bull S.A.
4  * Contact: jonas-team@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * --------------------------------------------------------------------------
22  * $Id: DataBaseServiceImpl.java,v 1.54 2005/07/28 07:52:27 durieuxp Exp $
23  * --------------------------------------------------------------------------
24  */

25
26
27 package org.objectweb.jonas.dbm;
28
29 import java.io.FileNotFoundException JavaDoc;
30 import java.util.Collection JavaDoc;
31 import java.util.Enumeration JavaDoc;
32 import java.util.Hashtable JavaDoc;
33 import java.util.Iterator JavaDoc;
34 import java.util.List JavaDoc;
35 import java.util.Properties JavaDoc;
36 import java.util.Set JavaDoc;
37 import java.util.StringTokenizer JavaDoc;
38 import java.util.Vector JavaDoc;
39
40 import javax.management.InstanceNotFoundException JavaDoc;
41 import javax.management.MBeanRegistrationException JavaDoc;
42 import javax.management.MBeanServer JavaDoc;
43 import javax.management.MalformedObjectNameException JavaDoc;
44 import javax.management.ObjectName JavaDoc;
45 import javax.management.modelmbean.ModelMBean JavaDoc;
46 import javax.naming.Context JavaDoc;
47 import javax.naming.InitialContext JavaDoc;
48 import javax.naming.NamingException JavaDoc;
49
50 import org.apache.commons.modeler.ManagedBean;
51 import org.apache.commons.modeler.Registry;
52 import org.objectweb.jonas.common.JModule;
53 import org.objectweb.jonas.common.JProp;
54 import org.objectweb.jonas.common.Log;
55 import org.objectweb.jonas.jmx.J2eeObjectName;
56 import org.objectweb.jonas.jmx.JmxService;
57 import org.objectweb.jonas.jmx.JonasObjectName;
58 import org.objectweb.jonas.jtm.TransactionService;
59 import org.objectweb.jonas.management.JonasMBeanTools;
60 import org.objectweb.jonas.service.AbsServiceImpl;
61 import org.objectweb.jonas.service.ServiceException;
62 import org.objectweb.jonas.service.ServiceManager;
63 import org.objectweb.util.monolog.api.BasicLevel;
64 import org.objectweb.util.monolog.api.Logger;
65
66 /**
67  * DatabaseService acts as a factory for the DataSource objects.
68  * Its goal is to create such objects and to register them in JNDI
69  *
70  * @author Philippe Durieux
71  *
72  * Contributor(s):
73  *
74  * 00/18/04 Jun Inamori (j-office@osa.att.ne.jp)
75  * New implementation of unbindDataSources for closing correctly
76  * all connections at server shutdown.<p>
77  * 03/01/14 Adriana Danes <p>
78  * Highlight Configuration properties
79  * Change createDataSource() signature : take additional argument, the datasource name
80  * Manage binded datasources (mapping of jndi name to datasource name)
81  * Use datasource name for naming MBeans (instead of jndi name)
82  * Modify MBean methods to take into account the previous points.<p>
83  * 03/05/25 Introduce pool size configuration
84  * 04/09/20 Create JSR77 MBeans JDBCResource, JDBCDataSource JDBCDriver
85  */

86 public class DataBaseServiceImpl extends AbsServiceImpl implements DataBaseService, DataBaseServiceImplMBean {
87
88     static private Logger logger = null;
89
90     // List of DataSource in use.
91
private Vector JavaDoc cmList = new Vector JavaDoc(); // ConnectionManager
92

93     // Binded Datasource (jndi name -> datasource name)
94
private Hashtable JavaDoc bindedDatasources = new Hashtable JavaDoc();
95
96     // Names of the DataSource to create when starting the DataBase service
97
private Vector JavaDoc dataSourceNames = new Vector JavaDoc();
98
99     // Transaction Service reference
100
private TransactionService transactionService = null;
101
102     // Initial Context for Naming
103
private Context JavaDoc ictx = null;
104
105     // The DataBase service configuration parameters
106
static final String JavaDoc DATASOURCES = "jonas.service.dbm.datasources";
107     static final String JavaDoc CLASS = "jonas.service.dbm.class";
108
109     // The Datasource configuration parameters
110
static final String JavaDoc NAME = "datasource.name"; // JNDI name of the datasource
111
static final String JavaDoc CLASSNAME = "datasource.classname";
112     static final String JavaDoc DEF_CLASSNAME = "no class name";
113     static final String JavaDoc URL = "datasource.url";
114     static final String JavaDoc DEF_URL = "no url";
115     static final String JavaDoc DESCRIPTION = "datasource.description";
116     static final String JavaDoc DEF_DESCRIPTION = "no desc";
117     static final String JavaDoc USERNAME = "datasource.username";
118     static final String JavaDoc DEF_USERNAME = "";
119     static final String JavaDoc PASSWORD = "datasource.password";
120     static final String JavaDoc DEF_PASSWORD = "";
121     static final String JavaDoc ISOLATIONLEVEL = "datasource.isolationlevel";
122     static final String JavaDoc DEF_ISOLATIONLEVEL = "";
123     static final String JavaDoc MAPPERNAME = "datasource.mapper";
124     static final String JavaDoc DEF_MAPPERNAME = "rdb";
125
126     // The ConnectionManager configuration parameters
127
public static final String JavaDoc CONNCHECKLEVEL = "jdbc.connchecklevel";
128     static final String JavaDoc DEF_CONNCHECKLEVEL = "1";
129     public static final String JavaDoc CONNMAXAGE = "jdbc.connmaxage";
130     static final String JavaDoc DEF_CONNMAXAGE = "1440";
131     public static final String JavaDoc MAXOPENTIME = "jdbc.maxopentime";
132     static final String JavaDoc DEF_MAXOPENTIME = "1440";
133     public static final String JavaDoc CONNTESTSTMT = "jdbc.connteststmt";
134     static final String JavaDoc DEF_CONNTESTSTMT = "SELECT 1";
135     static final String JavaDoc MINCONPOOL = "jdbc.minconpool";
136     static final String JavaDoc DEF_MINCONPOOL = "0";
137     static final String JavaDoc MAXCONPOOL = "jdbc.maxconpool";
138     static final String JavaDoc DEF_MAXCONPOOL = "-1";
139     static final String JavaDoc MAXWAITTIME = "jdbc.maxwaittime";
140     static final String JavaDoc DEF_MAXWAITTIME = "10";
141     static final String JavaDoc MAXWAITERS = "jdbc.maxwaiters";
142     static final String JavaDoc DEF_MAXWAITERS = "1000";
143     static final String JavaDoc SAMPLINGPERIOD = "jdbc.samplingperiod";
144     static final String JavaDoc DEF_SAMPLINGPERIOD = "60";
145
146     // JSR 77 variables
147
// ----------------
148
/**
149      * Our naming convention for JDBCResource ObjectName (value of the 'name' key property)
150      */

151     public static final String JavaDoc JDBCResourceName = "JDBCResource";
152     /**
153      * Reference of the JDBCResource MBeans
154      */

155     private JDBCResource jdbcResourceMBean = null;
156     private Registry oRegistry = null;
157     private ManagedBean oManaged = null;
158     private ModelMBean JavaDoc oMBean = null;
159     private MBeanServer JavaDoc mbeanServer = null;
160     private String JavaDoc domainName = null;
161     private String JavaDoc serverName = null;
162
163
164     // -------------------------------------------------------------------
165
// Service Implementation
166
// -------------------------------------------------------------------
167

168     /**
169      * Initialization of the DataBase service.
170      * Configuration information is passed thru a Context object.
171      */

172     public void doInit(Context JavaDoc ctx) throws ServiceException {
173         logger = Log.getLogger(Log.JONAS_DBM_PREFIX);
174         // Avoid using NamingManager here: performance is not a goal here.
175
try {
176             ictx = new InitialContext JavaDoc();
177         } catch (NamingException JavaDoc e) {
178             logger.log(BasicLevel.ERROR, "Cannot create initial context when DataBase service initializing");
179             throw new ServiceException("Cannot create initial context when DataBase service initializing", e);
180         }
181
182         // Get the list of the datasource names
183
String JavaDoc ds = null;
184         try {
185             ds = (String JavaDoc) ctx.lookup(DATASOURCES);
186         } catch (NamingException JavaDoc e) {
187             // No problem if there is no value for 'datasources'
188
}
189         if (ds != null) {
190             StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(ds, ",");
191             while (st.hasMoreTokens()) {
192                 dataSourceNames.add(st.nextToken().trim());
193             }
194         }
195
196         // Get un reference to the Transaction service
197
try {
198             transactionService =
199                 (TransactionService) ServiceManager.getInstance().getTransactionService();
200         } catch (ServiceException se) {
201             logger.log(BasicLevel.ERROR, "Cannot get the Transaction service: ");
202             throw se;
203         } catch (Exception JavaDoc e) {
204             logger.log(BasicLevel.ERROR, "Cannot get the Transaction service");
205             throw new ServiceException("Cannot get the Transaction service: ", e);
206         }
207
208         // Get the JMX Server via JMX Service
209
try {
210             mbeanServer =
211                 ((JmxService) ServiceManager.getInstance().getJmxService()).getJmxServer();
212         } catch (Exception JavaDoc e) {
213             // the JMX service may not be started
214
mbeanServer = null;
215         }
216         // Use Jakarta Common Modeler API
217
oRegistry = JonasMBeanTools.getRegistry();
218         if (logger.isLoggable(BasicLevel.DEBUG)) {
219             logger.log(BasicLevel.DEBUG, "DataBaseService initialized");
220         }
221     }
222
223     /**
224      * Starting DataBase service
225      * Initialization of the service is already done.
226      */

227     public void doStart() throws ServiceException {
228         /*
229          * Create JDBCResource MBean associated by the dbm service.
230          * This bean has to be created before the creation of the connection manager objects
231          * beacuse the creation of cms implies the creation of JDBCDataSource MBeans to
232          * be attached to the JDBCResource MBeans
233          */

234         domainName = getDomainName();
235         serverName = getJonasServerName();
236         if (mbeanServer != null) {
237             try {
238                 ObjectName JavaDoc onJDBCResource = J2eeObjectName.JDBCResource(domainName, serverName, JDBCResourceName);
239                 jdbcResourceMBean = new JDBCResource(onJDBCResource.toString());
240                 oManaged = oRegistry.findManagedBean("JDBCResource");
241                 oMBean = oManaged.createMBean(jdbcResourceMBean);
242                 if (logger.isLoggable(BasicLevel.DEBUG)) {
243                     logger.log(BasicLevel.DEBUG, "JDBResource J2EEResource created");
244                 }
245                 mbeanServer.registerMBean(oMBean, onJDBCResource);
246             } catch (Exception JavaDoc e) {
247                 logger.log(BasicLevel.ERROR, "JOnAS: Cannot register JDBCResource mBean", e);
248             }
249         }
250
251         // Creates connection managers for each datasource
252
String JavaDoc dsName = null;
253         for (int i = 0; i < dataSourceNames.size(); i++) {
254             dsName = (String JavaDoc) dataSourceNames.elementAt(i);
255             try {
256                 JProp prop = JProp.getInstance(dsName);
257                 if (logger.isLoggable(BasicLevel.DEBUG)) {
258                     logger.log(BasicLevel.DEBUG, "Creating Datasource " + dsName);
259                 }
260                 createDataSource(dsName, prop.getConfigFileEnv());
261             } catch (Exception JavaDoc e) {
262                 logger.log(BasicLevel.ERROR, "JOnAS: Cannot create datasource: '" + dsName + "'", e);
263                 logger.log(BasicLevel.ERROR, "Please check if " + dsName + ".properties is available");
264             }
265         }
266
267         // Register DataBaseService MBean
268
try {
269             mbeanServer.registerMBean(this, JonasObjectName.databaseService());
270         } catch (ServiceException se) {
271             // Jmx Service not available, do nothing
272
} catch (Exception JavaDoc e) {
273             logger.log(BasicLevel.ERROR, "DataBaseService: Cannot start the DataBase service");
274             throw new ServiceException("DataBaseService: Cannot start the DataBase service", e);
275         }
276         if (logger.isLoggable(BasicLevel.DEBUG)) {
277             logger.log(BasicLevel.DEBUG, "DataBaseService started");
278         }
279     }
280
281     /**
282      * Stopping DataBase service
283      * Unbind Datasource
284      */

285     public void doStop() throws ServiceException {
286         try {
287             unbindDataSources();
288         } catch (NamingException JavaDoc e) {
289             logger.log(BasicLevel.ERROR, "Cannot unbind datasources");
290             throw new ServiceException("Cannot unbind datasources ", e);
291         }
292
293         try {
294             if (mbeanServer != null) {
295                 // unregister DatabaseService MBean
296
mbeanServer.unregisterMBean(JonasObjectName.databaseService());
297                 // unregister JDBCResource
298
ObjectName JavaDoc onJDBCResource = J2eeObjectName.JDBCResource(domainName, serverName, JDBCResourceName);
299                 mbeanServer.unregisterMBean(onJDBCResource);
300             }
301         } catch (ServiceException se) {
302             // Jmx Service not available, do nothing
303
} catch (Exception JavaDoc e) {
304             logger.log(BasicLevel.ERROR, "EJBService: Cannot stop the DataBase service");
305             throw new ServiceException("DataBaseService: Cannot stop the DataBase service",e);
306         }
307         if (logger.isLoggable(BasicLevel.DEBUG)) {
308             logger.log(BasicLevel.DEBUG, "DataBaseService stopped");
309         }
310     }
311
312     // -------------------------------------------------------------------
313
// DataBaseService Implementation
314
// -------------------------------------------------------------------
315

316     /**
317      * Creates a ConnectionManager (implementation of sql.dataSource).
318      * @param datasourceName - datasource name
319      * @param dsd - a set of properties that describes a dataSource and the ConnectionPool
320      */

321     public void createDataSource(String JavaDoc datasourceName, Properties JavaDoc dsd) throws Exception JavaDoc {
322         // Get properties common to all DataSource types
323
String JavaDoc dsName = dsd.getProperty(NAME); // JDNI name of the datasource
324
if (dsName != null) {
325             dsName = dsName.trim();
326         } else {
327             logger.log(BasicLevel.ERROR, "");
328             throw new ServiceException("Cannot create datasource as JNDI name not provided");
329         }
330         String JavaDoc className = dsd.getProperty(CLASSNAME, DEF_CLASSNAME).trim();
331         String JavaDoc url = dsd.getProperty(URL, DEF_URL).trim();
332         String JavaDoc description = dsd.getProperty(DESCRIPTION, DEF_DESCRIPTION).trim();
333         String JavaDoc user = dsd.getProperty(USERNAME, DEF_USERNAME).trim();
334         String JavaDoc password = dsd.getProperty(PASSWORD, DEF_PASSWORD).trim();
335         String JavaDoc connCheckLevel = dsd.getProperty(CONNCHECKLEVEL, DEF_CONNCHECKLEVEL).trim();
336         String JavaDoc connMaxAge = dsd.getProperty(CONNMAXAGE, DEF_CONNMAXAGE).trim();
337         String JavaDoc maxOpenTime = dsd.getProperty(MAXOPENTIME, DEF_MAXOPENTIME).trim();
338         String JavaDoc minconpool = dsd.getProperty(MINCONPOOL, DEF_MINCONPOOL).trim();
339         String JavaDoc maxconpool = dsd.getProperty(MAXCONPOOL, DEF_MAXCONPOOL).trim();
340         String JavaDoc maxwaittime = dsd.getProperty(MAXWAITTIME, DEF_MAXWAITTIME).trim();
341         String JavaDoc maxwaiters = dsd.getProperty(MAXWAITERS, DEF_MAXWAITERS).trim();
342         String JavaDoc samplingperiod = dsd.getProperty(SAMPLINGPERIOD, DEF_SAMPLINGPERIOD).trim();
343         String JavaDoc defaultStatement = dsd.getProperty(CONNTESTSTMT, DEF_CONNTESTSTMT).trim();
344
345         // Create ConnectionManager (JOnAS DataSource)
346
if (logger.isLoggable(BasicLevel.DEBUG)) {
347             logger.log(BasicLevel.DEBUG, "create JOnAS ConnectionManager corresponding to data source " + datasourceName + " with JNDI name " + dsName);
348         }
349         ConnectionManager ds = new ConnectionManager();
350
351         // Initialize ConnectionManager
352
ds.setDatasourceName(datasourceName); // set datasource name
353
ds.setDSName(dsName); // set jndi name
354
ds.setUrl(url);
355         ds.setClassName(className);
356         ds.setUserName(user);
357         ds.setPassword(password);
358         ds.setTransactionIsolation(dsd.getProperty(ISOLATIONLEVEL, DEF_ISOLATIONLEVEL).trim());
359         ds.setMapperName(dsd.getProperty(MAPPERNAME, DEF_MAPPERNAME).trim());
360         ds.setDataSourceDescription(description);
361         ds.poolConfigure(connCheckLevel, connMaxAge, maxOpenTime, defaultStatement,
362                          minconpool, maxconpool, maxwaittime, maxwaiters, samplingperiod);
363
364         // Register ConnectionManager in JNDI and in internal data structures
365
cmList.addElement(ds);
366         ictx.rebind(dsName, ds);
367         // allows for getting the data source name from the jndi name
368
bindedDatasources.put(dsName, datasourceName);
369         logger.log(BasicLevel.INFO, "Mapping ConnectionManager " + url + " on " + dsName);
370
371         try {
372             // --------------------------
373
// Register MBeans cf. JSR 77
374
// --------------------------
375
if (mbeanServer != null) {
376                 // Available DataSources and Drivers
377
// ------------------------------------------------------------
378
// DataSource MBean
379
// -----------------------
380
String JavaDoc jdbcDataSourceName = datasourceName;
381                 ObjectName JavaDoc onJDBCDataSource =
382                     J2eeObjectName.getJDBCDataSource(domainName,
383                                                      serverName,
384                                                      jdbcDataSourceName);
385                 JDBCDataSource jdbcDataSourceMBean = new JDBCDataSource(onJDBCDataSource.toString(), ds);
386                 oManaged = oRegistry.findManagedBean("JDBCDataSource");
387                 oMBean = oManaged.createMBean(jdbcDataSourceMBean);
388                 if (logger.isLoggable(BasicLevel.DEBUG)) {
389                     logger.log(BasicLevel.DEBUG, "JDBCDataSource created");
390                 }
391                 mbeanServer.registerMBean(oMBean, onJDBCDataSource);
392
393                 // Update the list of dataSources in the JDBCResource MBean with the JDBCDataSource
394
// MBean's OBJECT_NAME
395
jdbcResourceMBean.addJdbcDataSource(onJDBCDataSource.toString());
396
397                 // JDBCDriver MBean
398
// ------------------------------
399
String JavaDoc jdbcDriverName = "aJDBCDriver-" + jdbcDataSourceName; // TO BE IMPLEMENTED
400
ObjectName JavaDoc onJDBCDriver =
401                     J2eeObjectName.getJDBCDriver(domainName,
402                                                  serverName,
403                                                  jdbcDriverName);
404                 JDBCDriver jdbcDriverMBean =
405                     new JDBCDriver(onJDBCDriver.toString());
406                 jdbcDriverMBean.setDriverClassName(className);
407                 oManaged = oRegistry.findManagedBean("JDBCDriver");
408                 oMBean = oManaged.createMBean(jdbcDriverMBean);
409                 if (logger.isLoggable(BasicLevel.DEBUG)) {
410                     logger.log(BasicLevel.DEBUG, "JDBCDriver created");
411                 }
412                 mbeanServer.registerMBean(oMBean, onJDBCDriver);
413
414                 // Update the JDBC DataSource with the JDBC Driver
415
jdbcDataSourceMBean.setJdbcDriver(onJDBCDriver.toString());
416                 if (logger.isLoggable(BasicLevel.DEBUG)) {
417                     logger.log(BasicLevel.DEBUG, "JDBCDataSource updated");
418                 }
419
420             } // end JMX registration
421
} catch (ServiceException se) {
422             // Jmx Service not available, do nothing
423
}
424
425     }
426
427     /**
428      * Unbind dataSource names from the registry, unregister MBeans
429      */

430     public void unbindDataSources() throws NamingException JavaDoc {
431
432         logger.log(BasicLevel.DEBUG, "");
433
434         try {
435             if (cmList.size() > 0) {
436                 String JavaDoc dsn = null;
437                 for (Enumeration JavaDoc lk = cmList.elements(); lk.hasMoreElements();) {
438                     ConnectionManager cm = (ConnectionManager) lk.nextElement();
439                     cm.closeAllConnection();
440                     dsn = cm.getDSName();
441                     ictx.unbind(dsn);
442                     bindedDatasources.remove(dsn);
443                 }
444             }
445         } catch (NamingException JavaDoc e) {
446             logger.log(BasicLevel.ERROR, "cannot unbind DataSources", e);
447             throw e;
448         }
449
450         // --------------------------
451
// unregister MBeans cf. JSR 77
452
// --------------------------
453
if (mbeanServer != null) {
454             try {
455                 // Get JDBCDataSource OBJECT_NAMEs
456
String JavaDoc[] ons = jdbcResourceMBean.getJdbcDataSources();
457                 ObjectName JavaDoc onJDBCDataSource = null;
458                 ObjectName JavaDoc onJDBCDriver = null;
459                 for (int i = 0; i < ons.length; i++) {
460                     onJDBCDataSource = ObjectName.getInstance(ons[i]);
461                     String JavaDoc jdbcDriverName = (String JavaDoc) mbeanServer.getAttribute(onJDBCDataSource, "jdbcDriver");
462                     onJDBCDriver = new ObjectName JavaDoc(jdbcDriverName);
463                     // Unregister JDBCDataSource MBean
464
mbeanServer.unregisterMBean(onJDBCDataSource);
465                     // Unregister JDBCDriver MBean
466
mbeanServer.unregisterMBean(onJDBCDriver);
467                     // Update the list of dataSources in the JDBCResource MBean
468
jdbcResourceMBean.removeJdbcDataSource(onJDBCDataSource.toString());
469                 }
470             } catch (MalformedObjectNameException JavaDoc ma) {
471                 logger.log(BasicLevel.ERROR, "Cannot cleanly unregister DataSource", ma);
472             } catch (MBeanRegistrationException JavaDoc mr) {
473                 logger.log(BasicLevel.ERROR, "Cannot cleanly unregister DataSource", mr);
474             } catch (InstanceNotFoundException JavaDoc infe) {
475                 logger.log(BasicLevel.ERROR, "Cannot cleanly unregister DataSource", infe);
476             } catch (Exception JavaDoc e) {
477                 logger.log(BasicLevel.ERROR, "Cannot cleanly unregister DataSource", e);
478             }
479         }
480     }
481
482     /**
483      * get ConnectionManager for the datasource having this JNDI name.
484      */

485     public ConnectionManager getConnectionManager(String JavaDoc dsname) {
486         ConnectionManager cm = null;
487         if (cmList.size() > 0) {
488             for (Enumeration JavaDoc lk = cmList.elements(); lk.hasMoreElements(); ) {
489                 cm = (ConnectionManager)lk.nextElement();
490                 if (cm.getDSName().equals(dsname)) {
491                     return cm;
492                 }
493             }
494         }
495         return null;
496     }
497
498     /**
499      * return the list of the datasources
500      * @return
501      */

502     public Collection JavaDoc getDSList() {
503         return cmList;
504     }
505     
506     //------------------------------------------------------------------------------------------
507

508     /**
509      * MBean method:
510      * @return the list of properties files describing datasources found in JONAS_BASE/conf
511      */

512     public List JavaDoc getDataSourcePropertiesFiles() throws Exception JavaDoc {
513         return JModule.getDatasourcePropsInDir();
514     }
515
516     /**
517      * MBean method:
518      * @return Integer Total Number of Datasource available in JOnAS
519      */

520     public Integer JavaDoc getCurrentNumberOfDataSource() {
521         return new Integer JavaDoc(cmList.size());
522     }
523
524     /**
525      * MBean method:
526      * @return Integer Total Number of JDBC connection open
527      */

528     public Integer JavaDoc getTotalCurrentNumberOfJDBCConnectionOpen(){
529         int result = 0;
530         if (cmList.size() > 0) {
531             for (Enumeration JavaDoc lk = cmList.elements(); lk.hasMoreElements(); ) {
532                 ConnectionManager cm = (ConnectionManager)lk.nextElement();
533                 result += cm.getPool().getCurrentOpened();
534             }
535         }
536         return new Integer JavaDoc(result);
537     }
538
539     /**
540      * MBean method:
541      * @return true if the datasource having thid jndi name is loadd
542      */

543     public boolean isLoadedDataSource(String JavaDoc dsName) {
544         boolean result = false;
545         if (cmList.size() > 0) {
546             for (Enumeration JavaDoc lk = cmList.elements(); lk.hasMoreElements(); ) {
547                 ConnectionManager cm = (ConnectionManager)lk.nextElement();
548                 if (cm.getDatasourceName().equals(dsName))
549                     return true;
550             }
551         }
552         return result;
553     }
554
555     /**
556      * MBean method:
557      * @param name of the data source to unload
558      */

559     public void unloadDataSource(String JavaDoc name) {
560         logger.log(BasicLevel.DEBUG, "");
561         try {
562             if (cmList.size() > 0) {
563                 for (Enumeration JavaDoc lk = cmList.elements(); lk.hasMoreElements();) {
564                     ConnectionManager cm = (ConnectionManager) lk.nextElement();
565                     String JavaDoc dsName = cm.getDatasourceName();
566                     String JavaDoc jndiName = cm.getDSName();
567                     if (dsName.equals(name)) {
568                         // unbind data source
569
cm.closeAllConnection();
570                         ictx.unbind(jndiName);
571                         // remove datasource
572
cmList.remove(cm);
573                         if (mbeanServer != null) {
574                             ObjectName JavaDoc onJDBCDataSource = J2eeObjectName.getJDBCDataSource(domainName, serverName, dsName);
575                             String JavaDoc jdbcDriverName = (String JavaDoc) mbeanServer.getAttribute(onJDBCDataSource, "jdbcDriver");
576                             ObjectName JavaDoc onJDBCDriver = new ObjectName JavaDoc(jdbcDriverName);
577                             // Unregister JDBCDataSource MBean
578
mbeanServer.unregisterMBean(onJDBCDataSource);
579                             // Unregister JDBCDriver MBean
580
mbeanServer.unregisterMBean(onJDBCDriver);
581                             // Update the list of dataSources in the JDBCResource MBean
582
jdbcResourceMBean.removeJdbcDataSource(onJDBCDataSource.toString());
583                         }
584                         return;
585                     }
586                 }
587             }
588         } catch (Exception JavaDoc e) {
589             logger.log(BasicLevel.ERROR, "cannot unload DataSources", e);
590         }
591     }
592
593     /**
594      * MBean method:
595      * @return datasource properties from a local file
596      */

597     public Properties JavaDoc getDataSourcePropertiesFile(String JavaDoc dsFile) throws Exception JavaDoc {
598         try {
599             return JProp.getInstance(dsFile).getConfigFileEnv();
600
601         } catch (Exception JavaDoc e) {
602             if (e instanceof FileNotFoundException JavaDoc) {
603                 logger.log(BasicLevel.ERROR, "Please check if '"+dsFile+".properties' is available in JONAS_BASE/conf/ directory");
604             } else {
605                 logger.log(BasicLevel.ERROR, "Error occured when reading file " + dsFile);
606             }
607             throw e;
608         }
609     }
610
611     /**
612      * MBean method:
613      * load a new datasource
614      * @param name datasource name
615      * @param prop datasource properties
616      */

617     public void loadDataSource(String JavaDoc name, Properties JavaDoc prop, Boolean JavaDoc loadFromFile) throws ServiceException {
618         boolean fromFile = loadFromFile.booleanValue();
619
620         if (fromFile) {
621             logger.log(BasicLevel.DEBUG, "Load data source named " + name + " from file");
622         } else {
623             logger.log(BasicLevel.DEBUG, "Load data source named " + name + " from form");
624             if (isLoadedDataSource(name)) {
625                 logger.log(BasicLevel.DEBUG, "This data source, " + name + " is already loaded ; Unload it !");
626                 unloadDataSource(name);
627             }
628             try {
629                 logger.log(BasicLevel.DEBUG, "Call getInstance on JProp in order to create the properties file");
630                 JProp.getInstance(name, prop);
631             } catch (Exception JavaDoc e) {
632                 logger.log(BasicLevel.ERROR, "Cannot create datasource " + name + " as cannot create properties file");
633                 throw new ServiceException("DatabaseService: Cannot create datasource '" + name + "'", e);
634             }
635         }
636
637         try {
638             logger.log(BasicLevel.DEBUG, "Call method to create a data source");
639             createDataSource(name, prop);
640             logger.log(BasicLevel.DEBUG, "New data source created");
641         } catch (Exception JavaDoc e) {
642             logger.log(BasicLevel.ERROR, "Cannot create datasource '" + name + "'.");
643             throw new ServiceException("DatabaseService: Cannot create datasource: " + name + "'", e);
644         }
645     }
646
647     /**
648      * MBean method allowing to determine the datasource name from its jndi name
649      * @param jndiName The jndi name of a datasource
650      * @return The datasource name
651      */

652     public String JavaDoc getDatasourceName(String JavaDoc jndiName) {
653         return (String JavaDoc)bindedDatasources.get(jndiName);
654     }
655
656 }
657
658
Popular Tags