You are viewing a plain text version of this content. The canonical link for it is here.
Posted to scm@geronimo.apache.org by dj...@apache.org on 2004/03/09 21:15:43 UTC
cvs commit: incubator-geronimo/modules/connector/src/test/org/apache/geronimo/connector/mock MockAdminObject.java MockAdminObjectImpl.java
djencks 2004/03/09 12:15:43
Modified: modules/connector/src/java/org/apache/geronimo/connector
AdminObjectWrapper.java
modules/connector/src/java/org/apache/geronimo/connector/outbound
ManagedConnectionFactoryWrapper.java
modules/connector/src/test/org/apache/geronimo/connector/mock
MockAdminObject.java MockAdminObjectImpl.java
Added: modules/connector/src/test/org/apache/geronimo/connector
AdminObjectWrapperTest.java
modules/connector/src/java/org/apache/geronimo/connector
ConnectorMethodInterceptor.java
Removed: modules/connector/src/java/org/apache/geronimo/connector/outbound
CFMethodInterceptor.java
Log:
proxies for admin objects
Revision Changes Path
1.1 incubator-geronimo/modules/connector/src/test/org/apache/geronimo/connector/AdminObjectWrapperTest.java
Index: AdminObjectWrapperTest.java
===================================================================
/**
*
* Copyright 2004 The Apache Software Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.geronimo.connector;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Collections;
import javax.management.ObjectName;
import junit.framework.TestCase;
import org.apache.geronimo.connector.mock.MockAdminObject;
import org.apache.geronimo.connector.mock.MockAdminObjectImpl;
import org.apache.geronimo.gbean.jmx.GBeanMBean;
import org.apache.geronimo.kernel.Kernel;
import org.apache.geronimo.naming.deployment.RefAdapter;
import org.apache.geronimo.naming.java.ComponentContextBuilder;
import org.apache.geronimo.naming.java.ReadOnlyContext;
import org.apache.geronimo.naming.jmx.JMXReferenceFactory;
import org.apache.xmlbeans.XmlObject;
/**
*
*
* @version $Revision: 1.1 $ $Date: 2004/03/09 20:15:43 $
*
* */
public class AdminObjectWrapperTest extends TestCase {
private Kernel kernel;
private ObjectName selfName;
private static final String KERNEL_NAME = "testKernel";
private static final String TARGET_NAME = "testAOName";
public void testProxy() throws Exception {
Object proxy = kernel.invoke(selfName, "getProxy");
assertNotNull(proxy);
assertTrue(proxy instanceof MockAdminObject);
MockAdminObject mockAdminObject = ((MockAdminObject) proxy).getSomething();
assertNotNull(mockAdminObject);
kernel.stopGBean(selfName);
try {
((MockAdminObject) proxy).getSomething();
fail();
} catch (IllegalStateException ise) {
}
kernel.startGBean(selfName);
((MockAdminObject) proxy).getSomething();
}
public void testSerialization() throws Exception {
MockAdminObject proxy = (MockAdminObject)kernel.invoke(selfName, "getProxy");
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(proxy);
oos.flush();
byte[] bytes = baos.toByteArray();
oos.close();
ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(bytes));
Object proxy2 = ois.readObject();
assertNotNull(proxy2);
assertTrue(proxy instanceof MockAdminObject);
MockAdminObject mockAdminObject = proxy.getSomething();
assertNotNull(mockAdminObject);
kernel.stopGBean(selfName);
ObjectInputStream ois2 = new ObjectInputStream(new ByteArrayInputStream(bytes));
MockAdminObject proxy3 = (MockAdminObject)ois2.readObject();
try {
proxy3.getSomething();
fail();
} catch (IllegalStateException ise) {
}
kernel.startGBean(selfName);
proxy3.getSomething();
}
public void testLocalLookup() throws Exception {
JMXReferenceFactory referenceFactory = new JMXReferenceFactory();
ComponentContextBuilder builder = new ComponentContextBuilder(referenceFactory);
builder.addResourceEnvRef("resourceenvref", MockAdminObject.class, new RefAdapter() {
public XmlObject getXmlObject() {
return null;
}
public void setXmlObject(XmlObject xmlObject) {
}
public String getRefName() {
return "resourceenvref";
}
public void setRefName(String name) {
}
public String getServerName() {
return null;
}
public void setServerName(String serverName) {
}
public String getKernelName() {
return KERNEL_NAME;
}
public void setKernelName(String kernelName) {
}
public String getTargetName() {
return TARGET_NAME;
}
public void setTargetName(String targetName) {
}
public String getExternalUri() {
return null;
}
public void setExternalUri(String externalURI) {
}
});
ReadOnlyContext roc = builder.getContext();
Object o = roc.lookup("env/resourceenvref");
assertNotNull(o);
assertTrue(o instanceof MockAdminObject);
}
protected void setUp() throws Exception {
kernel = new Kernel(KERNEL_NAME, "test.domain");
kernel.boot();
selfName = ObjectName.getInstance(JMXReferenceFactory.BASE_ADMIN_OBJECT_NAME + TARGET_NAME);
GBeanMBean aow = new GBeanMBean(AdminObjectWrapper.getGBeanInfo());
aow.setAttribute("AdminObjectInterface", MockAdminObject.class);
aow.setAttribute("AdminObjectClass", MockAdminObjectImpl.class);
aow.setReferencePatterns("Kernel", Collections.singleton(Kernel.KERNEL));
aow.setAttribute("SelfName", selfName);
kernel.loadGBean(selfName, aow);
kernel.startGBean(selfName);
}
protected void tearDown() throws Exception {
kernel.stopGBean(selfName);
kernel.shutdown();
}
}
1.4 +79 -12 incubator-geronimo/modules/connector/src/java/org/apache/geronimo/connector/AdminObjectWrapper.java
Index: AdminObjectWrapper.java
===================================================================
RCS file: /home/cvs/incubator-geronimo/modules/connector/src/java/org/apache/geronimo/connector/AdminObjectWrapper.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- AdminObjectWrapper.java 9 Mar 2004 18:02:02 -0000 1.3
+++ AdminObjectWrapper.java 9 Mar 2004 20:15:43 -0000 1.4
@@ -17,9 +17,19 @@
package org.apache.geronimo.connector;
+import javax.management.ObjectName;
+
+import net.sf.cglib.proxy.Callback;
+import net.sf.cglib.proxy.Enhancer;
+import net.sf.cglib.proxy.MethodInterceptor;
+import org.apache.geronimo.gbean.DynamicGBean;
import org.apache.geronimo.gbean.DynamicGBeanDelegate;
+import org.apache.geronimo.gbean.GBean;
+import org.apache.geronimo.gbean.GBeanContext;
import org.apache.geronimo.gbean.GBeanInfo;
import org.apache.geronimo.gbean.GBeanInfoFactory;
+import org.apache.geronimo.gbean.WaitingException;
+import org.apache.geronimo.kernel.KernelMBean;
/**
*
@@ -27,30 +37,39 @@
* @version $Revision$ $Date$
*
* */
-public class AdminObjectWrapper {
+public class AdminObjectWrapper implements GBean, DynamicGBean {
public static final GBeanInfo GBEAN_INFO;
+ private final Class adminObjectInterface;
private final Class adminObjectClass;
private final DynamicGBeanDelegate delegate;
private final Object adminObject;
- private final String name;
+ private final KernelMBean kernel;
+ private final ObjectName selfName;
+
+ private ConnectorMethodInterceptor interceptor;
+ private Object proxy;
//for use as endpoint
public AdminObjectWrapper() {
+ adminObjectInterface = null;
adminObjectClass = null;
adminObject = null;
delegate = null;
- name = null;
+ kernel = null;
+ selfName = null;
}
- public AdminObjectWrapper(Class adminObjectClass, String name) throws IllegalAccessException, InstantiationException {
+ public AdminObjectWrapper(Class adminObjectInterface, Class adminObjectClass, KernelMBean kernel, ObjectName selfName) throws IllegalAccessException, InstantiationException {
+ this.adminObjectInterface = adminObjectInterface;
this.adminObjectClass = adminObjectClass;
adminObject = adminObjectClass.newInstance();
delegate = new DynamicGBeanDelegate();
delegate.addAll(adminObject);
- this.name = name;
+ this.kernel = kernel;
+ this.selfName = selfName;
}
public Class getAdminObjectClass() {
@@ -58,19 +77,67 @@
}
public Object getProxy() {
- return adminObject;
+ return proxy;
+ }
+
+ public Object getMethodInterceptor() {
+ return interceptor;
+ }
+
+ //gbean implementation
+ public void setGBeanContext(GBeanContext context) {
+ }
+
+ public void doStart() throws WaitingException, Exception {
+ if (proxy == null) {
+ //build proxy
+ Enhancer enhancer = new Enhancer();
+ enhancer.setSuperclass(adminObjectInterface);
+ enhancer.setCallbackType(MethodInterceptor.class);
+ enhancer.setUseFactory(false);//????
+ interceptor = new ConnectorMethodInterceptor(kernel.getKernelName(), selfName);
+ enhancer.setCallbacks(new Callback[] {interceptor});
+ proxy = enhancer.create(new Class[0], new Object[0]);
+ }
+ //connect proxy
+ interceptor.setInternalProxy(adminObject);
+ }
+
+ public void doStop() throws WaitingException, Exception {
+ //disconnect proxy
+ interceptor.setInternalProxy(null);
}
- public Object getId() {
- return name;
+ public void doFail() {
+ }
+
+ //DynamicGBean implementation
+ public Object getAttribute(String name) throws Exception {
+ return delegate.getAttribute(name);
+ }
+
+ public void setAttribute(String name, Object value) throws Exception {
+ delegate.setAttribute(name, value);
+ }
+
+ public Object invoke(String name, Object[] arguments, String[] types) throws Exception {
+ //we have no dynamic operations.
+ return null;
}
static {
GBeanInfoFactory infoFactory = new GBeanInfoFactory(AdminObjectWrapper.class.getName());
+ infoFactory.addAttribute("AdminObjectInterface", true);
infoFactory.addAttribute("AdminObjectClass", true);
- infoFactory.addAttribute("Name", true);
- infoFactory.setConstructor(new String[] {"AdminObjectClass", "Name"},
- new Class[] {Class.class, String.class});
+ infoFactory.addAttribute("SelfName", true);
+
+ infoFactory.addOperation("getProxy");
+ infoFactory.addOperation("getMethodInterceptor");
+
+ infoFactory.addReference("Kernel", KernelMBean.class);
+
+ infoFactory.setConstructor(new String[] {"AdminObjectInterface", "AdminObjectClass", "Kernel", "SelfName"},
+ new Class[] {Class.class, Class.class, KernelMBean.class, ObjectName.class});
GBEAN_INFO = infoFactory.getBeanInfo();
}
1.1 incubator-geronimo/modules/connector/src/java/org/apache/geronimo/connector/ConnectorMethodInterceptor.java
Index: ConnectorMethodInterceptor.java
===================================================================
/**
*
* Copyright 2004 The Apache Software Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.geronimo.connector;
import java.lang.reflect.Method;
import java.io.Serializable;
import java.io.ObjectStreamException;
import java.io.InvalidObjectException;
import javax.management.ObjectName;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;
import org.apache.geronimo.kernel.Kernel;
/**
*
*
* @version $Revision: 1.1 $ $Date: 2004/03/09 20:15:43 $
*
* */
public class ConnectorMethodInterceptor implements MethodInterceptor, Serializable {
private final String kernelName;
private final ObjectName targetName;
private transient Object internalProxy;
public ConnectorMethodInterceptor(String kernelName, ObjectName targetName) {
this.kernelName = kernelName;
this.targetName = targetName;
}
public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
if (internalProxy == null) {
throw new IllegalStateException("Proxy is not connected");
}
return methodProxy.invoke(internalProxy, objects);
}
public void setInternalProxy(Object internalProxy) {
this.internalProxy = internalProxy;
}
private Object readResolve() throws ObjectStreamException {
Kernel kernel = Kernel.getKernel(kernelName);
try {
return kernel.invoke(targetName, "getMethodInterceptor");
} catch (Exception e) {
throw (InvalidObjectException)new InvalidObjectException("could not get method interceptor from ManagedConnectionFactoryWrapper").initCause(e);
}
}
}
1.6 +14 -16 incubator-geronimo/modules/connector/src/java/org/apache/geronimo/connector/outbound/ManagedConnectionFactoryWrapper.java
Index: ManagedConnectionFactoryWrapper.java
===================================================================
RCS file: /home/cvs/incubator-geronimo/modules/connector/src/java/org/apache/geronimo/connector/outbound/ManagedConnectionFactoryWrapper.java,v
retrieving revision 1.5
retrieving revision 1.6
diff -u -r1.5 -r1.6
--- ManagedConnectionFactoryWrapper.java 9 Mar 2004 18:02:03 -0000 1.5
+++ ManagedConnectionFactoryWrapper.java 9 Mar 2004 20:15:43 -0000 1.6
@@ -23,10 +23,10 @@
import net.sf.cglib.proxy.Callback;
import net.sf.cglib.proxy.Enhancer;
-import net.sf.cglib.proxy.MethodInterceptor;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.geronimo.connector.ResourceAdapterWrapper;
+import org.apache.geronimo.connector.ConnectorMethodInterceptor;
import org.apache.geronimo.connector.outbound.security.ManagedConnectionFactoryListener;
import org.apache.geronimo.gbean.DynamicGBean;
import org.apache.geronimo.gbean.DynamicGBeanDelegate;
@@ -72,9 +72,9 @@
private boolean registered = false;
private Object proxy;
- private CFMethodInterceptor interceptor;
- private KernelMBean kernel;
- private ObjectName selfName;
+ private ConnectorMethodInterceptor interceptor;
+ private final KernelMBean kernel;
+ private final ObjectName selfName;
//default constructor for enhancement proxy endpoint
public ManagedConnectionFactoryWrapper() {
@@ -83,6 +83,8 @@
connectionFactoryImplClass = null;
connectionInterface = null;
connectionImplClass = null;
+ kernel = null;
+ selfName = null;
}
public ManagedConnectionFactoryWrapper(
@@ -114,6 +116,7 @@
this.managedConnectionFactoryListener = managedConnectionFactoryListener;
this.kernel = kernel;
this.selfName = selfName;
+
}
public Class getManagedConnectionFactoryClass() {
@@ -140,12 +143,6 @@
return globalJNDIName;
}
- /*
- public Object getConnectionFactory() {
- return connectionFactory;
- }
- */
-
public ResourceAdapterWrapper getResourceAdapterWrapper() {
return resourceAdapterWrapper;
}
@@ -179,17 +176,18 @@
//create a new ConnectionFactory
connectionFactory = connectionManagerFactory.createConnectionFactory(managedConnectionFactory);
- //build a proxy that can be turned off
+ //build proxy
if (proxy == null) {
Enhancer enhancer = new Enhancer();
enhancer.setSuperclass(connectionFactoryInterface);
- enhancer.setCallbackType(MethodInterceptor.class);
+ enhancer.setCallbackType(net.sf.cglib.proxy.MethodInterceptor.class);
enhancer.setUseFactory(false);//????
- interceptor = new CFMethodInterceptor(kernel.getKernelName(), selfName);
+ interceptor = new ConnectorMethodInterceptor(kernel.getKernelName(), selfName);
enhancer.setCallbacks(new Callback[] {interceptor});
proxy = enhancer.create(new Class[0], new Object[0]);
}
- interceptor.setConnectionFactory(connectionFactory);
+ //connect proxy
+ interceptor.setInternalProxy(connectionFactory);
//If a globalJNDIName is supplied, bind it.
if (globalJNDIName != null) {
GeronimoContextManager.bind(globalJNDIName, proxy);
@@ -199,7 +197,7 @@
}
public void doStop() throws WaitingException {
- interceptor.setConnectionFactory(null);
+ interceptor.setInternalProxy(null);
//tear down login if present
if (managedConnectionFactoryListener != null) {
managedConnectionFactoryListener.setManagedConnectionFactory(null);
1.3 +7 -2 incubator-geronimo/modules/connector/src/test/org/apache/geronimo/connector/mock/MockAdminObject.java
Index: MockAdminObject.java
===================================================================
RCS file: /home/cvs/incubator-geronimo/modules/connector/src/test/org/apache/geronimo/connector/mock/MockAdminObject.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- MockAdminObject.java 25 Feb 2004 09:57:12 -0000 1.2
+++ MockAdminObject.java 9 Mar 2004 20:15:43 -0000 1.3
@@ -17,11 +17,16 @@
package org.apache.geronimo.connector.mock;
+import java.io.Serializable;
+
/**
*
*
* @version $Revision$ $Date$
*
* */
-public interface MockAdminObject {
+public interface MockAdminObject extends Serializable {
+
+ MockAdminObject getSomething();
+
}
1.3 +4 -1 incubator-geronimo/modules/connector/src/test/org/apache/geronimo/connector/mock/MockAdminObjectImpl.java
Index: MockAdminObjectImpl.java
===================================================================
RCS file: /home/cvs/incubator-geronimo/modules/connector/src/test/org/apache/geronimo/connector/mock/MockAdminObjectImpl.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- MockAdminObjectImpl.java 25 Feb 2004 09:57:12 -0000 1.2
+++ MockAdminObjectImpl.java 9 Mar 2004 20:15:43 -0000 1.3
@@ -24,4 +24,7 @@
*
* */
public class MockAdminObjectImpl implements MockAdminObject {
+ public MockAdminObject getSomething() {
+ return this;
+ }
}