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:demo.learn.shiro.util.SqlUtil.java

/**
 * Gets the JNDI {@link DataSource}. Look at
 * %webroot%/META-INF/context.xml./*from www.java  2  s .  c  om*/
 * @return {@link DataSource}.
 */
public static DataSource getJndiDataSource() {
    if (null == _jndiDataSource) {
        try {
            Context context = new InitialContext();
            _jndiDataSource = (DataSource) context.lookup(C.JNDI_DATASOURCE_NAME);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
    return _jndiDataSource;
}

From source file:com.codeasylum.stress.api.OuroborosImpl.java

public static Ouroboros getRemoteInterface(String rmiHost) throws NamingException {

    Ouroboros ouroboros;/*from  w w w . j  av a2 s  .c o  m*/
    InitialContext initContext;
    Context rmiContext;

    initContext = new InitialContext();
    rmiContext = (Context) initContext.lookup("rmi://" + rmiHost + ':' + registryPort.get());
    ouroboros = (Ouroboros) PortableRemoteObject.narrow(rmiContext.lookup(Ouroboros.class.getName()),
            Ouroboros.class);
    rmiContext.close();
    initContext.close();

    return ouroboros;
}

From source file:JNDIUtil.java

/**
 * Create a context path recursively.//from  w w  w .  j  av  a 2s. com
 */
public static Context createContext(Context c, String path) throws NamingException {
    Context crtContext = c;
    for (StringTokenizer st = new StringTokenizer(path, "/"); st.hasMoreTokens();) {
        String tok = st.nextToken();

        try {
            Object o = crtContext.lookup(tok);
            if (!(o instanceof Context)) {
                throw new NamingException("Path " + path + " overwrites and already bound object");
            }
            crtContext = (Context) o;
            continue;
        } catch (NameNotFoundException e) {
            // OK
        }
        crtContext = crtContext.createSubcontext(tok);
    }
    return crtContext;
}

From source file:com.wso2telco.mnc.resolver.mncrange.McnRangeDbUtil.java

/**
 * Initialize datasources./*from  w  w w  .  j av  a2s  .  c  o  m*/
 *
 * @throws MobileNtException the mobile nt exception
 */
public static void initializeDatasources() throws MobileNtException {
    if (axiataDatasource != null) {
        return;
    }

    try {
        Context ctx = new InitialContext();
        axiataDatasource = (DataSource) ctx.lookup(AXIATA_DATA_SOURCE);
    } catch (NamingException e) {
        handleException("Error while looking up the data source: " + AXIATA_DATA_SOURCE, e);
    }
}

From source file:ar.com.zauber.commons.spring.configurers.JndiInitialContextHelper.java

/** dado paths jndi retorna archivos de propiedades */
public static Resource[] getJndiLocations(final String[] filePathJndiNames) {

    final ResourceLoader resourceLoader = new DefaultResourceLoader();

    try {/*from w  ww.j ava2s . c o  m*/
        final InitialContext initCtx = new InitialContext();
        final Resource[] locations = new Resource[filePathJndiNames.length];
        boolean found = false;

        try {
            final Context envCtx = (Context) initCtx.lookup("java:comp/env");
            for (int i = 0; i < filePathJndiNames.length; i++) {
                locations[i] = resourceLoader.getResource((String) envCtx.lookup(filePathJndiNames[i]));
            }
            found = true;
        } catch (final NamingException e) {
            LOGGER.warn("Error JNDI looking up 'java:comp/env':" + e.getExplanation());
            // void
        }

        if (!found) {
            // Para Jetty 7 Server
            try {
                for (int i = 0; i < filePathJndiNames.length; i++) {
                    locations[i] = resourceLoader.getResource((String) initCtx.lookup(filePathJndiNames[i]));
                }
            } catch (final NamingException e) {
                LOGGER.warn("Hubo un error en el lookup de JNDI. Se usaran " + "properties del classpath: "
                        + e.getExplanation());
                return null;
            }
        }

        return locations;
    } catch (final NamingException e) {
        LOGGER.warn("Hubo un error en el lookup de JNDI. Se usaran " + "properties del classpath: "
                + e.getExplanation());
        return null;
    }
}

From source file:eu.planets_project.tb.impl.persistency.ServiceRecordPersistencyImpl.java

/**
 * A Factory method to build a reference to this interface.
 * @return/*from  w w w . j av  a2 s  .c  o m*/
 */
public static ServiceRecordPersistencyRemote getInstance() {
    Log log = LogFactory.getLog(ServiceRecordPersistencyImpl.class);
    try {
        Context jndiContext = new javax.naming.InitialContext();
        ServiceRecordPersistencyRemote dao_r = (ServiceRecordPersistencyRemote) PortableRemoteObject.narrow(
                jndiContext.lookup("testbed/ServiceRecordPersistencyImpl/remote"),
                ServiceRecordPersistencyRemote.class);
        return dao_r;
    } catch (NamingException e) {
        log.error("Failure in getting PortableRemoteObject: " + e.toString());
        return null;
    }
}

From source file:org.jboss.as.quickstarts.secured.ejb.remote.client.RemoteEJBClient.java

/**
 * Looks up and returns the proxy to remote stateful counter bean
 * //from www .  ja  v a2  s .  c  o m
 * @return
 * @throws NamingException
 */
private static RemoteCounter lookupRemoteStatefulCounter() throws NamingException {
    final Context context = getInitialContext();

    return (RemoteCounter) context.lookup(STR_EJB_PREFFIX + lookupString + STR_SLASH_SEPARATOR + ctrBeanString
            + STR_EXCLAM_SEPARATOR + RemoteCounter.class.getName() + STR_STATEFUL_SUFFIX);
}

From source file:org.jboss.as.quickstarts.secured.ejb.remote.client.RemoteEJBClient.java

/**
 * Looks up and returns the proxy to remote stateless calculator bean
 * /*from  w  ww  .  j  ava  2s.  c  om*/
 * @return
 * @throws NamingException
 */
private static RemoteCalculator lookupRemoteStatelessCalculator() throws NamingException {
    final Context context = getInitialContext();
    return (RemoteCalculator) context.lookup(STR_EJB_PREFFIX + lookupString + STR_SLASH_SEPARATOR
            + calcBeanString + STR_EXCLAM_SEPARATOR + RemoteCalculator.class.getName());
}

From source file:eu.eubrazilcc.lvl.core.conf.ConfigurationFinder.java

/**
 * Finds available configuration files, searching the different sources in the following order:
 * <ol>/*from  w  ww . j a  v a  2  s .  c  o m*/
 *   <li>Application environment;</li>
 *   <li>JNDI context;</li>
 *   <li>Default location in the file-system locally available to the application; and finally</li>
 *   <li>If none of the above works, configuration files are returned from the resources available 
 *       in the application class path.</li>
 * </ol>
 * @return configuration files available to the application
 */
public final static ImmutableList<URL> findConfigurationFiles() {
    ImmutableList<File> configFiles = null;
    // try to read environment variable and retrieve configuration files from the file system
    try {
        final String location = getenv(ENV_HOME_VAR) + "/etc";
        if (isNotBlank(location) && testConfigurationFiles(location, CONFIGURATION_FILENAMES)) {
            configFiles = convertPathListToFiles(location, CONFIGURATION_FILENAMES);
            LOGGER.trace("Environment variable '" + ENV_HOME_VAR + "' found. Configuration files: "
                    + filesToString(configFiles));
        } else {
            LOGGER.trace("Environment variable '" + ENV_HOME_VAR + "' was not found");
        }
    } catch (Exception ignore) {
    }
    // try to read from JNDI context
    if (configFiles == null) {
        try {
            final Context initCtx = new InitialContext();
            final Context envCtx = (Context) initCtx.lookup("java:comp/env");
            final String location = (String) envCtx.lookup(ENV_HOME_VAR) + "/etc";
            if (isNotBlank(location) && testConfigurationFiles(location, CONFIGURATION_FILENAMES)) {
                configFiles = convertPathListToFiles(location, CONFIGURATION_FILENAMES);
                LOGGER.trace("JNDI context variable '" + DEFAULT_LOCATION + "' found. Configuration files: "
                        + filesToString(configFiles));
            } else {
                LOGGER.trace("JNDI context variable '" + DEFAULT_LOCATION + "' was not found");
            }
        } catch (Exception e) {
            LOGGER.trace(
                    "Failed to search for configuration files in JNDI context variable: '" + ENV_HOME_VAR + "'",
                    e);
        }
    }
    // try to retrieve configuration files from the default location
    if (configFiles == null) {
        try {
            final String location = DEFAULT_LOCATION + "/etc";
            if (isNotBlank(location) && testConfigurationFiles(location, CONFIGURATION_FILENAMES)) {
                configFiles = convertPathListToFiles(location, CONFIGURATION_FILENAMES);
                LOGGER.trace("Default location '" + DEFAULT_LOCATION + "' found. Configuration files: "
                        + filesToString(configFiles));
            } else {
                LOGGER.trace("Default location '" + DEFAULT_LOCATION + "' was not found");
            }
        } catch (Exception e) {
            LOGGER.warn("Failed to search for configuration files in default location: " + DEFAULT_LOCATION, e);
        }
    }
    // return default configuration files
    if (configFiles == null) {
        return getDefaultConfiguration();
    } else {
        return from(configFiles).transform(new Function<File, URL>() {
            @Override
            public URL apply(final File file) {
                URL url = null;
                if (file != null) {
                    try {
                        url = file.toURI().toURL();
                    } catch (MalformedURLException e) {
                        LOGGER.warn("Ignoring file: " + file.getName(), e);
                    }
                }
                return url;
            }
        }).filter(notNull()).toList();
    }
}

From source file:com.xpn.xwiki.internal.XWikiCfgConfigurationSource.java

/**
 * @return the location where to find the configuration
 *//*w  ww .  ja  v a2  s  .co m*/
public static String getConfigPath() {
    String configurationLocation;

    try {
        Context envContext = (Context) new InitialContext().lookup("java:comp/env");
        configurationLocation = (String) envContext.lookup(CFG_ENV_NAME);
    } catch (Exception e) {
        configurationLocation = "/etc/xwiki/xwiki.cfg";
        if (!new File(configurationLocation).exists()) {
            configurationLocation = "/WEB-INF/xwiki.cfg";
        }
    }

    return configurationLocation;
}