User: oleg
Date: 00/09/13 08:04:05
Added: castorjdo/src/main/org/jboss/jdo/castor CastorJDOImpl.java
CastorJDOImplMBean.java
Log:
Castor JDO MBean (public draft :-)
Revision Changes Path
1.1
contrib/castorjdo/src/main/org/jboss/jdo/castor/CastorJDOImpl.java
Index: CastorJDOImpl.java
===================================================================
/*
* jBoss, the OpenSource EJB server
*
* Distributable under GPL license.
* See terms of license at gnu.org.
*/
package org.jboss.jdo.castor;
import java.io.PrintWriter;
//import java.lang.reflect.InvocationHandler;
//import java.lang.reflect.Proxy;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Hashtable;
import javax.management.*;
import javax.naming.spi.ObjectFactory;
import javax.naming.Referenceable;
import javax.naming.Reference;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.Name;
import javax.naming.NamingException;
import javax.naming.NameNotFoundException;
import org.xml.sax.EntityResolver;
import org.xml.sax.InputSource;
import org.exolab.castor.jdo.Database;
import org.exolab.castor.jdo.DataObjects;
import org.exolab.castor.jdo.JDO;
import org.exolab.castor.jdo.DatabaseNotFoundException;
import org.exolab.castor.jdo.PersistenceException;
import org.exolab.castor.persist.spi.LogInterceptor;
import org.exolab.castor.xml.Unmarshaller;
import org.jboss.logging.Log;
import org.jboss.logging.Logger;
import org.jboss.util.ServiceMBeanSupport;
import org.jboss.proxy.Proxy;
import org.jboss.proxy.Proxies;
import org.jboss.proxy.InvocationHandler;
/**
* Castor JDO support
*
* @author Oleg Nitz ([EMAIL PROTECTED])
* @version $Revision: 1.1 $
*/
public class CastorJDOImpl extends ServiceMBeanSupport
implements DataObjects, ObjectFactory, Referenceable,
CastorJDOImplMBean, MBeanRegistration, LogInterceptor {
private String _jndiName;
private JDO _jdo = new JDO();
private static HashMap instances = new HashMap();
public CastorJDOImpl() {
// Instance being used as ObjectFactory
}
public CastorJDOImpl(String dbConf, String jndiName, Integer lockTimeout,
Boolean logging) {
org.exolab.castor.jdo.conf.Database database;
Unmarshaller unm;
int pos;
int timeout = 0;
_jndiName = jndiName;
_jdo.setTransactionManager("TransactionManager");
_jdo.setConfiguration(dbConf);
try {
unm = new Unmarshaller(org.exolab.castor.jdo.conf.Database.class);
database = (org.exolab.castor.jdo.conf.Database) unm.unmarshal(new
InputSource(dbConf));
_jdo.setDatabaseName(database.getName());
} catch (Exception ex) {
log.error("Cannot read " + dbConf + ": " + ex);
}
if (lockTimeout != null) {
timeout = lockTimeout.intValue();
}
if (timeout > 0) {
_jdo.setLockTimeout(timeout);
}
if (logging != null && logging.booleanValue()) {
_jdo.setLogInterceptor(this);
}
instances.put(jndiName, this);
}
public ObjectName getObjectName(MBeanServer server, ObjectName name)
throws javax.management.MalformedObjectNameException {
return new ObjectName(OBJECT_NAME+",name="+_jndiName);
}
public String getName() {
return "CastorJDO";
}
public void initService() throws Exception {
// Bind in JNDI
bind(new InitialContext(), _jndiName, this);
}
public void stopService() {
// Unbind from JNDI
try {
new InitialContext().unbind(_jndiName);
} catch (NamingException e) {
}
}
// DataObjects implementation ----------------------------------
public Database getDatabase()
throws DatabaseNotFoundException, PersistenceException {
_jdo.setClassLoader(Thread.currentThread().getContextClassLoader());
return _jdo.getDatabase();
}
public void setDescription(String description) {
_jdo.setDescription(description);
}
public String getDescription() {
return _jdo.getDescription();
}
// Referenceable implementation ----------------------------------
public Reference getReference() {
return new Reference(getClass().getName(), getClass().getName(), null);
}
// ObjectFactory implementation ----------------------------------
public Object getObjectInstance(Object obj,
Name name,
Context nameCtx,
Hashtable environment)
throws Exception {
return instances.get(name.toString());
}
// Private -------------------------------------------------------
private void bind(Context ctx, String name, Object val)
throws NamingException {
// Bind val to name in ctx, and make sure that all intermediate contexts
exist
Name n = ctx.getNameParser("").parse(name);
while (n.size() > 1)
{
String ctxName = n.get(0);
try
{
ctx = (Context)ctx.lookup(ctxName);
} catch (NameNotFoundException e)
{
ctx = ctx.createSubcontext(ctxName);
}
n = n.getSuffix(1);
}
ctx.bind(n.get(0), val);
}
// LogInterceptor implementation ----------------------------------
public void loading(Class objClass, Object identity) {
log.debug( "Loading " + objClass.getName() + " (" + identity + ")" );
}
public void creating(Class objClass, Object identity) {
log.debug( "Creating " + objClass.getName() + " (" + identity + ")" );
}
public void removing(Class objClass, Object identity) {
log.debug( "Removing " + objClass.getName() + " (" + identity + ")" );
}
public void storing(Class objClass, Object identity) {
log.debug( "Storing " + objClass.getName() + " (" + identity + ")" );
}
public void storeStatement(String statement) {
log.debug(statement);
}
public void queryStatement(String statement) {
log.debug(statement);
}
public void message(String message) {
log.warning(message);
}
public void exception(Exception except) {
log.warning(except.toString());
}
}
1.1
contrib/castorjdo/src/main/org/jboss/jdo/castor/CastorJDOImplMBean.java
Index: CastorJDOImplMBean.java
===================================================================
/*
* jBoss, the OpenSource EJB server
*
* Distributable under GPL license.
* See terms of license at gnu.org.
*/
package org.jboss.jdo.castor;
/**
* Castor JDO support
*
* @author Oleg Nitz ([EMAIL PROTECTED])
* @version $Revision: 1.1 $
*/
public interface CastorJDOImplMBean
extends org.jboss.util.ServiceMBean
{
public static final String OBJECT_NAME = ":service=CastorJDO";
}