Example usage for javax.naming Context lookup

List of usage examples for javax.naming Context lookup

Introduction

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

Prototype

public Object lookup(String name) throws NamingException;

Source Link

Document

Retrieves the named object.

Usage

From source file:com.clican.pluto.common.util.JndiUtils.java

/**
 * Bind the resource manager instance to the JNDI directory.
 * <p>/*from   www . j a v a  2s. c o  m*/
 * This method will use the full JNDI path provided for the resource
 * manager, and will create if necessary the individual segments of that
 * path.
 * 
 * @param jndiName
 *            The full JNDI path at which the resource manager instance will
 *            be bound in the JNDI directory. JNDI clients can use that path
 *            to obtain the resource manager instance.
 * @param obj
 *            The object to be bound.
 * @return <b>true</b> if the resource manager was successfully bound to
 *         JNDI using the provided path; otherwise <b>false</b>.
 * 
 * @see #unbind(String)
 */
public static boolean bind(String jndiName, Object obj) {
    if (log.isDebugEnabled()) {
        log.debug("Binding object [" + obj + "] in JNDI at path [" + jndiName + "].");
    }
    Context ctx = null;

    try {
        // Create a binding that is local to this host only.
        Hashtable<String, String> ht = new Hashtable<String, String>();
        // If a special JNDI initial context factory was specified in the
        // constructor, then use it.
        if (jndiInitialContextFactory != null) {
            ht.put(Context.INITIAL_CONTEXT_FACTORY, jndiInitialContextFactory);
        }
        // Turn off binding replication .
        // ht.put(WLContext.REPLICATE_BINDINGS, "false");
        ctx = new InitialContext(ht);

        String[] arrJndiNames = jndiName.split("/");
        String subContext = "";

        for (int i = 0; i < arrJndiNames.length - 1; i++) {
            subContext = subContext + "/" + arrJndiNames[i];
            try {
                ctx.lookup(subContext);
            } catch (NameNotFoundException e) {
                ctx.createSubcontext(subContext);
            }

        }

        if (obj instanceof Serializable || jndiInitialContextFactory != null) {
            ctx.bind(jndiName, obj);
        } else {
            NonSerializableFactory.bind(jndiName, obj);
        }
    } catch (NamingException ex) {
        log.error("An error occured while binding [" + jndiName + "] to JNDI:", ex);
        return false;
    } finally {
        if (ctx != null) {
            try {
                ctx.close();
            } catch (NamingException ne) {
                log.error("Close context error:", ne);
            }
        }
    }
    return true;
}

From source file:org.jboss.dashboard.database.JNDIDataSourceEntry.java

public DataSource getDataSource() throws NamingException {
    if (dataSource != null)
        return dataSource;

    Context context = new InitialContext();
    return dataSource = (DataSource) context.lookup(getJndiPath());
}

From source file:fr.mosica.javawebtraining.WeatherResource.java

@GET
@Consumes("application/json")
@Produces("application/json")
public Weather getCurrentWeather(@QueryParam("zip") String zip) throws NamingException {
    Client client = ClientBuilder.newClient();
    // http://api.openweathermap.org/data/2.5/weather?zip=49600,fr&APPID=8c05dfed7d5d0d8ba3a2bc70b83b227f

    // rcupration JNDI -> classique
    Context ctx = new InitialContext();
    String host = (String) ctx.lookup("java:comp/env/openweathermap/weather");

    WebTarget target = client.target(host).path("data/2.5/weather").queryParam("zip", zip + ",fr")
            .queryParam("appid", "8c05dfed7d5d0d8ba3a2bc70b83b227f");

    return target.request(MediaType.APPLICATION_JSON_TYPE).get(Weather.class);

    //[TODEL] return new Weather("Beauprau");
}

From source file:br.ufac.sion.converter.NivelConverter.java

private NivelFacadeLocal lookupNivelFacadeLocal() {
    try {//from   w  ww .  jav a2s .c o m
        Context c = new InitialContext();
        return (NivelFacadeLocal) c.lookup("java:global/sion-ear/sion-ejb-2.0/NivelFacade");
    } catch (NamingException ne) {
        Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", ne);
        throw new RuntimeException(ne);
    }
}

From source file:br.ufac.sion.converter.VagaConverter.java

private VagaFacadeLocal lookupVagaFacadeLocal() {
    try {//w w w .  ja  v  a 2 s  .co  m
        Context c = new InitialContext();
        return (VagaFacadeLocal) c.lookup("java:global/sion-ear/sion-ejb-2.0/VagaFacade");
    } catch (NamingException ne) {
        Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", ne);
        throw new RuntimeException(ne);
    }
}

From source file:br.ufac.sion.converter.CargoConverter.java

private CargoFacadeLocal lookupVagaFacadeLocal() {
    try {/*  w  w  w  .j a  v a 2s .c  o  m*/
        Context c = new InitialContext();
        return (CargoFacadeLocal) c.lookup("java:global/sion-ear/sion-ejb-2.0/CargoFacade");
    } catch (NamingException ne) {
        Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", ne);
        throw new RuntimeException(ne);
    }
}

From source file:br.ufac.sion.converter.CidadeConverter.java

private CidadeFacadeLocal lookupCidadeFacadeLocal() {
    try {//w w w  .j a  va 2  s  .  co  m
        Context c = new InitialContext();
        return (CidadeFacadeLocal) c.lookup("java:global/sion-ear/sion-ejb-2.0/CidadeFacade");
    } catch (NamingException ne) {
        Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", ne);
        throw new RuntimeException(ne);
    }
}

From source file:br.ufac.sion.converter.EstadoConverter.java

private EstadoFacadeLocal lookupEstadoFacadeLocal() {
    try {// ww  w .jav  a  2s.  com
        Context c = new InitialContext();
        return (EstadoFacadeLocal) c.lookup("java:global/sion-ear/sion-ejb-2.0/EstadoFacade");
    } catch (NamingException ne) {
        Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", ne);
        throw new RuntimeException(ne);
    }
}

From source file:br.ufac.sion.converter.GpConverter.java

private GrupoFacadeLocal lookupGrupoFacadeLocal() {
    try {//from ww w.  jav a 2s  .  c  om
        Context c = new InitialContext();
        return (GrupoFacadeLocal) c.lookup("java:global/sion-ear/sion-ejb-2.0/GrupoFacade");
    } catch (NamingException ne) {
        Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", ne);
        throw new RuntimeException(ne);
    }
}

From source file:br.ufac.sion.converter.SetorConverter.java

private SetorFacadeLocal lookupSetorFacadeLocal() {
    try {/*from  www .  j  av a 2  s  . c o m*/
        Context c = new InitialContext();
        return (SetorFacadeLocal) c.lookup("java:global/sion-ear/sion-ejb-2.0/SetorFacade");
    } catch (NamingException ne) {
        Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", ne);
        throw new RuntimeException(ne);
    }
}