protected DirContext open() throws NamingException { if (context != null) { return context; } try { Hashtable<String, String> env = new Hashtable<String, String>(); env.put(Context.INITIAL_CONTEXT_FACTORY, initialContextFactory); if (connectionUsername != null && !"".equals(connectionUsername)) { env.put(Context.SECURITY_PRINCIPAL, connectionUsername); } else { throw new NamingException("Empty username is not allowed"); } if (connectionPassword != null && !"".equals(connectionPassword)) { env.put(Context.SECURITY_CREDENTIALS, connectionPassword); } else { throw new NamingException("Empty password is not allowed"); } env.put(Context.SECURITY_PROTOCOL, connectionProtocol); env.put(Context.PROVIDER_URL, connectionURL); env.put(Context.SECURITY_AUTHENTICATION, authentication); context = new InitialDirContext(env); } catch (NamingException e) { LOG.error(e.toString()); throw e; } return context; }
public boolean authenticateUser(String ldapUrl, String username, String password, String domains) { String[] domainArr = domains.split(","); for (String domain : domainArr) { Hashtable env = new Hashtable(); env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory"); env.put(Context.PROVIDER_URL, ldapUrl); env.put(Context.SECURITY_AUTHENTICATION, "simple"); env.put(Context.SECURITY_PRINCIPAL, domain + "\\" + username); env.put(Context.SECURITY_CREDENTIALS, password); try { ctx = new InitialDirContext(env); return true; } catch (NamingException e) { } finally { if (ctx != null) { try { ctx.close(); } catch (NamingException ex) { logger.warn(ex.getMessage()); } } } } return false; } }
private static ObjectFactory getURLObjectFactory(String scheme, String url, Hashtable environment) throws NamingException { String facProp = (String) environment.get(Context.URL_PKG_PREFIXES); if (facProp != null) { facProp += ":" + "com.sun.jndi.url"; NamingException ne = new NamingException(className); ne.setRootCause(e); throw ne; } catch (Exception e) {
/** * Return a general naming exception with a root cause. * * @param message the message * @param cause the exception cause, or {@code null} for none * @return the exception */ public static NamingException namingException(final String message, final Throwable cause) { final NamingException exception = new NamingException(message); if (cause != null) exception.initCause(cause); return exception; }
/** * Create a naming exception with a root cause. * * @param message the message * @param cause the cause, or {@code null} for none * @return the naming exception */ public static NamingException namingException(final String message, final Throwable cause) { final NamingException namingException = new NamingException(message); if (cause != null) { namingException.setRootCause(cause); } return namingException; } }
@Override public final NamingException obtainingDirContextCredentialFromAuthenticationContextFailed(final Throwable cause) { final NamingException result = new NamingException(String.format(getLoggingLocale(), obtainingDirContextCredentialFromAuthenticationContextFailed$str())); result.initCause(cause); final StackTraceElement[] st = result.getStackTrace(); result.setStackTrace(Arrays.copyOfRange(st, 1, st.length)); return result; } private static final String authenticationFailedEvidenceVerification = "ELY01151: Evidence Verification Failed.";
return new InitialContext(){ private Hashtable<String, DataSource> dataSources = new Hashtable<>(); if (dataSources.isEmpty()) { //init datasources MysqlConnectionPoolDataSource ds = new MysqlConnectionPoolDataSource(); ds.setURL("jdbc:mysql://localhost:3306/mydb"); ds.setUser("mydbuser"); ds.setPassword("mydbpass"); dataSources.put("jdbc/mydbname", ds); throw new NamingException("Unable to find datasource: "+name); ne.printStackTrace();
protected DirContext createContext() throws NamingException { Hashtable<String, String> env = new Hashtable<String, String>(); env.put(Context.INITIAL_CONTEXT_FACTORY, initialContextFactory); if (connectionUsername != null && !"".equals(connectionUsername)) { env.put(Context.SECURITY_PRINCIPAL, connectionUsername); } else { throw new NamingException("Empty username is not allowed"); } if (connectionPassword != null && !"".equals(connectionPassword)) { env.put(Context.SECURITY_CREDENTIALS, connectionPassword); } else { throw new NamingException("Empty password is not allowed"); } env.put(Context.SECURITY_PROTOCOL, connectionProtocol); env.put(Context.PROVIDER_URL, connectionURL); env.put(Context.SECURITY_AUTHENTICATION, authentication); return new InitialDirContext(env); }
private <E extends DomainObject> CrudService<E> lookUp(@NonNull Class<E> entityClass) throws NamingException { try { final Hashtable jndiProperties = new Hashtable(); jndiProperties.put(Context.URL_PKG_PREFIXES, "org.jboss.ejb.client.naming"); final Context context = new InitialContext(jndiProperties); return (CrudService<E>) context.lookup("java:module/"+entityClass.getSimpleName()+"Service"); } catch (NamingException e) { log.error("lookUp({}) throws NamingException {}",entityClass.getSimpleName(),e.getMessage()) ; throw e ; } }
public static void main(String args[]){ try { Hashtable env = new Hashtable(); env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory"); env.put(Context.PROVIDER_URL, "ldap://localhost:389"); Context ctx = new InitialContext(env); NameParser parser = ctx.getNameParser(""); Name dn = parser.parse("CN=x,OU=y,O=z,ST=v,C=COM"); System.out.println(dn.size() + dn.get( dn.size() -1 )); }catch(NamingException e) { e.printStackTrace(); } }
public ActiveMQCoreAdmin() { super(); jndiProps.put(Context.INITIAL_CONTEXT_FACTORY, ActiveMQInitialContextFactory.class.getCanonicalName()); try { Hashtable<String, String> env = new Hashtable<>(); env.put("java.naming.factory.initial", "org.apache.activemq.artemis.jndi.ActiveMQInitialContextFactory"); env.put("java.naming.provider.url", "tcp://localhost:61616"); context = new InitialContext(env); } catch (NamingException e) { e.printStackTrace(); } }
else if (bindName.isEmpty()) throw new NamingException(ContainerJNDIManager.JNDI_RESOURCES.getString( "WinstoneContext.AlreadyExists", name.toString())); else if (bindName.size() > 1) { Object ctx = lookup(bindName.get(0)); if (!(ctx instanceof Context)) throw new NotContextException(ContainerJNDIManager.JNDI_RESOURCES.getString( "WinstoneContext.NotContext", new String[] { bindName.get(0), ctx.getClass().getName() })); else if (ctx == null) throw new NameNotFoundException(ContainerJNDIManager.JNDI_RESOURCES.getString( "WinstoneContext.NameNotFound", bindName.get(0))); else try { if (allowOverwrites) ((Context) ctx).rebind(bindName.getSuffix(1), value); else ((Context) ctx).bind(bindName.getSuffix(1), value); } finally { ((Context) ctx).close(); } else if ((!allowOverwrites) && this.bindings.get(name.get(0)) != null) throw new NamingException(ContainerJNDIManager.JNDI_RESOURCES.getString( "WinstoneContext.AlreadyExists", name.toString())); else { value = NamingManager.getStateToBind(value, new CompositeName() .add(bindName.get(0)), this, this.environment); synchronized (this.contextLock) { this.bindings.put(bindName.get(0), value);
//TEST Object l_test = null; try { Context l_ctx = new InitialContext(); l_test = l_ctx.lookup("java:comp/env/DocStorageLocation"); } catch (NamingException e1) { // TODO e1.printStackTrace(); }
@Override public Object lookup(final String name) throws NamingException { try { final int index = name.indexOf('#'); if (index != -1) { final String server = name.substring(0, index); final String lookup = name.substring(index + 1); @SuppressWarnings("unchecked") final Hashtable<Object,Object> environment = (Hashtable<Object,Object>) this.environment.clone(); environment.put(Context.PROVIDER_URL, server); environment.put(ORBConstants.ORB_SERVER_ID_PROPERTY, "1"); return CNCtxFactory.INSTANCE.getInitialContext(environment).lookup(lookup); } else { return CNCtxFactory.INSTANCE.getInitialContext(environment).lookup(name); } } catch (NamingException e) { throw e; } catch (Exception e) { throw new NamingException(e.getMessage()); } }
@Override public Object lookup(Name name) throws NamingException { if (name.isEmpty()) { return this.clone(); String atomicName = normalizedName.get(0); Object atomicNameBinding = bindings.get(atomicName); if (normalizedName.size() == 1) { if (atomicNameBinding == null) { throw new NameNotFoundException("Name ["+atomicName+"] from ["+normalizedName+"] not found in this binding"); return NamingManager.getObjectInstance( atomicNameBinding, new CompositeName().add(atomicName), env); } catch (Exception e) { NamingException _e = new NamingException("NamingManager.getObjectInstance(...) failed"); _e.setRootCause(e); throw _e; return ((Context)atomicNameBinding).lookup(normalizedName.getSuffix(1)); throw new NotContextException(atomicName+" from ["+name+"] is not a context, cannot perform further lookup");
public void initialize() { try { this.workManager = (WorkManager) new InitialContext().lookup(workManagerName); } catch (NamingException e) { throw new IllegalStateException("Could not locate WorkManager: " + e.getMessage(), e); } }
InitialContext context = new InitialContext(); jdbcSource = context.lookup(jndiPath); throw new NamingException("Unable to resolve JNDI DataSource - " + e);
public class UseInitialContext { public UseInitialContext() { try { InitialContext ic = new InitialContext(); Object myObject = ic.lookup("myObject"); System.out.println(myObject); } catch (NamingException e) { e.printStackTrace(); } } }
public JndiDataSourceProvider(String dsName, String jndiName, String dialect, boolean showSql) { this.dsName = dsName; Context ctx; try { ctx = new InitialContext(); ds = (DataSource) ctx.lookup(jndiName); if (ds == null) { throw new DBException("Jndi could not found error for " + jndiName); } } catch (NamingException e) { throw new DBException(e.getMessage(), e); } this.dialect = DialectFactory.get(dialect == null ? "mysql" : dialect); this.showSql = showSql; }
if (pos > 0) { String scheme = name.substring(0, pos); Context ctx = NamingManager.getURLContext(scheme, environment); if (ctx == null) { throw new NamingException("scheme " + scheme + " not recognized"); return ctx.lookup(name); } else { Object obj = bindings.get(first); if (obj == null) { throw new NameNotFoundException(name); } else if (obj instanceof Context && path.size() > 1) { Context subContext = (Context) obj; obj = subContext.lookup(path.getSuffix(1)); result = NamingManager.getObjectInstance(result, null, null, this.environment); } catch (NamingException e) { throw e; } catch (Exception e) { throw (NamingException) new NamingException("could not look up : " + name).initCause(e);