Search in sources :

Example 41 with Reference

use of javax.naming.Reference in project Payara by payara.

the class ExternalJndiResourceDeployer method installExternalJndiResource.

/**
 * Installs the given external jndi resource. This method gets called
 * during server initialization and from external jndi resource
 * deployer to handle resource events.
 *
 * @param extJndiRes external jndi resource
 * @param resourceInfo Information about the resource
 */
public void installExternalJndiResource(org.glassfish.resources.beans.ExternalJndiResource extJndiRes, ResourceInfo resourceInfo) {
    try {
        // create the external JNDI factory, its initial context and
        // pass them as references.
        String factoryClass = extJndiRes.getFactoryClass();
        String jndiLookupName = extJndiRes.getJndiLookupName();
        if (_logger.isLoggable(Level.FINE)) {
            _logger.log(Level.FINE, "installExternalJndiResources resourceName " + resourceInfo + " factoryClass " + factoryClass + " jndiLookupName = " + jndiLookupName);
        }
        Object factory = ResourceUtil.loadObject(factoryClass);
        if (factory == null) {
            _logger.log(Level.WARNING, "jndi.factory_load_error", factoryClass);
            return;
        } else if (!(factory instanceof javax.naming.spi.InitialContextFactory)) {
            _logger.log(Level.WARNING, "jndi.factory_class_unexpected", factoryClass);
            return;
        }
        // Get properties to create the initial naming context
        // for the target JNDI factory
        Hashtable env = new Hashtable();
        for (Iterator props = extJndiRes.getProperties().iterator(); props.hasNext(); ) {
            ResourceProperty prop = (ResourceProperty) props.next();
            env.put(prop.getName(), prop.getValue());
        }
        Context context = null;
        try {
            context = ((InitialContextFactory) factory).getInitialContext(env);
        } catch (NamingException ne) {
            _logger.log(Level.SEVERE, "jndi.initial_context_error", factoryClass);
            _logger.log(Level.SEVERE, "jndi.initial_context_error_excp", ne.getMessage());
        }
        if (context == null) {
            _logger.log(Level.SEVERE, "jndi.factory_create_error", factoryClass);
            return;
        }
        // Bind a Reference to the proxy object factory; set the
        // initial context factory.
        // JndiProxyObjectFactory.setInitialContext(bindName, context);
        Reference ref = new Reference(extJndiRes.getResType(), "org.glassfish.resources.naming.JndiProxyObjectFactory", null);
        // unique JNDI name within server runtime
        ref.add(new SerializableObjectRefAddr("resourceInfo", resourceInfo));
        // target JNDI name
        ref.add(new StringRefAddr("jndiLookupName", jndiLookupName));
        // target JNDI factory class
        ref.add(new StringRefAddr("jndiFactoryClass", factoryClass));
        // add Context info as a reference address
        ref.add(new ProxyRefAddr(extJndiRes.getResourceInfo().getName(), env));
        // Publish the reference
        namingService.publishObject(resourceInfo, ref, true);
    } catch (Exception ex) {
        _logger.log(Level.SEVERE, "customrsrc.create_ref_error", resourceInfo);
        _logger.log(Level.SEVERE, "customrsrc.create_ref_error_excp", ex);
    }
}
Also used : Context(javax.naming.Context) Hashtable(java.util.Hashtable) Reference(javax.naming.Reference) SerializableObjectRefAddr(org.glassfish.resources.naming.SerializableObjectRefAddr) NamingException(javax.naming.NamingException) ResourceConflictException(org.glassfish.resourcebase.resources.api.ResourceConflictException) InitialContextFactory(javax.naming.spi.InitialContextFactory) ResourceProperty(com.sun.enterprise.repository.ResourceProperty) StringRefAddr(javax.naming.StringRefAddr) Iterator(java.util.Iterator) NamingException(javax.naming.NamingException) ProxyRefAddr(org.glassfish.resources.naming.ProxyRefAddr)

Example 42 with Reference

use of javax.naming.Reference in project Payara by payara.

the class ConnectorObjectFactory method getObjectInstance.

