/** * TestCase: get our local address and reverse look it up */ @Test public void testRDNS() throws Exception { InetAddress localhost = getLocalIPAddr(); try { String s = DNS.reverseDns(localhost, null); LOG.info("Local reverse DNS hostname is " + s); } catch (NameNotFoundException | CommunicationException e) { if (!localhost.isLinkLocalAddress() || localhost.isLoopbackAddress()) { //these addresses probably won't work with rDNS anyway, unless someone //has unusual entries in their DNS server mapping 1.0.0.127 to localhost LOG.info("Reverse DNS failing as due to incomplete networking", e); LOG.info("Address is " + localhost + " Loopback=" + localhost.isLoopbackAddress() + " Linklocal=" + localhost.isLinkLocalAddress()); } } }
/** * Unbinds the named object. Removes the terminal atomic name in name * from the target context--that named by all but the terminal atomic * part of name. * <p> * This method is idempotent. It succeeds even if the terminal atomic * name is not bound in the target context, but throws * NameNotFoundException if any of the intermediate contexts do not exist. * * @param name the name to bind; may not be empty * @exception NameNotFoundException if an intermediate context does not * exist * @exception NamingException if a naming exception is encountered */ @Override public void unbind(String name) throws NamingException { File file = file(name); if (file == null) throw new NameNotFoundException( sm.getString("resources.notFound", name)); if (!file.delete()) throw new NamingException (sm.getString("resources.unbindFailed", name)); }
/** * Binds a new name to the object bound to an old name, and unbinds the * old name. Both names are relative to this context. Any attributes * associated with the old name become associated with the new name. * Intermediate contexts of the old name are not changed. * * @param oldName the name of the existing binding; may not be empty * @param newName the name of the new binding; may not be empty * @exception NameAlreadyBoundException if newName is already bound * @exception NamingException if a naming exception is encountered */ @Override public void rename(String oldName, String newName) throws NamingException { File file = file(oldName); if (file == null) throw new NameNotFoundException (sm.getString("resources.notFound", oldName)); File newFile = new File(base, newName); if (!file.renameTo(newFile)) { throw new NamingException(sm.getString("resources.renameFail", oldName, newName)); } }
/** * Enumerates the names bound in the named context, along with the class * names of objects bound to them. The contents of any subcontexts are * not included. * <p> * If a binding is added to or removed from this context, its effect on * an enumeration previously returned is undefined. * * @param name the name of the context to list * @return an enumeration of the names and class names of the bindings in * this context. Each element of the enumeration is of type NameClassPair. * @exception NamingException if a naming exception is encountered */ @Override public NamingEnumeration<NameClassPair> list(Name name) throws NamingException { // Removing empty parts while ((!name.isEmpty()) && (name.get(0).length() == 0)) name = name.getSuffix(1); if (name.isEmpty()) { return new NamingContextEnumeration(bindings.values().iterator()); } NamingEntry entry = bindings.get(name.get(0)); if (entry == null) { throw new NameNotFoundException (sm.getString("namingContext.nameNotBound", name, name.get(0))); } if (entry.type != NamingEntry.CONTEXT) { throw new NamingException (sm.getString("namingContext.contextExpected")); } return ((Context) entry.value).list(name.getSuffix(1)); }
/** * Enumerates the names bound in the named context, along with the * objects bound to them. The contents of any subcontexts are not * included. * <p> * If a binding is added to or removed from this context, its effect on * an enumeration previously returned is undefined. * * @param name the name of the context to list * @return an enumeration of the bindings in this context. * Each element of the enumeration is of type Binding. * @exception NamingException if a naming exception is encountered */ @Override public NamingEnumeration<Binding> listBindings(Name name) throws NamingException { // Removing empty parts while ((!name.isEmpty()) && (name.get(0).length() == 0)) name = name.getSuffix(1); if (name.isEmpty()) { return new NamingContextBindingsEnumeration(bindings.values().iterator(), this); } NamingEntry entry = bindings.get(name.get(0)); if (entry == null) { throw new NameNotFoundException (sm.getString("namingContext.nameNotBound", name, name.get(0))); } if (entry.type != NamingEntry.CONTEXT) { throw new NamingException (sm.getString("namingContext.contextExpected")); } return ((Context) entry.value).listBindings(name.getSuffix(1)); }
/** * Bug 9280 - in case a NameNotFoundException probably the base DN is wrong * so throw a suitable exception. */ @Test(expected = ValidationException.class) public void validateLdapProperties_NameNotFoundException() throws Exception { final String baseDN = "baseDN"; final Properties props = new Properties(); props.put(SettingType.LDAP_BASE_DN.name(), baseDN); final LdapConnector connector = new LdapConnector(ldapAccess, props); final String extCause = "some external cause"; when( ldapAccess.dnSearch(any(Properties.class), anyString(), anyString())).thenThrow( new NameNotFoundException(extCause)); try { connector.validateLdapProperties(new VOUserDetails()); } catch (ValidationException e) { assertEquals(ReasonEnum.LDAP_BASE_DN_INVALID, e.getReason()); assertEquals(baseDN, e.getMessageParams()[0]); // verify(asb.sessionCtx, times(1)).setRollbackOnly(); throw e; } }
/** * Look up the object with the given name in the current JNDI context. * @param name the JNDI name of the object * @return object found (cannot be {@code null}; if a not so well-behaved * JNDI implementations returns null, a NamingException gets thrown) * @throws NamingException if there is no object with the given * name bound to JNDI */ public Object lookup(final String name) throws NamingException { if (logger.isDebugEnabled()) { logger.debug("Looking up JNDI object with name [" + name + "]"); } return execute(new JndiCallback<Object>() { @Override public Object doInContext(Context ctx) throws NamingException { Object located = ctx.lookup(name); if (located == null) { throw new NameNotFoundException( "JNDI object with [" + name + "] not found: JNDI implementation returned null"); } return located; } }); }
/** * Enumerates the names bound in the named context, along with the class * names of objects bound to them. The contents of any subcontexts are * not included. * <p> * If a binding is added to or removed from this context, its effect on * an enumeration previously returned is undefined. * * @param name the name of the context to list * @return an enumeration of the names and class names of the bindings in * this context. Each element of the enumeration is of type NameClassPair. * @exception NamingException if a naming exception is encountered */ public NamingEnumeration list(Name name) throws NamingException { // Removing empty parts while ((!name.isEmpty()) && (name.get(0).length() == 0)) name = name.getSuffix(1); if (name.isEmpty()) { return new NamingContextEnumeration(bindings.values().iterator()); } NamingEntry entry = (NamingEntry) bindings.get(name.get(0)); if (entry == null) { throw new NameNotFoundException (sm.getString("namingContext.nameNotBound", name.get(0))); } if (entry.type != NamingEntry.CONTEXT) { throw new NamingException (sm.getString("namingContext.contextExpected")); } return ((Context) entry.value).list(name.getSuffix(1)); }
/** * Enumerates the names bound in the named context, along with the * objects bound to them. The contents of any subcontexts are not * included. * <p> * If a binding is added to or removed from this context, its effect on * an enumeration previously returned is undefined. * * @param name the name of the context to list * @return an enumeration of the bindings in this context. * Each element of the enumeration is of type Binding. * @exception NamingException if a naming exception is encountered */ public NamingEnumeration listBindings(Name name) throws NamingException { // Removing empty parts while ((!name.isEmpty()) && (name.get(0).length() == 0)) name = name.getSuffix(1); if (name.isEmpty()) { return new NamingContextBindingsEnumeration(bindings.values().iterator(), this); } NamingEntry entry = (NamingEntry) bindings.get(name.get(0)); if (entry == null) { throw new NameNotFoundException (sm.getString("namingContext.nameNotBound", name.get(0))); } if (entry.type != NamingEntry.CONTEXT) { throw new NamingException (sm.getString("namingContext.contextExpected")); } return ((Context) entry.value).listBindings(name.getSuffix(1)); }
/** * Enumerates the names bound in the named context, along with the class * names of objects bound to them. The contents of any subcontexts are * not included. * <p> * If a binding is added to or removed from this context, its effect on * an enumeration previously returned is undefined. * * @param name the name of the context to list * @return an enumeration of the names and class names of the bindings in * this context. Each element of the enumeration is of type NameClassPair. * @exception NamingException if a naming exception is encountered */ public NamingEnumeration list(Name name) throws NamingException { // Removing empty parts while ((!name.isEmpty()) && (name.get(0).length() == 0)) name = name.getSuffix(1); if (name.isEmpty()) { return new NamingContextEnumeration(bindings.elements()); } NamingEntry entry = (NamingEntry) bindings.get(name.get(0)); if (entry == null) { throw new NameNotFoundException (sm.getString("namingContext.nameNotBound", name.get(0))); } if (entry.type != NamingEntry.CONTEXT) { throw new NamingException (sm.getString("namingContext.contextExpected")); } return ((Context) entry.value).list(name.getSuffix(1)); }
/** * Enumerates the names bound in the named context, along with the * objects bound to them. The contents of any subcontexts are not * included. * <p> * If a binding is added to or removed from this context, its effect on * an enumeration previously returned is undefined. * * @param name the name of the context to list * @return an enumeration of the bindings in this context. * Each element of the enumeration is of type Binding. * @exception NamingException if a naming exception is encountered */ public NamingEnumeration listBindings(Name name) throws NamingException { // Removing empty parts while ((!name.isEmpty()) && (name.get(0).length() == 0)) name = name.getSuffix(1); if (name.isEmpty()) { return new NamingContextBindingsEnumeration(bindings.elements()); } NamingEntry entry = (NamingEntry) bindings.get(name.get(0)); if (entry == null) { throw new NameNotFoundException (sm.getString("namingContext.nameNotBound", name.get(0))); } if (entry.type != NamingEntry.CONTEXT) { throw new NamingException (sm.getString("namingContext.contextExpected")); } return ((Context) entry.value).listBindings(name.getSuffix(1)); }
/** * TestCase: get our local address and reverse look it up */ @Test public void testRDNS() throws Exception { InetAddress localhost = getLocalIPAddr(); try { String s = DNS.reverseDns(localhost, null); LOG.info("Local revers DNS hostname is " + s); } catch (NameNotFoundException e) { if (!localhost.isLinkLocalAddress() || localhost.isLoopbackAddress()) { //these addresses probably won't work with rDNS anyway, unless someone //has unusual entries in their DNS server mapping 1.0.0.127 to localhost LOG.info("Reverse DNS failing as due to incomplete networking", e); LOG.info("Address is " + localhost + " Loopback=" + localhost.isLoopbackAddress() + " Linklocal=" + localhost.isLinkLocalAddress()); } } }
/** * Enumerates the names bound in the named context, along with the class * names of objects bound to them. The contents of any subcontexts are not * included. * <p> * If a binding is added to or removed from this context, its effect on an * enumeration previously returned is undefined. * * @param name * the name of the context to list * @return an enumeration of the names and class names of the bindings in * this context. Each element of the enumeration is of type * NameClassPair. * @exception NamingException * if a naming exception is encountered */ @Override public NamingEnumeration<NameClassPair> list(Name name) throws NamingException { // Removing empty parts while ((!name.isEmpty()) && (name.get(0).length() == 0)) name = name.getSuffix(1); if (name.isEmpty()) { return new NamingContextEnumeration(bindings.values().iterator()); } NamingEntry entry = bindings.get(name.get(0)); if (entry == null) { throw new NameNotFoundException(sm.getString("namingContext.nameNotBound", name, name.get(0))); } if (entry.type != NamingEntry.CONTEXT) { throw new NamingException(sm.getString("namingContext.contextExpected")); } return ((Context) entry.value).list(name.getSuffix(1)); }
/** * Enumerates the names bound in the named context, along with the objects * bound to them. The contents of any subcontexts are not included. * <p> * If a binding is added to or removed from this context, its effect on an * enumeration previously returned is undefined. * * @param name * the name of the context to list * @return an enumeration of the bindings in this context. Each element of * the enumeration is of type Binding. * @exception NamingException * if a naming exception is encountered */ @Override public NamingEnumeration<Binding> listBindings(Name name) throws NamingException { // Removing empty parts while ((!name.isEmpty()) && (name.get(0).length() == 0)) name = name.getSuffix(1); if (name.isEmpty()) { return new NamingContextBindingsEnumeration(bindings.values().iterator(), this); } NamingEntry entry = bindings.get(name.get(0)); if (entry == null) { throw new NameNotFoundException(sm.getString("namingContext.nameNotBound", name, name.get(0))); } if (entry.type != NamingEntry.CONTEXT) { throw new NamingException(sm.getString("namingContext.contextExpected")); } return ((Context) entry.value).listBindings(name.getSuffix(1)); }
@Test public void testLookupFails() throws Exception { NameNotFoundException ne = new NameNotFoundException(); String name = "foo"; final Context context = mock(Context.class); given(context.lookup(name)).willThrow(ne); JndiTemplate jt = new JndiTemplate() { @Override protected Context createInitialContext() { return context; } }; try { jt.lookup(name); fail("Should have thrown NamingException"); } catch (NameNotFoundException ex) { // Ok } verify(context).close(); }
@Test public void testLookupReturnsNull() throws Exception { String name = "foo"; final Context context = mock(Context.class); given(context.lookup(name)).willReturn(null); JndiTemplate jt = new JndiTemplate() { @Override protected Context createInitialContext() { return context; } }; try { jt.lookup(name); fail("Should have thrown NamingException"); } catch (NameNotFoundException ex) { // Ok } verify(context).close(); }
/** * Look up the object with the given name. * <p>Note: Not intended for direct use by applications. * Will be used by any standard InitialContext JNDI lookups. * @throws javax.naming.NameNotFoundException if the object could not be found */ @Override public Object lookup(String lookupName) throws NameNotFoundException { String name = this.root + lookupName; if (logger.isDebugEnabled()) { logger.debug("Static JNDI lookup: [" + name + "]"); } if ("".equals(name)) { return new SimpleNamingContext(this.root, this.boundObjects, this.environment); } Object found = this.boundObjects.get(name); if (found == null) { if (!name.endsWith("/")) { name = name + "/"; } for (String boundName : this.boundObjects.keySet()) { if (boundName.startsWith(name)) { return new SimpleNamingContext(name, this.boundObjects, this.environment); } } throw new NameNotFoundException( "Name [" + this.root + lookupName + "] not bound; " + this.boundObjects.size() + " bindings: [" + StringUtils.collectionToDelimitedString(this.boundObjects.keySet(), ",") + "]"); } return found; }
/** * Destroys subcontext with name name. The subcontext must be empty otherwise * ContextNotEmptyException is thrown. Once a context is destroyed, the * instance should not be used. * * @param name subcontext to destroy * @throws NoPermissionException if this context has been destroyed. * @throws InvalidNameException if name is empty or is CompositeName that * spans more than one naming system. * @throws ContextNotEmptyException if Context name is not empty. * @throws NameNotFoundException if subcontext with name name can not be * found. * @throws NotContextException if name is not bound to instance of * ContextImpl. * */ public void destroySubcontext(Name name) throws NamingException { checkIsDestroyed(); Name parsedName = getParsedName(name); if (parsedName.size() == 0 || parsedName.get(0).length() == 0) { throw new InvalidNameException(LocalizedStrings.ContextImpl_NAME_CAN_NOT_BE_EMPTY.toLocalizedString()); } String subContextName = parsedName.get(0); Object boundObject = ctxMaps.get(subContextName); if (boundObject == null) { throw new NameNotFoundException(LocalizedStrings.ContextImpl_NAME_0_NOT_FOUND_IN_THE_CONTEXT.toLocalizedString(subContextName)); } if (!(boundObject instanceof ContextImpl)) { throw new NotContextException(); } ContextImpl contextToDestroy = (ContextImpl) boundObject; if (parsedName.size() == 1) { // Check if the Context to be destroyed is empty. Can not destroy // non-empty Context. if (contextToDestroy.ctxMaps.size() == 0) { ctxMaps.remove(subContextName); contextToDestroy.destroyInternal(); } else { throw new ContextNotEmptyException(LocalizedStrings.ContextImpl_CAN_NOT_DESTROY_NONEMPTY_CONTEXT.toLocalizedString()); } } else { // Let the subcontext destroy the context ((ContextImpl) boundObject).destroySubcontext(parsedName.getSuffix(1)); } }
/** * Removes name and its associated object from the context. * * @param name name to remove * @throws NoPermissionException if this context has been destroyed. * @throws InvalidNameException if name is empty or is CompositeName that * spans more than one naming system * @throws NameNotFoundException if intermediate context can not be found * @throws NotContextException if name has more than one atomic name and * intermediate context is not found. * @throws NamingException if any other naming exception occurs * */ public void unbind(Name name) throws NamingException { checkIsDestroyed(); Name parsedName = getParsedName(name); if (parsedName.size() == 0 || parsedName.get(0).length() == 0) { throw new InvalidNameException(LocalizedStrings.ContextImpl_NAME_CAN_NOT_BE_EMPTY.toLocalizedString()); } String nameToRemove = parsedName.get(0); // scenerio unbind a // remove a and its associated object if (parsedName.size() == 1) { ctxMaps.remove(nameToRemove); } else { // scenerio unbind a/b or a/b/c // remove b and its associated object Object boundObject = ctxMaps.get(nameToRemove); if (boundObject instanceof Context) { // remove b and its associated object ((Context) boundObject).unbind(parsedName.getSuffix(1)); } else { // if the name is not found then throw exception if (!ctxMaps.containsKey(nameToRemove)) { throw new NameNotFoundException(LocalizedStrings.ContextImpl_CAN_NOT_FIND_0.toLocalizedString(name)); } throw new NotContextException(LocalizedStrings.ContextImpl_EXPECTED_CONTEXT_BUT_FOUND_0.toLocalizedString(boundObject)); } } }
/** * Access service from service registry for given interface name and filter. * * @param ctx caller bundle context * @param lookupName composite name to lookup. * @return service object from service registry. * @throws NamingException if No service found with the given URL. */ protected Object findService(BundleContext ctx, OSGiURL lookupName) throws NamingException { String serviceName = lookupName.getServiceName(); //this can be an interface or a JNDI-service name. String filter = lookupName.getFilter(); //osgi:service/foo/bar/myservice //osgi:service/org.wso2.service.Deployer/(serviceId=56) //find the service with the given serviceName and filter. Object result; try { result = getService(ctx, serviceName, filter); if (logger.isDebugEnabled()) { logger.debug("Successfully completed service registry lookup for name : " + lookupName); } } catch (NameNotFoundException e) { //if NameNotFoundException is occurred, the query might have used JNDI service name for the lookup. String jndiServiceName = lookupName.getJNDIServiceName(); filter = "(" + JNDIConstants.JNDI_SERVICENAME + "=" + jndiServiceName + ")"; if (logger.isDebugEnabled()) { logger.debug("No service found for name : " + lookupName + ". Retrying service registry lookup with jndi-service-name : " + jndiServiceName); } result = getService(ctx, null, filter); //parse as serviceName=null } return result; }
private Object getService(BundleContext bundleContext, String serviceName, String filter) throws NamingException { try { ServiceReference[] serviceReferences = bundleContext.getServiceReferences(serviceName, filter); if (serviceReferences != null) { for (ServiceReference reference : serviceReferences) { Object serviceObject = bundleContext.getService(reference); if (serviceObject != null) { return serviceObject; } } throw new NameNotFoundException(SM.getString("osgiUrlContext.noService")); } else { throw new NameNotFoundException(SM.getString("osgiUrlContext.noServiceReference", serviceName)); } } catch (InvalidSyntaxException e) { // If we get an invalid syntax exception we just ignore and return // a NameNotFoundException. eg: for queries :- osgi:service/foo/myService (where "foo/myService" is the // osgi.jndi.service.name and this may read filter=myService which causes an invalid syntax exception) throw new NameNotFoundException(SM.getString("osgiUrlContext.noServiceForFilter", filter)); } }
/** * Enumerates the names bound in the named context, along with the class * names of objects bound to them. The contents of any subcontexts are * not included. * <p> * If a binding is added to or removed from this context, its effect on * an enumeration previously returned is undefined. * * @param name the name of the context to list * @return an enumeration of the names and class names of the bindings in * this context. Each element of the enumeration is of type NameClassPair. * @throws NamingException if a jndi exception is encountered */ @Override public NamingEnumeration<NameClassPair> list(Name name) throws NamingException { // Removing empty parts while ((!name.isEmpty()) && (name.get(0).length() == 0)) { name = name.getSuffix(1); } if (name.isEmpty()) { return new NamingContextEnumeration(new ArrayList<>(bindings.values())); } NamingEntry entry = bindings.get(name.get(0)); if (entry == null) { throw new NameNotFoundException(SM.getString("namingContext.nameNotBound", name, name.get(0))); } if (entry.type != NamingEntry.CONTEXT) { throw new NamingException(SM.getString("namingContext.contextExpected")); } return ((Context) entry.value).list(name.getSuffix(1)); }
/** * Enumerates the names bound in the named context, along with the * objects bound to them. The contents of any subcontexts are not * included. * <p> * If a binding is added to or removed from this context, its effect on * an enumeration previously returned is undefined. * * @param name the name of the context to list * @return an enumeration of the bindings in this context. * Each element of the enumeration is of type Binding. * @throws NamingException if a jndi exception is encountered */ @Override public NamingEnumeration<Binding> listBindings(Name name) throws NamingException { // Removing empty parts while ((!name.isEmpty()) && (name.get(0).length() == 0)) { name = name.getSuffix(1); } if (name.isEmpty()) { return new NamingContextBindingsEnumeration(new ArrayList<>(bindings.values()), this); } NamingEntry entry = bindings.get(name.get(0)); if (entry == null) { throw new NameNotFoundException(SM.getString("namingContext.nameNotBound", name, name.get(0))); } if (entry.type != NamingEntry.CONTEXT) { throw new NamingException(SM.getString("namingContext.contextExpected")); } return ((Context) entry.value).listBindings(name.getSuffix(1)); }
/** * Check whether an entry with the given DN exists. The method performs a search to check this, which is not so * efficient than just reading the entry. * @param dn distinguished name * @return true if exists * @throws NamingException if DN can not be resolved */ public Boolean exists(final String dn) throws NamingException { WithContext<Boolean> action = ctx -> { SearchControls searchControls = new SearchControls(); searchControls.setSearchScope(SearchControls.OBJECT_SCOPE); searchControls.setReturningAttributes(new String[0]); searchControls.setReturningObjFlag(false); try { ctx.search(dn, "(objectClass=*)", searchControls); return true; } catch (NameNotFoundException e) { logger.log(Level.FINEST, e.getMessage(), e); } return false; }; return performWithContext(action); }
@SuppressWarnings("unchecked") public static <T> T jndiLookup(String name) { InitialContext context = null; try { context = new InitialContext(); return (T) context.lookup(name); } catch (NamingException e) { if (is(e, NameNotFoundException.class)) { return null; } else { throw new IllegalStateException(e); } } finally { close(context); } }
@Override public Attributes getAttributes(String name, String[] ids) throws NamingException { if (Validator.isNotNull(name)) { throw new NameNotFoundException(); } Attributes attributes = new BasicAttributes(true); for (int i = 0; i < ids.length; i++) { Attribute attribute = _attributes.get(ids[i]); if (attribute != null) { attributes.put(attribute); } } return attributes; }
private Connection retrieveJdbcDatasourceConnection() throws Exception { javax.naming.Context initCtx = new InitialContext(); Connection con=null; // String appServer = ConfigFlag.APPSERVER_TYPE.getValue(); // if (appServer.equals(AppServer.WEBSPHERE) || appServer.equals(AppServer.TOMCAT)) // { initCtx = (javax.naming.Context) initCtx.lookup("java:comp/env"); // } try { String environment = EnvironmentConfig.getMainDataSourceName(); DataSource dataSource =(DataSource) initCtx.lookup("jdbc/" + environment); con = dataSource.getConnection(); } catch (NameNotFoundException e) { System.out.println("No jdbc datasources are configured for this project."); } return con; }