Search in sources :

Example 46 with StringRefAddr

use of javax.naming.StringRefAddr in project activemq-artemis by apache.

the class JNDIReferenceFactory method getProperties.

public static Properties getProperties(Reference reference) {
    Properties properties = new Properties();
    for (Enumeration iter = reference.getAll(); iter.hasMoreElements(); ) {
        StringRefAddr addr = (StringRefAddr) iter.nextElement();
        properties.put(addr.getType(), (addr.getContent() == null) ? "" : addr.getContent());
    }
    return properties;
}
Also used : Enumeration(java.util.Enumeration) StringRefAddr(javax.naming.StringRefAddr) Properties(java.util.Properties)

Example 47 with StringRefAddr

use of javax.naming.StringRefAddr in project datanucleus-api-jdo by datanucleus.

the class JDOPersistenceManagerFactory method getObjectInstance.

// TODO Provide hashCode to match equals
/**
 * Create a PMF using the (JNDI) location or reference information specified.
 * @param obj The object
 * @param name Name of the object relative to the context
 * @param ctx The context
 * @param env properties used for creating the object
 * @return The PMF instance
 * @throws Exception If an error occurs generating the referenced object
 */
public Object getObjectInstance(Object obj, Name name, Context ctx, Hashtable env) throws Exception {
    JDOPersistenceManagerFactory pmf = null;
    if (NucleusLogger.PERSISTENCE.isDebugEnabled()) {
        NucleusLogger.PERSISTENCE.debug("Creating PersistenceManagerFactory instance via JNDI with values " + "[object] " + (obj == null ? "" : obj.toString()) + " " + "[name] " + (name == null ? "" : name.toString()) + " " + "[context] " + (ctx == null ? "" : ctx.toString()) + " " + "[env] " + (env == null ? "" : env.toString()) + " ");
    }
    if (obj instanceof Reference) {
        Reference ref = (Reference) obj;
        if (ref.getClassName().equals(JDOClassNameConstants.JDOPersistenceManagerFactory) || ref.getClassName().equals(JDOClassNameConstants.JAVAX_JDO_PersistenceManagerFactory)) {
            // Extract the properties to use for PMF creation
            Properties p = new Properties();
            for (Enumeration e = ref.getAll(); e.hasMoreElements(); ) {
                StringRefAddr sra = (StringRefAddr) e.nextElement();
                p.setProperty(sra.getType(), (String) sra.getContent());
            }
            // Create the PMF
            pmf = new JDOPersistenceManagerFactory(p);
            // Freeze the PMF config now that we are handing out PM's : see JDO 1.0.1 [11.7]
            pmf.freezeConfiguration();
            if (NucleusLogger.PERSISTENCE.isDebugEnabled()) {
                NucleusLogger.PERSISTENCE.debug(Localiser.msg("012006", name != null ? name.toString() : null));
            }
        } else {
            NucleusLogger.PERSISTENCE.warn(Localiser.msg("012007", ref.getClassName(), JDOClassNameConstants.JDOPersistenceManagerFactory));
        }
    } else {
        NucleusLogger.PERSISTENCE.warn(Localiser.msg("012008", (obj != null ? obj.getClass().getName() : null)));
    }
    return pmf;
}
Also used : Enumeration(java.util.Enumeration) StringRefAddr(javax.naming.StringRefAddr) Reference(javax.naming.Reference) Properties(java.util.Properties)

Example 48 with StringRefAddr

use of javax.naming.StringRefAddr in project datanucleus-api-jdo by datanucleus.

the class JDOPersistenceManagerFactory method getReference.

/**
 * Retrieves the (JNDI) reference of this PMF object.
 * @return The reference
 */
