Search in sources :

Example 26 with Name

use of javax.naming.Name in project aries by apache.

the class InitialContextTest method testURLContextErrorPropagation.

@Test
public void testURLContextErrorPropagation() throws Exception {
    ObjectFactory of = new ObjectFactory() {

        public Object getObjectInstance(Object obj, Name name, Context nameCtx, Hashtable<?, ?> environment) throws Exception {
            throw new Exception("doh");
        }
    };
    registerURLObjectFactory(of, "test");
    ic = initialContext();
    try {
        ic.lookup("test:something");
        Assert.fail("Expected NamingException");
    } catch (NamingException ne) {
        assertNotNull(ne.getCause());
        assertEquals("doh", ne.getCause().getMessage());
    }
}
Also used : InitialLdapContext(javax.naming.ldap.InitialLdapContext) Context(javax.naming.Context) InitialContext(javax.naming.InitialContext) BundleContext(org.osgi.framework.BundleContext) LdapContext(javax.naming.ldap.LdapContext) ObjectFactory(javax.naming.spi.ObjectFactory) Hashtable(java.util.Hashtable) NamingException(javax.naming.NamingException) NoInitialContextException(javax.naming.NoInitialContextException) NamingException(javax.naming.NamingException) Name(javax.naming.Name) Test(org.junit.Test)

Example 27 with Name

use of javax.naming.Name in project aries by apache.

the class ObjectFactoryTest method testFactoriesThatDoUnsafeCastsAreIgnored.

@Test
public void testFactoriesThatDoUnsafeCastsAreIgnored() throws Exception {
    Hashtable<String, Object> props = new Hashtable<String, Object>();
    props.put("aries.object.factory.requires.reference", Boolean.TRUE);
    bc.registerService(ObjectFactory.class.getName(), new ObjectFactory() {

        public Object getObjectInstance(Object arg0, Name arg1, Context arg2, Hashtable<?, ?> arg3) throws Exception {
            return (Reference) arg0;
        }
    }, props);
    NamingManager.getObjectInstance("Some dummy data", null, null, env);
}
Also used : BundleContext(org.osgi.framework.BundleContext) Context(javax.naming.Context) ObjectFactory(javax.naming.spi.ObjectFactory) Hashtable(java.util.Hashtable) Name(javax.naming.Name) Test(org.junit.Test)

Example 28 with Name

use of javax.naming.Name in project geode by apache.

the class ContextImpl method destroySubcontext.

/**
   * 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) {
        // 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));
    }
}
Also used : NotContextException(javax.naming.NotContextException) InvalidNameException(javax.naming.InvalidNameException) NameNotFoundException(javax.naming.NameNotFoundException) ContextNotEmptyException(javax.naming.ContextNotEmptyException) CompositeName(javax.naming.CompositeName) Name(javax.naming.Name)

Example 29 with Name

use of javax.naming.Name in project geode by apache.

the class ContextImpl method unbind.

/**
   * 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);
    // 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));
        }
    }
}
Also used : Context(javax.naming.Context) NotContextException(javax.naming.NotContextException) InvalidNameException(javax.naming.InvalidNameException) NameNotFoundException(javax.naming.NameNotFoundException) CompositeName(javax.naming.CompositeName) Name(javax.naming.Name)

Example 30 with Name

use of javax.naming.Name in project geode by apache.

the class ContextImpl method rebind.

/**
   * Rebinds object obj to name name . If there is existing binding it will be overwritten.
   * 
   * @param name name of the object to rebind.
   * @param obj object to add. Can be null.
   * @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 NotContextException if name has more than one atomic name and intermediate context is
   *         not found
   * @throws NamingException if any other naming error occurs
   * 
   */
public void rebind(Name name, Object obj) 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 nameToBind = parsedName.get(0);
    if (parsedName.size() == 1) {
        ctxMaps.put(nameToBind, obj);
    } else {
        Object boundObject = ctxMaps.get(nameToBind);
        if (boundObject instanceof Context) {
            /*
         * Let the subcontext bind the object.
         */
            ((Context) boundObject).bind(parsedName.getSuffix(1), obj);
        } else {
            if (boundObject == null) {
                // Create new subcontext and let it do the binding
                Context sub = createSubcontext(nameToBind);
                sub.bind(parsedName.getSuffix(1), obj);
            } else {
                throw new NotContextException(LocalizedStrings.ContextImpl_EXPECTED_CONTEXT_BUT_FOUND_0.toLocalizedString(boundObject));
            }
        }
    }
}
Also used : Context(javax.naming.Context) NotContextException(javax.naming.NotContextException) InvalidNameException(javax.naming.InvalidNameException) CompositeName(javax.naming.CompositeName) Name(javax.naming.Name)

Aggregations

Name (javax.naming.Name)107 CompositeName (javax.naming.CompositeName)48 NamingException (javax.naming.NamingException)38 Context (javax.naming.Context)37 InitialContext (javax.naming.InitialContext)36 NameNotFoundException (javax.naming.NameNotFoundException)34 Test (org.junit.Test)32 Reference (javax.naming.Reference)24 NotContextException (javax.naming.NotContextException)22 Binding (javax.naming.Binding)19 CompoundName (javax.naming.CompoundName)19 NameAlreadyBoundException (javax.naming.NameAlreadyBoundException)19 NameParser (javax.naming.NameParser)17 OperationNotSupportedException (javax.naming.OperationNotSupportedException)16 InvalidNameException (javax.naming.InvalidNameException)10 NamingContext (org.eclipse.jetty.jndi.NamingContext)10 IOException (java.io.IOException)8 LinkRef (javax.naming.LinkRef)7 ServiceName (org.jboss.msc.service.ServiceName)7 Referenceable (javax.naming.Referenceable)6