* We create the java:comp/env namespace with properties, EJB-References,
* and DataSource ressources.
*/
private void setupEnvironment() throws Exception
{
BeanMetaData beanMetaData = getBeanMetaData();
// debug
log.debug("Begin java:comp/env for EJB: " + beanMetaData.getEjbName());
ClassLoader tcl = SecurityActions.getContextClassLoader();
log.debug("TCL: " + tcl);
ORB orb = null;
HandleDelegate hd = null;
try
{
orb = (ORB)server.getAttribute(ORB_NAME, "ORB");
hd = (HandleDelegate)server.getAttribute(ORB_NAME, "HandleDelegate");
}
catch (Throwable t)
{
log.debug("Unable to retrieve orb" + t.toString());
}
// Since the BCL is already associated with this thread we can start
// using the java: namespace directly
Context ctx = (Context)new InitialContext().lookup("java:comp");
Object id = ENCFactory.getCurrentId();
log.debug("Using java:comp using id=" + id);
// Bind the orb
if (orb != null)
{
NonSerializableFactory.rebind(ctx, "ORB", orb);
log.debug("Bound java:comp/ORB for EJB: " + getBeanMetaData().getEjbName());
NonSerializableFactory.rebind(ctx, "HandleDelegate", hd);
log.debug("Bound java:comp/HandleDelegate for EJB: " + getBeanMetaData().getEjbName());
}
// JTA links
ctx.bind("TransactionSynchronizationRegistry", new LinkRef("java:TransactionSynchronizationRegistry"));
log.debug("Linked java:comp/TransactionSynchronizationRegistry to JNDI name: java:TransactionSynchronizationRegistry");
Context envCtx = ctx.createSubcontext("env");
// Bind environment properties
{
Iterator i = beanMetaData.getEnvironmentEntries();
while (i.hasNext())
{
EnvEntryMetaData entry = (EnvEntryMetaData)i.next();
log.debug("Binding env-entry: " + entry.getName() + " of type: " + entry.getType() + " to value:" + entry.getValue());
EnvEntryBinder.bindEnvEntry(envCtx, entry);
}
}
// Bind EJB references
{
Iterator i = beanMetaData.getEjbReferences();
while (i.hasNext())
{
EjbRefMetaData ref = (EjbRefMetaData)i.next();
log.debug("Binding an EJBReference " + ref.getName());
if (ref.getLink() != null)
{
// Internal link
String linkName = ref.getLink();
String jndiName = ref.getJndiName();
log.debug("Binding " + ref.getName() + " to ejb-link: " + linkName + " -> " + jndiName);
if (jndiName == null)
{
String msg = "Failed to resolve ejb-link: " + linkName + " from ejb-ref: " + ref.getName() + " in ejb: " + beanMetaData.getEjbName();
throw new DeploymentException(msg);
}
Util.bind(envCtx, ref.getName(), new LinkRef(jndiName));
}
else
{
// Get the invoker specific ejb-ref mappings
Iterator it = beanMetaData.getInvokerBindings();
Reference reference = null;
while (it.hasNext())
{
String invokerBinding = (String)it.next();
// Check for an invoker level jndi-name
String name = ref.getInvokerBinding(invokerBinding);
// Check for an global jndi-name
if (name == null)
name = ref.getJndiName();
if (name == null)
{
throw new DeploymentException("ejb-ref " + ref.getName() + ", expected either ejb-link in ejb-jar.xml or " + "jndi-name in jboss.xml");
}
StringRefAddr addr = new StringRefAddr(invokerBinding, name);
log.debug("adding " + invokerBinding + ":" + name + " to Reference");
if (reference == null)
{
reference = new Reference("javax.naming.LinkRef", ENCThreadLocalKey.class.getName(), null);
}
reference.add(addr);
}
// If there were invoker bindings create bind the reference
if (reference != null)
{
if (ref.getJndiName() != null)
{
// Add default for the bean level ejb-ref/jndi-name
StringRefAddr addr = new StringRefAddr("default", ref.getJndiName());
reference.add(addr);
}
if (reference.size() == 1 && reference.get("default") == null)
{
/* There is only one invoker binding and its not default so
create a default binding to allow the link to have a value
when accessed without an invoker active.
*/
StringRefAddr addr = (StringRefAddr)reference.get(0);
String target = (String)addr.getContent();
StringRefAddr addr1 = new StringRefAddr("default", target);
reference.add(addr1);
}
Util.bind(envCtx, ref.getName(), reference);
}
else
{
// Bind the bean level ejb-ref/jndi-name
if (ref.getJndiName() == null)
{
throw new DeploymentException("ejb-ref " + ref.getName() + ", expected either ejb-link in ejb-jar.xml " + "or jndi-name in jboss.xml");
}
Util.bind(envCtx, ref.getName(), new LinkRef(ref.getJndiName()));
}
}
}
}
// Bind Local EJB references
{
Iterator i = beanMetaData.getEjbLocalReferences();
while (i.hasNext())
{
EjbLocalRefMetaData ref = (EjbLocalRefMetaData)i.next();
String refName = ref.getName();
log.debug("Binding an EJBLocalReference " + ref.getName());
if (ref.getLink() != null)
{
// Internal link
log.debug("Binding " + refName + " to bean source: " + ref.getLink());
String jndiName = ref.getJndiName();
Util.bind(envCtx, ref.getName(), new LinkRef(jndiName));
}
else
{
// Bind the bean level ejb-local-ref/local-jndi-name
if (ref.getJndiName() == null)
{
throw new DeploymentException("ejb-local-ref " + ref.getName() + ", expected either ejb-link in ejb-jar.xml " + "or local-jndi-name in jboss.xml");
}
Util.bind(envCtx, ref.getName(), new LinkRef(ref.getJndiName()));
}
}
}
// Bind service references
{
ClassLoader loader = unit.getClassLoader();
UnifiedVirtualFile vfsRoot = new VirtualFileAdaptor(unit.getRoot());
Iterator<ServiceReferenceMetaData> serviceReferences = beanMetaData.getServiceReferences();
if (serviceReferences != null)
{
while (serviceReferences.hasNext())
{
ServiceReferenceMetaData sref = serviceReferences.next();
String refName = sref.getServiceRefName();
new ServiceReferenceHandler().bindServiceRef(envCtx, refName, vfsRoot, loader, sref);
}
}
}
// Bind resource references
{
Iterator i = beanMetaData.getResourceReferences();
// let's play guess the cast game ;) New metadata should fix this.
ApplicationMetaData application = beanMetaData.getApplicationMetaData();
while (i.hasNext())
{
ResourceRefMetaData ref = (ResourceRefMetaData)i.next();
String resourceName = ref.getResourceName();
String finalName = application.getResourceByName(resourceName);
String resType = ref.getType();
// If there was no resource-manager specified then an immeadiate
// jndi-name or res-url name should have been given
if (finalName == null)
finalName = ref.getJndiName();
if (finalName == null && resType.equals("java.net.URL") == false)
{
// the application assembler did not provide a resource manager
// if the type is javax.sql.Datasoure use the default one
if (ref.getType().equals("javax.sql.DataSource"))
{
// Go through JNDI and look for DataSource - use the first one
Context dsCtx = new InitialContext();
try
{
// Check if it is available in JNDI
dsCtx.lookup("java:/DefaultDS");
finalName = "java:/DefaultDS";
}
catch (Exception e)
{
log.debug("failed to lookup DefaultDS; ignoring", e);
}
finally
{
dsCtx.close();
}
}
// Default failed? Warn user and move on
// POTENTIALLY DANGEROUS: should this be a critical error?
if (finalName == null)
{
log.warn("No resource manager found for " + ref.getResourceName());
continue;
}
}
if (resType.equals("java.net.URL"))
{
// URL bindings
if (ref.getResURL() != null)
{
// The URL string was given by the res-url
log.debug("Binding URL: " + ref.getRefName() + " to JDNI ENC as: " + ref.getResURL());
URL resURL = new URL(ref.getResURL());
Util.bind(envCtx, ref.getRefName(), resURL);
}
else
{
log.debug("Binding URL: " + ref.getRefName() + " to: " + finalName);
Object bind = null;
if (ref.getJndiName() != null)
{
// Was the url given as a jndi-name reference to link to it
bind = new LinkRef(finalName);
}
else
{
// The url string was given via a resource-name mapping
bind = new URL(finalName);
}
Util.bind(envCtx, ref.getRefName(), bind);
}
}
else
{
// Resource Manager bindings, should validate the type...
log.debug("Binding resource manager: " + ref.getRefName() + " to JDNI ENC as: " + finalName);
Util.bind(envCtx, ref.getRefName(), new LinkRef(finalName));
}
}
}
// Bind resource env references
{
Iterator i = beanMetaData.getResourceEnvReferences();
while (i.hasNext())
{
ResourceEnvRefMetaData resRef = (ResourceEnvRefMetaData)i.next();
String encName = resRef.getRefName();
String jndiName = resRef.getJndiName();
// Should validate the type...
log.debug("Binding env resource: " + encName + " to JDNI ENC as: " + jndiName);
Util.bind(envCtx, encName, new LinkRef(jndiName));
}
}
// Bind message destination references
{
Iterator i = beanMetaData.getMessageDestinationReferences();
while (i.hasNext())
{
MessageDestinationRefMetaData ref = (MessageDestinationRefMetaData)i.next();
String refName = ref.getRefName();
String jndiName = ref.getJNDIName();
String link = ref.getLink();
if (link != null)
{
if (jndiName == null)
{
MessageDestinationMetaData messageDestination = getMessageDestination(link);
if (messageDestination == null)
throw new DeploymentException("message-destination-ref '" + refName + "' message-destination-link '" + link
+ "' not found and no jndi-name in jboss.xml");
else
{
String linkJNDIName = messageDestination.getJndiName();
if (linkJNDIName == null)
log.warn("message-destination '" + link + "' has no jndi-name in jboss.xml");
else
jndiName = linkJNDIName;
}
}
else
log.warn("message-destination-ref '" + refName + "' ignoring message-destination-link '" + link + "' because it has a jndi-name in jboss.xml");
}
else if (jndiName == null)
throw new DeploymentException("message-destination-ref '" + refName + "' has no message-destination-link in ejb-jar.xml and no jndi-name in jboss.xml");
Util.bind(envCtx, refName, new LinkRef(jndiName));
}
}
// Create a java:comp/env/security/security-domain link to the container
// or application security-domain if one exists so that access to the
// security manager can be made without knowing the global jndi name.
String securityDomain = metaData.getContainerConfiguration().getSecurityDomain();
if (securityDomain == null)
securityDomain = metaData.getApplicationMetaData().getSecurityDomain();
if (securityDomain != null)
{
//JBAS-6060: Tolerate a Security Domain configuration without the java:/jaas prefix
if(securityDomain.startsWith(SecurityConstants.JAAS_CONTEXT_ROOT) == false)
securityDomain = SecurityConstants.JAAS_CONTEXT_ROOT + "/" + securityDomain;
log.debug("Binding securityDomain: " + securityDomain + " to JDNI ENC as: security/security-domain");
Util.bind(envCtx, "security/security-domain", new LinkRef(securityDomain));
Util.bind(envCtx, "security/subject", new LinkRef(securityDomain + "/subject"));
Util.bind(envCtx, "security/realmMapping", new LinkRef(securityDomain + "/realmMapping"));
Util.bind(envCtx, "security/authorizationMgr", new LinkRef(securityDomain + "/authorizationMgr"));
}
log.debug("End java:comp/env for EJB: " + beanMetaData.getEjbName());
}