javax.naming.LinkRef Java Examples

The following examples show how to use javax.naming.LinkRef. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: TomcatJndiBuilder.java    From tomee with Apache License 2.0 5 votes vote down vote up
/**
 * LinkRef addresses need to be prefixed with java: or they won't resolve
 *
 * OpenEJB is fine with this, but Tomcat needs them
 *
 * @param value
 * @return
 */
private static Object normalize(final Object value) {
    try {

        if (!(value instanceof LinkRef)) {
            return value;
        }

        final LinkRef ref = (LinkRef) value;

        final RefAddr refAddr = ref.getAll().nextElement();

        final String address = refAddr.getContent().toString();

        if (address.startsWith("openejb:")) {
            return value;
        }

        if (!address.startsWith("java:")) {
            return new LinkRef("java:" + address);
        }

    } catch (final Exception e) {
        // no-op
    }

    return value;
}
 
Example #2
Source File: NamingContext.java    From Tomcat8-Source-Read with MIT License 4 votes vote down vote up
/**
 * Binds a name to an object. All intermediate contexts and the target
 * context (that named by all but terminal atomic component of the name)
 * must already exist.
 *
 * @param name the name to bind; may not be empty
 * @param obj the object to bind; possibly null
 * @param rebind if true, then perform a rebind (ie, overwrite)
 * @exception NameAlreadyBoundException if name is already bound
 * @exception javax.naming.directory.InvalidAttributesException if object
 * did not supply all mandatory attributes
 * @exception NamingException if a naming exception is encountered
 */
protected void bind(Name name, Object obj, boolean rebind)
    throws NamingException {

    if (!checkWritable()) {
        return;
    }

    while ((!name.isEmpty()) && (name.get(0).length() == 0))
        name = name.getSuffix(1);
    if (name.isEmpty())
        throw new NamingException
            (sm.getString("namingContext.invalidName"));

    NamingEntry entry = bindings.get(name.get(0));

    if (name.size() > 1) {
        if (entry == null) {
            throw new NameNotFoundException(sm.getString(
                    "namingContext.nameNotBound", name, name.get(0)));
        }
        if (entry.type == NamingEntry.CONTEXT) {
            if (rebind) {
                ((Context) entry.value).rebind(name.getSuffix(1), obj);
            } else {
                ((Context) entry.value).bind(name.getSuffix(1), obj);
            }
        } else {
            throw new NamingException
                (sm.getString("namingContext.contextExpected"));
        }
    } else {
        if ((!rebind) && (entry != null)) {
            throw new NameAlreadyBoundException
                (sm.getString("namingContext.alreadyBound", name.get(0)));
        } else {
            // Getting the type of the object and wrapping it within a new
            // NamingEntry
            Object toBind =
                NamingManager.getStateToBind(obj, name, this, env);
            if (toBind instanceof Context) {
                entry = new NamingEntry(name.get(0), toBind,
                                        NamingEntry.CONTEXT);
            } else if (toBind instanceof LinkRef) {
                entry = new NamingEntry(name.get(0), toBind,
                                        NamingEntry.LINK_REF);
            } else if (toBind instanceof Reference) {
                entry = new NamingEntry(name.get(0), toBind,
                                        NamingEntry.REFERENCE);
            } else if (toBind instanceof Referenceable) {
                toBind = ((Referenceable) toBind).getReference();
                entry = new NamingEntry(name.get(0), toBind,
                                        NamingEntry.REFERENCE);
            } else {
                entry = new NamingEntry(name.get(0), toBind,
                                        NamingEntry.ENTRY);
            }
            bindings.put(name.get(0), entry);
        }
    }

}
 
Example #3
Source File: NamingContext.java    From Tomcat7.0.67 with Apache License 2.0 4 votes vote down vote up
/**
 * Binds a name to an object. All intermediate contexts and the target 
 * context (that named by all but terminal atomic component of the name) 
 * must already exist.
 * 
 * @param name the name to bind; may not be empty
 * @param obj the object to bind; possibly null
 * @param rebind if true, then perform a rebind (ie, overwrite)
 * @exception NameAlreadyBoundException if name is already bound
 * @exception javax.naming.directory.InvalidAttributesException if object
 * did not supply all mandatory attributes
 * @exception NamingException if a naming exception is encountered
 */
