Search in sources :

Example 86 with RefAddr

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

the class BasicDataSourceFactory method getObjectInstance.

/**
 * Creates and return a new <code>BasicDataSource</code> instance. If no instance can be created, return
 * <code>null</code> instead.
 *
 * @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
 *
 * @throws Exception
 *             if an exception occurs creating the instance
 */
@Override
public Object getObjectInstance(final Object obj, final Name name, final Context nameCtx, final Hashtable<?, ?> environment) throws Exception {
    // that specify a class name of "javax.sql.DataSource"
    if (obj == null || !(obj instanceof Reference)) {
        return null;
    }
    final Reference ref = (Reference) obj;
    if (!"javax.sql.DataSource".equals(ref.getClassName())) {
        return null;
    }
    // Check property names and log warnings about obsolete and / or unknown properties
    final List<String> warnMessages = new ArrayList<>();
    final List<String> infoMessages = new ArrayList<>();
    validatePropertyNames(ref, name, warnMessages, infoMessages);
    warnMessages.forEach(log::warn);
    infoMessages.forEach(log::info);
    final Properties properties = new Properties();
    for (final String propertyName : ALL_PROPERTIES) {
        final RefAddr ra = ref.get(propertyName);
        if (ra != null) {
            properties.setProperty(propertyName, Objects.toString(ra.getContent(), null));
        }
    }
    return createDataSource(properties);
}
Also used : RefAddr(javax.naming.RefAddr) Reference(javax.naming.Reference) ArrayList(java.util.ArrayList) Properties(java.util.Properties)

Example 87 with RefAddr

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

the class GenericNamingResourcesFactory method getObjectInstance.

@Override
public Object getObjectInstance(Object obj, Name name, Context nameCtx, Hashtable<?, ?> environment) throws Exception {
    if ((obj == null) || !(obj instanceof Reference)) {
        return null;
    }
    Reference ref = (Reference) obj;
    Enumeration<RefAddr> refs = ref.getAll();
    String type = ref.getClassName();
    Object o = ClassLoaderUtil.loadClass(type, GenericNamingResourcesFactory.class.getClassLoader(), Thread.currentThread().getContextClassLoader()).getConstructor().newInstance();
    while (refs.hasMoreElements()) {
        RefAddr addr = refs.nextElement();
        String param = addr.getType();
        String value = null;
        if (addr.getContent() != null) {
            value = addr.getContent().toString();
        }
        if (setProperty(o, param, value)) {
        } else {
            log.debug("Property not configured[" + param + "]. No setter found on[" + o + "].");
        }
    }
    return o;
}
Also used : RefAddr(javax.naming.RefAddr) Reference(javax.naming.Reference)

Example 88 with RefAddr

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

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(Constants.KEY_USER);
            if (isNotEmpty(ra)) {
                setUser(getStringContent(ra));
            }
            ra = ref.get(Constants.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 89 with RefAddr

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

the class InstanceKeyDataSourceFactory method getObjectInstance.

/**
 * Implements ObjectFactory to create an instance of SharedPoolDataSource or PerUserPoolDataSource
 */
@Override
public Object getObjectInstance(final Object refObj, final Name name, final Context context, final 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) {
        final Reference ref = (Reference) refObj;
        if (isCorrectClass(ref.getClassName())) {
            final RefAddr refAddr = ref.get("instanceKey");
            if (refAddr != null && refAddr.getContent() != null) {
                // object was bound to JNDI via Referenceable API.
                obj = INSTANCE_MAP.get(refAddr.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 = INSTANCE_MAP.get(key);
                }
                if (obj == null) {
                    final InstanceKeyDataSource ds = getNewInstance(ref);
                    setCommonProperties(ref, ds);
                    obj = ds;
                    if (key != null) {
                        INSTANCE_MAP.put(key, ds);
                    }
                }
            }
        }
    }
    return obj;
}
Also used : RefAddr(javax.naming.RefAddr) Reference(javax.naming.Reference)

Example 90 with RefAddr

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

the class InstanceKeyDataSourceFactory method setCommonProperties.

