/** * Creates the initial JNDI context to work with. * * @return context {@link InitialContext} * @throws ProcessingException */ public InitialContext createInitialContext() throws ProcessingException { Hashtable<String, String> props = new Hashtable<String, String>(); if (m_initialContextFactory != null) { props.put(Context.INITIAL_CONTEXT_FACTORY, m_initialContextFactory); } if (m_providerUrl != null) { props.put(Context.PROVIDER_URL, m_providerUrl); } if (m_userName != null && m_userName.length() > 0) { props.put(Context.SECURITY_PRINCIPAL, m_userName); } if (m_password != null) { props.put(Context.SECURITY_CREDENTIALS, m_password); } InitialContext ctx; try { if (props.size() > 0) { ctx = new InitialContext(props); } else { ctx = new InitialContext(); } } catch (NamingException e) { throw new ProcessingException(e.getMessage(), e.getCause()); } return ctx; }
/* (non-Javadoc) * @see jrds.Starter#stop() */ @Override public void stopConnection() { uptime = 1; if (dctx != null) try { dctx.close(); } catch (NamingException e) { log(Level.ERROR, e, "Error close to %s, cause: %s", getLevel(), e.getCause()); } dctx = null; }
/** * Setup a connection to the database The connection specifics are managed in the tomcat server * configuration and referenced through web.xml and context.xml */ public DatabaseBean() { try { InitialContext initContext = new InitialContext(); DataSource ds = (DataSource) initContext.lookup("java:/comp/env/jdbc/theMetaCity"); conn = ds.getConnection(); } catch (SQLException SQLEx) { logger.fatal("There was a problem with the database connection:"); logger.fatal(SQLEx); logger.fatal(SQLEx.getCause()); } catch (NamingException nameEx) { logger.fatal("There was a naming exception:"); logger.fatal(nameEx); logger.fatal(nameEx.getCause()); } }
/* (non-Javadoc) * @see jrds.Starter#start() */ @Override public boolean startConnection() { Hashtable<String, String> env = new Hashtable<String, String>(); env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory"); env.put(Context.PROVIDER_URL, "ldap://" + getHostName() + ":" + port); if (binddn != null && password != null) { env.put(Context.SECURITY_AUTHENTICATION, "simple"); env.put(Context.SECURITY_PRINCIPAL, binddn); env.put(Context.SECURITY_CREDENTIALS, password); } env.put("com.sun.jndi.ldap.connect.timeout", "" + getTimeout() * 1000); try { dctx = new InitialDirContext(env); } catch (NamingException e) { log(Level.ERROR, e, "Cannot connect to %s, cause: ", getLevel(), e.getCause()); return false; } log(Level.DEBUG, "Binding to: %s with dn: %s", env.get(Context.PROVIDER_URL), binddn); return dctx != null; }