protected void bind(Name name, Object obj, boolean rebind)
    throws NamingException {
    
    if (!checkWritable()) {
        return;
    }
    
    while ((!name.isEmpty()) && (name.get(0).length() == 0))
        name = name.getSuffix(1);
    if (name.isEmpty())
        throw new NamingException
            (sm.getString("namingContext.invalidName"));
    
    NamingEntry entry = bindings.get(name.get(0));
    
    if (name.size() > 1) {
        if (entry == null) {
            throw new NameNotFoundException(sm.getString(
                    "namingContext.nameNotBound", name, name.get(0)));
        }
        if (entry.type == NamingEntry.CONTEXT) {
            if (rebind) {
                ((Context) entry.value).rebind(name.getSuffix(1), obj);
            } else {
                ((Context) entry.value).bind(name.getSuffix(1), obj);
            }
        } else {
            throw new NamingException
                (sm.getString("namingContext.contextExpected"));
        }
    } else {
        if ((!rebind) && (entry != null)) {
            throw new NameAlreadyBoundException
                (sm.getString("namingContext.alreadyBound", name.get(0)));
        } else {
            // Getting the type of the object and wrapping it within a new
            // NamingEntry
            Object toBind = 
                NamingManager.getStateToBind(obj, name, this, env);
            if (toBind instanceof Context) {
                entry = new NamingEntry(name.get(0), toBind, 
                                        NamingEntry.CONTEXT);
            } else if (toBind instanceof LinkRef) {
                entry = new NamingEntry(name.get(0), toBind, 
                                        NamingEntry.LINK_REF);
            } else if (toBind instanceof Reference) {
                entry = new NamingEntry(name.get(0), toBind, 
                                        NamingEntry.REFERENCE);
            } else if (toBind instanceof Referenceable) {
                toBind = ((Referenceable) toBind).getReference();
                entry = new NamingEntry(name.get(0), toBind, 
                                        NamingEntry.REFERENCE);
            } else {
                entry = new NamingEntry(name.get(0), toBind, 
                                        NamingEntry.ENTRY);
            }
            bindings.put(name.get(0), entry);
        }
    }
    
}
 
Example #4
Source File: NamingContext.java    From tomcatsrc with Apache License 2.0 4 votes vote down vote up
/**
 * Binds a name to an object. All intermediate contexts and the target 
 * context (that named by all but terminal atomic component of the name) 
 * must already exist.
 * 
 * @param name the name to bind; may not be empty
 * @param obj the object to bind; possibly null
 * @param rebind if true, then perform a rebind (ie, overwrite)
 * @exception NameAlreadyBoundException if name is already bound
 * @exception javax.naming.directory.InvalidAttributesException if object
 * did not supply all mandatory attributes
 * @exception NamingException if a naming exception is encountered
 */
protected void bind(Name name, Object obj, boolean rebind)
    throws NamingException {
    
    if (!checkWritable()) {
        return;
    }
    
    while ((!name.isEmpty()) && (name.get(0).length() == 0))
        name = name.getSuffix(1);
    if (name.isEmpty())
        throw new NamingException
            (sm.getString("namingContext.invalidName"));
    
    NamingEntry entry = bindings.get(name.get(0));
    
    if (name.size() > 1) {
        if (entry == null) {
            throw new NameNotFoundException(sm.getString(
                    "namingContext.nameNotBound", name, name.get(0)));
        }
        if (entry.type == NamingEntry.CONTEXT) {
            if (rebind) {
                ((Context) entry.value).rebind(name.getSuffix(1), obj);
            } else {
                ((Context) entry.value).bind(name.getSuffix(1), obj);
            }
        } else {
            throw new NamingException
                (sm.getString("namingContext.contextExpected"));
        }
    } else {
        if ((!rebind) && (entry != null)) {
            throw new NameAlreadyBoundException
                (sm.getString("namingContext.alreadyBound", name.get(0)));
        } else {
            // Getting the type of the object and wrapping it within a new
            // NamingEntry
            Object toBind = 
                NamingManager.getStateToBind(obj, name, this, env);
            if (toBind instanceof Context) {
                entry = new NamingEntry(name.get(0), toBind, 
                                        NamingEntry.CONTEXT);
            } else if (toBind instanceof LinkRef) {
                entry = new NamingEntry(name.get(0), toBind, 
                                        NamingEntry.LINK_REF);
            } else if (toBind instanceof Reference) {
                entry = new NamingEntry(name.get(0), toBind, 
                                        NamingEntry.REFERENCE);
            } else if (toBind instanceof Referenceable) {
                toBind = ((Referenceable) toBind).getReference();
                entry = new NamingEntry(name.get(0), toBind, 
                                        NamingEntry.REFERENCE);
            } else {
                entry = new NamingEntry(name.get(0), toBind, 
                                        NamingEntry.ENTRY);
            }
            bindings.put(name.get(0), entry);
        }
    }
    
}
 
