Example usage for javax.naming StringRefAddr StringRefAddr

List of usage examples for javax.naming StringRefAddr StringRefAddr

Introduction

In this page you can find the example usage for javax.naming StringRefAddr StringRefAddr.

Prototype

public StringRefAddr(String addrType, String addr) 

Source Link

Document

Constructs a new instance of StringRefAddr using its address type and contents.

Usage

From source file:Bind.java

License:asdf

public Reference getReference() throws NamingException {
    String cName = Car.class.getName();
    StringRefAddr cRef = new StringRefAddr("Car Description", make + ":" + model);
    String cfName = "asdf";
    Reference ref = new Reference(cName, cRef, cfName, null);
    return ref;//from   w ww .j a v  a 2 s .co  m
}

From source file:com.caricah.iotracah.datastore.IotDataSource.java

public void setupDatasource(String driver, String dbUrl, String username, String password)
        throws NamingException {

    System.setProperty(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.fscontext.RefFSContextFactory");
    System.setProperty(Context.PROVIDER_URL, "file:////tmp");

    Context ctx = new InitialContext();

    // Construct DriverAdapterCPDS reference
    Reference cpdsRef = new Reference("org.apache.commons.dbcp2.cpdsadapter.DriverAdapterCPDS",
            "org.apache.commons.dbcp2.cpdsadapter.DriverAdapterCPDS", null);
    cpdsRef.add(new StringRefAddr("driver", driver));
    cpdsRef.add(new StringRefAddr("url", dbUrl));
    cpdsRef.add(new StringRefAddr("user", username));
    cpdsRef.add(new StringRefAddr("password", password));
    ctx.rebind("jdbc_cpds", cpdsRef);

    Reference ref = new Reference("org.apache.commons.dbcp2.datasources.SharedPoolDataSource",
            "org.apache.commons.dbcp2.datasources.SharedPoolDataSourceFactory", null);
    ref.add(new StringRefAddr("dataSourceName", "jdbc_cpds"));
    ref.add(new StringRefAddr("defaultMaxTotal", "100"));
    ref.add(new StringRefAddr("defaultMaxIdle", "30"));
    ref.add(new StringRefAddr("defaultMaxWaitMillis", "10000"));

    ctx.rebind("jdbc_commonpool", ref);

}

From source file:net.chrisrichardson.foodToGo.ejb3.facadeWithSpringDI.SpringBeanReferenceInitializer.java

private void bind(InitialContext ctx, String jndiName, Class type, String beanName) throws NamingException {
    Reference reference = new Reference(type.getName(), new StringRefAddr("beanName", beanName),
            SpringObjectFactory.class.getName(), null);
    ctx.bind(jndiName, reference);//from w ww  . j av a 2 s .  c  o m
    jndiNames.add(jndiName);
    logger.debug("Bound it: " + jndiName);
}

From source file:com.enioka.jqm.tools.ResourceParser.java

private static JndiResourceDescriptor fromDatabase(String alias) throws NamingException {
    JndiObjectResource resource = null;//from ww w.jav  a2  s .  com
    EntityManager em = null;
    try {
        // Using the horrible CriteriaBuilder API instead of a string query. This avoids classloading issues - Hibernate binds
        // the entities at run time with the thread current classloader...
        em = Helpers.getNewEm();

        CriteriaBuilder cb = em.getCriteriaBuilder();
        CriteriaQuery<JndiObjectResource> q = cb.createQuery(JndiObjectResource.class);
        Root<JndiObjectResource> c = q.from(JndiObjectResource.class);
        ParameterExpression<String> p = cb.parameter(String.class);
        q.select(c).where(cb.equal(c.get("name"), p));

        TypedQuery<JndiObjectResource> query = em.createQuery(q);
        query.setParameter(p, alias);
        resource = query.getSingleResult();
    } catch (Exception e) {
        NamingException ex = new NamingException("Could not find a JNDI object resource of name " + alias);
        ex.setRootCause(e);
        throw ex;
    } finally {
        if (em != null) {
            em.close();
        }
    }

    // Create the ResourceDescriptor from the JPA object
    JndiResourceDescriptor d = new JndiResourceDescriptor(resource.getType(), resource.getDescription(), null,
            resource.getAuth(), resource.getFactory(), resource.getSingleton());
    for (JndiObjectResourceParameter prm : resource.getParameters()) {
        d.add(new StringRefAddr(prm.getKey(), prm.getValue()));
    }

    return d;
}

From source file:com.stratelia.silverpeas.versioning.jcr.impl.AbstractJcrTestCase.java

@Resource
public void setDataSource(DataSource datasource) {
    this.datasource = datasource;
    try {/*from  www . j a  va2 s  . co  m*/
        prepareJndi();
        Hashtable env = new Hashtable();
        env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.fscontext.RefFSContextFactory");
        InitialContext ic = new InitialContext(env);
        Properties properties = new Properties();
        properties.load(PathTestUtil.class.getClassLoader().getResourceAsStream("jdbc.properties"));
        // Construct BasicDataSource reference
        Reference ref = new Reference("javax.sql.DataSource", "org.apache.commons.dbcp.BasicDataSourceFactory",
                null);
        ref.add(new StringRefAddr("driverClassName",
                properties.getProperty("driverClassName", "org.postgresql.Driver")));
        ref.add(new StringRefAddr("url",
                properties.getProperty("url", "jdbc:postgresql://localhost:5432/postgres")));
        ref.add(new StringRefAddr("username", properties.getProperty("username", "postgres")));
        ref.add(new StringRefAddr("password", properties.getProperty("password", "postgres")));
        ref.add(new StringRefAddr("maxActive", "4"));
        ref.add(new StringRefAddr("maxWait", "5000"));
        ref.add(new StringRefAddr("removeAbandoned", "true"));
        ref.add(new StringRefAddr("removeAbandonedTimeout", "5000"));
        rebind(ic, JNDINames.DATABASE_DATASOURCE, ref);
        rebind(ic, JNDINames.ADMIN_DATASOURCE, ref);
    } catch (NamingException nex) {
        nex.printStackTrace();
    } catch (IOException nex) {
        nex.printStackTrace();
    }
}

From source file:com.funambol.server.db.DataSourceFactoryTest.java

/**
 * Test of getObjectInstance method, of class DataSourceFactory.
 *//*from ww  w .  j av a2s  . c o  m*/
public void testGetObjectInstance_WitWrongJNDIName() throws Exception {
    DataSourceFactory dataSourceFactory = new DataSourceFactory();
    InitialContext context = new InitialContext();
    Hashtable environment = new Hashtable();

    Name name = new CompositeName("fnbl");

    Reference ref = new Reference("javax.sql.DataSource");
    RefAddr url = new StringRefAddr("url", "jdbc:hsqldb:hsql://localhost/funambol");
    ref.add(url);

    Object result = dataSourceFactory.getObjectInstance(ref, name, context, environment);
    assertTrue(result instanceof org.apache.tomcat.dbcp.dbcp.BasicDataSource);

    BasicDataSource bds = (BasicDataSource) result;
    assertNull(bds.getPassword());
    assertEquals("jdbc:hsqldb:hsql://localhost/funambol", bds.getUrl());
    //
    // These are read from the db.xml since there is not the fnbl.xml file
    //
    assertEquals("db", bds.getUsername());
    assertEquals("org.hsqldb.jdbcDriver", bds.getDriverClassName());

}

From source file:Rebind.java

public Reference getReference() throws NamingException {

    return new Reference(Fruit.class.getName(), new StringRefAddr("fruit", fruit), FruitFactory.class.getName(),
            null); // factory location
}

From source file:com.enioka.jqm.tools.ResourceParser.java

private static void importXml() throws NamingException {
    InputStream is = ResourceParser.class.getClassLoader().getResourceAsStream(Helpers.resourceFile);
    DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();

    try {//  w ww .j ava  2s.  co  m
        DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
        Document doc = dBuilder.parse(is);
        doc.getDocumentElement().normalize();

        NodeList nList = doc.getElementsByTagName("resource");

        String jndiAlias = null, resourceClass = null, description = "no description", scope = null,
                auth = "Container", factory = null;
        boolean singleton = false;

        for (int i = 0; i < nList.getLength(); i++) {
            Node n = nList.item(i);
            Map<String, String> otherParams = new HashMap<String, String>();

            NamedNodeMap attrs = n.getAttributes();
            for (int j = 0; j < attrs.getLength(); j++) {
                Node attr = attrs.item(j);
                String key = attr.getNodeName();
                String value = attr.getNodeValue();

                if ("name".equals(key)) {
                    jndiAlias = value;
                } else if ("type".equals(key)) {
                    resourceClass = value;
                } else if ("description".equals(key)) {
                    description = value;
                } else if ("factory".equals(key)) {
                    factory = value;
                } else if ("auth".equals(key)) {
                    auth = value;
                } else if ("singleton".equals(key)) {
                    singleton = Boolean.parseBoolean(value);
                } else {
                    otherParams.put(key, value);
                }
            }

            if (resourceClass == null || jndiAlias == null || factory == null) {
                throw new NamingException("could not load the resource.xml file");
            }

            JndiResourceDescriptor jrd = new JndiResourceDescriptor(resourceClass, description, scope, auth,
                    factory, singleton);
            for (Map.Entry<String, String> prm : otherParams.entrySet()) {
                jrd.add(new StringRefAddr(prm.getKey(), prm.getValue()));
            }
            xml.put(jndiAlias, jrd);
        }
    } catch (Exception e) {
        NamingException pp = new NamingException("could not initialize the JNDI local resources");
        pp.setRootCause(e);
        throw pp;
    } finally {
        IOUtils.closeQuietly(is);
    }
}

From source file:com.funambol.server.db.DataSourceFactoryTest.java

/**
 * Test of getObjectInstance method, of class DataSourceFactory.
 */// www .  j  a  v  a2s  . c o  m
public void testGetObjectInstance_fnblds() throws Exception {
    DataSourceFactory dataSourceFactory = new DataSourceFactory();
    InitialContext context = new InitialContext();
    Hashtable environment = new Hashtable();

    Name name = new CompositeName("fnblds");

    Reference ref = new Reference("javax.sql.DataSource");
    RefAddr minIdle = new StringRefAddr("minIdle", "3");
    ref.add(minIdle);

    //
    // At the end this should be overwritten by the value in the fnblds.xml file
    //
    RefAddr url = new StringRefAddr("url", "this-is-the-url");
    ref.add(url);

    Object result = dataSourceFactory.getObjectInstance(ref, name, context, environment);
    assertTrue(result instanceof org.apache.tomcat.dbcp.dbcp.BasicDataSource);

    BasicDataSource bds = (BasicDataSource) result;
    assertEquals(3, bds.getMinIdle());
    //
    // These are read from the db.xml
    //
    assertEquals("db", bds.getUsername());
    assertEquals("org.hsqldb.jdbcDriver", bds.getDriverClassName());

    //
    // These are read from the fnblds.xml
    //
    assertEquals("", bds.getPassword());
    assertEquals("jdbc:hsqldb:hsql://localhost/funambol", bds.getUrl());
}

From source file:com.funambol.server.db.DataSourceFactoryTest.java

/**
 * Test of getObjectInstance method, of class DataSourceFactory.
 *//* w  w w .  j av  a  2  s . c o  m*/
public void testGetObjectInstance_fnblcore() throws Exception {
    DataSourceFactory dataSourceFactory = new DataSourceFactory();
    InitialContext context = new InitialContext();
    Hashtable environment = new Hashtable();

    Name name = new CompositeName("fnblcore");

    Reference ref = new Reference("javax.sql.DataSource");
    RefAddr minIdle = new StringRefAddr("minIdle", "3");
    ref.add(minIdle);

    Object result = dataSourceFactory.getObjectInstance(ref, name, context, environment);
    assertTrue(result instanceof org.apache.tomcat.dbcp.dbcp.BasicDataSource);

    BasicDataSource bds = (BasicDataSource) result;
    assertEquals(3, bds.getMinIdle());
    //
    // These are read from the db.xml
    //
    assertEquals("db", bds.getUsername());
    assertEquals("org.hsqldb.jdbcDriver", bds.getDriverClassName());

    //
    // These are read from the fnblds.xml
    //
    assertEquals("ss", bds.getPassword());
    assertEquals("jdbc:hsqldb:hsql://localhost/funambol", bds.getUrl());
}