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.fon.documentmanagementsystem.config.AppConfig.java

@Bean
public DataSource dataSource() throws NamingException {
    Context initCtx = new InitialContext();
    Context envCtx = (Context) initCtx.lookup("java:comp/env");
    DataSource ds = (DataSource) envCtx.lookup("jdbc/nst");
    return ds;/*from   w  w w . ja v  a2 s. co  m*/
}

From source file:org.gss_project.gss.server.BaseServlet.java

/**
 * A helper method that retrieves a reference to the ExternalAPI bean and
 * stores it for future use.//from www .  j  a  v  a  2 s . com
 *
 * @return an ExternalAPI instance
 * @throws RpcException in case an error occurs
 */
protected ExternalAPI getService() throws RpcException {
    try {
        final Context ctx = new InitialContext();
        final Object ref = ctx.lookup(getConfiguration().getString("externalApiPath"));
        return (ExternalAPI) PortableRemoteObject.narrow(ref, ExternalAPI.class);
    } catch (final NamingException e) {
        logger.error("Unable to retrieve the ExternalAPI EJB", e);
        throw new RpcException("An error occurred while contacting the naming service");
    }
}

From source file:wtw.ui.GetChartAction.java

private ProjectManager lookupProjectManagerBean() {
    try {/*  w ww . j  a  v  a2  s .c  o  m*/
        Context c = new InitialContext();
        return (ProjectManager) c.lookup("java:global/WorkToWorker/ProjectManager!wtw.biz.ProjectManager");
    } catch (NamingException ne) {
        Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", ne);
        throw new RuntimeException(ne);
    }
}

From source file:com.janrain.utils.BackplaneSystemProps.java

private void load(String paramName, boolean required) {
    String result = System.getProperty(paramName);
    if (StringUtils.isBlank(result)) {
        try {//w  w  w.ja  v  a2 s.  c  o m
            javax.naming.Context initCtx = new InitialContext();
            result = (String) initCtx.lookup("java:comp/env/" + paramName);
            System.setProperty(paramName, result);
            System.out.println(
                    "Parameter " + paramName + " fetched from context and inserted as system property");
        } catch (Exception e) {
            //continue
            if (required) {
                System.out.println("An error occurred trying to locate required parameter " + paramName + " => "
                        + e.getMessage());
            }
        }
    } else {
        System.out.println("Parameter " + paramName + " exists as a system property");
    }
}

From source file:com.alliander.osgp.webdevicesimulator.application.config.WebDeviceSimulatorInitializer.java

@Override
public void onStartup(final ServletContext servletContext) throws ServletException {
    try {/*  w  w  w . ja va2s.c o m*/
        // Force the timezone of application to UTC (required for
        // Hibernate/JDBC)
        TimeZone.setDefault(TimeZone.getTimeZone("UTC"));

        final Context initialContext = new InitialContext();

        final String logLocation = (String) initialContext
                .lookup("java:comp/env/osp/webDeviceSimulator/log-config");
        LogbackConfigurer.initLogging(logLocation);
        InternalLoggerFactory.setDefaultFactory(new Slf4JLoggerFactory());

        final AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
        rootContext.register(ApplicationContext.class);

        final ServletRegistration.Dynamic dispatcher = servletContext.addServlet(DISPATCHER_SERVLET_NAME,
                new DispatcherServlet(rootContext));
        dispatcher.setLoadOnStartup(1);
        dispatcher.addMapping(DISPATCHER_SERVLET_MAPPING);

        servletContext.addListener(new ContextLoaderListener(rootContext));
    } catch (final NamingException e) {
        throw new ServletException("naming exception", e);
    } catch (final FileNotFoundException e) {
        throw new ServletException("Logging file not found", e);
    } catch (final JoranException e) {
        throw new ServletException("Logback exception", e);
    }
}

From source file:com.flexive.core.Database.java

