List of usage examples for javax.naming Context close
public void close() throws NamingException;
From source file:Unbind.java
public static void main(String[] args) { // Set up the environment for creating the initial context Hashtable<String, Object> env = new Hashtable<String, Object>(11); env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory"); env.put(Context.PROVIDER_URL, "ldap://localhost:389/o=JNDITutorial"); try {/*from w ww . j a v a 2 s.co m*/ // Create the initial context Context ctx = new InitialContext(env); // Remove the binding ctx.unbind("cn=Favorite Fruit"); // Check that it is gone Object obj = null; try { obj = ctx.lookup("cn=Favorite Fruit"); } catch (NameNotFoundException ne) { System.out.println("unbind successful"); return; } System.out.println("unbind failed; object still there: " + obj); // Close the context when we're done ctx.close(); } catch (NamingException e) { System.out.println("Operation failed: " + e); } }
From source file:Rename.java
public static void main(String[] args) { // Set up the environment for creating the initial context Hashtable<String, Object> env = new Hashtable<String, Object>(11); env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory"); env.put(Context.PROVIDER_URL, "ldap://localhost:389/ou=People,o=JNDITutorial"); try {//from w w w . ja v a 2 s . c om // Create the initial context Context ctx = new InitialContext(env); // Rename to Scott S ctx.rename("cn=Scott Seligman", "cn=Scott S"); // Check that it is there using new name Object obj = ctx.lookup("cn=Scott S"); System.out.println(obj); // Rename back to Scott Seligman ctx.rename("cn=Scott S", "cn=Scott Seligman"); // Check that it is there with original name obj = ctx.lookup("cn=Scott Seligman"); System.out.println(obj); // Close the context when we're done ctx.close(); } catch (NamingException e) { System.out.println("Rename failed: " + e); } }
From source file:JNDIUtil.java
/** * Lookup an object through the JNDI context. * * @param objectName The name of the object to be looked up. * @param jndiProperties JNDI properties. * @return The object.//from ww w .jav a2 s .c o m * @throws NamingException Error getting object. */ public static Object lookup(final String objectName, final Properties jndiProperties) throws NamingException { Object object = null; Context context; context = JNDIUtil.getNamingContext(jndiProperties); try { object = context.lookup(objectName); } finally { try { context.close(); } catch (NamingException ne) { System.out.println("Failed to close Naming Context." + ne); } } return object; }
From source file:com.codeasylum.stress.api.OuroborosImpl.java
public static Ouroboros getRemoteInterface(String rmiHost) throws NamingException { Ouroboros ouroboros;//from w w w . j a v a2 s. com 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:com.joseflavio.iperoxo.IpeRoxo.java
/** * Inicia a {@link DataSource} e a {@link EntityManagerFactory}. *//*from w w w . j av a2 s . c om*/ private static void executarFonteDeDados() throws IOException, NamingException { if (Boolean.parseBoolean(getPropriedade("DataSource.Enable"))) { log.info(getMensagem(null, "Log.Iniciando.DataSource")); } else { return; } System.setProperty(Context.INITIAL_CONTEXT_FACTORY, "org.apache.naming.java.javaURLContextFactory"); System.setProperty(Context.URL_PKG_PREFIXES, "org.apache.naming"); dataSource = new BasicDataSource(); dataSource.setDriverClassName(getPropriedade("DataSource.Driver")); dataSource.setUrl(getPropriedade("DataSource.URL")); dataSource.setUsername(getPropriedade("DataSource.Username")); dataSource.setPassword(getPropriedade("DataSource.Password")); dataSource.setInitialSize(Integer.parseInt(getPropriedade("DataSource.InitialSize"))); dataSource.setMaxTotal(Integer.parseInt(getPropriedade("DataSource.MaxTotal"))); dataSource.setMinIdle(Integer.parseInt(getPropriedade("DataSource.MinIdle"))); dataSource.setMaxIdle(Integer.parseInt(getPropriedade("DataSource.MaxIdle"))); dataSource.setTestOnCreate(Boolean.parseBoolean(getPropriedade("DataSource.TestOnCreate"))); dataSource.setTestWhileIdle(Boolean.parseBoolean(getPropriedade("DataSource.TestWhileIdle"))); dataSource.setTestOnBorrow(Boolean.parseBoolean(getPropriedade("DataSource.TestOnBorrow"))); dataSource.setTestOnReturn(Boolean.parseBoolean(getPropriedade("DataSource.TestOnReturn"))); Context contexto = new InitialContext(); try { contexto.bind("FONTE", dataSource); } finally { contexto.close(); } while (true) { try (Connection con = getConnection()) { break; } catch (Exception e) { try { Thread.sleep(2000); } catch (InterruptedException f) { } } } if (Boolean.parseBoolean(getPropriedade("DataSource.JPA.Enable"))) { log.info(getMensagem(null, "Log.Iniciando.JPA")); } else { return; } emf = Persistence.createEntityManagerFactory("JPA"); try { emf.createEntityManager().close(); } catch (Exception e) { log.error(e.getMessage(), e); } }
From source file:com.mg.jet.birt.report.data.oda.ejbql.HibernateUtil.java
private static synchronized void initSessionFactory(String hibfile, String mapdir, String jndiName) throws HibernateException { //ClassLoader cl1; if (sessionFactory == null) { if (jndiName == null || jndiName.trim().length() == 0) jndiName = CommonConstant.DEFAULT_JNDI_URL; Context initCtx = null; try {/*from ww w .ja v a 2s.co m*/ initCtx = new InitialContext(); sessionFactory = (SessionFactory) initCtx.lookup(jndiName); return; } catch (Exception e) { logger.log(Level.INFO, "Unable to get JNDI data source connection", e); } finally { if (initCtx != null) try { initCtx.close(); } catch (NamingException e) { //ignore } } Thread thread = Thread.currentThread(); try { //Class.forName("org.hibernate.Configuration"); //Configuration ffff = new Configuration(); //Class.forName("org.apache.commons.logging.LogFactory"); oldloader = thread.getContextClassLoader(); //Class thwy = oldloader.loadClass("org.hibernate.cfg.Configuration"); //Class thwy2 = oldloader.loadClass("org.apache.commons.logging.LogFactory"); //refreshURLs(); //ClassLoader changeLoader = new URLClassLoader( (URL [])URLList.toArray(new URL[0]),HibernateUtil.class.getClassLoader()); ClassLoader testLoader = new URLClassLoader((URL[]) URLList.toArray(new URL[0]), pluginLoader); //changeLoader = new URLClassLoader( (URL [])URLList.toArray(new URL[0])); thread.setContextClassLoader(testLoader); //Class thwy2 = changeLoader.loadClass("org.hibernate.cfg.Configuration"); //Class.forName("org.apache.commons.logging.LogFactory", true, changeLoader); //Class cls = Class.forName("org.hibernate.cfg.Configuration", true, changeLoader); //Configuration cfg=null; //cfg = new Configuration(); //Object oo = cls.newInstance(); //Configuration cfg = (Configuration)oo; Configuration cfg = new Configuration(); buildConfig(hibfile, mapdir, cfg); Class<? extends Driver> driverClass = testLoader .loadClass(cfg.getProperty("connection.driver_class")).asSubclass(Driver.class); Driver driver = driverClass.newInstance(); WrappedDriver wd = new WrappedDriver(driver, cfg.getProperty("connection.driver_class")); boolean foundDriver = false; Enumeration<Driver> drivers = DriverManager.getDrivers(); while (drivers.hasMoreElements()) { Driver nextDriver = (Driver) drivers.nextElement(); if (nextDriver.getClass() == wd.getClass()) { if (nextDriver.toString().equals(wd.toString())) { foundDriver = true; break; } } } if (!foundDriver) { DriverManager.registerDriver(wd); } sessionFactory = cfg.buildSessionFactory(); //configuration = cfg; HibernateMapDirectory = mapdir; HibernateConfigFile = hibfile; } catch (Throwable e) { e.printStackTrace(); throw new HibernateException("No Session Factory Created " + e.getLocalizedMessage(), e); } finally { thread.setContextClassLoader(oldloader); } } }
From source file:com.clican.pluto.common.util.JndiUtils.java
/** * Unbind the rsource manager instance from the JNDI directory. * <p>/* w w w.j a v a 2s .c o m*/ * After this method is called, it is no longer possible to obtain the * resource manager instance from the JNDI directory. * <p> * Note that this method will not remove the JNDI contexts corresponding to * the individual path segments of the full resource manager JNDI path. * * @param jndiName * The full JNDI path at which the resource manager instance was * previously bound. * @return <b>true</b> if the resource manager was successfully unbound * from JNDI; otherwise <b>false</b>. * * @see #bind(String, Object) */ public static boolean unbind(String jndiName) { if (log.isDebugEnabled()) { log.debug("Unbinding object at path [" + jndiName + "] from the JNDI repository."); } Context ctx = null; try { Hashtable<String, String> ht = new Hashtable<String, String>(); // If a special JNDI initial context factory was specified in the // constructor, then use it. if (jndiInitialContextFactory != null) { ht.put(Context.INITIAL_CONTEXT_FACTORY, jndiInitialContextFactory); } ctx = new InitialContext(ht); Object obj = lookupObject(jndiName); if (obj instanceof Serializable || jndiInitialContextFactory != null) { ctx.unbind(jndiName); } else { NonSerializableFactory.unbind(jndiName); } } catch (NamingException ex) { log.error("An error occured while unbinding [" + jndiName + "] from JNDI:", ex); return false; } finally { if (ctx != null) { try { ctx.close(); } catch (NamingException ne) { log.error("Close context error:", ne); } } } return true; }
From source file:EmailJndiServlet.java
public void init() throws ServletException { Context env = null; try {/*from ww w . j a v a 2 s.c om*/ env = (Context) new InitialContext(); mailSession = (Session) env.lookup("MyEmail"); if (mailSession == null) throw new ServletException("MyEmail is an unknown JNDI object"); //close the InitialContext env.close(); } catch (NamingException ne) { try { env.close(); } catch (NamingException nex) { } throw new ServletException(ne); } }
From source file:BeanServlet.java
public void init() throws ServletException { Context env = null; try {//from w ww .j ava 2s. co m // Compile error since there is no StockPriceBean.class // change the name according to your requirements env = (Context) new InitialContext().lookup("java:comp/env"); spbean = (StockPriceBean) env.lookup("bean/pricebean"); //close the InitialContext env.close(); if (spbean == null) throw new ServletException("bean/pricebean is an unknown JNDI object"); } catch (NamingException ne) { try { env.close(); } catch (NamingException nex) { } throw new ServletException(ne); } }
From source file:gov.medicaid.dao.impl.LDAPIdentityProviderDAOBean.java
/** * Closes the given context.//w ww .j ava2 s.c om * @param ctx the context to be closed */ private void closeContext(Context ctx) { if (ctx != null) { try { ctx.close(); } catch (NamingException e) { throw new PortalServiceRuntimeException("Unable to close resource.", e); } } }