use of javax.naming.Reference in project tomcat70 by apache.
the class ResourceFactory method getObjectInstance.
// ----------------------------------------------------------- Constructors
// -------------------------------------------------------------- Constants
// ----------------------------------------------------- Instance Variables
// --------------------------------------------------------- Public Methods
// -------------------------------------------------- ObjectFactory Methods
/**
* Crete 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 Exception {
if (obj instanceof ResourceRef) {
Reference ref = (Reference) obj;
ObjectFactory factory = null;
RefAddr factoryRefAddr = ref.get(Constants.FACTORY);
if (factoryRefAddr != null) {
// Using the specified factory
String factoryClassName = factoryRefAddr.getContent().toString();
// Loading factory
ClassLoader tcl = Thread.currentThread().getContextClassLoader();
Class<?> factoryClass = null;
if (tcl != null) {
try {
factoryClass = tcl.loadClass(factoryClassName);
} catch (ClassNotFoundException e) {
NamingException ex = new NamingException("Could not load resource factory class");
ex.initCause(e);
throw ex;
}
} else {
try {
factoryClass = Class.forName(factoryClassName);
} catch (ClassNotFoundException e) {
NamingException ex = new NamingException("Could not load resource factory class");
ex.initCause(e);
throw ex;
}
}
if (factoryClass != null) {
try {
factory = (ObjectFactory) factoryClass.newInstance();
} catch (Exception e) {
if (e instanceof NamingException)
throw (NamingException) e;
NamingException ex = new NamingException("Could not create resource factory instance");
ex.initCause(e);
throw ex;
}
}
} else {
if (ref.getClassName().equals("javax.sql.DataSource")) {
String javaxSqlDataSourceFactoryClassName = System.getProperty("javax.sql.DataSource.Factory", Constants.DBCP_DATASOURCE_FACTORY);
try {
factory = (ObjectFactory) Class.forName(javaxSqlDataSourceFactoryClassName).newInstance();
} catch (Exception e) {
NamingException ex = new NamingException("Could not create resource factory instance");
ex.initCause(e);
throw ex;
}
} else if (ref.getClassName().equals("javax.mail.Session")) {
String javaxMailSessionFactoryClassName = System.getProperty("javax.mail.Session.Factory", "org.apache.naming.factory.MailSessionFactory");
try {
factory = (ObjectFactory) Class.forName(javaxMailSessionFactoryClassName).newInstance();
} catch (Throwable t) {
NamingException ex = new NamingException("Could not create resource factory instance");
ex.initCause(t);
throw ex;
}
}
}
if (factory != null) {
return factory.getObjectInstance(obj, name, nameCtx, environment);
} else {
throw new NamingException("Cannot create resource instance");
}
}
return null;
}
use of javax.naming.Reference in project tomcat70 by apache.
the class NamingContextListener method addEjb.
/**
* Set the specified EJBs in the naming context.
*/
public void addEjb(ContextEjb ejb) {
// Create a reference to the EJB.
Reference ref = new EjbRef(ejb.getType(), ejb.getHome(), ejb.getRemote(), ejb.getLink());
// Adding the additional parameters, if any
Iterator<String> params = ejb.listProperties();
while (params.hasNext()) {
String paramName = params.next();
String paramValue = (String) ejb.getProperty(paramName);
StringRefAddr refAddr = new StringRefAddr(paramName, paramValue);
ref.add(refAddr);
}
try {
createSubcontexts(envCtx, ejb.getName());
envCtx.bind(ejb.getName(), ref);
} catch (NamingException e) {
logger.error(sm.getString("naming.bindFailed", e));
}
}
use of javax.naming.Reference 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 javax.naming.Reference in project tomcat70 by apache.
the class NamingContextListener method addResource.
/**
* Set the specified resources in the naming context.
*/
public void addResource(ContextResource resource) {
// Create a reference to the resource.
Reference ref = new ResourceRef(resource.getType(), resource.getDescription(), resource.getScope(), resource.getAuth(), resource.getSingleton());
// Adding the additional parameters, if any
Iterator<String> params = resource.listProperties();
while (params.hasNext()) {
String paramName = params.next();
String paramValue = (String) resource.getProperty(paramName);
StringRefAddr refAddr = new StringRefAddr(paramName, paramValue);
ref.add(refAddr);
}
try {
if (logger.isDebugEnabled()) {
logger.debug(" Adding resource ref " + resource.getName() + " " + ref);
}
createSubcontexts(envCtx, resource.getName());
envCtx.bind(resource.getName(), ref);
} catch (NamingException e) {
logger.error(sm.getString("naming.bindFailed", e));
}
if ("javax.sql.DataSource".equals(ref.getClassName()) && resource.getSingleton()) {
try {
ObjectName on = createObjectName(resource);
Object actualResource = envCtx.lookup(resource.getName());
Registry.getRegistry(null, null).registerComponent(actualResource, on, null);
objectNames.put(resource.getName(), on);
} catch (Exception e) {
logger.warn(sm.getString("naming.jmxRegistrationFailed", e));
}
}
}
use of javax.naming.Reference in project tomcat70 by apache.
the class MemoryUserDatabaseFactory method getObjectInstance.
// --------------------------------------------------------- Public Methods
/**
* <p>Create and return a new <code>MemoryUserDatabase</code> instance
* that has been configured according to the properties of the
* specified <code>Reference</code>. If you instance can be created,
* return <code>null</code> instead.</p>
*
* @param obj The possibly null object containing location or
* reference information that can be used in creating an object
* @param name The name of this object relative to <code>nameCtx</code>
* @param nameCtx The context relative to which the <code>name</code>
* parameter is specified, or <code>null</code> if <code>name</code>
* is relative to the default initial context
* @param environment The possibly null environment that is used in
* creating this object
*/
@Override
public Object getObjectInstance(Object obj, Name name, Context nameCtx, Hashtable<?, ?> environment) throws Exception {
// that specify a class name of "org.apache.catalina.UserDatabase"
if ((obj == null) || !(obj instanceof Reference)) {
return (null);
}
Reference ref = (Reference) obj;
if (!"org.apache.catalina.UserDatabase".equals(ref.getClassName())) {
return (null);
}
// Create and configure a MemoryUserDatabase instance based on the
// RefAddr values associated with this Reference
MemoryUserDatabase database = new MemoryUserDatabase(name.toString());
RefAddr ra = null;
ra = ref.get("pathname");
if (ra != null) {
database.setPathname(ra.getContent().toString());
}
ra = ref.get("readonly");
if (ra != null) {
database.setReadonly(Boolean.parseBoolean(ra.getContent().toString()));
}
// Return the configured database instance
database.open();
// Don't try something we know won't work
if (!database.getReadonly())
database.save();
return (database);
}
Aggregations