Search in sources :

Example 26 with RefAddr

use of javax.naming.RefAddr in project druid by alibaba.

the class DruidDataSourceFactoryTest method test_factory.

@SuppressWarnings("serial")
public void test_factory() throws Exception {
    DruidDataSourceFactory factory = new DruidDataSourceFactory();
    Assert.assertNull(factory.getObjectInstance(null, null, null, null));
    Assert.assertNull(factory.getObjectInstance(new Reference("javax.sql.Date"), null, null, null));
    Reference ref = new Reference("javax.sql.DataSource");
    ref.add(new RefAddr("user") {

        @Override
        public Object getContent() {
            return null;
        }
    });
    ref.add(new RefAddr("defaultReadOnly") {

        @Override
        public Object getContent() {
            return Boolean.TRUE;
        }
    });
    factory.getObjectInstance(ref, null, null, new Hashtable<Object, Object>());
}
Also used : RefAddr(javax.naming.RefAddr) Reference(javax.naming.Reference) DruidDataSourceFactory(com.alibaba.druid.pool.DruidDataSourceFactory)

Example 27 with RefAddr

use of javax.naming.RefAddr in project mongo-java-driver by mongodb.

the class MongoClientFactory method getObjectInstance.

/**
     * This implementation will create instances of {@link MongoClient} based on a connection string conforming to the format specified in
     * {@link MongoClientURI}.
     * <p>The connection string is specified in one of two ways:</p>
     * <ul>
     * <li>As the {@code String} value of a property in the {@code environment} parameter with a key of {@code "connectionString"}</li>
     * <li>As the {@code String} value of a {@link RefAddr} with type {@code "connectionString"} in an {@code obj} parameter
     * of type {@link Reference}</li>
     * </ul>
     *
     * Specification of the connection string in the {@code environment} parameter takes precedence over specification in the {@code obj}
     * parameter.  The {@code name} and {@code nameCtx} parameters are ignored.
     *
     * If a non-empty connection string is not specified in either of these two ways, a {@link MongoException} is thrown.
     * @return an instance of {@link MongoClient} based on the specified connection string
     * @throws MongoException
     *
     * Note: Not all options that can be specified via {@link com.mongodb.MongoClientOptions} can be specified via the connection string.
     */
@Override
public Object getObjectInstance(final Object obj, final Name name, final Context nameCtx, final Hashtable<?, ?> environment) throws Exception {
    // Some app servers, e.g. Wildfly, use the environment to pass location information to an ObjectFactory
    String connectionString = null;
    if (environment.get(CONNECTION_STRING) instanceof String) {
        connectionString = (String) environment.get(CONNECTION_STRING);
    }
    if (connectionString == null || connectionString.isEmpty()) {
        LOGGER.debug(format("No '%s' property in environment.  Casting 'obj' to java.naming.Reference to look for a " + "javax.naming.RefAddr with type equal to '%s'", CONNECTION_STRING, CONNECTION_STRING));
        // javax.naming.RefAddr
        if (obj instanceof Reference) {
            Enumeration<RefAddr> props = ((Reference) obj).getAll();
            while (props.hasMoreElements()) {
                RefAddr addr = props.nextElement();
                if (addr != null) {
                    if (CONNECTION_STRING.equals(addr.getType())) {
                        if (addr.getContent() instanceof String) {
                            connectionString = (String) addr.getContent();
                            break;
                        }
                    }
                }
            }
        }
    }
    if (connectionString == null || connectionString.isEmpty()) {
        throw new MongoException(format("Could not locate '%s' in either environment or obj", CONNECTION_STRING));
    }
    MongoClientURI uri = new MongoClientURI(connectionString);
    return new MongoClient(uri);
}
Also used : RefAddr(javax.naming.RefAddr) MongoClient(com.mongodb.MongoClient) MongoException(com.mongodb.MongoException) Reference(javax.naming.Reference) MongoClientURI(com.mongodb.MongoClientURI)

Example 28 with RefAddr

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

the class ExceptionMapper method tryFed.

