Example usage for javax.naming InitialContext InitialContext

List of usage examples for javax.naming InitialContext InitialContext

Introduction

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

Prototype

public InitialContext() throws NamingException 

Source Link

Document

Constructs an initial context.

Usage

From source file:com.emc.plants.utils.Util.java

/**
 * Return the cached Initial Context.//from w  w  w .  j av  a 2s.  c  o m
 *
 * @return InitialContext, or null if a naming exception.
 */
static public InitialContext getInitialContext() {
    try {
        // Get InitialContext if it has not been gotten yet.
        if (initCtx == null) {
            // properties are in the system properties
            initCtx = new InitialContext();
        }
    }
    // Naming Exception will cause a null return.
    catch (NamingException e) {
    }
    return initCtx;
}

From source file:com.jaspersoft.jasperserver.api.engine.common.virtualdatasourcequery.teiid.impl.HiveTeiidConnectorImpl.java

public Object getConnectionFactory() throws Exception {
    if (translatorConfig == null) {
        translatorConfig = new TranslatorConfig();
        translatorConfig.setProductName("hive");
        translatorConfig.setTranslatorName("hive");
        translatorConfig.setTranslatorFactoryClass("org.teiid.translator.hive.HiveExecutionFactory");
    }//w  w  w  . j  a va  2  s.  c  o  m
    Context ctx = new InitialContext();
    DataSource ds = (DataSource) ctx.lookup("java:comp/env/" + jndiName);
    translatorConfig.setupTranslator();
    return ds;
}

From source file:com.francetelecom.clara.cloud.commons.toggles.PaasJndiStateRepository.java

/**
 * wrap new InitialContext for testing purpose
 * @return//from  ww w. j  ava  2 s.  c om
 * @throws NamingException
 */
protected InitialContext createInitialContext() throws NamingException {
    return new InitialContext();
}

From source file:com.glaf.core.jdbc.DBConnectionFactory.java

public static Connection getConnection(java.util.Properties props) {
    Connection connection = null;
    try {/*  w w  w.  j av  a 2 s  .c om*/
        if (props != null) {
            if (StringUtils.isNotEmpty(props.getProperty(DBConfiguration.JDBC_DATASOURCE))) {
                InitialContext ctx = new InitialContext();
                DataSource ds = (DataSource) ctx.lookup(props.getProperty(DBConfiguration.JDBC_DATASOURCE));
                connection = ds.getConnection();
            } else {
                String systemName = props.getProperty(DBConfiguration.JDBC_NAME);
                if (StringUtils.isNotEmpty(systemName)) {
                    ConnectionProvider provider = ConnectionProviderFactory.createProvider(systemName);
                    if (provider != null) {
                        connection = provider.getConnection();
                    }
                } else {
                    ConnectionProvider provider = ConnectionProviderFactory.createProvider(props);
                    if (provider != null) {
                        connection = provider.getConnection();
                    }
                }
            }
        }
        if (connection != null) {
            ConnectionThreadHolder.addConnection(connection);
        }
        return connection;
    } catch (Exception ex) {
        logger.error(ex);
        ex.printStackTrace();
        throw new RuntimeException(ex);
    }
}

From source file:de.zib.gndms.dspace.service.globus.resource.DSpaceResourceBase.java

public DSpaceResourceConfiguration getConfiguration() {
    if (this.configuration != null) {
        return this.configuration;
    }/* w  w w . java  2s  .c o  m*/
    MessageContext ctx = MessageContext.getCurrentContext();

    String servicePath = ctx.getTargetService();
    servicePath = servicePath.substring(0, servicePath.lastIndexOf("/"));
    servicePath += "/DSpace";

    String jndiName = Constants.JNDI_SERVICES_BASE_NAME + servicePath + "/configuration";
    logger.debug("Will read configuration from jndi name: " + jndiName);
    try {
        Context initialContext = new InitialContext();
        this.configuration = (DSpaceResourceConfiguration) initialContext.lookup(jndiName);
    } catch (Exception e) {
        logger.error("when performing JNDI lookup for " + jndiName + ": " + e, e);
    }

    return this.configuration;
}

From source file:io.apiman.servers.gateway_h2.Starter.java

/**
 * Ensure that the given name is bound to a context.
 * @param ctx/*ww  w.j  av  a  2s  .c  om*/
 * @param name
 * @throws NamingException
 */
private static void ensureCtx(InitialContext ctx, String name) throws NamingException {
    try {
        ctx.bind(name, new InitialContext());
    } catch (NameAlreadyBoundException e) {
        // this is ok
    }
}