private static DataSource getDataSource(String dataSourceName, boolean useDefaultDataSource)
        throws NamingException, SQLException {
    if (dataSourcesByName.containsKey(dataSourceName)) {
        return dataSourcesByName.get(dataSourceName);
    }//  w  w w  . j  a va 2 s.  com
    final Context c = EJBLookup.getInitialContext();
    DataSource dataSource = null;
    for (String path : getPossibleJndiNames(dataSourceName)) {
        try {
            dataSource = (DataSource) c.lookup(path);
            break;
        } catch (NamingException e) {
            // pass
        }
    }
    if (dataSource == null) {
        // Geronimo
        String name = dataSourceName;
        if (name.startsWith("jdbc/")) {
            name = name.substring(5);
        }
        Object o = null;
        try {
            o = c.lookup("jca:/console.dbpool/" + name + "/JCAManagedConnectionFactory/" + name);
            dataSource = (DataSource) o.getClass().getMethod("$getResource").invoke(o);
        } catch (NamingException e) {
            // pass
        } catch (NoSuchMethodException e) {
            if (o instanceof DataSource) {
                return (DataSource) o;
            }
            String sErr = "Unable to retrieve Connection to [" + dataSourceName
                    + "]: JNDI resource is no DataSource and method $getResource not found!";
            LOG.error(sErr);
            throw new SQLException(sErr);
        } catch (Exception ex) {
            String sErr = "Unable to retrieve Connection to [" + dataSourceName + "]: " + ex.getMessage();
            LOG.error(sErr);
            throw new SQLException(sErr);
        }
    }
    if (dataSource == null && useDefaultDataSource && !dataSourceName.contains("/flexiveTest")) {
        dataSource = tryGetDefaultDataSource(c,
                GlobalConfigurationEngineBean.DEFAULT_DS
                        + (dataSourceName.endsWith(NO_TX_SUFFIX) ? NO_TX_SUFFIX : ""),
                new DefaultDivisionDataSourceInitializer());
    }
    if (dataSource == null) {
        // throw exception with the base datasource name, not the last looked-up path
        throw new NamingException("JNDI data source not found: " + dataSourceName);
    }
    dataSourcesByName.put(dataSourceName, dataSource);
    return dataSource;
}

From source file:de.highbyte_le.weberknecht.db.DefaultWebDbConnectionProvider2.java

public DefaultWebDbConnectionProvider2(String jdbcContextName) throws NamingException {
    //JNDI-Context
    Context ctx = new InitialContext();
    Context envCtx = (Context) ctx.lookup("java:comp/env");

    //JNDI-Lookup for jdbc connection
    this.datasource = (DataSource) envCtx.lookup(jdbcContextName);

    this.type = DbType.MYSQL; //FIXME implement some property handling for db type
}

From source file:minor.eft.ETFQuote.java

private ETFDetailsFacadeLocal lookupETFDetailsFacadeLocal() {
    try {/* ww  w . java  2s .  c om*/
        Context c = new InitialContext();
        return (ETFDetailsFacadeLocal) c
                .lookup("java:global/MinorTest/ETFDetailsFacade!minor.session.ETFDetailsFacadeLocal");
    } catch (NamingException ne) {
        Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", ne);
        throw new RuntimeException(ne);
    }
}

From source file:net.sf.jasperreports.data.jndi.JndiDataAdapterService.java

@Override
public void contributeParameters(Map<String, Object> parameters) throws JRException {
    JndiDataAdapter jndiDataAdapter = getJndiDataAdapter();
    if (jndiDataAdapter != null) {

        try {//from w w  w.  ja  va  2s. c o m
            Context ctx = new InitialContext();
            DataSource dataSource = (DataSource) ctx
                    .lookup("java:comp/env/" + jndiDataAdapter.getDataSourceName());
            connection = dataSource.getConnection();
        } catch (Exception ex) {
            throw new JRException(ex);
        }

        parameters.put(JRParameter.REPORT_CONNECTION, connection);
    }
}

From source file:nu.kelvin.jfileshare.servlets.FileDownloadServlet.java

@Override
public void init(ServletConfig config) throws ServletException {
    super.init(config);
    try {//from w  w w .java2 s.co m
        Context env = (Context) new InitialContext().lookup("java:comp/env");
        ds = (DataSource) env.lookup("jdbc/jfileshare");
    } catch (NamingException e) {
        throw new ServletException(e);
    }
}