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:org.jbpm.bpel.tutorial.purchase.ejb.ShippingCallbackMessageBean.java

protected PurchaseOrderService lookupPurchaseService() throws NamingException {
    Context initialContext = new InitialContext();
    try {//from ww  w.  ja  va2s. com
        return (PurchaseOrderService) initialContext.lookup("java:comp/env/service/PurchaseOrder");
    } finally {
        initialContext.close();
    }
}

From source file:com.jaspersoft.jasperserver.api.engine.jasperreports.service.impl.JndiJdbcDataSourceService.java

protected Connection createConnection() {

    try {/*from w  w  w  . ja v  a2  s  . com*/
        Context ctx = new InitialContext();
        DataSource ds = (DataSource) ctx.lookup("java:comp/env/" + jndiName);
        Connection c = ds.getConnection();
        if (log.isDebugEnabled()) {
            log.debug(
                    "CreateConnection successful at com.jaspersoft.jasperserver.api.engine.jasperreports.service.impl.JndiJdbcDataSourceService.createConnection");
        }
        return c;
    } catch (NamingException e) {
        try {
            //Added as short time solution due of http://bugzilla.jaspersoft.com/show_bug.cgi?id=26570.
            //The main problem - this code executes in separate tread (non http).
            //Jboss 7 support team recommend that you use the non-component environment namespace for such situations.
            Context ctx = new InitialContext();
            DataSource ds = (DataSource) ctx.lookup(jndiName);
            Connection c = ds.getConnection();
            if (log.isDebugEnabled()) {
                log.debug(
                        "CreateConnection successful at com.jaspersoft.jasperserver.api.engine.jasperreports.service.impl.JndiJdbcDataSourceService.createConnection");
            }
            return c;

        } catch (NamingException ex) {
            if (log.isDebugEnabled())
                log.debug(e, e);
            throw new JSExceptionWrapper(e);
        } catch (SQLException ex) {
            if (log.isDebugEnabled())
                log.debug(e, e);
            throw new JSExceptionWrapper(e);
        }

    } catch (SQLException e) {
        if (log.isDebugEnabled())
            log.debug(e, e);
        throw new JSExceptionWrapper(e);
    }
}

From source file:com.wso2telco.gsma.authenticators.DBUtils.java

/**
 * Initialize datasources.//from  ww  w .  j  av  a  2s.com
 *
 * @throws AuthenticatorException the authenticator exception
 */
private static void initializeDatasources() throws AuthenticatorException {
    if (mConnectDatasource != null) {
        return;
    }

    String dataSourceName = null;
    try {
        Context ctx = new InitialContext();
        ConfigurationService configurationService = new ConfigurationServiceImpl();
        dataSourceName = configurationService.getDataHolder().getMobileConnectConfig().getDataSourceName();
        mConnectDatasource = (DataSource) ctx.lookup(dataSourceName);
    } catch (NamingException e) {
        handleException("Error while looking up the data source: " + dataSourceName, e);
    }
}

From source file:com.jaspersoft.jasperserver.remote.connection.JndiConnectionStrategy.java

@Override
public JndiConnection createConnection(JndiConnection connectionDescription, Map<String, Object> data)
        throws IllegalParameterValueException {
    Connection conn = null;//from w w  w. jav  a  2 s . c om
    boolean passed = false;
    Throwable exception = null;
    try {
        Context ctx = new InitialContext();
        DataSource dataSource = (DataSource) ctx.lookup("java:comp/env/" + connectionDescription.getJndiName());
        conn = dataSource.getConnection();
        if (conn != null) {
            passed = true;
        }
    } catch (SQLException vex) {
        if (vex.getMessage().indexOf("[JI_CONNECTION_VALID]") >= 0)
            passed = true;
        exception = vex;
    } catch (Throwable e) {
        exception = e;
    } finally {
        if (conn != null)
            try {
                conn.close();
            } catch (SQLException e) {
                log.error("Couldn't disconnect JNDI connection", e);
            }
    }
    if (!passed) {
        throw new ConnectionFailedException(connectionDescription.getJndiName(), "jndiName",
                "Invalid JNDI name: " + connectionDescription.getJndiName(), exception);
    }
    return connectionDescription;
}

From source file:it.unipmn.di.dcs.sharegrid.web.model.StandardEnvironment.java

protected Context getOrCreateSubcontext(String contextName, Context context) throws NamingException {
    try {//from w ww . jav  a  2 s  . c  om
        return (Context) context.lookup(contextName);
    } catch (Exception exp) {
        return context.createSubcontext(contextName);
    }
}

