Search in sources :

Example 6 with NameParser

use of javax.naming.NameParser in project jetty.project by eclipse.

the class ContextFactory method newNamingContext.

/**
     * Create a new NamingContext.
     * @param obj the object to create
     * @param loader the classloader for the naming context
     * @param env the jndi env for the entry
     * @param name the name of the entry
     * @param parentCtx the parent context of the entry
     * @return the newly created naming context
     * @throws Exception if unable to create a new naming context
     */
public NamingContext newNamingContext(Object obj, ClassLoader loader, Hashtable env, Name name, Context parentCtx) throws Exception {
    Reference ref = (Reference) obj;
    StringRefAddr parserAddr = (StringRefAddr) ref.get("parser");
    String parserClassName = (parserAddr == null ? null : (String) parserAddr.getContent());
    NameParser parser = (NameParser) (parserClassName == null ? null : loader.loadClass(parserClassName).newInstance());
    return new NamingContext(env, name.get(0), (NamingContext) parentCtx, parser);
}
Also used : StringRefAddr(javax.naming.StringRefAddr) Reference(javax.naming.Reference) NameParser(javax.naming.NameParser)

Example 7 with NameParser

use of javax.naming.NameParser in project ACS by ACS-Community.

the class ManagerImpl method bind.

/**
	 * Bind object to remote directory.
	 *
	 * Use INS syntax specified in the INS specification.
	 * In short, the syntax is that components are left-to-right slash ('/') separated and case-sensitive.
	 * The id and kind of each component are separated by the period character ('.').
	 *
	 * @param	remoteDirectory	remote directory context to be used.
	 * @param	name	name of the object, code non-<code>null</code>
	 * @param	object	object to be binded
	 */
private void bind(Context remoteDirectory, String name, String type, Object object) {
    assert (name != null);
    // do not bind interdomain names
    if (name.startsWith(CURL_URI_SCHEMA))
        return;
    if (remoteDirectory != null) {
        try {
            // hierarchical name check
            int pos = name.indexOf('/');
            if (pos != -1) {
                if (pos == 0 || pos == name.length())
                    throw new IllegalArgumentException("Invalid hierarchical name '" + name + "'.");
                String parent = name.substring(0, pos);
                String child = name.substring(pos + 1);
                // lookup for subcontext, if not found create one
                Context parentContext = null;
                try {
                    parentContext = (Context) remoteDirectory.lookup(parent);
                } catch (NameNotFoundException nnfe) {
                // noop
                }
                if (parentContext == null) {
                    parentContext = remoteDirectory.createSubcontext(parent);
                /// @todo temp. commented out
                //generateHiearchyContexts(remoteDirectory, parent, parentContext);
                }
                // delegate
                bind(parentContext, child, type, object);
                return;
            }
            NameParser parser = remoteDirectory.getNameParser("");
            Name n;
            if (type != null)
                n = parser.parse(name + "." + type);
            else
                n = parser.parse(name);
            // special case
            if (name.endsWith(".D")) {
                remoteDirectory.rebind(n, object);
            /// @todo temp. commented out
            //generateHiearchyContexts(remoteDirectory, name, (Context)remoteDirectory.lookup(name));
            } else
                remoteDirectory.bind(n, object);
        } catch (NameAlreadyBoundException nabe) {
            rebind(remoteDirectory, name, type, object);
        } catch (NamingException ne) {
            CoreException ce = new CoreException("Failed to bind name '" + name + "' to the remote directory.", ne);
            logger.log(Level.FINE, ce.getMessage(), ce);
        }
    }
}
Also used : InitialContext(javax.naming.InitialContext) Context(javax.naming.Context) NameAlreadyBoundException(javax.naming.NameAlreadyBoundException) CoreException(com.cosylab.acs.maci.CoreException) NameNotFoundException(javax.naming.NameNotFoundException) NamingException(javax.naming.NamingException) NameParser(javax.naming.NameParser) Name(javax.naming.Name)

Example 8 with NameParser

use of javax.naming.NameParser in project ACS by ACS-Community.

the class ManagerImpl method lookup.

