/*
* JBoss, Home of Professional Open Source
* Copyright (c) 2010, JBoss 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.ejb3.core.businessobject;
import org.jboss.ejb3.common.registrar.spi.Ejb3RegistrarLocator;
import org.jboss.ejb3.proxy.factory.ProxyFactoryHelper;
import org.jboss.ejb3.proxy.impl.factory.session.stateful.StatefulSessionProxyFactory;
import org.jboss.ejb3.proxy.impl.jndiregistrar.JndiStatefulSessionRegistrar;
import org.jboss.ejb3.session.SessionContainer;
import org.jboss.ejb3.stateful.StatefulContainer;
import org.jboss.metadata.ejb.jboss.JBossSessionBeanMetaData;
import java.io.Serializable;
/**
* @author <a href="mailto:cdewolf@redhat.com">Carlo de Wolf</a>
*/
public class LegacyStatefulBusinessObjectFactory implements BusinessObjectFactory
{
public <B> B createBusinessObject(SessionContainer container, Serializable sessionId, Class<B> businessInterface)
{
assert businessInterface != null : "businessInterface is null";
boolean isRemote = false;
boolean found = false;
Class<?>[] remoteInterfaces = ProxyFactoryHelper.getRemoteAndBusinessRemoteInterfaces(container);
for (Class<?> intf : remoteInterfaces)
{
if (intf.getName().equals(businessInterface.getName()))
{
isRemote = true;
found = true;
break;
}
}
if (found == false)
{
Class<?>[] localInterfaces = ProxyFactoryHelper.getLocalAndBusinessLocalInterfaces(container);
for (Class<?> intf : localInterfaces)
{
if (intf.getName().equals(businessInterface.getName()))
{
found = true;
break;
}
}
}
if (found == false)
throw new IllegalStateException(businessInterface.getName() + " is not a business interface for container "
+ this);
// Obtain SFSB JNDI Registrar
String sfsbJndiRegistrarObjectStoreBindName = ((StatefulContainer) container).getJndiRegistrarBindName();
JndiStatefulSessionRegistrar sfsbJndiRegistrar = Ejb3RegistrarLocator.locateRegistrar().lookup(
sfsbJndiRegistrarObjectStoreBindName, JndiStatefulSessionRegistrar.class);
// Get the metadata
JBossSessionBeanMetaData smd = container.getMetaData();
// Get the appropriate JNDI Name
String jndiName = !isRemote ? smd.getLocalJndiName() : smd.getJndiName();
// Find the Proxy Factory Key for this SFSB
String proxyFactoryKey = sfsbJndiRegistrar.getProxyFactoryRegistryKey(jndiName, smd, !isRemote);
// Lookup the Proxy Factory in the Object Store
StatefulSessionProxyFactory proxyFactory = Ejb3RegistrarLocator.locateRegistrar().lookup(proxyFactoryKey,
StatefulSessionProxyFactory.class);
// Create a new business proxy
Object proxy = proxyFactory.createProxyBusiness(sessionId, businessInterface.getName());
// Return the Proxy
return businessInterface.cast(proxy);
// Collection<ProxyFactory> proxyFactories = this.proxyDeployer.getProxyFactories().values();
// for (ProxyFactory factory : proxyFactories)
// {
// if (isRemote && factory instanceof StatefulRemoteProxyFactory)
// {
// return ((StatefulRemoteProxyFactory) factory).createProxyBusiness(ctx.getId(),null);
// }
// else if (!isRemote && factory instanceof StatefulLocalProxyFactory)
// {
// return ((StatefulLocalProxyFactory) factory).createProxyBusiness(ctx.getId(),null);
// }
// }
// throw new IllegalStateException("Unable to create proxy for getBusinessObject as a proxy factory was not found");
}
}