private static NamingException tryFed(NotFound e, CNCtx ctx, NameComponent[] inputName) throws NamingException {
    NameComponent[] rest = ((NotFound) e).rest_of_name;
    if (debug) {
    }
    // If one of those is not found, you get "aa" as 'rest'.
    if (rest.length == 1 && inputName != null) {
        // Check that we're not talking to 1.2/1.3 Sun tnameserv
        NameComponent lastIn = inputName[inputName.length - 1];
        if (rest[0].id.equals(lastIn.id) && rest[0].kind != null && rest[0].kind.equals(lastIn.kind)) {
        // Might be legit
        } else {
            // Due to 1.2/1.3 bug that always returns single-item 'rest'
            NamingException ne = new NameNotFoundException();
            ne.setRemainingName(org.wildfly.iiop.openjdk.naming.jndi.CNNameParser.cosNameToName(rest));
            ne.setRootCause(e);
            throw ne;
        }
    }
    // Fixed in 1.4; perform calculations based on correct (1.4) behavior
    // Calculate the components of the name that has been resolved
    NameComponent[] resolvedName = null;
    int len = 0;
    if (inputName != null && (inputName.length >= rest.length)) {
        if (e.why == NotFoundReason.not_context) {
            // First component of rest is found but not a context; keep it
            // as part of resolved name
            len = inputName.length - (rest.length - 1);
            // Remove resolved component from rest
            if (rest.length == 1) {
                // No more remaining
                rest = null;
            } else {
                NameComponent[] tmp = new NameComponent[rest.length - 1];
                System.arraycopy(rest, 1, tmp, 0, tmp.length);
                rest = tmp;
            }
        } else {
            len = inputName.length - rest.length;
        }
        if (len > 0) {
            resolvedName = new NameComponent[len];
            System.arraycopy(inputName, 0, resolvedName, 0, len);
        }
    }
    // Create CPE and set common fields
    CannotProceedException cpe = new CannotProceedException();
    cpe.setRootCause(e);
    if (rest != null && rest.length > 0) {
        cpe.setRemainingName(org.wildfly.iiop.openjdk.naming.jndi.CNNameParser.cosNameToName(rest));
    }
    cpe.setEnvironment(ctx._env);
    // Lookup resolved name to get resolved object
    final java.lang.Object resolvedObj = (resolvedName != null) ? ctx.callResolve(resolvedName) : ctx;
    if (resolvedObj instanceof javax.naming.Context) {
        // obj is a context and child is not found
        // try getting its nns dynamically by constructing
        // a Reference containing obj.
        RefAddr addr = new RefAddr("nns") {

            public java.lang.Object getContent() {
                return resolvedObj;
            }

            private static final long serialVersionUID = 669984699392133792L;
        };
        Reference ref = new Reference("java.lang.Object", addr);
        // Resolved name has trailing slash to indicate nns
        CompositeName cname = new CompositeName();
        // add trailing slash
        cname.add("");
        cpe.setResolvedObj(ref);
        cpe.setAltName(cname);
        cpe.setAltNameCtx((javax.naming.Context) resolvedObj);
        return cpe;
    } else {
        // Not a context, use object factory to transform object.
        Name cname = org.wildfly.iiop.openjdk.naming.jndi.CNNameParser.cosNameToName(resolvedName);
        java.lang.Object resolvedObj2;
        try {
            resolvedObj2 = NamingManager.getObjectInstance(resolvedObj, cname, ctx, ctx._env);
        } catch (NamingException ge) {
            throw ge;
        } catch (Exception ge) {
            NamingException ne = IIOPLogger.ROOT_LOGGER.errorGeneratingObjectViaFactory();
            ne.setRootCause(ge);
            throw ne;
        }
        // If a context, continue operation with context
        if (resolvedObj2 instanceof javax.naming.Context) {
            cpe.setResolvedObj(resolvedObj2);
        } else {
            // Add trailing slash
            cname.add("");
            cpe.setAltName(cname);
            // Create nns reference
            final java.lang.Object rf2 = resolvedObj2;
            RefAddr addr = new RefAddr("nns") {

                public java.lang.Object getContent() {
                    return rf2;
                }

                private static final long serialVersionUID = -785132553978269772L;
            };
            Reference ref = new Reference("java.lang.Object", addr);
            cpe.setResolvedObj(ref);
            cpe.setAltNameCtx(ctx);
        }
        return cpe;
    }
}
Also used : NamingContext(org.omg.CosNaming.NamingContext) NameComponent(org.omg.CosNaming.NameComponent) NameNotFoundException(javax.naming.NameNotFoundException) Reference(javax.naming.Reference) CompositeName(javax.naming.CompositeName) CannotProceedException(javax.naming.CannotProceedException) NameAlreadyBoundException(javax.naming.NameAlreadyBoundException) NamingException(javax.naming.NamingException) ContextNotEmptyException(javax.naming.ContextNotEmptyException) InvalidNameException(javax.naming.InvalidNameException) NameNotFoundException(javax.naming.NameNotFoundException) CompositeName(javax.naming.CompositeName) InvalidName(org.omg.CosNaming.NamingContextPackage.InvalidName) Name(javax.naming.Name) RefAddr(javax.naming.RefAddr) CannotProceedException(javax.naming.CannotProceedException) NamingException(javax.naming.NamingException) NotFound(org.omg.CosNaming.NamingContextPackage.NotFound)

