Search in sources :

Example 1 with RefAddr

use of javax.naming.RefAddr in project jetty.project by eclipse.

the class MailSessionReference method getObjectInstance.

/**
     * Create a javax.mail.Session instance based on the information passed in the Reference
     * @see javax.naming.spi.ObjectFactory#getObjectInstance(java.lang.Object, javax.naming.Name, javax.naming.Context, java.util.Hashtable)
     * @param ref the Reference
     * @param arg1 not used
     * @param arg2 not used
     * @param arg3 not used
     * @return the object found
     * @throws Exception if unable to get object instance
     */
public Object getObjectInstance(Object ref, Name arg1, Context arg2, Hashtable arg3) throws Exception {
    if (ref == null)
        return null;
    Reference reference = (Reference) ref;
    Properties props = new Properties();
    String user = null;
    String password = null;
    Enumeration refs = reference.getAll();
    while (refs.hasMoreElements()) {
        RefAddr refAddr = (RefAddr) refs.nextElement();
        String name = refAddr.getType();
        String value = (String) refAddr.getContent();
        if (name.equalsIgnoreCase("user"))
            user = value;
        else if (name.equalsIgnoreCase("pwd"))
            password = value;
        else
            props.put(name, value);
    }
    if (password == null)
        return Session.getInstance(props);
    else
        return Session.getInstance(props, new PasswordAuthenticator(user, password));
}
Also used : RefAddr(javax.naming.RefAddr) StringRefAddr(javax.naming.StringRefAddr) Enumeration(java.util.Enumeration) Reference(javax.naming.Reference) Properties(java.util.Properties)

Example 2 with RefAddr

use of javax.naming.RefAddr in project aries by apache.

the class ObjectFactoryHelper method getObjectInstanceUsingRefAddress.

private Object getObjectInstanceUsingRefAddress(Enumeration<RefAddr> addresses, Object obj, Name name, Context nameCtx, Hashtable<?, ?> environment) throws Exception {
    Object result = null;
    while (addresses.hasMoreElements()) {
        RefAddr address = addresses.nextElement();
        if (address instanceof StringRefAddr && "URL".equals(address.getType())) {
            String urlScheme = getUrlScheme((String) address.getContent());
            ServicePair<ObjectFactory> factoryService = ContextHelper.getURLObjectFactory(callerContext, urlScheme, environment);
            if (factoryService != null) {
                ObjectFactory factory = factoryService.get();
                String value = (String) address.getContent();
                try {
                    result = factory.getObjectInstance(value, name, nameCtx, environment);
                } finally {
                    factoryService.unget();
                }
                // loop we are in.
                if (result != null && result != obj) {
                    break;
                }
            }
        }
    }
    return (result == null) ? obj : result;
}
Also used : RefAddr(javax.naming.RefAddr) StringRefAddr(javax.naming.StringRefAddr) DirObjectFactory(javax.naming.spi.DirObjectFactory) ObjectFactory(javax.naming.spi.ObjectFactory) StringRefAddr(javax.naming.StringRefAddr)

Example 3 with RefAddr

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

the class InstanceKeyDataSourceFactory method getObjectInstance.

/**
 * implements ObjectFactory to create an instance of SharedPoolDataSource
 * or PerUserPoolDataSource
 */
@Override
public Object getObjectInstance(Object refObj, Name name, Context context, Hashtable<?, ?> env) throws IOException, ClassNotFoundException {
    // The spec says to return null if we can't create an instance
    // of the reference
    Object obj = null;
    if (refObj instanceof Reference) {
        Reference ref = (Reference) refObj;
        if (isCorrectClass(ref.getClassName())) {
            RefAddr ra = ref.get("instanceKey");
            if (ra != null && ra.getContent() != null) {
                // object was bound to jndi via Referenceable api.
                obj = instanceMap.get(ra.getContent());
            } else {
                // tomcat jndi creates a Reference out of server.xml
                // <ResourceParam> configuration and passes it to an
                // instance of the factory given in server.xml.
                String key = null;
                if (name != null) {
                    key = name.toString();
                    obj = instanceMap.get(key);
                }
                if (obj == null) {
                    InstanceKeyDataSource ds = getNewInstance(ref);
                    setCommonProperties(ref, ds);
                    obj = ds;
                    if (key != null) {
                        instanceMap.put(key, ds);
                    }
                }
            }
        }
    }
    return obj;
}
Also used : RefAddr(javax.naming.RefAddr) Reference(javax.naming.Reference)

Example 4 with RefAddr

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

the class InstanceKeyDataSourceFactory method setCommonProperties.

