Example usage for javax.naming.spi NamingManager setInitialContextFactoryBuilder

List of usage examples for javax.naming.spi NamingManager setInitialContextFactoryBuilder

Introduction

In this page you can find the example usage for javax.naming.spi NamingManager setInitialContextFactoryBuilder.

Prototype

public static synchronized void setInitialContextFactoryBuilder(InitialContextFactoryBuilder builder)
        throws NamingException 

Source Link

Document

Sets the InitialContextFactory builder to be builder.

Usage

From source file:com.enioka.jqm.tools.JndiContext.java

/**
 * Will create a JNDI Context and register it as the initial context factory builder
 * /*from w w  w. ja v  a  2 s.c o m*/
 * @return the context
 * @throws NamingException
 *             on any issue during initial context factory builder registration
 */
static JndiContext createJndiContext() throws NamingException {
    try {
        if (!NamingManager.hasInitialContextFactoryBuilder()) {
            JndiContext ctx = new JndiContext();
            NamingManager.setInitialContextFactoryBuilder(ctx);
            return ctx;
        } else {
            return (JndiContext) NamingManager.getInitialContext(null);
        }
    } catch (Exception e) {
        jqmlogger.error("Could not create JNDI context: " + e.getMessage());
        NamingException ex = new NamingException("Could not initialize JNDI Context");
        ex.setRootCause(e);
        throw ex;
    }
}

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

/** Initialized the JNDI. */
protected void initJNDI() throws ManagementException {
    try {/*from   w ww  .  j av a 2s .co m*/
        this.ctxFactoryBuild = new ManagementContextFactoryBuilder();
        NamingManager.setInitialContextFactoryBuilder(this.ctxFactoryBuild);
        NamingManager.setObjectFactoryBuilder(this.ctxFactoryBuild);

        // Initial environment with various properties
        Hashtable<String, String> env = new Hashtable<String, String>();
        env.put(Context.INITIAL_CONTEXT_FACTORY, ManagementContextFactory.class.getName());
        //env.put(Context.PROVIDER_URL, "");
        //env.put(Context.OBJECT_FACTORIES, "foo.bar.ObjFactory");
        env.put(Context.URL_PKG_PREFIXES, ManagementContextFactory.class.getPackage().getName());

        this.initCtx = new InitialContext(env);
        Context javaCompCtx = (Context) this.getOrCreateSubcontext("java:comp", initCtx);
        if (javaCompCtx == null) {
            throw new ManagementException("JNDI problem. Cannot get java:comp context from InitialContext.");
        }
        Context envCtx = (Context) this.getOrCreateSubcontext("env", javaCompCtx);
        if (envCtx == null) {
            throw new ManagementException("JNDI problem. Cannot get env context from java:comp context.");
        }
        Context jdbcCtx = (Context) this.getOrCreateSubcontext("jdbc", envCtx);
        if (jdbcCtx == null) {
            throw new ManagementException("JNDI problem. Cannot get jdbc context from java:comp/env context.");
        }

        // Create the DataSource

        //Properties properties = new Properties();
        //properties.put( "driverClassName", "com.mysql.jdbc.Driver" );
        //properties.put( "url", "jdbc:mysql://localhost:3306/DB" );
        //properties.put( "username", "username" );
        //properties.put( "password", "********" );
        //
        //DataSource dataSource = BasicDataSourceFactory.createDataSource( properties );
        //initContext.bind( "java:comp/env/jdbc/db", dataSource );

        //Reference ref = new Reference( "javax.sql.DataSource", "org.apache.commons.dbcp.BasicDataSourceFactory", null ); 
        //ref.add(new StringRefAddr("driverClassName", "com.mysql.jdbc.Driver"));
        //ref.add(new StringRefAddr("url", "jdbc:mysql://localhost:3306/sharegrid"));
        //ref.add(new StringRefAddr("username", "root"));
        //ref.add(new StringRefAddr("password", ""));
        //initCtx.rebind( "java:comp/env/jdbc/mysql", ref );
        java.util.Properties properties = new java.util.Properties();
        properties.put("driverClassName", "com.mysql.jdbc.Driver");
        properties.put("url", "jdbc:mysql://localhost:3306/sharegrid");
        properties.put("username", "root");
        properties.put("password", "");
        javax.sql.DataSource dataSource = org.apache.commons.dbcp.BasicDataSourceFactory
                .createDataSource(properties);
        initCtx.rebind("java:comp/env/jdbc/mysql", dataSource);

    } catch (Exception ex) {
        throw new ManagementException("JNDI problem.", ex);
    }
}