Example 29 with RefAddr

use of javax.naming.RefAddr in project jackrabbit by apache.

the class ClientRepositoryFactory method getObjectInstance.

/**
     * JNDI factory method for creating JCR-RMI clients. Creates a lazy
     * client repository instance that uses the reference parameter "url"
     * as the RMI URL where the remote repository is looked up when accessed.
     *
     * @param object      reference parameters
     * @param name        unused
     * @param context     unused
     * @param environment unused
     * @return repository client
     */
public Object getObjectInstance(Object object, Name name, Context context, Hashtable environment) {
    if (object instanceof Reference) {
        Reference reference = (Reference) object;
        RefAddr url = reference.get(URL_PARAMETER);
        if (url != null && url.getContent() != null) {
            try {
                return getRepository(url.getContent().toString());
            } catch (Exception e) {
                return null;
            }
        }
    }
    return null;
}
Also used : RefAddr(javax.naming.RefAddr) Reference(javax.naming.Reference) MalformedURLException(java.net.MalformedURLException) NotBoundException(java.rmi.NotBoundException) RemoteException(java.rmi.RemoteException)

Example 30 with RefAddr

use of javax.naming.RefAddr in project jackrabbit by apache.

the class RepositoryImpl method getReference.

//------------------------------------------------------< Referenceable >---
/**
     * @see Referenceable#getReference()
     */
public Reference getReference() throws NamingException {
    if (config instanceof Referenceable) {
        Referenceable confref = (Referenceable) config;
        if (reference == null) {
            reference = new Reference(RepositoryImpl.class.getName(), RepositoryImpl.Factory.class.getName(), null);
            // carry over all addresses from referenceable config
            for (Enumeration<RefAddr> en = confref.getReference().getAll(); en.hasMoreElements(); ) {
                reference.add(en.nextElement());
            }
            // also add the information required by factory class
            reference.add(new StringRefAddr(Factory.RCF, confref.getReference().getFactoryClassName()));
            reference.add(new StringRefAddr(Factory.RCC, config.getClass().getName()));
        }
        return reference;
    } else {
        throw new javax.naming.OperationNotSupportedException("Contained RepositoryConfig needs to implement javax.naming.Referenceable");
    }
}
Also used : RefAddr(javax.naming.RefAddr) StringRefAddr(javax.naming.StringRefAddr) Referenceable(javax.naming.Referenceable) StringRefAddr(javax.naming.StringRefAddr) Reference(javax.naming.Reference)

Aggregations

RefAddr (javax.naming.RefAddr)33 Reference (javax.naming.Reference)22 Properties (java.util.Properties)8 NamingException (javax.naming.NamingException)6 StringRefAddr (javax.naming.StringRefAddr)6 InitialContext (javax.naming.InitialContext)4 ObjectFactory (javax.naming.spi.ObjectFactory)3 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 Method (java.lang.reflect.Method)2 MalformedURLException (java.net.MalformedURLException)2 ArrayList (java.util.ArrayList)2 Enumeration (java.util.Enumeration)2 NameNotFoundException (javax.naming.NameNotFoundException)2 DirObjectFactory (javax.naming.spi.DirObjectFactory)2 DruidDataSourceFactory (com.alibaba.druid.pool.DruidDataSourceFactory)1 MongoClient (com.mongodb.MongoClient)1 MongoClientURI (com.mongodb.MongoClientURI)1 MongoException (com.mongodb.MongoException)1 BeanInfo (java.beans.BeanInfo)1 PropertyDescriptor (java.beans.PropertyDescriptor)1