public void start() throws Exception {
// Lookup the invoker in the object registry. This typically cannot
// be done until our start method as the invokers may need to be started
// themselves.
ObjectName oname = new ObjectName(invokerMetaData.getInvokerMBean());
servantRegistries = (ServantRegistries)Registry.lookup(oname);
if (servantRegistries == null)
throw new Exception("invoker is null: " + oname);
Policy[] policies = null;
if (codebasePolicy == null)
policies = new Policy[] { sslPolicy, csiv2Policy };
else
policies = new Policy[] { codebasePolicy, sslPolicy, csiv2Policy };
// Read POA usage model from proxy factory config.
ServantRegistryKind registryWithTransientPOA;
ServantRegistryKind registryWithPersistentPOA;
Element proxyFactoryConfig = invokerMetaData.getProxyFactoryConfig();
String poaUsageModel =
MetaData.getOptionalChildContent(proxyFactoryConfig, "poa");
if (poaUsageModel == null || poaUsageModel.equals("shared")) {
registryWithTransientPOA = ServantRegistryKind.SHARED_TRANSIENT_POA;
registryWithPersistentPOA = ServantRegistryKind.SHARED_PERSISTENT_POA;
}
else if (poaUsageModel.equals("per-servant")) {
registryWithTransientPOA =
ServantRegistryKind.TRANSIENT_POA_PER_SERVANT;
registryWithPersistentPOA =
ServantRegistryKind.PERSISTENT_POA_PER_SERVANT;
}
else {
throw new Exception("invalid poa element in proxy factory config: "
+ poaUsageModel);
}
// If there is an interface repository, then get
// the homeInterfaceDef from the IR
InterfaceDef homeInterfaceDef = null;
if (iri != null) {
Repository ir = iri.getReference();
homeInterfaceDef =
InterfaceDefHelper.narrow(ir.lookup_id(homeRepositoryIds[0]));
}
// Instantiate home servant, bind it to the servant registry, and
// create CORBA reference to the EJBHome.
homeServantRegistry =
servantRegistries.getServantRegistry(registryWithPersistentPOA);
String homeServantLoggerName =
EjbHomeCorbaServant.class.getName() + '."+ jndiName;
ServantWithMBeanServer homeServant =
new EjbHomeCorbaServant(container.getJmxName(),
container.getClassLoader(),
homeMethodMap,
homeRepositoryIds,
homeInterfaceDef,
Logger.getLogger(homeServantLoggerName));
homeReferenceFactory = homeServantRegistry.bind(homeServantName(jndiName), homeServant, policies);
org.omg.CORBA.Object corbaRef =
homeReferenceFactory.createReference(homeRepositoryIds[0]);
ejbHome = (EJBHome)PortableRemoteObject.narrow(corbaRef, EJBHome.class);
((EjbHomeCorbaServant)homeServant).setHomeHandle(
new HomeHandleImplIIOP(ejbHome));
// Initialize beanPOA and create metadata depending on the kind of bean
if (container.getBeanMetaData() instanceof EntityMetaData) {
// This is an entity bean (lifespan: persistent)
beanServantRegistry =
servantRegistries.getServantRegistry(registryWithPersistentPOA);
Class pkClass;
EntityMetaData metaData = (EntityMetaData)container.getBeanMetaData();
String pkClassName = metaData.getPrimaryKeyClass();
try {
if (pkClassName != null)
pkClass = container.getClassLoader().loadClass(pkClassName);
else
pkClass = container.getClassLoader().loadClass(
metaData.getEjbClass()).getField(
metaData.getPrimKeyField()).getClass();
}
catch (NoSuchFieldException e) {
logger.error("Unable to identify Bean's Primary Key class! "
+ "Did you specify a primary key class and/or field? "
+ "Does that field exist?");
throw new Exception("Primary Key Problem");
}
catch (NullPointerException e) {
logger.error("Unable to identify Bean's Primary Key class! "
+ "Did you specify a primary key class and/or field? "
+ "Does that field exist?");
throw new Exception("Primary Key Problem");
}
ejbMetaData = new EJBMetaDataImplIIOP(
((EJBProxyFactoryContainer)container).getRemoteClass(),
((EJBProxyFactoryContainer)container).getHomeClass(),
pkClass,
false, // Session
false, // Stateless
ejbHome);
}
else {
// This is a session bean (lifespan: transient)
beanServantRegistry =
servantRegistries.getServantRegistry(registryWithTransientPOA);
if (((SessionMetaData)container.getBeanMetaData()).isStateless()) {
// Stateless session bean
ejbMetaData = new EJBMetaDataImplIIOP(
((EJBProxyFactoryContainer)container).getRemoteClass(),
((EJBProxyFactoryContainer)container).getHomeClass(),
null, // No PK
true, // Session
true, // Stateless
ejbHome);
}
else {
// Stateful session bean
ejbMetaData = new EJBMetaDataImplIIOP(
((EJBProxyFactoryContainer)container).getRemoteClass(),
((EJBProxyFactoryContainer)container).getHomeClass(),
null, // No PK
true, // Session
false, // Stateless
ejbHome);
}
}
// If there is an interface repository, then get
// the beanInterfaceDef from the IR
InterfaceDef beanInterfaceDef = null;
if (iri != null) {
Repository ir = iri.getReference();
beanInterfaceDef =
InterfaceDefHelper.narrow(ir.lookup_id(beanRepositoryIds[0]));
}
String beanServantLoggerName =
EjbObjectCorbaServant.class.getName() + '."+ jndiName;
ServantWithMBeanServer beanServant =
new EjbObjectCorbaServant(container.getJmxName(),
container.getClassLoader(),
poaCurrent,
beanMethodMap,
beanRepositoryIds,
beanInterfaceDef,
Logger.getLogger(beanServantLoggerName));
beanReferenceFactory = beanServantRegistry.bind(beanServantName(jndiName), beanServant, policies);
// Just for testing
logger.info("EJBHome reference for " + jndiName + ":\n"
+ orb.object_to_string((org.omg.CORBA.Object)ejbHome));
// Get JNP usage info from proxy factory config.
useJNPContext = MetaData.getOptionalChildBooleanContent(
proxyFactoryConfig, "register-ejbs-in-jnp-context");
Context initialContext = new InitialContext();
if (useJNPContext) {
String jnpContext =
MetaData.getOptionalChildContent(proxyFactoryConfig,
"jnp-context");
if (jnpContext != null && !jnpContext.equals("")) {
jnpName = jnpContext + "/" + jndiName;
}
else {
jnpName = jndiName;
}
try {
// Bind the bean home in the JNDI initial context
Util.rebind(initialContext,
jnpName,
new Reference(
"javax.ejb.EJBHome",
new StringRefAddr("IOR",
orb.object_to_string(
(org.omg.CORBA.Object)ejbHome)),
IIOPHomeFactory.class.getName(),
null));
logger.info("Home IOR for " + container.getBeanMetaData().getEjbName()
+ " bound to " + jnpName + " in JNP naming service");
}
catch (NamingException e) {
throw new Exception("Cannot bind EJBHome in JNDI:\n" + e);
}
}
NamingContextExt corbaContext = null;
try {
// Obtain local (in-VM) CORBA naming context
corbaContext = NamingContextExtHelper.narrow((org.omg.CORBA.Object)
initialContext.lookup("java:/" +
CorbaNamingService.NAMING_NAME));
}
catch (NamingException e) {
throw new Exception("Cannot lookup java:/" +
CorbaNamingService.NAMING_NAME + ":\n" + e);
}
try {
// Register bean home in local CORBA naming context
rebind(corbaContext, jndiName, (org.omg.CORBA.Object)ejbHome);
logger.info("Home IOR for " + container.getBeanMetaData().getEjbName()
+ " bound to " + jndiName + " in CORBA naming service");
}
catch (Exception e) {
logger.error("Cannot bind EJBHome in CORBA naming service:", e);
throw new Exception("Cannot bind EJBHome in CORBA naming service:\n"
+ e);
}
}
|