From source file:io.pivotal.poc.gemfire.gtx.jndi.SimpleNamingContextBuilder.java

/**
 * Register the context builder by registering it with the JNDI NamingManager.
 * Note that once this has been done, {@code new InitialContext()} will always
 * return a context from this factory. Use the {@code emptyActivatedContextBuilder()}
 * static method to get an empty context (for example, in test methods).
 * @throws IllegalStateException if there's already a naming context builder
 * registered with the JNDI NamingManager
 *//*from  w ww  .ja  v  a  2 s.  c om*/
public void activate() throws IllegalStateException, NamingException {
    logger.info("Activating simple JNDI environment");
    synchronized (initializationLock) {
        if (!initialized) {
            if (NamingManager.hasInitialContextFactoryBuilder()) {
                throw new IllegalStateException(
                        "Cannot activate SimpleNamingContextBuilder: there is already a JNDI provider registered. "
                                + "Note that JNDI is a JVM-wide service, shared at the JVM system class loader level, "
                                + "with no reset option. As a consequence, a JNDI provider must only be registered once per JVM.");
            }
            NamingManager.setInitialContextFactoryBuilder(this);
            initialized = true;
        }
    }
    activated = this;
}

From source file:com.interface21.jndi.support.SimpleNamingContextBuilder.java

/**
 * Register the context builder by registering it with the JNDI NamingManager.
 * Note that once this has been done, new InitialContext() will always return
 * a context from this factory. Use the emptyActivatedContextBuilder() static
 * method to get an empty context (for example, in test methods).
 * @throws IllegalStateException if there's already a naming context builder
 * registered with the JNDI NamingManager
 *///ww w .ja v a  2  s  .c o m
public void activate() throws IllegalStateException, NamingException {
    logger.info("Activating simple JNDI environment");
    NamingManager.setInitialContextFactoryBuilder(this);
    activated = this;
}

From source file:org.apache.cayenne.unit.JNDISetup.java

public static void doSetup() {
    if (!setup) {

        synchronized (JNDISetup.class) {

            if (!setup) {
                try {
                    NamingManager.setInitialContextFactoryBuilder(new SimpleNamingContextBuilder());
                } catch (NamingException e) {
                    logger.error("Can't perform JNDI setup, ignoring...", e);
                }//from w w w . j  av a 2 s.c om

                setup = true;
            }
        }
    }
}

From source file:org.apache.marmotta.platform.core.test.base.AbstractMarmotta.java

protected AbstractMarmotta() {

    // initialise JNDI environment
    try {/*ww w. j  av  a2 s .co m*/
        NamingManager.setInitialContextFactoryBuilder(new MarmottaInitialContextFactoryBuilder());
    } catch (NamingException e) {

    } catch (IllegalStateException e) {
    }

    // initialise CDI environment
    weld = new Weld();
    container = weld.initialize();

    cleanJNDI();

    // put bean manager into JNDI
    try {
        new InitialContext().bind("java:comp/BeanManager", container.getBeanManager());
    } catch (NamingException e) {
        log.error("error adding bean manager to JNDI", e);
    }

    // create temporary marmotta home directory
    home = Files.createTempDir();

    // create a temporary configuration with an in-memory database URL for H2
    override = new MapConfiguration(new HashMap<String, Object>());
    override.setProperty("database.h2.url", "jdbc:h2:mem;MVCC=true;DB_CLOSE_ON_EXIT=FALSE;DB_CLOSE_DELAY=10");
    override.setProperty("logging.template", "/logback-testing.xml");
    override.setProperty("testing.enabled", true);

    // initialise marmotta using a temporary directory
    startupService = getService(MarmottaStartupService.class);
}

From source file:org.pentaho.reporting.engine.classic.core.testsupport.gold.GoldTestBase.java