From source file:eu.planets_project.tb.impl.system.TestbedStatelessAdminBean.java

/**
 * Hook up to a local instance of the Planets Data Manager.
 * //from  w  ww.ja v a  2 s .  c o m
 * NOTE Trying to get the remote DM and narrow it to the local one did not work.
 * 
 * TODO Switch to the DigitalObjectManager form.
 * 
 * @return A DataManagerLocal, as discovered via JNDI.
 */
public DataManagerLocal getPlanetsDataManagerAsAdmin() {
    try {
        Context jndiContext = new javax.naming.InitialContext();
        DataManagerLocal um = (DataManagerLocal) jndiContext.lookup("planets-project.eu/DataManager/local");
        return um;
    } catch (NamingException e) {
        log.error("Failure during lookup of the local DataManager: " + e.toString());
        return null;
    }
}

From source file:it.infn.ct.futuregateway.apiserver.APIContextListener.java

@Override
public final void contextDestroyed(final ServletContextEvent sce) {
    ExecutorService exServ;/*from  w  w w .  ja v  a  2s.c om*/
    try {
        Context ctx = new InitialContext();
        exServ = (ExecutorService) ctx.lookup("java:comp/env/threads/Submitter");
    } catch (NamingException ex) {
        exServ = (ExecutorService) sce.getServletContext().getAttribute("SubmissionThreadPool");
    }
    exServ.shutdown();
    try {
        if (!exServ.awaitTermination(Constants.MAXTHREADWAIT, TimeUnit.MINUTES)) {
            log.warn("Failed to shutdown the submission thread pool.");
        }
    } catch (InterruptedException ex) {
        log.error(ex);
    }
}

From source file:org.dspace.app.rest.utils.DSpaceKernelInitializer.java

/**
 * Find DSpace's "home" directory (from current environment)
 * Initially look for JNDI Resource called "java:/comp/env/dspace.dir".
 * If not found, use value provided in "dspace.dir" in Spring Environment
 *///w ww  .  jav a 2  s . c om
private String getDSpaceHome(ConfigurableEnvironment environment) {
    // Load the "dspace.dir" property from Spring Boot's Configuration (application.properties)
    // This gives us the location of our DSpace configurations, necessary to start the kernel
    String providedHome = environment.getProperty(DSpaceConfigurationService.DSPACE_HOME);

    String dspaceHome = null;
    try {
        // Allow ability to override home directory via JNDI
        Context ctx = new InitialContext();
        dspaceHome = (String) ctx.lookup("java:/comp/env/" + DSpaceConfigurationService.DSPACE_HOME);
    } catch (Exception e) {
        // do nothing
    }

    // Otherwise, verify the 'providedHome' value is non-empty, exists and includes DSpace configs
    if (dspaceHome == null) {
        if (StringUtils.isNotBlank(providedHome)
                && !providedHome.equals("${" + DSpaceConfigurationService.DSPACE_HOME + "}")) {
            File test = new File(providedHome);
            if (test.exists() && new File(test, DSpaceConfigurationService.DSPACE_CONFIG_PATH).exists()) {
                dspaceHome = providedHome;
            }
        }
    }
    return dspaceHome;
}

From source file:ar.com.zauber.commons.web.proxy.impl.dao.properties.provider.JndiPropertiesProvider.java

/**
 * Creates the JndiPropertiesProvider.//ww w.  j a va2s . c  o m
 *
 * @param jndiVariable
 * @param resourceLoader
 * @param fallback
 * @throws IOException .
 */
public JndiPropertiesProvider(final String jndiVariable, final ResourceLoader resourceLoader,
        final PropertiesProvider fallback) throws IOException {
    Validate.notEmpty(jndiVariable);
    Validate.notNull(resourceLoader);
    Validate.notNull(fallback);

    try {
        final InitialContext initCtx = new InitialContext();
        final Context envCtx = (Context) initCtx.lookup("java:comp/env");
        final Resource resource = resourceLoader.getResource((String) envCtx.lookup(jndiVariable));
        target = new ResourcePropertiesProvider(resource);
    } catch (final NamingException e) {
        target = fallback;
    }
}

From source file:it.unipmn.di.dcs.sharegrid.web.management.ManagementEnv.java

/** Returns the given (sub-)context (eventually, creating it). */
protected Context getOrCreateSubcontext(String contextName, Context context) throws NamingException {
    try {//from   w  w  w  .  ja v  a 2 s .  c o  m
        return (Context) context.lookup(contextName);
    } catch (Exception exp) {
        return context.createSubcontext(contextName);
    }
}