Example usage for javax.naming Context getEnvironment

List of usage examples for javax.naming Context getEnvironment

Introduction

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

Prototype

public Hashtable<?, ?> getEnvironment() throws NamingException;

Source Link

Document

Retrieves the environment in effect for this context.

Usage

From source file:Util.java

/**
 * Checks an object implements the given class
 * /* ww w .  j  av a 2s  .  c om*/
 * @param context
 *          the context
 * @param name
 *          the name to lookup
 * @param object
 *          the object
 * @param clazz
 *          the expected type
 * @throws Exception
 *           for any error
 */
protected static void checkObject(Context context, String name, Object object, Class clazz) throws Exception {
    Class objectClass = object.getClass();
    if (clazz.isAssignableFrom(objectClass) == false) {
        StringBuffer buffer = new StringBuffer(100);
        buffer.append("Object at '").append(name);
        buffer.append("' in context ").append(context.getEnvironment());
        buffer.append(" is not an instance of ");
        appendClassInfo(buffer, clazz);
        buffer.append(" object class is ");
        appendClassInfo(buffer, object.getClass());
        throw new ClassCastException(buffer.toString());
    }
}

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

public DefaultNameParser(final Context parent) throws NamingException {
    this.properties = new Properties();
    properties.putAll(parent.getEnvironment());
}

From source file:org.apache.axis2.transaction.TransactionConfiguration.java

private synchronized TransactionManager lookupTransactionManager() throws AxisFault {

    try {/*from  ww w. j  ava2s  .c o  m*/

        Context context = new InitialContext(jndiProperties);
        Object transactionManager = context.lookup(transactionManagerJNDIName);
        if (transactionManager != null && transactionManager instanceof TransactionManager) {
            return (TransactionManager) transactionManager;
        } else {
            log.error("TransactionManager : " + transactionManagerJNDIName + " not found when looking up"
                    + " using JNDI properties : " + context.getEnvironment());
            throw new AxisFault("TransactionManager : " + transactionManagerJNDIName
                    + " not found when looking up" + " using JNDI properties : " + context.getEnvironment());
        }

    } catch (NamingException e) {

        log.error(new StringBuilder().append("Error looking up TransactionManager ")
                .append(" using JNDI properties : ").append(jndiProperties));
        throw new AxisFault(
                "TransactionManager not found when looking up" + " using JNDI properties : " + jndiProperties);
    }
}

From source file:org.apache.qpid.disttest.db.ResultsDbWriter.java

/**
 * Uses the context's environment to load the JDBC driver class and return the
 * JDBC URL specified therein./*from ww  w.  ja  v  a  2s  .  com*/
 * @return the JDBC URL
 */
private String initialiseJdbc(Context context) {
    Hashtable<?, ?> environment = null;
    try {
        environment = context.getEnvironment();

        String driverName = (String) environment.get(DRIVER_NAME);
        if (driverName == null) {
            throw new IllegalArgumentException(
                    "JDBC driver name " + DRIVER_NAME + " missing from context environment: " + environment);
        }

        Class.forName(driverName);

        Object url = environment.get(URL);
        if (url == null) {
            throw new IllegalArgumentException(
                    "JDBC URL " + URL + " missing from context environment: " + environment);
        }
        return (String) url;
    } catch (NamingException e) {
        throw constructorRethrow(e, environment);
    } catch (ClassNotFoundException e) {
        throw constructorRethrow(e, environment);
    }
}

From source file:org.apache.synapse.commons.datasource.DataSourceFinder.java

/**
 * Find a DataSource using the given name and naming context
 *
 * @param dsName  Name of the DataSource to be found
 * @param context Naming Context/*from   w  ww.  j a va  2 s .c  o  m*/
 * @return DataSource if found , otherwise null
 */
public static DataSource find(String dsName, Context context) {

    try {
        Object dataSourceO = context.lookup(dsName);
        if (dataSourceO != null && dataSourceO instanceof DataSource) {
            return (DataSource) dataSourceO;
        } else {
            throw new SynapseCommonsException("DataSource : " + dsName + " not found "
                    + "when looking up using JNDI properties : " + context.getEnvironment(), log);
        }

    } catch (NamingException e) {
        throw new SynapseCommonsException(
                "Error looking up DataSource : " + dsName + " using JNDI" + " properties : " + context, e, log);
    }
}

From source file:org.apache.torque.dsfactory.JndiDataSourceFactory.java

/**
 *
 * @param ctx the context//from  ww w . jav  a 2  s  .  c  om
 * @throws NamingException
 */