From source file:de.zib.gndms.GORFX.service.globus.resource.GORFXResourceBase.java

public GORFXResourceConfiguration getConfiguration() {
    if (this.configuration != null) {
        return this.configuration;
    }//from  w w  w .j a  v a2  s  . com
    MessageContext ctx = MessageContext.getCurrentContext();

    String servicePath = ctx.getTargetService();
    servicePath = servicePath.substring(0, servicePath.lastIndexOf("/"));
    servicePath += "/GORFX";

    String jndiName = Constants.JNDI_SERVICES_BASE_NAME + servicePath + "/configuration";
    logger.debug("Will read configuration from jndi name: " + jndiName);
    try {
        Context initialContext = new InitialContext();
        this.configuration = (GORFXResourceConfiguration) initialContext.lookup(jndiName);
    } catch (Exception e) {
        logger.error("when performing JNDI lookup for " + jndiName + ": " + e, e);
    }

    return this.configuration;
}

From source file:info.bunji.jdbc.logger.JdbcLoggerFactory.java

/**
 ********************************************
 * get loggerName from jdbc url./*from   www  .  j a  v a  2 s  . co  m*/
 *
 * @param url connection url
 * @return loggerName(jdbc url or DataSourceName)
 ********************************************
 */
private static String getLoggerName(String url) {
    if (!dsNameMap.containsKey(url)) {
        dsNameMap.put(url, url);

        // datasource????
        InitialContext ctx = null;
        try {
            // JNDI??????
            ctx = new InitialContext();
            NamingEnumeration<NameClassPair> ne = ctx.list("java:comp/env/jdbc");
            while (ne.hasMoreElements()) {
                NameClassPair nc = ne.nextElement();
                DataSource ds = (DataSource) ctx.lookup("java:comp/env/jdbc/" + nc.getName());
                // ?DataSource????getUrl()
                Method method = ds.getClass().getMethod("getUrl");
                String dsUrl = (String) method.invoke(ds);
                if (dsUrl.startsWith(DriverEx.DRIVER_URL_PREFIX)) {
                    dsUrl = dsUrl.replace(DriverEx.DRIVER_URL_PREFIX, "jdbc:");
                    if (dsUrl.equals(url)) {
                        dsNameMap.put(url, nc.getName());
                        break;
                    }
                }
            }
        } catch (Throwable t) {
            printThrowable(t);
        } finally {
            try {
                if (ctx != null)
                    ctx.close();
            } catch (NamingException e) {
            }
        }
    }
    return dsNameMap.get(url);
}

From source file:com.dattack.naming.StandaloneJndiTest.java

@Test
public void testLookupInvalidObjectName() throws NamingException {
    final InitialContext context = new InitialContext();
    final String name = getCompositeName(VALID_CONTEXT, INVALID_OBJECT_NAME);
    final Object obj = context.lookup(name);
    assertNull(obj);//from   ww w.  jav  a  2s  .  c o  m
}

From source file:com.silverpeas.sharing.model.SimpleFileAccessControlTest.java

@Before
public void generalSetUp() throws Exception {
    InitialContext ic = new InitialContext();
    ic.rebind(JNDINames.ATTACHMENT_DATASOURCE, dataSource);
    IDatabaseConnection connection = new DatabaseConnection(dataSource.getConnection());
    DatabaseOperation.DELETE_ALL.execute(connection, dataSet);
    DatabaseOperation.CLEAN_INSERT.execute(connection, dataSet);
    DBUtil.getInstanceForTest(dataSource.getConnection());
    if (!registred) {
        Reader reader = null;/*from   w  ww . j av a 2  s  .  c  o  m*/
        try {
            reader = new InputStreamReader(SimpleFileAccessControlTest.class.getClassLoader()
                    .getResourceAsStream("silverpeas-jcr.txt"), CharEncoding.UTF_8);
            SilverpeasRegister.registerNodeTypes(reader);
        } finally {
            IOUtils.closeQuietly(reader);
        }
        registred = true;
        DBUtil.getInstanceForTest(dataSource.getConnection());
    }
    Session session = null;
    try {
        session = getRepository().login(new SilverpeasSystemCredentials());
        if (!session.getRootNode().hasNode(instanceId)) {
            session.getRootNode().addNode(instanceId, NT_FOLDER);
        }
        session.save();
    } finally {
        if (session != null) {
            session.logout();
        }
    }
}