An mbean service that pvovides the detached invoker ops for the
javax.sql.DataSource and related java.sql.* interfaces.
TODO this does not belong in the resource adapter
| Method from org.jboss.resource.adapter.jdbc.remote.WrapperDataSourceService Detail: |
protected void bindConnectionFactory() throws Exception {
InitialContext ctx = new InitialContext();
try
{
log.debug("Binding object '" + cf + "' into JNDI at '" + bindName + "'");
// Associated the local cf with the NonSerializable factory
NonSerializableFactory.rebind(bindName, cf);
/* Create a reference that uses the the DataSourceFactory as the
reference factory class. This class detects whether the lookup
is being done locally or remotely and returns either the just bound
connection factory, or a DataSource proxy that uses the detached
invoker framework to expose remote proxies to the server side
DataSource and related elements.
*/
Referenceable referenceable = (Referenceable) cf;
// Set the DataSource proxy as the ProxyData ref address
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(theProxy);
oos.close();
byte[] proxyBytes = baos.toByteArray();
BinaryRefAddr dsAddr = new BinaryRefAddr("ProxyData", proxyBytes);
String factory = DataSourceFactory.class.getName();
Reference dsRef = new Reference("javax.sql.DataSource", dsAddr, factory, null);
referenceable.setReference(dsRef);
// Set the VMID as the address local/remote indicator
baos.reset();
ObjectOutputStream oos2 = new ObjectOutputStream(baos);
oos2.writeObject(DataSourceFactory.vmID);
oos2.close();
byte[] id = baos.toByteArray();
BinaryRefAddr localAddr = new BinaryRefAddr("VMID", id);
dsRef.add(localAddr);
/* Bind the Referenceable connection factory into JNDI and set the
JndiName value of the reference address for use by the DataSourceFactory
when looking up the local factory from the NonSerializableFactory.
*/
StringRefAddr jndiRef = new StringRefAddr("JndiName", bindName);
dsRef.add(jndiRef);
Util.rebind(ctx, bindName, cf);
log.info("Bound ConnectionManager '" + serviceName + "' to JNDI name '" + bindName + "'");
}
catch (NamingException ne)
{
throw new DeploymentException("Could not bind ConnectionFactory into jndi: " + bindName, ne);
}
finally
{
ctx.close();
}
}
|
protected void calculateMethodHases() throws Exception {
Method[] methods = DataSource.class.getMethods();
for(int m = 0; m < methods.length; m ++)
{
Method method = methods[m];
Long hash = new Long(MarshalledInvocation.calculateHash(method));
marshalledInvocationMapping.put(hash, method);
}
// Get the Long to Method mappings
Map m = MarshalledInvocation.methodToHashesMap(Connection.class);
displayHashes(m);
marshalledInvocationMapping.putAll(m);
m = MarshalledInvocation.methodToHashesMap(Statement.class);
displayHashes(m);
marshalledInvocationMapping.putAll(m);
m = MarshalledInvocation.methodToHashesMap(CallableStatement.class);
displayHashes(m);
marshalledInvocationMapping.putAll(m);
m = MarshalledInvocation.methodToHashesMap(PreparedStatement.class);
displayHashes(m);
marshalledInvocationMapping.putAll(m);
m = MarshalledInvocation.methodToHashesMap(ResultSet.class);
displayHashes(m);
marshalledInvocationMapping.putAll(m);
m = MarshalledInvocation.methodToHashesMap(DatabaseMetaData.class);
displayHashes(m);
marshalledInvocationMapping.putAll(m);
}
Calculate the method hashes |
protected void createProxy() throws Exception {
/* Create an JRMPInvokerProxy that will be associated with a naming JMX
invoker given by the jmxInvokerName.
*/
delegateInvoker = (Invoker) Registry.lookup(jmxInvokerName);
log.debug("Using delegate: " + delegateInvoker
+ " for invoker=" + jmxInvokerName);
ObjectName targetName = getServiceName();
Integer nameHash = new Integer(targetName.hashCode());
Registry.bind(nameHash, targetName);
Object cacheID = null;
String proxyBindingName = null;
String jndiName = null;
Class[] ifaces = {javax.sql.DataSource.class};
/* Initialize interceptorClasses with default client interceptor list
if no client interceptor configuration was provided */
ArrayList interceptorClasses = new ArrayList();
interceptorClasses.add(ClientMethodInterceptor.class);
interceptorClasses.add(InvokerInterceptor.class);
ClassLoader loader = Thread.currentThread().getContextClassLoader();
GenericProxyFactory proxyFactory = new GenericProxyFactory();
theProxy = proxyFactory.createProxy(cacheID, targetName,
delegateInvoker, jndiName, proxyBindingName, interceptorClasses,
loader, ifaces);
log.debug("Created proxy for invoker=" + jmxInvokerName
+ ", targetName=" + targetName + ", nameHash=" + nameHash);
}
Create the proxy
TODO this should be external configuration |
protected void destroyProxy() throws Exception {
ObjectName name = getServiceName();
Integer nameHash = new Integer(name.hashCode());
Registry.unbind(nameHash);
}
|
public ObjectName getJMXInvokerName() {
return jmxInvokerName;
}
|
public Object invoke(Invocation invocation) throws Exception {
// Set the method hash to Method mapping
if (invocation instanceof MarshalledInvocation)
{
MarshalledInvocation mi = (MarshalledInvocation) invocation;
mi.setMethodMap(marshalledInvocationMapping);
}
// Invoke the Naming method via reflection
Method method = invocation.getMethod();
Class methodClass = method.getDeclaringClass();
Object[] args = invocation.getArguments();
Object value = null;
try
{
if( methodClass.isAssignableFrom(DataSource.class) )
{
InitialContext ctx = new InitialContext();
DataSource ds = (DataSource) ctx.lookup(bindName);
value = doDataSourceMethod(ds, method, args);
}
else if( methodClass.isAssignableFrom(Connection.class) )
{
Integer id = (Integer) invocation.getId();
Connection conn = (Connection) connectionMap.get(id);
if( conn == null )
{
throw new IllegalAccessException("Failed to find connection: "+id);
}
value = doConnectionMethod(conn, method, args);
}
else if( methodClass.isAssignableFrom(Statement.class) ||
methodClass.isAssignableFrom(PreparedStatement.class) ||
methodClass.isAssignableFrom(CallableStatement.class))
{
Integer id = (Integer) invocation.getId();
Statement stmt = (Statement) statementMap.get(id);
if( stmt == null )
{
throw new SQLException("Failed to find Statement: " + id);
}
value = doStatementMethod(stmt, method, args);
}
else if( methodClass.isAssignableFrom(ResultSet.class) )
{
Integer id = (Integer) invocation.getId();
ResultSet results = (ResultSet) resultSetMap.get(id);
if( results == null )
{
throw new IllegalAccessException("Failed to find ResultSet: "+id);
}
value = doResultSetMethod(results, method, args);
}
else if (methodClass.isAssignableFrom(DatabaseMetaData.class))
{
Integer id = (Integer) invocation.getId();
DatabaseMetaData dbMetaData = (DatabaseMetaData) databaseMetaDataMap.get(id);
if(dbMetaData == null)
{
throw new IllegalAccessException("Failed to find DatabaseMetaData: " + id);
}
value = doDatabaseMetaDataMethod(dbMetaData, method, args);
}
else
{
throw new UnsupportedOperationException("Do not know how to handle method="+method);
}
}
catch (InvocationTargetException e)
{
Throwable t = e.getTargetException();
if (t instanceof Exception)
throw (Exception) t;
else
throw new UndeclaredThrowableException(t, method.toString());
}
return value;
}
|
public void setJMXInvokerName(ObjectName jmxInvokerName) {
this.jmxInvokerName = jmxInvokerName;
}
|
protected void startService() throws Exception {
determineBindName();
createConnectionFactory();
if( jmxInvokerName != null )
{
createProxy();
calculateMethodHases();
bindConnectionFactory();
}
else
{
super.bindConnectionFactory();
}
}
|
protected void stopService() throws Exception {
unbindConnectionFactory();
if( jmxInvokerName != null )
destroyProxy();
}
|