Search in sources :

Example 71 with RefAddr

use of javax.naming.RefAddr 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;
    }
}
Also used : BeanInfo(java.beans.BeanInfo) RefAddr(javax.naming.RefAddr) NamingException(javax.naming.NamingException) PropertyDescriptor(java.beans.PropertyDescriptor) Reference(javax.naming.Reference) Method(java.lang.reflect.Method) ResourceRef(org.apache.naming.ResourceRef)

Example 72 with RefAddr

use of javax.naming.RefAddr in project Payara by payara.

the class OpenEjbFactory method getObjectInstance.

// -------------------------------------------------- ObjectFactory Methods
/**
 * Crete a new EJB instance using OpenEJB.
 *
 * @param obj The reference object describing the DataSource
 */
public Object getObjectInstance(Object obj, Name name, Context nameCtx, Hashtable<?, ?> environment) throws Exception {
    Object beanObj = null;
    if (obj instanceof EjbRef) {
        Reference ref = (Reference) obj;
        String factory = DEFAULT_OPENEJB_FACTORY;
        RefAddr factoryRefAddr = ref.get("openejb.factory");
        if (factoryRefAddr != null) {
            // Retrieving the OpenEJB factory
            factory = factoryRefAddr.getContent().toString();
        }
        Properties env = new Properties();
        env.put(Context.INITIAL_CONTEXT_FACTORY, factory);
        RefAddr linkRefAddr = ref.get("openejb.link");
        if (linkRefAddr != null) {
            String ejbLink = linkRefAddr.getContent().toString();
            beanObj = (new InitialContext(env)).lookup(ejbLink);
        }
    }
    return beanObj;
}
Also used : RefAddr(javax.naming.RefAddr) Reference(javax.naming.Reference) EjbRef(org.apache.naming.EjbRef) Properties(java.util.Properties) InitialContext(javax.naming.InitialContext)

Example 73 with RefAddr

use of javax.naming.RefAddr in project vibur-dbcp by vibur.

the class ViburDBCPObjectFactory method getObjectInstance.

@Override
public Object getObjectInstance(Object obj, Name name, Context nameCtx, Hashtable<?, ?> environment) throws ViburDBCPException {
    Reference reference = (Reference) obj;
    Enumeration<RefAddr> enumeration = reference.getAll();
    Properties props = new Properties();
    while (enumeration.hasMoreElements()) {
        RefAddr refAddr = enumeration.nextElement();
        String pName = refAddr.getType();
        String pValue = (String) refAddr.getContent();
        props.setProperty(pName, pValue);
    }
    ViburDBCPDataSource dataSource = new ViburDBCPDataSource(props);
    dataSource.start();
    return dataSource;
}
Also used : RefAddr(javax.naming.RefAddr) Reference(javax.naming.Reference) Properties(java.util.Properties)

Example 74 with RefAddr

use of javax.naming.RefAddr in project datanucleus-rdbms by datanucleus.

the class DriverAdapterCPDS method getObjectInstance.

/**
 * Implements {@link ObjectFactory} to create an instance of this class
 */
@Override
public Object getObjectInstance(final Object refObj, final Name name, final Context context, final Hashtable<?, ?> env) throws Exception {
    // The spec says to return null if we can't create an instance
    // of the reference
    DriverAdapterCPDS cpds = null;
    if (refObj instanceof Reference) {
        final Reference ref = (Reference) refObj;
        if (ref.getClassName().equals(getClass().getName())) {
            RefAddr ra = ref.get("description");
            if (isNotEmpty(ra)) {
                setDescription(getStringContent(ra));
            }
            ra = ref.get("driver");
            if (isNotEmpty(ra)) {
                setDriver(getStringContent(ra));
            }
            ra = ref.get("url");
            if (isNotEmpty(ra)) {
                setUrl(getStringContent(ra));
            }
            ra = ref.get(KEY_USER);
            if (isNotEmpty(ra)) {
                setUser(getStringContent(ra));
            }
            ra = ref.get(KEY_PASSWORD);
            if (isNotEmpty(ra)) {
                setPassword(getStringContent(ra));
            }
            ra = ref.get("poolPreparedStatements");
            if (isNotEmpty(ra)) {
                setPoolPreparedStatements(getBooleanContentString(ra));
            }
            ra = ref.get("maxIdle");
            if (isNotEmpty(ra)) {
                setMaxIdle(getIntegerStringContent(ra));
            }
            ra = ref.get("timeBetweenEvictionRunsMillis");
            if (isNotEmpty(ra)) {
                setTimeBetweenEvictionRunsMillis(getIntegerStringContent(ra));
            }
            ra = ref.get("numTestsPerEvictionRun");
            if (isNotEmpty(ra)) {
                setNumTestsPerEvictionRun(getIntegerStringContent(ra));
            }
            ra = ref.get("minEvictableIdleTimeMillis");
            if (isNotEmpty(ra)) {
                setMinEvictableIdleTimeMillis(getIntegerStringContent(ra));
            }
            ra = ref.get("maxPreparedStatements");
            if (isNotEmpty(ra)) {
                setMaxPreparedStatements(getIntegerStringContent(ra));
            }
            ra = ref.get("accessToUnderlyingConnectionAllowed");
            if (isNotEmpty(ra)) {
                setAccessToUnderlyingConnectionAllowed(getBooleanContentString(ra));
            }
            cpds = this;
        }
    }
    return cpds;
}
Also used : RefAddr(javax.naming.RefAddr) StringRefAddr(javax.naming.StringRefAddr) Reference(javax.naming.Reference)

