Skip to content

Commit

Permalink
Add support for SFSB proxy serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
stuartwdouglas committed Oct 1, 2011
1 parent 092056b commit 3b466ee
Show file tree
Hide file tree
Showing 15 changed files with 427 additions and 17 deletions.
Expand Up @@ -55,6 +55,7 @@
<module name="org.jboss.as.server"/>
<module name="org.jboss.as.transactions"/>
<module name="org.jboss.as.web"/>
<module name="org.jboss.ejb-client"/>
<module name="org.jboss.ejb3"/>
<module name="org.jboss.interceptor"/>
<module name="org.jboss.interceptor.spi"/>
Expand Down
Expand Up @@ -23,6 +23,7 @@
package org.jboss.as.ee.component;

import org.jboss.as.ee.component.interceptors.InterceptorOrder;
import org.jboss.as.ee.component.serialization.WriteReplaceInterface;
import org.jboss.as.naming.ManagedReferenceFactory;
import org.jboss.as.naming.ValueManagedReferenceFactory;
import org.jboss.as.server.deployment.DeploymentPhaseContext;
Expand All @@ -47,6 +48,7 @@
import org.jboss.msc.value.InjectedValue;
import org.jboss.msc.value.Value;

import java.io.Serializable;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
Expand Down Expand Up @@ -865,6 +867,12 @@ public void handle(EEModuleClassConfiguration configuration, EEModuleClassDescri
proxyConfiguration.setClassLoader(module.getClassLoader());
proxyConfiguration.setProtectionDomain(viewClass.getProtectionDomain());
proxyConfiguration.setMetadataSource(proxyReflectionIndex);
if (view.isSerializable()) {
proxyConfiguration.addAdditionalInterface(Serializable.class);
if (view.isUseWriteReplace()) {
proxyConfiguration.addAdditionalInterface(WriteReplaceInterface.class);
}
}

//we define it in the modules class loader to prevent permgen leaks
if (viewClass.isInterface()) {
Expand Down
18 changes: 18 additions & 0 deletions ee/src/main/java/org/jboss/as/ee/component/ViewDescription.java
Expand Up @@ -57,6 +57,8 @@ public class ViewDescription {
private final List<String> viewNameParts = new ArrayList<String>();
private final Set<String> bindingNames = new HashSet<String>();
private final Deque<ViewConfigurator> configurators = new ArrayDeque<ViewConfigurator>();
private boolean serializable;
private boolean useWriteReplace;

/**
* Construct a new instance.
Expand Down Expand Up @@ -230,4 +232,20 @@ public Object processInvocation(final InterceptorContext context) throws Excepti
}
}
}

public boolean isSerializable() {
return serializable;
}

public void setSerializable(final boolean serializable) {
this.serializable = serializable;
}

public boolean isUseWriteReplace() {
return useWriteReplace;
}

public void setUseWriteReplace(final boolean useWriteReplace) {
this.useWriteReplace = useWriteReplace;
}
}
Expand Up @@ -144,7 +144,8 @@ public static final class Client {
public static final int EJB_EQUALS_HASHCODE = 0x101;
public static final int LOCAL_ASYNC_INVOCATION = 0x200;
public static final int ASSOCIATING_INTERCEPTOR = 0x300;
public static final int CLIENT_DISPATCHER = 0x400;
public static final int WRITE_REPLACE = 0x400;
public static final int CLIENT_DISPATCHER = 0x500;

private Client() {
}
Expand Down
@@ -0,0 +1,35 @@
/*
* JBoss, Home of Professional Open Source
* Copyright 2010, Red Hat Inc., and individual contributors as indicated
* by the @authors tag. See the copyright.txt in the distribution for a
* full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.jboss.as.ee.component.serialization;

import java.io.IOException;

/**
* Interface used to allow the proxy to supprt serialization. Currently used to work around
* a limitation in the ProxyFactory
* @author Stuart Douglas
*/
public interface WriteReplaceInterface {

Object writeReplace() throws IOException;

}
Expand Up @@ -50,6 +50,7 @@ public class JavaEEDependencyProcessor implements DeploymentUnitProcessor {
private static ModuleIdentifier HIBERNATE_VALIDATOR_ID = ModuleIdentifier.create("org.hibernate.validator");

private static ModuleIdentifier JBOSS_INVOCATION_ID = ModuleIdentifier.create("org.jboss.invocation");
private static ModuleIdentifier JBOSS_AS_EE = ModuleIdentifier.create("org.jboss.as.ee");


/**
Expand All @@ -72,6 +73,10 @@ public void deploy(DeploymentPhaseContext phaseContext) throws DeploymentUnitPro
ModuleDependency invocation = new ModuleDependency(moduleLoader, JBOSS_INVOCATION_ID, false, false, false);
invocation.addImportFilter(PathFilters.is("org.jboss.invocation.proxy.classloading"), true);
moduleSpecification.addSystemDependency(invocation);

ModuleDependency ee = new ModuleDependency(moduleLoader, JBOSS_AS_EE, false, false, false);
ee.addImportFilter(PathFilters.is("org.jboss.as.ee.component.serialization"), true);
moduleSpecification.addSystemDependency(ee);
}

public void undeploy(final DeploymentUnit context) {
Expand Down
Expand Up @@ -32,11 +32,14 @@
import org.jboss.as.ee.component.ViewConfiguration;
import org.jboss.as.ee.component.ViewConfigurator;
import org.jboss.as.ee.component.ViewDescription;
import org.jboss.as.ee.component.serialization.WriteReplaceInterface;
import org.jboss.as.ee.component.interceptors.InterceptorOrder;
import org.jboss.as.ejb3.component.session.SessionBeanComponentDescription;
import org.jboss.as.ejb3.deployment.EjbJarDescription;
import org.jboss.as.server.deployment.DeploymentPhaseContext;
import org.jboss.as.server.deployment.DeploymentUnitProcessingException;
import org.jboss.as.server.deployment.reflect.ClassReflectionIndex;
import org.jboss.as.server.deployment.reflect.DeploymentReflectionIndex;
import org.jboss.invocation.ImmediateInterceptorFactory;
import org.jboss.invocation.Interceptor;
import org.jboss.invocation.InterceptorFactory;
Expand Down Expand Up @@ -122,7 +125,6 @@ public StatefulComponentDescription(final String componentName, final String com
super(componentName, componentClassName, ejbJarDescription, deploymentUnitServiceName);

addStatefulSessionSynchronizationInterceptor();

addInitMethodInvokingInterceptor();
}

Expand Down Expand Up @@ -224,9 +226,26 @@ protected void setupViewInterceptors(ViewDescription view) {
// setup the instance associating interceptors
this.addStatefulInstanceAssociatingInterceptor(view);

this.addViewSerializationInterceptor(view);


}

private void addViewSerializationInterceptor(final ViewDescription view) {
view.setSerializable(true);
view.setUseWriteReplace(true);
view.getConfigurators().add(new ViewConfigurator() {
@Override
public void configure(final DeploymentPhaseContext context, final ComponentConfiguration componentConfiguration, final ViewDescription description, final ViewConfiguration configuration) throws DeploymentUnitProcessingException {
final DeploymentReflectionIndex index = context.getDeploymentUnit().getAttachment(org.jboss.as.server.deployment.Attachments.REFLECTION_INDEX);
ClassReflectionIndex<WriteReplaceInterface> classIndex = index.getClassIndex(WriteReplaceInterface.class);
for(Method method : classIndex.getMethods()) {
configuration.addClientInterceptor(method, new WriteReplaceInterceptor.Factory(configuration.getViewServiceName().getCanonicalName()), InterceptorOrder.Client.WRITE_REPLACE);
}
}
});
}

public void addRemoveMethod(final MethodIdentifier removeMethod, final boolean retainIfException) {
if (removeMethod == null) {
throw new IllegalArgumentException("@Remove method identifier cannot be null");
Expand Down
@@ -0,0 +1,55 @@
/*
* JBoss, Home of Professional Open Source
* Copyright 2010, Red Hat Inc., and individual contributors as indicated
* by the @authors tag. See the copyright.txt in the distribution for a
* full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.jboss.as.ejb3.component.stateful;

import org.jboss.as.ee.component.ComponentView;
import org.jboss.as.server.CurrentServiceContainer;
import org.jboss.ejb.client.SessionID;
import org.jboss.msc.service.ServiceController;
import org.jboss.msc.service.ServiceName;

import java.io.ObjectStreamException;
import java.io.Serializable;
import java.util.Collections;

/**
* Serialized form of a SFSB
*
* @author Stuart Douglas
*/
public class StatefulSerializedProxy implements Serializable {


private static final long serialVersionUID = 627023970448688592L;
private final String viewName;
private final SessionID sessionID;

public StatefulSerializedProxy(final String viewName, final SessionID sessionID) {
this.viewName = viewName;
this.sessionID = sessionID;
}

private Object readResolve() throws ObjectStreamException {
ServiceController<ComponentView> view = (ServiceController<ComponentView>) CurrentServiceContainer.getServiceContainer().getRequiredService(ServiceName.parse(viewName));
return view.getValue().createInstance(Collections.<Object, Object>singletonMap(StatefulSessionComponent.SESSION_ATTACH_KEY, sessionID)).createProxy();
}
}
@@ -0,0 +1,62 @@
/*
* JBoss, Home of Professional Open Source
* Copyright 2010, Red Hat Inc., and individual contributors as indicated
* by the @authors tag. See the copyright.txt in the distribution for a
* full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.jboss.as.ejb3.component.stateful;

import org.jboss.ejb.client.SessionID;
import org.jboss.invocation.Interceptor;
import org.jboss.invocation.InterceptorContext;
import org.jboss.invocation.InterceptorFactory;
import org.jboss.invocation.InterceptorFactoryContext;

/**
* Interceptor that handles the writeReplace method for a SFSB
*
* @author Stuart Douglas
*/
public class WriteReplaceInterceptor implements Interceptor {

private final String serviceName;

public WriteReplaceInterceptor(final String serviceName) {
this.serviceName = serviceName;
}

@Override
public Object processInvocation(final InterceptorContext context) throws Exception {
SessionID sessionId = (SessionID) context.getPrivateData(StatefulContextIdKey.INSTANCE);
return new StatefulSerializedProxy(serviceName, sessionId);
}

public static class Factory implements InterceptorFactory {

private final String serviceName;

public Factory(final String serviceName) {
this.serviceName = serviceName;
}

@Override
public Interceptor create(final InterceptorFactoryContext context) {
return new WriteReplaceInterceptor(serviceName);
}
}
}
Expand Up @@ -53,6 +53,7 @@ public class EjbDependencyDeploymentUnitProcessor implements DeploymentUnitProce
* TODO: restrict visibility
*/
private static final ModuleIdentifier EJB3_TIMERS = ModuleIdentifier.create("org.jboss.as.ejb3");
private static final ModuleIdentifier EJB_CLIENT = ModuleIdentifier.create("org.jboss.ejb-client");


/**
Expand Down Expand Up @@ -86,6 +87,7 @@ public void deploy(DeploymentPhaseContext phaseContext) throws DeploymentUnitPro

moduleSpecification.addSystemDependency(new ModuleDependency(moduleLoader, JAVAEE_MODULE_IDENTIFIER, false, false, false));
moduleSpecification.addSystemDependency(new ModuleDependency(moduleLoader, EJB3_TIMERS, false, false, false));
moduleSpecification.addSystemDependency(new ModuleDependency(moduleLoader, EJB_CLIENT, false, false, false));
}

@Override
Expand Down
Expand Up @@ -27,6 +27,7 @@
import org.jboss.as.ee.component.ViewConfigurator;
import org.jboss.as.ee.component.ViewDescription;
import org.jboss.as.ee.component.interceptors.InterceptorOrder;
import org.jboss.as.ee.component.serialization.WriteReplaceInterface;
import org.jboss.as.ejb3.component.EJBComponentDescription;
import org.jboss.as.server.deployment.DeploymentPhaseContext;
import org.jboss.as.server.deployment.DeploymentUnitProcessingException;
Expand Down Expand Up @@ -70,6 +71,9 @@ public void configure(DeploymentPhaseContext context, ComponentConfiguration com
if (!Modifier.isPublic(viewMethod.getModifiers())) {
continue;
}
if(viewMethod.getDeclaringClass() == WriteReplaceInterface.class) {
continue;
}
final EJBMethodSecurityMetaData ejbMethodSecurityMetaData = new EJBMethodSecurityMetaData(componentConfiguration, viewClassName, viewMethod);
// setup the authorization interceptor
final Interceptor authorizationInterceptor = new AuthorizationInterceptor(ejbMethodSecurityMetaData, viewClassName, viewMethod);
Expand Down
@@ -0,0 +1,43 @@
/*
* JBoss, Home of Professional Open Source.
* Copyright 2011, Red Hat, Inc., and individual contributors
* as indicated by the @author tags. See the copyright.txt file in the
* distribution for a full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/

package org.jboss.as.testsuite.integration.ejb.stateful.serialization;

import javax.ejb.Stateful;

/**
* stateful session bean
*
*/
@Stateful
public class AnnotatedBean {

private int value;

public int getValue() {
return value;
}

public void setValue(final int value) {
this.value = value;
}
}

0 comments on commit 3b466ee

Please sign in to comment.