1 25 26 27 package org.objectweb.jonas.dbm; 28 29 import java.io.FileNotFoundException ; 30 import java.util.Collection ; 31 import java.util.Enumeration ; 32 import java.util.Hashtable ; 33 import java.util.Iterator ; 34 import java.util.List ; 35 import java.util.Properties ; 36 import java.util.Set ; 37 import java.util.StringTokenizer ; 38 import java.util.Vector ; 39 40 import javax.management.InstanceNotFoundException ; 41 import javax.management.MBeanRegistrationException ; 42 import javax.management.MBeanServer ; 43 import javax.management.MalformedObjectNameException ; 44 import javax.management.ObjectName ; 45 import javax.management.modelmbean.ModelMBean ; 46 import javax.naming.Context ; 47 import javax.naming.InitialContext ; 48 import javax.naming.NamingException ; 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 86 public class DataBaseServiceImpl extends AbsServiceImpl implements DataBaseService, DataBaseServiceImplMBean { 87 88 static private Logger logger = null; 89 90 private Vector cmList = new Vector (); 93 private Hashtable bindedDatasources = new Hashtable (); 95 96 private Vector dataSourceNames = new Vector (); 98 99 private TransactionService transactionService = null; 101 102 private Context ictx = null; 104 105 static final String DATASOURCES = "jonas.service.dbm.datasources"; 107 static final String CLASS = "jonas.service.dbm.class"; 108 109 static final String NAME = "datasource.name"; static final String CLASSNAME = "datasource.classname"; 112 static final String DEF_CLASSNAME = "no class name"; 113 static final String URL = "datasource.url"; 114 static final String DEF_URL = "no url"; 115 static final String DESCRIPTION = "datasource.description"; 116 static final String DEF_DESCRIPTION = "no desc"; 117 static final String USERNAME = "datasource.username"; 118 static final String DEF_USERNAME = ""; 119 static final String PASSWORD = "datasource.password"; 120 static final String DEF_PASSWORD = ""; 121 static final String ISOLATIONLEVEL = "datasource.isolationlevel"; 122 static final String DEF_ISOLATIONLEVEL = ""; 123 static final String MAPPERNAME = "datasource.mapper"; 124 static final String DEF_MAPPERNAME = "rdb"; 125 126 public static final String CONNCHECKLEVEL = "jdbc.connchecklevel"; 128 static final String DEF_CONNCHECKLEVEL = "1"; 129 public static final String CONNMAXAGE = "jdbc.connmaxage"; 130 static final String DEF_CONNMAXAGE = "1440"; 131 public static final String MAXOPENTIME = "jdbc.maxopentime"; 132 static final String DEF_MAXOPENTIME = "1440"; 133 public static final String CONNTESTSTMT = "jdbc.connteststmt"; 134 static final String DEF_CONNTESTSTMT = "SELECT 1"; 135 static final String MINCONPOOL = "jdbc.minconpool"; 136 static final String DEF_MINCONPOOL = "0"; 137 static final String MAXCONPOOL = "jdbc.maxconpool"; 138 static final String DEF_MAXCONPOOL = "-1"; 139 static final String MAXWAITTIME = "jdbc.maxwaittime"; 140 static final String DEF_MAXWAITTIME = "10"; 141 static final String MAXWAITERS = "jdbc.maxwaiters"; 142 static final String DEF_MAXWAITERS = "1000"; 143 static final String SAMPLINGPERIOD = "jdbc.samplingperiod"; 144 static final String DEF_SAMPLINGPERIOD = "60"; 145 146 151 public static final String JDBCResourceName = "JDBCResource"; 152 155 private JDBCResource jdbcResourceMBean = null; 156 private Registry oRegistry = null; 157 private ManagedBean oManaged = null; 158 private ModelMBean oMBean = null; 159 private MBeanServer mbeanServer = null; 160 private String domainName = null; 161 private String serverName = null; 162 163 164 168 172 public void doInit(Context ctx) throws ServiceException { 173 logger = Log.getLogger(Log.JONAS_DBM_PREFIX); 174 try { 176 ictx = new InitialContext (); 177 } catch (NamingException 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 String ds = null; 184 try { 185 ds = (String ) ctx.lookup(DATASOURCES); 186 } catch (NamingException e) { 187 } 189 if (ds != null) { 190 StringTokenizer st = new StringTokenizer (ds, ","); 191 while (st.hasMoreTokens()) { 192 dataSourceNames.add(st.nextToken().trim()); 193 } 194 } 195 196 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 e) { 204 logger.log(BasicLevel.ERROR, "Cannot get the Transaction service"); 205 throw new ServiceException("Cannot get the Transaction service: ", e); 206 } 207 208 try { 210 mbeanServer = 211 ((JmxService) ServiceManager.getInstance().getJmxService()).getJmxServer(); 212 } catch (Exception e) { 213 mbeanServer = null; 215 } 216 oRegistry = JonasMBeanTools.getRegistry(); 218 if (logger.isLoggable(BasicLevel.DEBUG)) { 219 logger.log(BasicLevel.DEBUG, "DataBaseService initialized"); 220 } 221 } 222 223 227 public void doStart() throws ServiceException { 228 234 domainName = getDomainName(); 235 serverName = getJonasServerName(); 236 if (mbeanServer != null) { 237 try { 238 ObjectName 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 e) { 247 logger.log(BasicLevel.ERROR, "JOnAS: Cannot register JDBCResource mBean", e); 248 } 249 } 250 251 String dsName = null; 253 for (int i = 0; i < dataSourceNames.size(); i++) { 254 dsName = (String ) 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 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 try { 269 mbeanServer.registerMBean(this, JonasObjectName.databaseService()); 270 } catch (ServiceException se) { 271 } catch (Exception 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 285 public void doStop() throws ServiceException { 286 try { 287 unbindDataSources(); 288 } catch (NamingException 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 mbeanServer.unregisterMBean(JonasObjectName.databaseService()); 297 ObjectName onJDBCResource = J2eeObjectName.JDBCResource(domainName, serverName, JDBCResourceName); 299 mbeanServer.unregisterMBean(onJDBCResource); 300 } 301 } catch (ServiceException se) { 302 } catch (Exception 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 316 321 public void createDataSource(String datasourceName, Properties dsd) throws Exception { 322 String dsName = dsd.getProperty(NAME); 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 className = dsd.getProperty(CLASSNAME, DEF_CLASSNAME).trim(); 331 String url = dsd.getProperty(URL, DEF_URL).trim(); 332 String description = dsd.getProperty(DESCRIPTION, DEF_DESCRIPTION).trim(); 333 String user = dsd.getProperty(USERNAME, DEF_USERNAME).trim(); 334 String password = dsd.getProperty(PASSWORD, DEF_PASSWORD).trim(); 335 String connCheckLevel = dsd.getProperty(CONNCHECKLEVEL, DEF_CONNCHECKLEVEL).trim(); 336 String connMaxAge = dsd.getProperty(CONNMAXAGE, DEF_CONNMAXAGE).trim(); 337 String maxOpenTime = dsd.getProperty(MAXOPENTIME, DEF_MAXOPENTIME).trim(); 338 String minconpool = dsd.getProperty(MINCONPOOL, DEF_MINCONPOOL).trim(); 339 String maxconpool = dsd.getProperty(MAXCONPOOL, DEF_MAXCONPOOL).trim(); 340 String maxwaittime = dsd.getProperty(MAXWAITTIME, DEF_MAXWAITTIME).trim(); 341 String maxwaiters = dsd.getProperty(MAXWAITERS, DEF_MAXWAITERS).trim(); 342 String samplingperiod = dsd.getProperty(SAMPLINGPERIOD, DEF_SAMPLINGPERIOD).trim(); 343 String defaultStatement = dsd.getProperty(CONNTESTSTMT, DEF_CONNTESTSTMT).trim(); 344 345 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 ds.setDatasourceName(datasourceName); ds.setDSName(dsName); 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 cmList.addElement(ds); 366 ictx.rebind(dsName, ds); 367 bindedDatasources.put(dsName, datasourceName); 369 logger.log(BasicLevel.INFO, "Mapping ConnectionManager " + url + " on " + dsName); 370 371 try { 372 if (mbeanServer != null) { 376 String jdbcDataSourceName = datasourceName; 381 ObjectName 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 jdbcResourceMBean.addJdbcDataSource(onJDBCDataSource.toString()); 396 397 String jdbcDriverName = "aJDBCDriver-" + jdbcDataSourceName; ObjectName 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 jdbcDataSourceMBean.setJdbcDriver(onJDBCDriver.toString()); 416 if (logger.isLoggable(BasicLevel.DEBUG)) { 417 logger.log(BasicLevel.DEBUG, "JDBCDataSource updated"); 418 } 419 420 } } catch (ServiceException se) { 422 } 424 425 } 426 427 430 public void unbindDataSources() throws NamingException { 431 432 logger.log(BasicLevel.DEBUG, ""); 433 434 try { 435 if (cmList.size() > 0) { 436 String dsn = null; 437 for (Enumeration 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 e) { 446 logger.log(BasicLevel.ERROR, "cannot unbind DataSources", e); 447 throw e; 448 } 449 450 if (mbeanServer != null) { 454 try { 455 String [] ons = jdbcResourceMBean.getJdbcDataSources(); 457 ObjectName onJDBCDataSource = null; 458 ObjectName onJDBCDriver = null; 459 for (int i = 0; i < ons.length; i++) { 460 onJDBCDataSource = ObjectName.getInstance(ons[i]); 461 String jdbcDriverName = (String ) mbeanServer.getAttribute(onJDBCDataSource, "jdbcDriver"); 462 onJDBCDriver = new ObjectName (jdbcDriverName); 463 mbeanServer.unregisterMBean(onJDBCDataSource); 465 mbeanServer.unregisterMBean(onJDBCDriver); 467 jdbcResourceMBean.removeJdbcDataSource(onJDBCDataSource.toString()); 469 } 470 } catch (MalformedObjectNameException ma) { 471 logger.log(BasicLevel.ERROR, "Cannot cleanly unregister DataSource", ma); 472 } catch (MBeanRegistrationException mr) { 473 logger.log(BasicLevel.ERROR, "Cannot cleanly unregister DataSource", mr); 474 } catch (InstanceNotFoundException infe) { 475 logger.log(BasicLevel.ERROR, "Cannot cleanly unregister DataSource", infe); 476 } catch (Exception e) { 477 logger.log(BasicLevel.ERROR, "Cannot cleanly unregister DataSource", e); 478 } 479 } 480 } 481 482 485 public ConnectionManager getConnectionManager(String dsname) { 486 ConnectionManager cm = null; 487 if (cmList.size() > 0) { 488 for (Enumeration 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 502 public Collection getDSList() { 503 return cmList; 504 } 505 506 508 512 public List getDataSourcePropertiesFiles() throws Exception { 513 return JModule.getDatasourcePropsInDir(); 514 } 515 516 520 public Integer getCurrentNumberOfDataSource() { 521 return new Integer (cmList.size()); 522 } 523 524 528 public Integer getTotalCurrentNumberOfJDBCConnectionOpen(){ 529 int result = 0; 530 if (cmList.size() > 0) { 531 for (Enumeration lk = cmList.elements(); lk.hasMoreElements(); ) { 532 ConnectionManager cm = (ConnectionManager)lk.nextElement(); 533 result += cm.getPool().getCurrentOpened(); 534 } 535 } 536 return new Integer (result); 537 } 538 539 543 public boolean isLoadedDataSource(String dsName) { 544 boolean result = false; 545 if (cmList.size() > 0) { 546 for (Enumeration 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 559 public void unloadDataSource(String name) { 560 logger.log(BasicLevel.DEBUG, ""); 561 try { 562 if (cmList.size() > 0) { 563 for (Enumeration lk = cmList.elements(); lk.hasMoreElements();) { 564 ConnectionManager cm = (ConnectionManager) lk.nextElement(); 565 String dsName = cm.getDatasourceName(); 566 String jndiName = cm.getDSName(); 567 if (dsName.equals(name)) { 568 cm.closeAllConnection(); 570 ictx.unbind(jndiName); 571 cmList.remove(cm); 573 if (mbeanServer != null) { 574 ObjectName onJDBCDataSource = J2eeObjectName.getJDBCDataSource(domainName, serverName, dsName); 575 String jdbcDriverName = (String ) mbeanServer.getAttribute(onJDBCDataSource, "jdbcDriver"); 576 ObjectName onJDBCDriver = new ObjectName (jdbcDriverName); 577 mbeanServer.unregisterMBean(onJDBCDataSource); 579 mbeanServer.unregisterMBean(onJDBCDriver); 581 jdbcResourceMBean.removeJdbcDataSource(onJDBCDataSource.toString()); 583 } 584 return; 585 } 586 } 587 } 588 } catch (Exception e) { 589 logger.log(BasicLevel.ERROR, "cannot unload DataSources", e); 590 } 591 } 592 593 597 public Properties getDataSourcePropertiesFile(String dsFile) throws Exception { 598 try { 599 return JProp.getInstance(dsFile).getConfigFileEnv(); 600 601 } catch (Exception e) { 602 if (e instanceof FileNotFoundException ) { 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 617 public void loadDataSource(String name, Properties prop, Boolean 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 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 e) { 642 logger.log(BasicLevel.ERROR, "Cannot create datasource '" + name + "'."); 643 throw new ServiceException("DatabaseService: Cannot create datasource: " + name + "'", e); 644 } 645 } 646 647 652 public String getDatasourceName(String jndiName) { 653 return (String )bindedDatasources.get(jndiName); 654 } 655 656 } 657 658 | Popular Tags |