Example 75 with RefAddr

use of javax.naming.RefAddr in project datanucleus-rdbms by datanucleus.

the class PerUserPoolDataSourceFactory method getNewInstance.

// Avoid warnings on deserialization
@SuppressWarnings("unchecked")
@Override
protected InstanceKeyDataSource getNewInstance(final Reference ref) throws IOException, ClassNotFoundException {
    final PerUserPoolDataSource pupds = new PerUserPoolDataSource();
    RefAddr ra = ref.get("defaultMaxTotal");
    if (ra != null && ra.getContent() != null) {
        pupds.setDefaultMaxTotal(Integer.parseInt(ra.getContent().toString()));
    }
    ra = ref.get("defaultMaxIdle");
    if (ra != null && ra.getContent() != null) {
        pupds.setDefaultMaxIdle(Integer.parseInt(ra.getContent().toString()));
    }
    ra = ref.get("defaultMaxWaitMillis");
    if (ra != null && ra.getContent() != null) {
        pupds.setDefaultMaxWaitMillis(Integer.parseInt(ra.getContent().toString()));
    }
    ra = ref.get("perUserDefaultAutoCommit");
    if (ra != null && ra.getContent() != null) {
        final byte[] serialized = (byte[]) ra.getContent();
        pupds.setPerUserDefaultAutoCommit((Map<String, Boolean>) deserialize(serialized));
    }
    ra = ref.get("perUserDefaultTransactionIsolation");
    if (ra != null && ra.getContent() != null) {
        final byte[] serialized = (byte[]) ra.getContent();
        pupds.setPerUserDefaultTransactionIsolation((Map<String, Integer>) deserialize(serialized));
    }
    ra = ref.get("perUserMaxTotal");
    if (ra != null && ra.getContent() != null) {
        final byte[] serialized = (byte[]) ra.getContent();
        pupds.setPerUserMaxTotal((Map<String, Integer>) deserialize(serialized));
    }
    ra = ref.get("perUserMaxIdle");
    if (ra != null && ra.getContent() != null) {
        final byte[] serialized = (byte[]) ra.getContent();
        pupds.setPerUserMaxIdle((Map<String, Integer>) deserialize(serialized));
    }
    ra = ref.get("perUserMaxWaitMillis");
    if (ra != null && ra.getContent() != null) {
        final byte[] serialized = (byte[]) ra.getContent();
        pupds.setPerUserMaxWaitMillis((Map<String, Long>) deserialize(serialized));
    }
    ra = ref.get("perUserDefaultReadOnly");
    if (ra != null && ra.getContent() != null) {
        final byte[] serialized = (byte[]) ra.getContent();
        pupds.setPerUserDefaultReadOnly((Map<String, Boolean>) deserialize(serialized));
    }
    return pupds;
}
Also used : RefAddr(javax.naming.RefAddr)

Aggregations

RefAddr (javax.naming.RefAddr)94 Reference (javax.naming.Reference)60 NamingException (javax.naming.NamingException)23 StringRefAddr (javax.naming.StringRefAddr)18 Properties (java.util.Properties)16 ObjectFactory (javax.naming.spi.ObjectFactory)12 InitialContext (javax.naming.InitialContext)9 Method (java.lang.reflect.Method)7 NameNotFoundException (javax.naming.NameNotFoundException)6 EjbRef (org.apache.naming.EjbRef)5 ResourceRef (org.apache.naming.ResourceRef)5 ResourceInfo (org.glassfish.resourcebase.resources.api.ResourceInfo)5 SerializableObjectRefAddr (org.glassfish.resources.naming.SerializableObjectRefAddr)5 InvocationTargetException (java.lang.reflect.InvocationTargetException)4 ArrayList (java.util.ArrayList)4 Enumeration (java.util.Enumeration)4 DataSource (javax.sql.DataSource)4 BeanInfo (java.beans.BeanInfo)3 PropertyDescriptor (java.beans.PropertyDescriptor)3 ResourceLinkRef (org.apache.naming.ResourceLinkRef)3