/**
	 * Lookups for an object in remote directory.
	 *
	 * Use INS syntax specified in the INS specification.
	 * In short, the syntax is that components are left-to-right slash ('/') separated and case-sensitive.
	 * The id and kind of each component are separated by the period character ('.').
	 *
	 * @param	remoteDirectory	remote directory context to be used.
	 * @param	name	name of the object, code non-<code>null</code>
	 * @param	type	type of the object
	 * @return	object	object found in the remote directory, <code>null<code> if nout found
	 */
private Object lookup(Context remoteDirectory, String name, String type) {
    assert (name != null);
    // do not look for interdomain names
    if (name.startsWith(CURL_URI_SCHEMA))
        return null;
    Object resolved = null;
    if (remoteDirectory != null) {
        try {
            NameParser parser = remoteDirectory.getNameParser("");
            Name n;
            if (type != null)
                n = parser.parse(name + "." + type);
            else
                n = parser.parse(name);
            resolved = remoteDirectory.lookup(n);
        } catch (NamingException ne) {
            CoreException ce = new CoreException("Failed to lookup name '" + name + "' in the remote directory.", ne);
            logger.log(Level.FINE, ce.getMessage(), ce);
        }
    }
    return resolved;
}
Also used : CoreException(com.cosylab.acs.maci.CoreException) NamingException(javax.naming.NamingException) NameParser(javax.naming.NameParser) Name(javax.naming.Name)

Example 9 with NameParser

use of javax.naming.NameParser in project wildfly by wildfly.

the class ServiceBasedNamingStoreTestCase method testStoredContext.

@Test
public void testStoredContext() throws Exception {
    final ServiceName bindingName = ServiceName.JBOSS.append("foo-stored").append("again");
    bindObject(bindingName, new Context() {

        @Override
        public Object lookup(Name name) throws NamingException {
            if ("blah/blah2".equals(name.toString())) {
                return new Integer(5);
            }
            return null;
        }

        @Override
        public Object lookup(String name) throws NamingException {
            return lookup(new CompositeName(name));
        }

        @Override
        public void bind(Name name, Object obj) throws NamingException {
        }

        @Override
        public void bind(String name, Object obj) throws NamingException {
        }

        @Override
        public void rebind(Name name, Object obj) throws NamingException {
        }

        @Override
        public void rebind(String name, Object obj) throws NamingException {
        }

        @Override
        public void unbind(Name name) throws NamingException {
        }

        @Override
        public void unbind(String name) throws NamingException {
        }

        @Override
        public void rename(Name oldName, Name newName) throws NamingException {
        }

        @Override
        public void rename(String oldName, String newName) throws NamingException {
        }

        @Override
        public NamingEnumeration<NameClassPair> list(Name name) throws NamingException {
            return null;
        }

        @Override
        public NamingEnumeration<NameClassPair> list(String name) throws NamingException {
            return null;
        }

        @Override
        public NamingEnumeration<Binding> listBindings(Name name) throws NamingException {
            if (!"hi/there".equals(name.toString()))
                throw new IllegalArgumentException("Expected hi/there");
            return null;
        }

        @Override
        public NamingEnumeration<Binding> listBindings(String name) throws NamingException {
            return null;
        }

        @Override
        public void destroySubcontext(Name name) throws NamingException {
        }

        @Override
        public void destroySubcontext(String name) throws NamingException {
        }

        @Override
        public Context createSubcontext(Name name) throws NamingException {
            return null;
        }

        @Override
        public Context createSubcontext(String name) throws NamingException {
            return null;
        }

        @Override
        public Object lookupLink(Name name) throws NamingException {
            return null;
        }

        @Override
        public Object lookupLink(String name) throws NamingException {
            return null;
        }

        @Override
        public NameParser getNameParser(Name name) throws NamingException {
            return null;
        }

        @Override
        public NameParser getNameParser(String name) throws NamingException {
            return null;
        }

        @Override
        public Name composeName(Name name, Name prefix) throws NamingException {
            return null;
        }

        @Override
        public String composeName(String name, String prefix) throws NamingException {
            return null;
        }

        @Override
        public Object addToEnvironment(String propName, Object propVal) throws NamingException {
            return null;
        }

        @Override
        public Object removeFromEnvironment(String propName) throws NamingException {
            return null;
        }

        @Override
        public Hashtable<?, ?> getEnvironment() throws NamingException {
            return null;
        }

        @Override
        public void close() throws NamingException {
        }

        @Override
        public String getNameInNamespace() throws NamingException {
            return null;
        }
    });
    final NamingContext ctx = new NamingContext(new CompositeName(), store, null);
    final Object obj = ctx.lookup(new CompositeName("foo-stored/again/blah/blah2"));
    ctx.listBindings("foo-stored/again/hi/there");
    assertNotNull(obj);
    assertEquals(new Integer(5), obj);
}
Also used : StopContext(org.jboss.msc.service.StopContext) Context(javax.naming.Context) StartContext(org.jboss.msc.service.StartContext) Hashtable(java.util.Hashtable) CompositeName(javax.naming.CompositeName) NamingEnumeration(javax.naming.NamingEnumeration) CompositeName(javax.naming.CompositeName) Name(javax.naming.Name) ServiceName(org.jboss.msc.service.ServiceName) ServiceName(org.jboss.msc.service.ServiceName) NamingException(javax.naming.NamingException) NameParser(javax.naming.NameParser) Test(org.junit.Test)