private void debugCtx(Context ctx) throws NamingException {
    log.debug("InitialContext -------------------------------");
    Map<?, ?> env = ctx.getEnvironment();
    Iterator<?> qw = env.entrySet().iterator();
    log.debug("Environment properties:" + env.size());
    while (qw.hasNext()) {
        Map.Entry<?, ?> entry = (Map.Entry<?, ?>) qw.next();
        log.debug("    " + entry.getKey() + ": " + entry.getValue());
    }
    log.debug("----------------------------------------------");
}

From source file:org.grouter.common.jndi.JNDIUtils.java

/**
 * Print out all JNDI variables for provided Context.
 *
 * @param ctx     Context//from   w w  w  .  jav  a2  s .  c  o  m
 * @param alogger if null it will usethis class logger
 */
public static void printJNDI(Context ctx, Logger alogger) {
    Hashtable table;
    try {
        table = ctx.getEnvironment();
        Set set = table.keySet();
        Iterator it = set.iterator();
        alogger.info("Context contains key value pairs:");
        while (it.hasNext()) {
            String key = (String) it.next();
            alogger.info("(key,value) = (" + key + "," + (table.get(key)) + ")");
        }
    } catch (NamingException ex) {
        logger.error("Retrieving the environment in effect for this context failed.");
    }
}

From source file:org.grouter.common.jndi.JNDIUtils.java

/**
 * Print out all JNDI variables for provided Context.
 *
 * @param ctx     Context//from   w  ww . j  a v a2s. c  o m
 * @param alogger if null it will usethis class logger
 */
public static void printJNDI(Context ctx, final Log alogger) {
    Hashtable table;
    try {
        table = ctx.getEnvironment();
        Set set = table.keySet();
        Iterator it = set.iterator();
        alogger.info("Context contains key value pairs:");
        while (it.hasNext()) {
            String key = (String) it.next();
            alogger.info("(key,value) = (" + key + "," + (table.get(key)) + ")");
        }
    } catch (NamingException ex) {
        logger.error("Retrieving the environment in effect for this context failed.");
    }
}

From source file:org.wso2.appcloud.tierapi.util.DataSourceJDBC.java

public static Connection getConnection() {
    Connection conn = null;/*from ww w. j  a  va  2 s. c o  m*/
    try {
        Context initCtx = new InitialContext();
        SelectorContext selectorContext = new SelectorContext(
                (Hashtable<String, Object>) initCtx.getEnvironment(), false);
        Context envCtx = (Context) selectorContext.lookup("java:comp/env");

        DataSource ds = (DataSource) envCtx.lookup("jdbc/WSO2AppCloud1");

        conn = ds.getConnection();

    } catch (NamingException e) {
        String msg = "Error while connecting to Data Source ";
        log.error(msg, e);
    } catch (SQLException e) {
        String msg = "Error while getting connection to Data Base ";
        log.error(msg, e);
    }
    return conn;
}

From source file:org.wso2.carbon.bpmn.extensions.jms.JMSUtils.java

/**
 *
 * @param context/* w  w w  . ja v a  2 s  .c o  m*/
 * @param destinationName
 * @param destinationType
 * @return
 * @throws NamingException
 */
public Destination lookupDestination(Context context, String destinationName, String destinationType)
        throws NamingException {
    if (destinationName == null) {
        return null;
    }

    try {
        return lookup(context, Destination.class, destinationName);
    } catch (NameNotFoundException e) {
        try {
            Properties initialProperties = new Properties();
            if (context.getEnvironment() != null) {
                if (context.getEnvironment().get(JMSConstants.NAMING_FACTORY_INITIAL) != null) {
                    initialProperties.put(JMSConstants.NAMING_FACTORY_INITIAL,
                            context.getEnvironment().get(JMSConstants.NAMING_FACTORY_INITIAL));
                }
                if (context.getEnvironment().get(JMSConstants.JMS_PROVIDER_URL) != null) {
                    initialProperties.put(JMSConstants.JMS_PROVIDER_URL,
                            context.getEnvironment().get(JMSConstants.JMS_PROVIDER_URL));
                }
                if (context.getEnvironment().get(JMSConstants.CONNECTION_STRING) != null) {
                    initialProperties.put(JMSConstants.CONNECTION_STRING,
                            context.getEnvironment().get(JMSConstants.CONNECTION_STRING));
                }
            }

            if (JMSConstants.DESTINATION_TYPE_QUEUE.equalsIgnoreCase(destinationType)) {
                initialProperties.put(JMSConstants.QUEUE_PREFIX + destinationName, destinationName);
            } else if (JMSConstants.DESTINATION_TYPE_TOPIC.equalsIgnoreCase(destinationType)) {
                initialProperties.put(JMSConstants.TOPIC_PREFIX + destinationName, destinationName);
            }

            InitialContext initialContext = new InitialContext(initialProperties);
            return lookup(initialContext, Destination.class, destinationName);

        } catch (NamingException ex) {
            log.warn("Cannot locate destination: " + destinationName);
            throw ex;
        }
    }
}