public Reference getReference() {
    Reference rc = null;
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    try {
        ObjectOutputStream oos = new ObjectOutputStream(baos);
        oos.writeObject(this);
        rc = new Reference(JDOClassNameConstants.JAVAX_JDO_PersistenceManagerFactory, JDOClassNameConstants.JDOPersistenceManagerFactory, null);
        Map p = getConfiguration().getPersistenceProperties();
        for (Iterator<Map.Entry> i = p.entrySet().iterator(); i.hasNext(); ) {
            Map.Entry entry = i.next();
            String key = (String) entry.getKey();
            Object valueObj = entry.getValue();
            if (valueObj instanceof String) {
                String value = (String) valueObj;
                rc.add(new StringRefAddr(key, value));
                if (NucleusLogger.PERSISTENCE.isDebugEnabled()) {
                    NucleusLogger.PERSISTENCE.debug(Localiser.msg("012009", key, value));
                }
            } else if (valueObj instanceof Long) {
                String value = "" + valueObj;
                rc.add(new StringRefAddr(key, value));
                if (NucleusLogger.PERSISTENCE.isDebugEnabled()) {
                    NucleusLogger.PERSISTENCE.debug(Localiser.msg("012009", key, value));
                }
            } else if (valueObj instanceof Integer) {
                String value = "" + valueObj;
                rc.add(new StringRefAddr(key, value));
                if (NucleusLogger.PERSISTENCE.isDebugEnabled()) {
                    NucleusLogger.PERSISTENCE.debug(Localiser.msg("012009", key, value));
                }
            } else if (valueObj instanceof Boolean) {
                String value = (((Boolean) valueObj).booleanValue() ? "true" : "false");
                rc.add(new StringRefAddr(key, value));
                if (NucleusLogger.PERSISTENCE.isDebugEnabled()) {
                    NucleusLogger.PERSISTENCE.debug(Localiser.msg("012009", key, value));
                }
            } else {
                NucleusLogger.PERSISTENCE.warn(Localiser.msg("012010", key));
            }
        }
        if (p.isEmpty() && NucleusLogger.PERSISTENCE.isDebugEnabled()) {
            NucleusLogger.PERSISTENCE.debug(Localiser.msg("012011"));
        }
    } catch (IOException ex) {
        NucleusLogger.PERSISTENCE.error(ex.getMessage());
        throw new NucleusException(ex.getMessage(), ex);
    }
    return rc;
}
Also used : Reference(javax.naming.Reference) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) ObjectOutputStream(java.io.ObjectOutputStream) StringRefAddr(javax.naming.StringRefAddr) NucleusException(org.datanucleus.exceptions.NucleusException) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap)

Example 49 with StringRefAddr

use of javax.naming.StringRefAddr in project sqlite-jna by gwenn.

the class BasicDataSource method getReference.

@Override
public Reference getReference() {
    final Reference ref = new Reference(getClass().getName());
    ref.add(new StringRefAddr("filename", filename));
    return ref;
}
Also used : StringRefAddr(javax.naming.StringRefAddr) Reference(javax.naming.Reference)

Example 50 with StringRefAddr

use of javax.naming.StringRefAddr in project mssql-jdbc by Microsoft.

the class SQLServerDataSource method initializeFromReference.

// Initialize this datasource from properties found inside the reference ref.
// Called by SQLServerDataSourceObjectFactory to initialize new DataSource instance.
void initializeFromReference(javax.naming.Reference ref) {
    // Enumerate all the StringRefAddr objects in the Reference and assign properties appropriately.
    Enumeration<?> e = ref.getAll();
    while (e.hasMoreElements()) {
        StringRefAddr addr = (StringRefAddr) e.nextElement();
        String propertyName = addr.getType();
        String propertyValue = (String) addr.getContent();
        // Special case dataSourceURL and dataSourceDescription.
        if ("dataSourceURL".equals(propertyName)) {
            dataSourceURL = propertyValue;
        } else if ("dataSourceDescription".equals(propertyName)) {
            dataSourceDescription = propertyValue;
        } else if ("trustStorePasswordStripped".equals(propertyName)) {
            trustStorePasswordStripped = true;
        } else // Just skip "class" StringRefAddr, it does not go into connectionProps
        if (!"class".equals(propertyName)) {
            connectionProps.setProperty(propertyName, propertyValue);
        }
    }
}
Also used : StringRefAddr(javax.naming.StringRefAddr)

Aggregations

StringRefAddr (javax.naming.StringRefAddr)68 Reference (javax.naming.Reference)51 NamingException (javax.naming.NamingException)18 RefAddr (javax.naming.RefAddr)9 Test (org.junit.Test)8 Name (javax.naming.Name)7 Properties (java.util.Properties)6 CompositeName (javax.naming.CompositeName)5 NameAlreadyBoundException (javax.naming.NameAlreadyBoundException)5 ObjectFactory (javax.naming.spi.ObjectFactory)5 MalformedURLException (java.net.MalformedURLException)4 Enumeration (java.util.Enumeration)4 Hashtable (java.util.Hashtable)4 Iterator (java.util.Iterator)4 Context (javax.naming.Context)4 HashMap (java.util.HashMap)3 Map (java.util.Map)3 NameNotFoundException (javax.naming.NameNotFoundException)3 Context (org.apache.catalina.Context)3 NamingContext (org.apache.naming.NamingContext)3