private void setCommonProperties(final Reference ref, final InstanceKeyDataSource ikds) throws IOException, ClassNotFoundException {
    RefAddr refAddr = ref.get("dataSourceName");
    if (refAddr != null && refAddr.getContent() != null) {
        ikds.setDataSourceName(toString(refAddr));
    }
    refAddr = ref.get("description");
    if (refAddr != null && refAddr.getContent() != null) {
        ikds.setDescription(toString(refAddr));
    }
    refAddr = ref.get("jndiEnvironment");
    if (refAddr != null && refAddr.getContent() != null) {
        final byte[] serialized = (byte[]) refAddr.getContent();
        ikds.setJndiEnvironment((Properties) deserialize(serialized));
    }
    refAddr = ref.get("loginTimeout");
    if (refAddr != null && refAddr.getContent() != null) {
        ikds.setLoginTimeout(Duration.ofSeconds(parseInt(refAddr)));
    }
    // Pool properties
    refAddr = ref.get("blockWhenExhausted");
    if (refAddr != null && refAddr.getContent() != null) {
        ikds.setDefaultBlockWhenExhausted(parseBoolean(refAddr));
    }
    refAddr = ref.get("evictionPolicyClassName");
    if (refAddr != null && refAddr.getContent() != null) {
        ikds.setDefaultEvictionPolicyClassName(toString(refAddr));
    }
    // Pool properties
    refAddr = ref.get("lifo");
    if (refAddr != null && refAddr.getContent() != null) {
        ikds.setDefaultLifo(parseBoolean(refAddr));
    }
    refAddr = ref.get("maxIdlePerKey");
    if (refAddr != null && refAddr.getContent() != null) {
        ikds.setDefaultMaxIdle(parseInt(refAddr));
    }
    refAddr = ref.get("maxTotalPerKey");
    if (refAddr != null && refAddr.getContent() != null) {
        ikds.setDefaultMaxTotal(parseInt(refAddr));
    }
    refAddr = ref.get("maxWaitMillis");
    if (refAddr != null && refAddr.getContent() != null) {
        ikds.setDefaultMaxWait(Duration.ofMillis(parseLong(refAddr)));
    }
    refAddr = ref.get("minEvictableIdleTimeMillis");
    if (refAddr != null && refAddr.getContent() != null) {
        ikds.setDefaultMinEvictableIdle(Duration.ofMillis(parseLong(refAddr)));
    }
    refAddr = ref.get("minIdlePerKey");
    if (refAddr != null && refAddr.getContent() != null) {
        ikds.setDefaultMinIdle(parseInt(refAddr));
    }
    refAddr = ref.get("numTestsPerEvictionRun");
    if (refAddr != null && refAddr.getContent() != null) {
        ikds.setDefaultNumTestsPerEvictionRun(parseInt(refAddr));
    }
    refAddr = ref.get("softMinEvictableIdleTimeMillis");
    if (refAddr != null && refAddr.getContent() != null) {
        ikds.setDefaultSoftMinEvictableIdle(Duration.ofMillis(parseLong(refAddr)));
    }
    refAddr = ref.get("testOnCreate");
    if (refAddr != null && refAddr.getContent() != null) {
        ikds.setDefaultTestOnCreate(parseBoolean(refAddr));
    }
    refAddr = ref.get("testOnBorrow");
    if (refAddr != null && refAddr.getContent() != null) {
        ikds.setDefaultTestOnBorrow(parseBoolean(refAddr));
    }
    refAddr = ref.get("testOnReturn");
    if (refAddr != null && refAddr.getContent() != null) {
        ikds.setDefaultTestOnReturn(parseBoolean(refAddr));
    }
    refAddr = ref.get("testWhileIdle");
    if (refAddr != null && refAddr.getContent() != null) {
        ikds.setDefaultTestWhileIdle(parseBoolean(refAddr));
    }
    refAddr = ref.get("timeBetweenEvictionRunsMillis");
    if (refAddr != null && refAddr.getContent() != null) {
        ikds.setDefaultDurationBetweenEvictionRuns(Duration.ofMillis(parseLong(refAddr)));
    }
    // Connection factory properties
    refAddr = ref.get("validationQuery");
    if (refAddr != null && refAddr.getContent() != null) {
        ikds.setValidationQuery(toString(refAddr));
    }
    refAddr = ref.get("validationQueryTimeout");
    if (refAddr != null && refAddr.getContent() != null) {
        ikds.setValidationQueryTimeout(Duration.ofSeconds(parseInt(refAddr)));
    }
    refAddr = ref.get("rollbackAfterValidation");
    if (refAddr != null && refAddr.getContent() != null) {
        ikds.setRollbackAfterValidation(parseBoolean(refAddr));
    }
    refAddr = ref.get("maxConnLifetimeMillis");
    if (refAddr != null && refAddr.getContent() != null) {
        ikds.setMaxConnLifetime(Duration.ofMillis(parseLong(refAddr)));
    }
    // Connection properties
    refAddr = ref.get("defaultAutoCommit");
    if (refAddr != null && refAddr.getContent() != null) {
        ikds.setDefaultAutoCommit(Boolean.valueOf(toString(refAddr)));
    }
    refAddr = ref.get("defaultTransactionIsolation");
    if (refAddr != null && refAddr.getContent() != null) {
        ikds.setDefaultTransactionIsolation(parseInt(refAddr));
    }
    refAddr = ref.get("defaultReadOnly");
    if (refAddr != null && refAddr.getContent() != null) {
        ikds.setDefaultReadOnly(Boolean.valueOf(toString(refAddr)));
    }
}
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