diff --git a/core/src/main/java/org/acegisecurity/ui/rmi/ContextPropagatingRemoteInvocation.java b/core/src/main/java/org/acegisecurity/ui/rmi/ContextPropagatingRemoteInvocation.java new file mode 100644 index 0000000000..1f92373060 --- /dev/null +++ b/core/src/main/java/org/acegisecurity/ui/rmi/ContextPropagatingRemoteInvocation.java @@ -0,0 +1,111 @@ +/* Copyright 2004 Acegi Technology Pty Limited + * + * 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 net.sf.acegisecurity.ui.rmi; + +import net.sf.acegisecurity.context.Context; +import net.sf.acegisecurity.context.ContextHolder; + +import org.aopalliance.intercept.MethodInvocation; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + +import org.springframework.remoting.support.RemoteInvocation; + +import java.lang.reflect.InvocationTargetException; + + +/** + * The actual RemoteInvocation that is passed from the client to + * the server, which contains the contents of {@link ContextHolder}. + * + *

+ * When constructed on the client via {@link + * net.sf.acegisecurity.ui.rmi.ContextPropagatingRemoteInvocationFactory}, the + * contents of the ContextHolder are stored inside the object. + * The object is then passed to the server that is processing the remote + * invocation. Upon the server invoking the remote invocation, it will + * retrieve the passed contents of the ContextHolder and set them + * to the server-side ContextHolder whilst the target object is + * invoked. When the target invocation has been completed, the server-side + * ContextHolder will be reset to null. + *

+ * + * @author James Monaghan + * @author Ben Alex + * @version $Id$ + */ +public class ContextPropagatingRemoteInvocation extends RemoteInvocation { + //~ Static fields/initializers ============================================= + + private static final Log logger = LogFactory.getLog(ContextPropagatingRemoteInvocation.class); + + //~ Instance fields ======================================================== + + private Context context; + + //~ Constructors =========================================================== + + /** + * Constructs the object, storing the value of the client-side + * ContextHolder inside the object. + * + * @param methodInvocation the method to invoke + */ + public ContextPropagatingRemoteInvocation(MethodInvocation methodInvocation) { + super(methodInvocation); + context = ContextHolder.getContext(); + + if (logger.isDebugEnabled()) { + logger.debug("RemoteInvocation now has context of: " + + context.toString()); + } + } + + //~ Methods ================================================================ + + /** + * Invoked on the server-side as described in the class JavaDocs. + * + * @param targetObject the target object to apply the invocation to + * + * @return the invocation result + * + * @throws NoSuchMethodException if the method name could not be resolved + * @throws IllegalAccessException if the method could not be accessed + * @throws InvocationTargetException if the method invocation resulted in + * an exception + */ + public Object invoke(Object targetObject) + throws NoSuchMethodException, IllegalAccessException, + InvocationTargetException { + ContextHolder.setContext(context); + + if (logger.isDebugEnabled()) { + logger.debug("Set ContextHolder to contain: " + context.toString()); + } + + Object result = super.invoke(targetObject); + + ContextHolder.setContext(null); + + if (logger.isDebugEnabled()) { + logger.debug("Set ContextHolder to null"); + } + + return result; + } +} diff --git a/sandbox/src/main/java/org/acegisecurity/remoting/AcegiRemoteInvocationFactory.java b/core/src/main/java/org/acegisecurity/ui/rmi/ContextPropagatingRemoteInvocationFactory.java similarity index 67% rename from sandbox/src/main/java/org/acegisecurity/remoting/AcegiRemoteInvocationFactory.java rename to core/src/main/java/org/acegisecurity/ui/rmi/ContextPropagatingRemoteInvocationFactory.java index fabf02e770..bab65e9751 100644 --- a/sandbox/src/main/java/org/acegisecurity/remoting/AcegiRemoteInvocationFactory.java +++ b/core/src/main/java/org/acegisecurity/ui/rmi/ContextPropagatingRemoteInvocationFactory.java @@ -13,7 +13,7 @@ * limitations under the License. */ -package net.sf.acegisecurity.remoting; +package net.sf.acegisecurity.ui.rmi; import org.aopalliance.intercept.MethodInvocation; @@ -22,16 +22,25 @@ import org.springframework.remoting.support.RemoteInvocationFactory; /** - * DOCUMENT ME! + * Called by a client-side instance of + * org.springframework.remoting.rmi.RmiProxyFactoryBean when it + * wishes to create a remote invocation. + * + *

+ * Set an instance of this bean against the above class' + * remoteInvocationFactory property. + *

* * @author James Monaghan + * @author Ben Alex * @version $Id$ */ -public class AcegiRemoteInvocationFactory implements RemoteInvocationFactory { +public class ContextPropagatingRemoteInvocationFactory + implements RemoteInvocationFactory { //~ Methods ================================================================ public RemoteInvocation createRemoteInvocation( MethodInvocation methodInvocation) { - return new AcegiRemoteInvocation(methodInvocation); + return new ContextPropagatingRemoteInvocation(methodInvocation); } } diff --git a/sandbox/src/main/java/org/acegisecurity/remoting/package.html b/core/src/main/java/org/acegisecurity/ui/rmi/package.html similarity index 73% rename from sandbox/src/main/java/org/acegisecurity/remoting/package.html rename to core/src/main/java/org/acegisecurity/ui/rmi/package.html index 1b0cf81e9d..e8694b69b2 100644 --- a/sandbox/src/main/java/org/acegisecurity/remoting/package.html +++ b/core/src/main/java/org/acegisecurity/ui/rmi/package.html @@ -1,7 +1,9 @@ -Enables use of Spring's remoting extension points to propogate -security identity from one JVM to the remote JVM. +Enables use of Spring's RMI remoting extension points to propagate +the ContextHolder (which should contain an +Authentication request token) +from one JVM to the remote JVM.

The beans are wired as follows: @@ -14,7 +16,7 @@ security identity from one JVM to the remote JVM. <property name="remoteInvocationFactory"><ref bean="remoteInvocationFactory"/></property>
</bean>

-<bean id="remoteInvocationFactory" class="net.sf.acegisecurity.remoting.AcegiRemoteInvocationFactory"/>
+<bean id="remoteInvocationFactory" class="net.sf.acegisecurity.ui.rmi.ContextPropagatingRemoteInvocationFactory"/>
diff --git a/sandbox/src/main/java/org/acegisecurity/remoting/AcegiRemoteInvocation.java b/sandbox/src/main/java/org/acegisecurity/remoting/AcegiRemoteInvocation.java deleted file mode 100644 index 89dd349849..0000000000 --- a/sandbox/src/main/java/org/acegisecurity/remoting/AcegiRemoteInvocation.java +++ /dev/null @@ -1,58 +0,0 @@ -/* Copyright 2004 Acegi Technology Pty Limited - * - * 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 net.sf.acegisecurity.remoting; - -import net.sf.acegisecurity.context.Context; -import net.sf.acegisecurity.context.ContextHolder; - -import org.aopalliance.intercept.MethodInvocation; - -import org.springframework.remoting.support.RemoteInvocation; - -import java.lang.reflect.InvocationTargetException; - - -/** - * DOCUMENT ME! - * - * @author James Monaghan - * @version $Id$ - */ -public class AcegiRemoteInvocation extends RemoteInvocation { - //~ Instance fields ======================================================== - - private Context context; - - //~ Constructors =========================================================== - - public AcegiRemoteInvocation(MethodInvocation methodInvocation) { - super(methodInvocation); - context = ContextHolder.getContext(); - } - - //~ Methods ================================================================ - - public Object invoke(Object targetObject) - throws NoSuchMethodException, IllegalAccessException, - InvocationTargetException { - ContextHolder.setContext(context); - - Object result = super.invoke(targetObject); - ContextHolder.setContext(null); - - return result; - } -}