use of org.apache.naming.ResourceRef in project tomcat by apache.
the class NamingContextListener method addResource.
/**
* Set the specified resources in the naming context.
*
* @param resource the resource descriptor
*/
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 (log.isDebugEnabled()) {
log.debug(" Adding resource ref " + resource.getName() + " " + ref);
}
createSubcontexts(envCtx, resource.getName());
envCtx.bind(resource.getName(), ref);
} catch (NamingException e) {
log.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) {
log.warn(sm.getString("naming.jmxRegistrationFailed", e));
}
}
}
use of org.apache.naming.ResourceRef in project tomee by apache.
the class PersistenceUnitFactory method getObjectInstance.
public Object getObjectInstance(final Object object, final Name name, final Context context, final Hashtable environment) throws Exception {
// ignore non resource-refs
if (!(object instanceof ResourceRef)) {
return null;
}
final Reference ref = (Reference) object;
final Object value;
if (getProperty(ref, JNDI_NAME) != null) {
// lookup the value in JNDI
value = super.getObjectInstance(object, name, context, environment);
} else {
// value is hard hard coded in the properties
value = getStaticValue(ref);
}
return value;
}
use of org.apache.naming.ResourceRef in project Payara by payara.
the class BeanFactory method getObjectInstance.
// ----------------------------------------------------------- Constructors
// -------------------------------------------------------------- Constants
// ----------------------------------------------------- Instance Variables
// --------------------------------------------------------- Public Methods
// -------------------------------------------------- ObjectFactory Methods
/**
* Create a new Bean instance.
*
* @param obj The reference object describing the Bean
*/
public Object getObjectInstance(Object obj, Name name, Context nameCtx, Hashtable<?, ?> environment) throws NamingException {
if (obj instanceof ResourceRef) {
try {
Reference ref = (Reference) obj;
String beanClassName = ref.getClassName();
Class<?> beanClass = null;
ClassLoader tcl = Thread.currentThread().getContextClassLoader();
if (tcl != null) {
try {
beanClass = tcl.loadClass(beanClassName);
} catch (ClassNotFoundException e) {
throw (NamingException) new NamingException().initCause(e);
}
} else {
try {
beanClass = Class.forName(beanClassName);
} catch (ClassNotFoundException e) {
throw (NamingException) new NamingException().initCause(e);
}
}
if (beanClass == null) {
throw new NamingException("Class not found: " + beanClassName);
}
BeanInfo bi = Introspector.getBeanInfo(beanClass);
PropertyDescriptor[] pda = bi.getPropertyDescriptors();
Object bean = beanClass.newInstance();
Enumeration<RefAddr> e = ref.getAll();
while (e.hasMoreElements()) {
RefAddr ra = e.nextElement();
String propName = ra.getType();
if (propName.equals(Constants.FACTORY) || propName.equals("scope") || propName.equals("auth")) {
continue;
}
String value = (String) ra.getContent();
Object[] valueArray = new Object[1];
int i = 0;
for (i = 0; i < pda.length; i++) {
if (pda[i].getName().equals(propName)) {
Class<?> propType = pda[i].getPropertyType();
if (propType.equals(String.class)) {
valueArray[0] = value;
} else if (propType.equals(Character.class) || propType.equals(char.class)) {
valueArray[0] = Character.valueOf(value.charAt(0));
} else if (propType.equals(Byte.class) || propType.equals(byte.class)) {
valueArray[0] = Byte.valueOf(value);
} else if (propType.equals(Short.class) || propType.equals(short.class)) {
valueArray[0] = Short.valueOf(value);
} else if (propType.equals(Integer.class) || propType.equals(int.class)) {
valueArray[0] = Integer.valueOf(value);
} else if (propType.equals(Long.class) || propType.equals(long.class)) {
valueArray[0] = Long.valueOf(value);
} else if (propType.equals(Float.class) || propType.equals(float.class)) {
valueArray[0] = Float.valueOf(value);
} else if (propType.equals(Double.class) || propType.equals(double.class)) {
valueArray[0] = Double.valueOf(value);
} else {
throw new NamingException("String conversion for property type '" + propType.getName() + "' not available");
}
Method setProp = pda[i].getWriteMethod();
if (setProp != null) {
setProp.invoke(bean, valueArray);
} else {
throw new NamingException("Write not allowed for property: " + propName);
}
break;
}
}
if (i == pda.length) {
throw new NamingException("No set method found for property: " + propName);
}
}
return bean;
} catch (java.beans.IntrospectionException ie) {
throw new NamingException(ie.getMessage());
} catch (java.lang.IllegalAccessException iae) {
throw new NamingException(iae.getMessage());
} catch (java.lang.InstantiationException ie2) {
throw new NamingException(ie2.getMessage());
} catch (java.lang.reflect.InvocationTargetException ite) {
throw new NamingException(ite.getMessage());
}
} else {
return null;
}
}
Aggregations