private void setCommonProperties(Reference ref, InstanceKeyDataSource ikds) throws IOException, ClassNotFoundException {
    RefAddr ra = ref.get("dataSourceName");
    if (ra != null && ra.getContent() != null) {
        ikds.setDataSourceName(ra.getContent().toString());
    }
    ra = ref.get("description");
    if (ra != null && ra.getContent() != null) {
        ikds.setDescription(ra.getContent().toString());
    }
    ra = ref.get("jndiEnvironment");
    if (ra != null && ra.getContent() != null) {
        byte[] serialized = (byte[]) ra.getContent();
        ikds.setJndiEnvironment((Properties) deserialize(serialized));
    }
    ra = ref.get("loginTimeout");
    if (ra != null && ra.getContent() != null) {
        ikds.setLoginTimeout(Integer.parseInt(ra.getContent().toString()));
    }
    // Pool properties
    ra = ref.get("blockWhenExhausted");
    if (ra != null && ra.getContent() != null) {
        ikds.setDefaultBlockWhenExhausted(Boolean.valueOf(ra.getContent().toString()).booleanValue());
    }
    ra = ref.get("evictionPolicyClassName");
    if (ra != null && ra.getContent() != null) {
        ikds.setDefaultEvictionPolicyClassName(ra.getContent().toString());
    }
    // Pool properties
    ra = ref.get("lifo");
    if (ra != null && ra.getContent() != null) {
        ikds.setDefaultLifo(Boolean.valueOf(ra.getContent().toString()).booleanValue());
    }
    ra = ref.get("maxIdlePerKey");
    if (ra != null && ra.getContent() != null) {
        ikds.setDefaultMaxIdle(Integer.parseInt(ra.getContent().toString()));
    }
    ra = ref.get("maxTotalPerKey");
    if (ra != null && ra.getContent() != null) {
        ikds.setDefaultMaxTotal(Integer.parseInt(ra.getContent().toString()));
    }
    ra = ref.get("maxWaitMillis");
    if (ra != null && ra.getContent() != null) {
        ikds.setDefaultMaxWaitMillis(Long.parseLong(ra.getContent().toString()));
    }
    ra = ref.get("minEvictableIdleTimeMillis");
    if (ra != null && ra.getContent() != null) {
        ikds.setDefaultMinEvictableIdleTimeMillis(Long.parseLong(ra.getContent().toString()));
    }
    ra = ref.get("minIdlePerKey");
    if (ra != null && ra.getContent() != null) {
        ikds.setDefaultMinIdle(Integer.parseInt(ra.getContent().toString()));
    }
    ra = ref.get("numTestsPerEvictionRun");
    if (ra != null && ra.getContent() != null) {
        ikds.setDefaultNumTestsPerEvictionRun(Integer.parseInt(ra.getContent().toString()));
    }
    ra = ref.get("softMinEvictableIdleTimeMillis");
    if (ra != null && ra.getContent() != null) {
        ikds.setDefaultSoftMinEvictableIdleTimeMillis(Long.parseLong(ra.getContent().toString()));
    }
    ra = ref.get("testOnCreate");
    if (ra != null && ra.getContent() != null) {
        ikds.setDefaultTestOnCreate(Boolean.valueOf(ra.getContent().toString()).booleanValue());
    }
    ra = ref.get("testOnBorrow");
    if (ra != null && ra.getContent() != null) {
        ikds.setDefaultTestOnBorrow(Boolean.valueOf(ra.getContent().toString()).booleanValue());
    }
    ra = ref.get("testOnReturn");
    if (ra != null && ra.getContent() != null) {
        ikds.setDefaultTestOnReturn(Boolean.valueOf(ra.getContent().toString()).booleanValue());
    }
    ra = ref.get("testWhileIdle");
    if (ra != null && ra.getContent() != null) {
        ikds.setDefaultTestWhileIdle(Boolean.valueOf(ra.getContent().toString()).booleanValue());
    }
    ra = ref.get("timeBetweenEvictionRunsMillis");
    if (ra != null && ra.getContent() != null) {
        ikds.setDefaultTimeBetweenEvictionRunsMillis(Long.parseLong(ra.getContent().toString()));
    }
    // Connection factory properties
    ra = ref.get("validationQuery");
    if (ra != null && ra.getContent() != null) {
        ikds.setValidationQuery(ra.getContent().toString());
    }
    ra = ref.get("validationQueryTimeout");
    if (ra != null && ra.getContent() != null) {
        ikds.setValidationQueryTimeout(Integer.parseInt(ra.getContent().toString()));
    }
    ra = ref.get("rollbackAfterValidation");
    if (ra != null && ra.getContent() != null) {
        ikds.setRollbackAfterValidation(Boolean.valueOf(ra.getContent().toString()).booleanValue());
    }
    ra = ref.get("maxConnLifetimeMillis");
    if (ra != null && ra.getContent() != null) {
        ikds.setMaxConnLifetimeMillis(Long.parseLong(ra.getContent().toString()));
    }
    // Connection properties
    ra = ref.get("defaultAutoCommit");
    if (ra != null && ra.getContent() != null) {
        ikds.setDefaultAutoCommit(Boolean.valueOf(ra.getContent().toString()));
    }
    ra = ref.get("defaultTransactionIsolation");
    if (ra != null && ra.getContent() != null) {
        ikds.setDefaultTransactionIsolation(Integer.parseInt(ra.getContent().toString()));
    }
    ra = ref.get("defaultReadOnly");
    if (ra != null && ra.getContent() != null) {
        ikds.setDefaultReadOnly(Boolean.valueOf(ra.getContent().toString()));
    }
}
Also used : RefAddr(javax.naming.RefAddr)

Example 5 with RefAddr

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

the class SharedPoolDataSourceFactory method getNewInstance.

@Override
protected InstanceKeyDataSource getNewInstance(Reference ref) {
    SharedPoolDataSource spds = new SharedPoolDataSource();
    RefAddr ra = ref.get("maxTotal");
    if (ra != null && ra.getContent() != null) {
        spds.setMaxTotal(Integer.parseInt(ra.getContent().toString()));
    }
    return spds;
}
Also used : RefAddr(javax.naming.RefAddr)

Aggregations

RefAddr (javax.naming.RefAddr)94 Reference (javax.naming.Reference)59 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