Example #5
Source File: IvmContext.java    From tomee with Apache License 2.0 4 votes vote down vote up
public Object lookup(final String compositName) throws NamingException {
    if (compositName.isEmpty()) {
        return this;
    }

    final String compoundName;
    final int index = compositName.indexOf(':');
    if (index > -1) {

        final String prefix = compositName.substring(0, index);

        String path = compositName.substring(index + 1);
        final ParsedName name = new ParsedName(path);

        if (prefix.equals("openejb")) {
            path = name.path();
            return openejbURLContextFactory.getContext().lookup(path);
        } else if (prefix.equals("java")) {
            if (name.getComponent().equals("openejb")) {
                path = name.remaining().path();
                return openejbURLContextFactory.getContext().lookup(path);
            } else {
                path = name.path();
                return javaURLContextFactory.getContext().lookup(path);
            }
        } else {
            // we don't know what the prefix means, throw an exception
            throw new NamingException("Unknown JNDI name prefix '" + prefix + ":'");
        }
    } else {
        /*
          the resolve method always starts with the comparison assuming that the first
          component of the name is a context of a peer node or the same node, so we have
          to prepend the current context name to the relative lookup path.
        */
        compoundName = mynode.getAtomicName() + '/' + compositName;
    }

    /*
       If the object has been resolved in the past from this context and the specified path (name)
       it will be in the fastCache which is significantly faster then peruse the Node graph.
       80 ms compared to 300 ms for a full node path search.
    */
    Object obj = fastCache.get(compoundName);
    if (obj == null) {
        try {
            obj = mynode.resolve(new ParsedName(compoundName), readOnly);
        } catch (final NameNotFoundException nnfe) {
            obj = federate(compositName);
        }

        // don't cache proxies
        if (!(obj instanceof IntraVmProxy) && !(obj instanceof ContextualJndiReference)) {
            fastCache.put(compoundName, obj);
        }
    }

    if (obj == null) {
        throw new NameNotFoundException("Name \"" + compositName + "\" not found.");
    }

    if (obj.getClass() == IvmContext.class) {
        ((IvmContext) obj).myEnv = myEnv;
    } else if (obj instanceof Reference) {
        /**
         * EJB references and resource references are wrapped in special
         * org.apache.openejb.core.ivm.naming.Reference types that check to
         * see if the current operation is allowed access to the entry (See EJB 1.1/2.0 Allowed Operations)
         * If the operation is not allowed, a javax.naming.NameNotFoundException is thrown.
         *
         * A Reference type can also carry out dynamic resolution of references if necessary.
         */

        // TODO: JRG - this needs a test
        while (obj instanceof Reference) {
            obj = ((Reference)obj).getObject();
        }
    } else if (obj instanceof LinkRef) {
        obj = lookup(((LinkRef) obj).getLinkName());
    }
    return obj;
}
 
Example #6
Source File: JavaLookupTest.java    From tomee with Apache License 2.0 4 votes vote down vote up
public void testLinking() throws Exception {

        final Assembler assembler = new Assembler();
        final ConfigurationFactory config = new ConfigurationFactory();

        assembler.createTransactionManager(config.configureService(TransactionServiceInfo.class));
        assembler.createSecurityService(config.configureService(SecurityServiceInfo.class));

        final InitialContext context = new InitialContext();

        final Context javaContext = (Context) context.lookup("java:");

        javaContext.bind("java:TransactionManager", new JndiUrlReference("java:comp/TransactionManager"));
        javaContext.bind("java:TransactionManagerLink", new LinkRef("java:comp/TransactionManager"));

        assertTrue(context.lookup("java:TransactionManager") instanceof TransactionManager);
        assertTrue(context.lookup("java:TransactionManagerLink") instanceof TransactionManager);

        new InitialContext().bind("java:foo", new LinkRef("java:comp/TransactionManager"));

        assertTrue(context.lookup("java:foo") instanceof TransactionManager);


    }