1 22 package org.jboss.jdo.castor; 23 24 import java.io.PrintWriter ; 25 import java.io.Serializable ; 26 import java.io.FileNotFoundException ; 27 28 import java.lang.reflect.Method ; 29 30 import java.util.HashMap ; 31 import java.util.Hashtable ; 32 33 import java.util.Enumeration ; 34 import java.net.URL ; 35 36 import javax.management.*; 37 import javax.naming.spi.ObjectFactory ; 38 import javax.naming.Referenceable ; 39 import javax.naming.Reference ; 40 import javax.naming.Context ; 41 import javax.naming.InitialContext ; 42 import javax.naming.Name ; 43 import javax.naming.NamingException ; 44 import javax.naming.NameNotFoundException ; 45 46 import org.xml.sax.EntityResolver ; 47 import org.xml.sax.InputSource ; 48 49 import org.exolab.castor.jdo.Database; 50 import org.exolab.castor.jdo.DataObjects; 51 import org.exolab.castor.jdo.JDO; 52 import org.exolab.castor.jdo.DatabaseNotFoundException; 53 import org.exolab.castor.jdo.PersistenceException; 54 import org.exolab.castor.persist.spi.LogInterceptor; 55 import org.exolab.castor.xml.Unmarshaller; 56 57 import org.jboss.logging.util.LoggerPluginWriter; 58 59 import org.jboss.system.ServiceMBeanSupport; 60 61 62 71 public class CastorJDOImpl 72 extends ServiceMBeanSupport 73 implements DataObjects, ObjectFactory , Referenceable , Serializable , 74 CastorJDOImplMBean, MBeanRegistration, LogInterceptor 75 { 76 77 private String _jndiName; 78 79 private String _dbConf; 80 81 private String _dbUrl; 82 83 private JDO _jdo = new JDO(); 84 85 private String _dataSourceName; 86 87 private static HashMap _instances = new HashMap (); 88 89 private transient PrintWriter writer; 90 91 95 private boolean _commonClassPath; 96 97 101 private boolean _autoStore = false; 102 103 108 private boolean _dbPooling = false; 109 110 public CastorJDOImpl() { 111 } 112 113 protected ObjectName getObjectName(MBeanServer server, ObjectName name) 114 throws javax.management.MalformedObjectNameException 115 { 116 if (name == null) { 117 return new ObjectName(OBJECT_NAME+",name="+_jndiName); 118 } 119 120 return name; 121 } 122 123 protected void startService() throws Exception 124 { 125 org.exolab.castor.jdo.conf.Database database; 126 Unmarshaller unm; 127 int pos; 128 Method m; 129 130 boolean debug = log.isDebugEnabled(); 131 132 bind(new InitialContext (), "java:/" + _jndiName, this); 134 135 URL confUrl = Thread.currentThread().getContextClassLoader().getResource( _dbConf ); 137 138 if ( null == confUrl ) 139 { 140 FileNotFoundException e = new FileNotFoundException ( 141 "CastorJDOImpl.startService(): Unable to resolve Configuration attribute to URL = " 142 + _dbConf ); 143 log.error( "CastorJDOImpl.startService(): Unable to find " + _dbConf + " file.", e ); 144 } 145 146 _dbUrl = confUrl.toString(); 147 148 _jdo.setTransactionManager("java:/TransactionManager"); 149 _jdo.setConfiguration( _dbUrl ); 150 unm = new Unmarshaller(org.exolab.castor.jdo.conf.Database.class); 151 database = (org.exolab.castor.jdo.conf.Database) unm.unmarshal(new InputSource ( _dbUrl )); 152 _jdo.setDatabaseName(database.getName()); 153 if (database.getJndi() != null) { 154 _dataSourceName = database.getJndi().getName(); 155 } 156 try { 161 m = _jdo.getClass().getMethod("setAutoStore", 163 new Class [] {boolean.class}); 164 m.invoke(_jdo, new Object [] {new Boolean (_autoStore)}); 165 } catch (Exception ex) { 166 if (debug) 167 log.debug("couldn't invoke setAutoStore()"); 168 } 169 170 try { 171 m = _jdo.getClass().getMethod("setDatabasePooling", 173 new Class [] {boolean.class}); 174 m.invoke(_jdo, new Object [] {new Boolean (_dbPooling)}); 175 } catch (Exception ex) { 176 if (debug) 177 log.debug("couldn't invoke setDatabasePooling()"); 178 } 179 _instances.put(_jndiName, this); 180 if (debug) 181 log.debug("DataObjects factory for " + _dataSourceName + " bound to " + _jndiName); 182 } 183 184 protected void stopService() throws Exception 185 { 186 InitialContext ctx = new InitialContext (); 188 try { 189 ctx.unbind("java:/" + _jndiName); 190 } 191 finally { 192 ctx.close(); 193 } 194 } 195 196 197 200 public URL findResourceInJar( String name ) 201 { 202 URL url = null; 203 204 try 205 { 206 url = getClass().getClassLoader().getResource( name ); 207 } 208 catch ( Exception e ) 209 { 210 log.error( "Could not find resource: " + name, e ); 211 } 212 213 return url; 214 } 215 216 217 219 222 public void setJndiName(String jndiName) { 223 _jndiName = jndiName; 224 } 225 226 229 public String getJndiName() { 230 return _jndiName; 231 } 232 233 236 public void setConfiguration(String dbConf) { 237 _dbConf = dbConf; 238 } 239 240 243 public String getConfiguration() { 244 return _dbConf; 245 } 246 247 250 public String getConfigurationURL() { 251 return _dbUrl; 252 } 253 254 257 public void setLockTimeout(int lockTimeout) { 258 _jdo.setLockTimeout(lockTimeout); 259 } 260 261 264 public int getLockTimeout() { 265 return _jdo.getLockTimeout(); 266 } 267 268 271 public void setLoggingEnabled(boolean loggingEnabled) { 272 _jdo.setLogInterceptor(loggingEnabled ? this : null); 273 } 274 275 278 public boolean getLoggingEnabled() { 279 return (_jdo.getLogInterceptor() != null); 280 } 281 282 285 public void setCommonClassPath(boolean commonClassPath) { 286 _commonClassPath = commonClassPath; 287 } 288 289 292 public boolean getCommonClassPath() { 293 return _commonClassPath; 294 } 295 296 302 public void setAutoStore( boolean autoStore ) { 303 _autoStore = autoStore; 304 } 305 306 311 public boolean isAutoStore() { 312 return _autoStore; 313 } 314 315 321 public void setDatabasePooling(boolean dbPooling) { 322 _dbPooling = dbPooling; 323 } 324 325 330 public boolean isDatabasePooling() { 331 return _dbPooling; 332 } 333 334 335 337 public Database getDatabase() 338 throws DatabaseNotFoundException, PersistenceException 339 { 340 Method m; 341 342 if (_commonClassPath) { 343 _jdo.setClassLoader(null); 344 } else { 345 _jdo.setClassLoader(Thread.currentThread().getContextClassLoader()); 346 } 347 return _jdo.getDatabase(); 348 } 349 350 public void setDescription(String description) { 351 _jdo.setDescription(description); 352 } 353 354 public String getDescription() { 355 return _jdo.getDescription(); 356 } 357 358 359 361 public Reference getReference() { 362 return new Reference (getClass().getName(), getClass().getName(), null); 363 } 364 365 366 368 public Object getObjectInstance(Object obj, 369 Name name, 370 Context nameCtx, 371 Hashtable environment) 372 throws Exception 373 { 374 return _instances.get(name.toString()); 375 } 376 377 378 380 private void bind(Context ctx, String name, Object val) 381 throws NamingException 382 { 383 385 Name n = ctx.getNameParser("").parse(name); 386 while (n.size() > 1) 387 { 388 String ctxName = n.get(0); 389 try 390 { 391 ctx = (Context )ctx.lookup(ctxName); 392 } catch (NameNotFoundException e) 393 { 394 ctx = ctx.createSubcontext(ctxName); 395 } 396 n = n.getSuffix(1); 397 } 398 399 ctx.bind(n.get(0), val); 400 } 401 402 403 405 public void loading(Class objClass, Object identity) { 406 if (log.isDebugEnabled()) 407 log.debug( "Loading " + objClass.getName() + " (" + identity + ")" ); 408 } 409 410 public void creating(Class objClass, Object identity) { 411 if (log.isDebugEnabled()) 412 log.debug( "Creating " + objClass.getName() + " (" + identity + ")" ); 413 } 414 415 public void removing(Class objClass, Object identity) { 416 if (log.isDebugEnabled()) 417 log.debug( "Removing " + objClass.getName() + " (" + identity + ")" ); 418 } 419 420 421 public void storing(Class objClass, Object identity) { 422 if (log.isDebugEnabled()) 423 log.debug( "Storing " + objClass.getName() + " (" + identity + ")" ); 424 } 425 426 427 429 public void loading(Object objClass, Object identity) { 430 if (log.isDebugEnabled()) 431 log.debug( "Loading " + objClass + " (" + identity + ")" ); 432 } 433 434 public void creating(Object objClass, Object identity) { 435 if (log.isDebugEnabled()) 436 log.debug( "Creating " + objClass + " (" + identity + ")" ); 437 } 438 439 public void removing(Object objClass, Object identity) { 440 if (log.isDebugEnabled()) 441 log.debug( "Removing " + objClass + " (" + identity + ")" ); 442 } 443 444 public void storing(Object objClass, Object identity) { 445 if (log.isDebugEnabled()) 446 log.debug( "Storing " + objClass + " (" + identity + ")" ); 447 } 448 449 451 public void storeStatement(String statement) { 452 log.debug(statement); 453 } 454 455 public void queryStatement(String statement) { 456 log.debug(statement); 457 } 458 459 public void message(String message) { 460 log.debug(message); 461 } 462 463 public void exception(Exception except) { 464 log.error("Exception", except); 465 } 466 467 public PrintWriter getPrintWriter() 468 { 469 if (writer == null) 470 { 471 writer = new LoggerPluginWriter(log.getLoggerPlugin ()); 472 } 473 474 return writer; 475 } 476 } 477 478 | Popular Tags |