use of org.apache.naming.ResourceLinkRef in project tomcat70 by apache.
the class ResourceLinkFactory method getObjectInstance.
// -------------------------------------------------- ObjectFactory Methods
/**
* Create a new DataSource instance.
*
* @param obj The reference object describing the DataSource
*/
@Override
public Object getObjectInstance(Object obj, Name name, Context nameCtx, Hashtable<?, ?> environment) throws NamingException {
if (!(obj instanceof ResourceLinkRef)) {
return null;
}
// Can we process this request?
Reference ref = (Reference) obj;
// Read the global ref addr
String globalName = null;
RefAddr refAddr = ref.get(ResourceLinkRef.GLOBALNAME);
if (refAddr != null) {
globalName = refAddr.getContent().toString();
// to access the specified global resource
if (!validateGlobalResourceAccess(globalName)) {
return null;
}
Object result = null;
result = globalContext.lookup(globalName);
// Check the expected type
String expectedClassName = ref.getClassName();
if (expectedClassName == null) {
throw new IllegalArgumentException(sm.getString("resourceLinkFactory.nullType", name, globalName));
}
try {
Class<?> expectedClazz = Class.forName(expectedClassName, true, Thread.currentThread().getContextClassLoader());
if (!expectedClazz.isAssignableFrom(result.getClass())) {
throw new IllegalArgumentException(sm.getString("resourceLinkFactory.wrongType", name, globalName, expectedClassName, result.getClass().getName()));
}
} catch (ClassNotFoundException e) {
throw new IllegalArgumentException(sm.getString("resourceLinkFactory.unknownType", name, globalName, expectedClassName), e);
}
return result;
}
return null;
}
use of org.apache.naming.ResourceLinkRef in project tomcat70 by apache.
the class NamingContextListener method addResourceLink.
/**
* Set the specified resource link in the naming context.
*/
public void addResourceLink(ContextResourceLink resourceLink) {
// Create a reference to the resource.
Reference ref = new ResourceLinkRef(resourceLink.getType(), resourceLink.getGlobal(), resourceLink.getFactory(), null);
Iterator<String> i = resourceLink.listProperties();
while (i.hasNext()) {
String key = i.next();
Object val = resourceLink.getProperty(key);
if (val != null) {
StringRefAddr refAddr = new StringRefAddr(key, val.toString());
ref.add(refAddr);
}
}
javax.naming.Context ctx = "UserTransaction".equals(resourceLink.getName()) ? compCtx : envCtx;
try {
if (logger.isDebugEnabled())
log.debug(" Adding resource link " + resourceLink.getName());
createSubcontexts(envCtx, resourceLink.getName());
ctx.bind(resourceLink.getName(), ref);
} catch (NamingException e) {
logger.error(sm.getString("naming.bindFailed", e));
}
ResourceLinkFactory.registerGlobalResourceAccess(getGlobalNamingContext(), resourceLink.getName(), resourceLink.getGlobal());
}
use of org.apache.naming.ResourceLinkRef in project tomcat by apache.
the class ResourceLinkFactory method getObjectInstance.
// -------------------------------------------------- ObjectFactory Methods
/**
* Create a new DataSource instance.
*
* @param obj The reference object describing the DataSource
*/
@Override
public Object getObjectInstance(Object obj, Name name, Context nameCtx, Hashtable<?, ?> environment) throws NamingException {
if (!(obj instanceof ResourceLinkRef)) {
return null;
}
// Can we process this request?
Reference ref = (Reference) obj;
// Read the global ref addr
String globalName = null;
RefAddr refAddr = ref.get(ResourceLinkRef.GLOBALNAME);
if (refAddr != null) {
globalName = refAddr.getContent().toString();
// to access the specified global resource
if (!validateGlobalResourceAccess(globalName)) {
return null;
}
Object result = null;
result = globalContext.lookup(globalName);
// Check the expected type
String expectedClassName = ref.getClassName();
if (expectedClassName == null) {
throw new IllegalArgumentException(sm.getString("resourceLinkFactory.nullType", name, globalName));
}
try {
Class<?> expectedClazz = Class.forName(expectedClassName, true, Thread.currentThread().getContextClassLoader());
if (!expectedClazz.isAssignableFrom(result.getClass())) {
throw new IllegalArgumentException(sm.getString("resourceLinkFactory.wrongType", name, globalName, expectedClassName, result.getClass().getName()));
}
} catch (ClassNotFoundException e) {
throw new IllegalArgumentException(sm.getString("resourceLinkFactory.unknownType", name, globalName, expectedClassName), e);
}
return result;
}
return null;
}
use of org.apache.naming.ResourceLinkRef in project Payara by payara.
the class ResourceLinkFactory method getObjectInstance.
// -------------------------------------------------- ObjectFactory Methods
/**
* Create a new DataSource instance.
*
* @param obj The reference object describing the DataSource
*/
public Object getObjectInstance(Object obj, Name name, Context nameCtx, Hashtable<?, ?> environment) throws NamingException {
if (!(obj instanceof ResourceLinkRef))
return null;
// Can we process this request?
Reference ref = (Reference) obj;
// Read the global ref addr
String globalName = null;
RefAddr refAddr = ref.get(ResourceLinkRef.GLOBALNAME);
if (refAddr != null) {
globalName = refAddr.getContent().toString();
Object result = null;
result = globalContext.lookup(globalName);
// FIXME: Check type
return result;
}
return (null);
}
use of org.apache.naming.ResourceLinkRef in project tomcat by apache.
the class NamingContextListener method addResourceLink.
/**
* Set the specified resource link in the naming context.
*
* @param resourceLink the resource link
*/
public void addResourceLink(ContextResourceLink resourceLink) {
// Create a reference to the resource.
Reference ref = new ResourceLinkRef(resourceLink.getType(), resourceLink.getGlobal(), resourceLink.getFactory(), null);
Iterator<String> i = resourceLink.listProperties();
while (i.hasNext()) {
String key = i.next();
Object val = resourceLink.getProperty(key);
if (val != null) {
StringRefAddr refAddr = new StringRefAddr(key, val.toString());
ref.add(refAddr);
}
}
javax.naming.Context ctx = "UserTransaction".equals(resourceLink.getName()) ? compCtx : envCtx;
try {
if (log.isDebugEnabled()) {
log.debug(" Adding resource link " + resourceLink.getName());
}
createSubcontexts(envCtx, resourceLink.getName());
ctx.bind(resourceLink.getName(), ref);
} catch (NamingException e) {
log.error(sm.getString("naming.bindFailed", e));
}
ResourceLinkFactory.registerGlobalResourceAccess(getGlobalNamingContext(), resourceLink.getName(), resourceLink.getGlobal());
}
Aggregations