@Before
public void setUp() throws Exception {
    Locale.setDefault(Locale.US);
    TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
    // enforce binary compatibility for the xml-files so that comparing them can be faster.

    ClassicEngineBoot.getInstance().start();
    if (NamingManager.hasInitialContextFactoryBuilder() == false) {
        NamingManager.setInitialContextFactoryBuilder(new DebugJndiContextFactoryBuilder());
    }//ww w.j  a  v  a2  s. co  m

    localFontRegistry = new LocalFontRegistry();
    localFontRegistry.initialize();
}

From source file:org.rhq.enterprise.server.naming.NamingHack.java

public static void bruteForceInitialContextFactoryBuilder() {
    LOG.info("Modifying the naming subsystem to enable secure execution of scripts inside the server...");

    //it is important to synchronize on the NamingManager class because the get/setInitialContextFactoryBuilder
    //methods in that class are static synchronized. By holding the lock on the NamingManager class we prevent
    //any other thread to check for the initial context factory builder (and therefore de-facto do any JNDI lookup).
    synchronized (NamingManager.class) {

        if (NamingManager.hasInitialContextFactoryBuilder()) {
            //if anyone ever changes the implementation of the NamingManager, we will break here...
            //but as of now, we have no other option but to do this and wait for 
            //https://issues.jboss.org/browse/AS7-6109 to be resolved and incorporated into our underlying container

            try {
                Field f = NamingManager.class.getDeclaredField("initctx_factory_builder");
                f.setAccessible(true);//from  w  w  w.j av  a  2 s  . c o  m

                if (originalBuilder == null) {
                    originalBuilder = (InitialContextFactoryBuilder) f.get(null);
                }

                f.set(null, null);
            } catch (Exception e) {
                LOG.error(
                        "Failed to install a custom initial context factory builder. RHQ installation is unsecure!",
                        e);

                return;
            }
        }

        try {
            InitialContextFactory defaultFactory = null;
            if (originalBuilder != null) {
                defaultFactory = originalBuilder.createInitialContextFactory(new Hashtable<String, String>());
            }

            NamingManager.setInitialContextFactoryBuilder(
                    new AccessCheckingInitialContextFactoryBuilder(defaultFactory, originalBuilder == null));
        } catch (Exception e) {
            LOG.error(
                    "Failed to install a custom initial context factory builder. RHQ installation is unsecure!",
                    e);

            if (originalBuilder != null) {
                if (LOG.isDebugEnabled()) {
                    LOG.debug("Trying to restore the original initial context factory builder: "
                            + originalBuilder);
                }

                try {
                    NamingManager.setInitialContextFactoryBuilder(originalBuilder);
                } catch (Exception e2) {
                    LOG.error(
                            "Failed to restore the original initial context factory builder. The JNDI lookup may"
                                    + " not function properly from this point on...",
                            e2);
                }
            }
        }
    }
}

From source file:org.rhq.jndi.mbean.AccessCheckingInitialContextFactoryBuilderInstaller.java

public void start() throws Exception {
    LOG.info("Installing RHQ's access permission checking initial context factory builder");

    NamingManager.setInitialContextFactoryBuilder(new AccessCheckingInitialContextFactoryBuilder());
}

From source file:org.springframework.mock.jndi.SimpleNamingContextBuilder.java

/**
 * Register the context builder by registering it with the JNDI NamingManager.
 * Note that once this has been done, {@code new InitialContext()} will always
 * return a context from this factory. Use the {@code emptyActivatedContextBuilder()}
 * static method to get an empty context (for example, in test methods).
 * @throws IllegalStateException if there's already a naming context builder
 * registered with the JNDI NamingManager
 *///from  w  w w  .  j  a  va  2s .  c  o  m
public void activate() throws IllegalStateException, NamingException {
    logger.info("Activating simple JNDI environment");
    synchronized (initializationLock) {
        if (!initialized) {
            Assert.state(!NamingManager.hasInitialContextFactoryBuilder(),
                    "Cannot activate SimpleNamingContextBuilder: there is already a JNDI provider registered. "
                            + "Note that JNDI is a JVM-wide service, shared at the JVM system class loader level, "
                            + "with no reset option. As a consequence, a JNDI provider must only be registered once per JVM.");
            NamingManager.setInitialContextFactoryBuilder(this);
            initialized = true;
        }
    }
    activated = this;
}