public Object getObjectInstance(Object obj, Name name, Context nameCtx, Hashtable env) throws Exception {
    Reference ref = (Reference) obj;
    if (_logger.isLoggable(Level.FINE)) {
        _logger.log(Level.FINE, "ConnectorObjectFactory: " + ref + " Name:" + name);
    }
    PoolInfo poolInfo = (PoolInfo) ref.get(0).getContent();
    String moduleName = (String) ref.get(1).getContent();
    ResourceInfo resourceInfo = (ResourceInfo) ref.get(2).getContent();
    if (getRuntime().isACCRuntime() || getRuntime().isNonACCRuntime()) {
        ConnectorDescriptor connectorDescriptor = null;
        String descriptorJNDIName = ConnectorAdminServiceUtils.getReservePrefixedJNDINameForDescriptor(moduleName);
        Context ic = new InitialContext(env);
        connectorDescriptor = (ConnectorDescriptor) ic.lookup(descriptorJNDIName);
        try {
            getRuntime().createActiveResourceAdapter(connectorDescriptor, moduleName, null);
        } catch (ConnectorRuntimeException e) {
            if (_logger.isLoggable(Level.FINE)) {
                _logger.log(Level.FINE, "Failed to look up ConnectorDescriptor from JNDI", moduleName);
            }
            NamingException ne = new NamingException("Failed to look up ConnectorDescriptor from JNDI");
            ne.setRootCause(e);
            throw ne;
        }
    }
    ClassLoader loader = Thread.currentThread().getContextClassLoader();
    if (!getRuntime().checkAccessibility(moduleName, loader)) {
        String msg = localStrings.getString("cof.no_access_to_embedded_rar", moduleName);
        throw new NamingException(msg);
    }
    Object cf = null;
    try {
        ManagedConnectionFactory mcf = getRuntime().obtainManagedConnectionFactory(poolInfo, env);
        if (mcf == null) {
            if (_logger.isLoggable(Level.FINE)) {
                _logger.log(Level.FINE, "Failed to create MCF ", poolInfo);
            }
            throw new ConnectorRuntimeException("Failed to create MCF");
        }
        boolean forceNoLazyAssoc = false;
        String jndiName = name.toString();
        if (jndiName.endsWith(ConnectorConstants.PM_JNDI_SUFFIX)) {
            forceNoLazyAssoc = true;
        }
        String derivedJndiName = ConnectorsUtil.deriveJndiName(jndiName, env);
        ConnectionManagerImpl mgr = (ConnectionManagerImpl) getRuntime().obtainConnectionManager(poolInfo, forceNoLazyAssoc, resourceInfo);
        mgr.setJndiName(derivedJndiName);
        mgr.setRarName(moduleName);
        String logicalName = (String) env.get(GlassfishNamingManager.LOGICAL_NAME);
        if (logicalName != null) {
            mgr.setLogicalName(logicalName);
        }
        mgr.initialize();
        cf = mcf.createConnectionFactory(mgr);
        if (cf == null) {
            String msg = localStrings.getString("cof.no.resource.adapter");
            throw new RuntimeException(new ConfigurationException(msg));
        }
        if (getRuntime().isServer() || getRuntime().isEmbedded()) {
            ConnectorRegistry registry = ConnectorRegistry.getInstance();
            if (registry.isTransparentDynamicReconfigPool(poolInfo)) {
                Resources resources = getRuntime().getResources(poolInfo);
                ResourcePool resourcePool = null;
                if (resources != null) {
                    resourcePool = (ResourcePool) ConnectorsUtil.getResourceByName(resources, ResourcePool.class, poolInfo.getName());
                    if (resourcePool != null) {
                        ResourceDeployer deployer = getRuntime().getResourceDeployer(resourcePool);
                        if (deployer != null && deployer.supportsDynamicReconfiguration() && ConnectorsUtil.isDynamicReconfigurationEnabled(resourcePool)) {
                            Object o = env.get(ConnectorConstants.DYNAMIC_RECONFIGURATION_PROXY_CALL);
                            if (o == null || Boolean.valueOf(o.toString()).equals(false)) {
                                // TODO use list ? (even in the ResourceDeployer API)
                                Class[] classes = deployer.getProxyClassesForDynamicReconfiguration();
                                Class[] proxyClasses = new Class[classes.length + 1];
                                for (int i = 0; i < classes.length; i++) {
                                    proxyClasses[i] = classes[i];
                                }
                                proxyClasses[proxyClasses.length - 1] = DynamicallyReconfigurableResource.class;
                                cf = getProxyObject(cf, proxyClasses, resourceInfo);
                            }
                        }
                    }
                }
            }
        }
        if (_logger.isLoggable(Level.FINE)) {
            _logger.log(Level.FINE, "Connection Factory:" + cf);
        }
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
    return cf;
}
Also used : Context(javax.naming.Context) InitialContext(javax.naming.InitialContext) ConnectorRuntimeException(com.sun.appserv.connectors.internal.api.ConnectorRuntimeException) ResourceInfo(org.glassfish.resourcebase.resources.api.ResourceInfo) ConnectionManagerImpl(com.sun.enterprise.connectors.ConnectionManagerImpl) Reference(javax.naming.Reference) ResourcePool(com.sun.enterprise.config.serverbeans.ResourcePool) InitialContext(javax.naming.InitialContext) ConfigurationException(javax.naming.ConfigurationException) NamingException(javax.naming.NamingException) ConnectorRuntimeException(com.sun.appserv.connectors.internal.api.ConnectorRuntimeException) ConnectorDescriptor(com.sun.enterprise.deployment.ConnectorDescriptor) ManagedConnectionFactory(javax.resource.spi.ManagedConnectionFactory) ConnectorRuntimeException(com.sun.appserv.connectors.internal.api.ConnectorRuntimeException) ConfigurationException(javax.naming.ConfigurationException) ResourceDeployer(org.glassfish.resourcebase.resources.api.ResourceDeployer) PoolInfo(org.glassfish.resourcebase.resources.api.PoolInfo) NamingException(javax.naming.NamingException) Resources(com.sun.enterprise.config.serverbeans.Resources) ConnectorRegistry(com.sun.enterprise.connectors.ConnectorRegistry)

Example 43 with Reference

use of javax.naming.Reference in project rabbitmq-jms-client by rabbitmq.

the class RMQDestination method getReference.

@Override
public Reference getReference() throws NamingException {
    Reference ref = new Reference(this.getClass().getCanonicalName());
    addStringProperty(ref, "destinationName", this.destinationName);
    addBooleanProperty(ref, "amqp", this.amqp);
    addBooleanProperty(ref, "isQueue", this.isQueue);
    addStringProperty(ref, "amqpExchangeName", this.amqpExchangeName);
    addStringProperty(ref, "amqpRoutingKey", this.amqpRoutingKey);
    addStringProperty(ref, "amqpQueueName", this.amqpQueueName);
    return ref;
}
Also used : Reference(javax.naming.Reference)

Example 44 with Reference

use of javax.naming.Reference in project rabbitmq-jms-client by rabbitmq.

the class RMQObjectFactory method getObjectInstance.

/**
 * {@inheritDoc}
 */
@Override
public Object getObjectInstance(Object obj, Name name, Context ctx, Hashtable<?, ?> environment) throws Exception {
    if ((obj == null)) {
        return null;
    }
    Reference ref = obj instanceof Reference ? (Reference) obj : null;
    if (ref == null && (environment == null || environment.isEmpty())) {
        throw new NamingException("Unable to instantiate object: obj is not a Reference instance and environment table is empty");
    }
    String className = ref != null ? ref.getClassName() : (String) environment.get(ENV_CLASS_NAME);
    if (className == null || className.trim().length() == 0) {
        throw new NamingException("Unable to instantiate object: type has not been specified");
    }
    /*
         * Valid class names are:
         * javax.jms.ConnectionFactory
         * javax.jms.QueueConnectionFactory
         * javax.jms.TopicConnectionFactory
         * javax.jms.Topic
         * javax.jms.Queue
         *
         */
    boolean topic = false;
    boolean connectionFactory = false;
    if (javax.jms.QueueConnectionFactory.class.getName().equals(className) || javax.jms.TopicConnectionFactory.class.getName().equals(className) || javax.jms.ConnectionFactory.class.getName().equals(className)) {
        connectionFactory = true;
    } else if (javax.jms.Topic.class.getName().equals(className)) {
        topic = true;
    } else if (javax.jms.Queue.class.getName().equals(className)) {
    } else {
        throw new NamingException(String.format("Unknown class [%s]", className));
    }
    if (connectionFactory) {
        return createConnectionFactory(ref, environment, name);
    } else {
        return createDestination(ref, environment, name, topic);
    }
}
Also used : Reference(javax.naming.Reference) NamingException(javax.naming.NamingException) Queue(javax.jms.Queue)

Example 45 with Reference

use of javax.naming.Reference in project rabbitmq-jms-client by rabbitmq.

the class RMQConnectionFactoryTest method testUpdatedConnectionFactoryReference.

@Test
public void testUpdatedConnectionFactoryReference() throws Exception {
    RMQConnectionFactory connFactory = new RMQConnectionFactory();
    connFactory.setHost("sillyHost");
    connFactory.setPassword("my-password");
    connFactory.setPort(42);
    connFactory.setQueueBrowserReadMax(52);
    connFactory.setOnMessageTimeoutMs(62);
    connFactory.setSsl(true);
    connFactory.setTerminationTimeout(1234567890123456789L);
    connFactory.setUsername("fred");
    connFactory.setVirtualHost("bill");
    Reference ref = connFactory.getReference();
    Properties newProps = getProps(ref);
    assertEquals("amqps://fred:my-password@sillyHost:42/bill", newProps.getProperty("uri"), "Not the correct uri");
    assertEquals("52", newProps.getProperty("queueBrowserReadMax"), "Not the correct queueBrowserReadMax");
    assertEquals("62", newProps.getProperty("onMessageTimeoutMs"), "Not the correct onMessageTimeoutMs");
}
Also used : Reference(javax.naming.Reference) Properties(java.util.Properties) Test(org.junit.jupiter.api.Test)

Aggregations

Reference (javax.naming.Reference)226 NamingException (javax.naming.NamingException)79 RefAddr (javax.naming.RefAddr)59 StringRefAddr (javax.naming.StringRefAddr)54 Context (javax.naming.Context)41 NameNotFoundException (javax.naming.NameNotFoundException)40 InitialContext (javax.naming.InitialContext)37 Test (org.junit.Test)33 Name (javax.naming.Name)30 NameAlreadyBoundException (javax.naming.NameAlreadyBoundException)28 Properties (java.util.Properties)26 OperationNotSupportedException (javax.naming.OperationNotSupportedException)25 NotContextException (javax.naming.NotContextException)24 ObjectFactory (javax.naming.spi.ObjectFactory)23 CompositeName (javax.naming.CompositeName)21 Referenceable (javax.naming.Referenceable)18 Binding (javax.naming.Binding)17 CompoundName (javax.naming.CompoundName)16 LinkRef (javax.naming.LinkRef)15 Hashtable (java.util.Hashtable)12