List of usage examples for javax.naming.spi NamingManager setObjectFactoryBuilder
public static synchronized void setObjectFactoryBuilder(ObjectFactoryBuilder builder) throws NamingException
From source file:it.unipmn.di.dcs.sharegrid.web.management.ManagementEnv.java
/** Initialized the JNDI. */ protected void initJNDI() throws ManagementException { try {//from w w w. j ava2s . c om 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); } }