Example 10 with NameParser

use of javax.naming.NameParser in project jetty.project by eclipse.

the class TestLocalJNDI method testLocal.

@Test
public void testLocal() throws Exception {
    InitialContext ic = new InitialContext();
    NameParser parser = ic.getNameParser("");
    ic.bind("foo", "xxx");
    Object o = ic.lookup("foo");
    assertNotNull(o);
    assertEquals("xxx", (String) o);
    ic.unbind("foo");
    try {
        ic.lookup("foo");
        fail("Foo exists");
    } catch (NameNotFoundException e) {
    //expected
    }
    Name name = parser.parse("a");
    name.addAll(parser.parse("b/c/d"));
    NamingUtil.bind(ic, name.toString(), "333");
    assertNotNull(ic.lookup("a"));
    assertNotNull(ic.lookup("a/b"));
    assertNotNull(ic.lookup("a/b/c"));
    Context c = (Context) ic.lookup("a/b/c");
    o = c.lookup("d");
    assertNotNull(o);
    assertEquals("333", (String) o);
    assertEquals("333", ic.lookup(name));
    ic.destroySubcontext("a");
    try {
        ic.lookup("a");
        fail("context a was not destroyed");
    } catch (NameNotFoundException e) {
    //expected
    }
    name = parser.parse("");
    name.add("x");
    Name suffix = parser.parse("y/z");
    name.addAll(suffix);
    NamingUtil.bind(ic, name.toString(), "555");
    assertEquals("555", ic.lookup(name));
    ic.destroySubcontext("x");
}
Also used : InitialContext(javax.naming.InitialContext) Context(javax.naming.Context) NameNotFoundException(javax.naming.NameNotFoundException) InitialContext(javax.naming.InitialContext) NameParser(javax.naming.NameParser) Name(javax.naming.Name) Test(org.junit.Test)

Aggregations

NameParser (javax.naming.NameParser)18 Name (javax.naming.Name)17 InitialContext (javax.naming.InitialContext)9 Context (javax.naming.Context)7 NamingException (javax.naming.NamingException)7 CoreException (com.cosylab.acs.maci.CoreException)4 HashMap (java.util.HashMap)3 CompositeName (javax.naming.CompositeName)3 NameNotFoundException (javax.naming.NameNotFoundException)3 Test (org.junit.Test)3 NamingEnumeration (javax.naming.NamingEnumeration)2 URI (java.net.URI)1 URISyntaxException (java.net.URISyntaxException)1 ArrayList (java.util.ArrayList)1 Hashtable (java.util.Hashtable)1 Entry (java.util.Map.Entry)1 Properties (java.util.Properties)1 Session (javax.mail.Session)1 Binding (javax.naming.Binding)1 InvalidNameException (javax.naming.InvalidNameException)1