Search in sources :

Example 76 with Reference

use of javax.naming.Reference in project tomcat70 by apache.

the class ResourceLinkFactory method getObjectInstance.

// -------------------------------------------------- ObjectFactory Methods
/**
 * Create a new DataSource instance.
 *
 * @param obj The reference object describing the DataSource
 */
@Override
public Object getObjectInstance(Object obj, Name name, Context nameCtx, Hashtable<?, ?> environment) throws NamingException {
    if (!(obj instanceof ResourceLinkRef)) {
        return null;
    }
    // Can we process this request?
    Reference ref = (Reference) obj;
    // Read the global ref addr
    String globalName = null;
    RefAddr refAddr = ref.get(ResourceLinkRef.GLOBALNAME);
    if (refAddr != null) {
        globalName = refAddr.getContent().toString();
        // to access the specified global resource
        if (!validateGlobalResourceAccess(globalName)) {
            return null;
        }
        Object result = null;
        result = globalContext.lookup(globalName);
        // Check the expected type
        String expectedClassName = ref.getClassName();
        if (expectedClassName == null) {
            throw new IllegalArgumentException(sm.getString("resourceLinkFactory.nullType", name, globalName));
        }
        try {
            Class<?> expectedClazz = Class.forName(expectedClassName, true, Thread.currentThread().getContextClassLoader());
            if (!expectedClazz.isAssignableFrom(result.getClass())) {
                throw new IllegalArgumentException(sm.getString("resourceLinkFactory.wrongType", name, globalName, expectedClassName, result.getClass().getName()));
            }
        } catch (ClassNotFoundException e) {
            throw new IllegalArgumentException(sm.getString("resourceLinkFactory.unknownType", name, globalName, expectedClassName), e);
        }
        return result;
    }
    return null;
}
Also used : RefAddr(javax.naming.RefAddr) ResourceLinkRef(org.apache.naming.ResourceLinkRef) Reference(javax.naming.Reference)

Example 77 with Reference

use of javax.naming.Reference in project tomcat70 by apache.

the class ServiceRefFactory method getObjectInstance.

// ----------------------------------------------------------- Constructors
// -------------------------------------------------------------- Constants
// ----------------------------------------------------- Instance Variables
// --------------------------------------------------------- Public Methods
// -------------------------------------------------- ObjectFactory Methods
/**
 * Crete a new serviceref instance.
 *
 * @param obj The reference object describing the webservice
 */
@Override
public Object getObjectInstance(Object obj, Name name, Context nameCtx, Hashtable<?, ?> environment) throws Exception {
    if (obj instanceof ServiceRef) {
        Reference ref = (Reference) obj;
        // ClassLoader
        ClassLoader tcl = Thread.currentThread().getContextClassLoader();
        if (tcl == null)
            tcl = this.getClass().getClassLoader();
        ServiceFactory factory = ServiceFactory.newInstance();
        javax.xml.rpc.Service service = null;
        // Service Interface
        RefAddr tmp = ref.get(ServiceRef.SERVICE_INTERFACE);
        String serviceInterface = null;
        if (tmp != null)
            serviceInterface = (String) tmp.getContent();
        // WSDL
        tmp = ref.get(ServiceRef.WSDL);
        String wsdlRefAddr = null;
        if (tmp != null)
            wsdlRefAddr = (String) tmp.getContent();
        // PortComponent
        Hashtable<String, QName> portComponentRef = new Hashtable<String, QName>();
        // Create QName object
        QName serviceQname = null;
        tmp = ref.get(ServiceRef.SERVICE_LOCAL_PART);
        if (tmp != null) {
            String serviceLocalPart = (String) tmp.getContent();
            tmp = ref.get(ServiceRef.SERVICE_NAMESPACE);
            if (tmp == null) {
                serviceQname = new QName(serviceLocalPart);
            } else {
                String serviceNamespace = (String) tmp.getContent();
                serviceQname = new QName(serviceNamespace, serviceLocalPart);
            }
        }
        Class<?> serviceInterfaceClass = null;
        // Create service object
        if (serviceInterface == null) {
            if (serviceQname == null) {
                throw new NamingException("Could not create service-ref instance");
            }
            try {
                if (wsdlRefAddr == null) {
                    service = factory.createService(serviceQname);
                } else {
                    service = factory.createService(new URL(wsdlRefAddr), serviceQname);
                }
            } catch (Exception e) {
                NamingException ex = new NamingException("Could not create service");
                ex.initCause(e);
                throw ex;
            }
        } else {
            // Loading service Interface
            try {
                serviceInterfaceClass = tcl.loadClass(serviceInterface);
            } catch (ClassNotFoundException e) {
                NamingException ex = new NamingException("Could not load service Interface");
                ex.initCause(e);
                throw ex;
            }
            if (serviceInterfaceClass == null) {
                throw new NamingException("Could not load service Interface");
            }
            try {
                if (wsdlRefAddr == null) {
                    if (!Service.class.isAssignableFrom(serviceInterfaceClass)) {
                        throw new NamingException("service Interface should extend javax.xml.rpc.Service");
                    }
                    service = factory.loadService(serviceInterfaceClass);
                } else {
                    service = factory.loadService(new URL(wsdlRefAddr), serviceInterfaceClass, new Properties());
                }
            } catch (Exception e) {
                NamingException ex = new NamingException("Could not create service");
                ex.initCause(e);
                throw ex;
            }
        }
        if (service == null) {
            throw new NamingException("Cannot create service object");
        }
        serviceQname = service.getServiceName();
        serviceInterfaceClass = service.getClass();
        if (wsdlRefAddr != null) {
            try {
                WSDLFactory wsdlfactory = WSDLFactory.newInstance();
                WSDLReader reader = wsdlfactory.newWSDLReader();
                reader.setFeature("javax.wsdl.importDocuments", true);
                Definition def = reader.readWSDL((new URL(wsdlRefAddr)).toExternalForm());
                javax.wsdl.Service wsdlservice = def.getService(serviceQname);
                // Can't change the API
                @SuppressWarnings("unchecked") Map<String, ?> ports = wsdlservice.getPorts();
                Method m = serviceInterfaceClass.getMethod("setEndpointAddress", new Class[] { java.lang.String.class, java.lang.String.class });
                for (Iterator<String> i = ports.keySet().iterator(); i.hasNext(); ) {
                    String portName = i.next();
                    Port port = wsdlservice.getPort(portName);
                    String endpoint = getSOAPLocation(port);
                    m.invoke(service, new Object[] { port.getName(), endpoint });
                    portComponentRef.put(endpoint, new QName(port.getName()));
                }
            } catch (Exception e) {
                if (e instanceof InvocationTargetException) {
                    Throwable cause = e.getCause();
                    if (cause instanceof ThreadDeath) {
                        throw (ThreadDeath) cause;
                    }
                    if (cause instanceof VirtualMachineError) {
                        throw (VirtualMachineError) cause;
                    }
                }
                NamingException ex = new NamingException("Error while reading Wsdl File");
                ex.initCause(e);
                throw ex;
            }
        }
        ServiceProxy proxy = new ServiceProxy(service);
        // Use port-component-ref
        for (int i = 0; i < ref.size(); i++) if (ServiceRef.SERVICEENDPOINTINTERFACE.equals(ref.get(i).getType())) {
            String serviceendpoint = "";
            String portlink = "";
            serviceendpoint = (String) ref.get(i).getContent();
            if (ServiceRef.PORTCOMPONENTLINK.equals(ref.get(i + 1).getType())) {
                i++;
                portlink = (String) ref.get(i).getContent();
            }
            portComponentRef.put(serviceendpoint, new QName(portlink));
        }
        proxy.setPortComponentRef(portComponentRef);
        // Instantiate service with proxy class
        Class<?>[] interfaces = null;
        Class<?>[] serviceInterfaces = serviceInterfaceClass.getInterfaces();
        interfaces = new Class[serviceInterfaces.length + 1];
        for (int i = 0; i < serviceInterfaces.length; i++) {
            interfaces[i] = serviceInterfaces[i];
        }
        interfaces[interfaces.length - 1] = javax.xml.rpc.Service.class;
        Object proxyInstance = null;
        try {
            proxyInstance = Proxy.newProxyInstance(tcl, interfaces, proxy);
        } catch (IllegalArgumentException e) {
            proxyInstance = Proxy.newProxyInstance(tcl, serviceInterfaces, proxy);
        }
        // Use handler
        if (((ServiceRef) ref).getHandlersSize() > 0) {
            HandlerRegistry handlerRegistry = service.getHandlerRegistry();
            ArrayList<String> soaproles = new ArrayList<String>();
            while (((ServiceRef) ref).getHandlersSize() > 0) {
                HandlerRef handlerRef = ((ServiceRef) ref).getHandler();
                HandlerInfo handlerInfo = new HandlerInfo();
                // Loading handler Class
                tmp = handlerRef.get(HandlerRef.HANDLER_CLASS);
                if ((tmp == null) || (tmp.getContent() == null))
                    break;
                Class<?> handlerClass = null;
                try {
                    handlerClass = tcl.loadClass((String) tmp.getContent());
                } catch (ClassNotFoundException e) {
                    break;
                }
                // Load all datas relative to the handler : SOAPHeaders, config init element,
                // portNames to be set on
                ArrayList<QName> headers = new ArrayList<QName>();
                Hashtable<String, String> config = new Hashtable<String, String>();
                ArrayList<String> portNames = new ArrayList<String>();
                for (int i = 0; i < handlerRef.size(); i++) if (HandlerRef.HANDLER_LOCALPART.equals(handlerRef.get(i).getType())) {
                    String localpart = "";
                    String namespace = "";
                    localpart = (String) handlerRef.get(i).getContent();
                    if (HandlerRef.HANDLER_NAMESPACE.equals(handlerRef.get(i + 1).getType())) {
                        i++;
                        namespace = (String) handlerRef.get(i).getContent();
                    }
                    QName header = new QName(namespace, localpart);
                    headers.add(header);
                } else if (HandlerRef.HANDLER_PARAMNAME.equals(handlerRef.get(i).getType())) {
                    String paramName = "";
                    String paramValue = "";
                    paramName = (String) handlerRef.get(i).getContent();
                    if (HandlerRef.HANDLER_PARAMVALUE.equals(handlerRef.get(i + 1).getType())) {
                        i++;
                        paramValue = (String) handlerRef.get(i).getContent();
                    }
                    config.put(paramName, paramValue);
                } else if (HandlerRef.HANDLER_SOAPROLE.equals(handlerRef.get(i).getType())) {
                    String soaprole = "";
                    soaprole = (String) handlerRef.get(i).getContent();
                    soaproles.add(soaprole);
                } else if (HandlerRef.HANDLER_PORTNAME.equals(handlerRef.get(i).getType())) {
                    String portName = "";
                    portName = (String) handlerRef.get(i).getContent();
                    portNames.add(portName);
                }
                // Set the handlers informations
                handlerInfo.setHandlerClass(handlerClass);
                handlerInfo.setHeaders(headers.toArray(new QName[headers.size()]));
                handlerInfo.setHandlerConfig(config);
                if (!portNames.isEmpty()) {
                    Iterator<String> iter = portNames.iterator();
                    while (iter.hasNext()) initHandlerChain(new QName(iter.next()), handlerRegistry, handlerInfo, soaproles);
                } else {
                    Enumeration<QName> e = portComponentRef.elements();
                    while (e.hasMoreElements()) initHandlerChain(e.nextElement(), handlerRegistry, handlerInfo, soaproles);
                }
            }
        }
        return proxyInstance;
    }
    return null;
}
Also used : ServiceFactory(javax.xml.rpc.ServiceFactory) Port(javax.wsdl.Port) ArrayList(java.util.ArrayList) Properties(java.util.Properties) URL(java.net.URL) RefAddr(javax.naming.RefAddr) HandlerRegistry(javax.xml.rpc.handler.HandlerRegistry) NamingException(javax.naming.NamingException) HandlerRef(org.apache.naming.HandlerRef) HandlerInfo(javax.xml.rpc.handler.HandlerInfo) Reference(javax.naming.Reference) QName(javax.xml.namespace.QName) Hashtable(java.util.Hashtable) Definition(javax.wsdl.Definition) Service(javax.xml.rpc.Service) Method(java.lang.reflect.Method) NamingException(javax.naming.NamingException) InvocationTargetException(java.lang.reflect.InvocationTargetException) InvocationTargetException(java.lang.reflect.InvocationTargetException) WSDLFactory(javax.wsdl.factory.WSDLFactory) Service(javax.xml.rpc.Service) ServiceRef(org.apache.naming.ServiceRef) WSDLReader(javax.wsdl.xml.WSDLReader)

Example 78 with Reference

use of javax.naming.Reference in project tomcat70 by apache.

the class NamingContext method bind.

/**
 * 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);
        }
    }
}
Also used : InitialContext(javax.naming.InitialContext) Context(javax.naming.Context) NameAlreadyBoundException(javax.naming.NameAlreadyBoundException) Referenceable(javax.naming.Referenceable) NameNotFoundException(javax.naming.NameNotFoundException) Reference(javax.naming.Reference) NamingException(javax.naming.NamingException) LinkRef(javax.naming.LinkRef)

Example 79 with Reference

use of javax.naming.Reference in project tomcat70 by apache.

the class EjbFactory method getObjectInstance.

// ----------------------------------------------------------- Constructors
// -------------------------------------------------------------- Constants
// ----------------------------------------------------- Instance Variables
// --------------------------------------------------------- Public Methods
// -------------------------------------------------- ObjectFactory Methods
/**
 * Create a new EJB instance.
 *
 * @param obj The reference object describing the DataSource
 */
@Override
public Object getObjectInstance(Object obj, Name name, Context nameCtx, Hashtable<?, ?> environment) throws Exception {
    if (obj instanceof EjbRef) {
        Reference ref = (Reference) obj;
        // If ejb-link has been specified, resolving the link using JNDI
        RefAddr linkRefAddr = ref.get(EjbRef.LINK);
        if (linkRefAddr != null) {
            // Retrieving the EJB link
            String ejbLink = linkRefAddr.getContent().toString();
            Object beanObj = (new InitialContext()).lookup(ejbLink);
            /*
                String homeClassName = ref.getClassName();
                try {
                    Class home = Class.forName(homeClassName);
                    if (home.isInstance(beanObj)) {
                        System.out.println("Bean of type " 
                                           + beanObj.getClass().getName() 
                                           + " implements home interface " 
                                           + home.getName());
                    } else {
                        System.out.println("Bean of type " 
                                           + beanObj.getClass().getName() 
                                           + " doesn't implement home interface " 
                                           + home.getName());
                        throw new NamingException
                            ("Bean of type " + beanObj.getClass().getName() 
                             + " doesn't implement home interface " 
                             + home.getName());
                    }
                } catch (ClassNotFoundException e) {
                    System.out.println("Couldn't load home interface "
                                       + homeClassName);
                }
                */
            return beanObj;
        }
        ObjectFactory factory = null;
        RefAddr factoryRefAddr = ref.get(Constants.FACTORY);
        if (factoryRefAddr != null) {
            // Using the specified factory
            String factoryClassName = factoryRefAddr.getContent().toString();
            // Loading factory
            ClassLoader tcl = Thread.currentThread().getContextClassLoader();
            Class<?> factoryClass = null;
            if (tcl != null) {
                try {
                    factoryClass = tcl.loadClass(factoryClassName);
                } catch (ClassNotFoundException e) {
                    NamingException ex = new NamingException("Could not load resource factory class");
                    ex.initCause(e);
                    throw ex;
                }
            } else {
                try {
                    factoryClass = Class.forName(factoryClassName);
                } catch (ClassNotFoundException e) {
                    NamingException ex = new NamingException("Could not load resource factory class");
                    ex.initCause(e);
                    throw ex;
                }
            }
            if (factoryClass != null) {
                try {
                    factory = (ObjectFactory) factoryClass.newInstance();
                } catch (Throwable t) {
                    NamingException ex = new NamingException("Could not load resource factory class");
                    ex.initCause(t);
                    throw ex;
                }
            }
        } else {
            String javaxEjbFactoryClassName = System.getProperty("javax.ejb.Factory", Constants.OPENEJB_EJB_FACTORY);
            try {
                factory = (ObjectFactory) Class.forName(javaxEjbFactoryClassName).newInstance();
            } catch (Throwable t) {
                if (t instanceof NamingException)
                    throw (NamingException) t;
                NamingException ex = new NamingException("Could not create resource factory instance");
                ex.initCause(t);
                throw ex;
            }
        }
        if (factory != null) {
            return factory.getObjectInstance(obj, name, nameCtx, environment);
        } else {
            throw new NamingException("Cannot create resource instance");
        }
    }
    return null;
}
Also used : RefAddr(javax.naming.RefAddr) ObjectFactory(javax.naming.spi.ObjectFactory) Reference(javax.naming.Reference) EjbRef(org.apache.naming.EjbRef) NamingException(javax.naming.NamingException) InitialContext(javax.naming.InitialContext)

Example 80 with Reference

use of javax.naming.Reference in project tomcat70 by apache.

the class OpenEjbFactory method getObjectInstance.

// -------------------------------------------------- ObjectFactory Methods
/**
 * Create a new EJB instance using OpenEJB.
 *
 * @param obj The reference object describing the DataSource
 */
@Override
public Object getObjectInstance(Object obj, Name name, Context nameCtx, Hashtable<?, ?> environment) throws Exception {
    Object beanObj = null;
    if (obj instanceof EjbRef) {
        Reference ref = (Reference) obj;
        String factory = DEFAULT_OPENEJB_FACTORY;
        RefAddr factoryRefAddr = ref.get("openejb.factory");
        if (factoryRefAddr != null) {
            // Retrieving the OpenEJB factory
            factory = factoryRefAddr.getContent().toString();
        }
        Properties env = new Properties();
        env.put(Context.INITIAL_CONTEXT_FACTORY, factory);
        RefAddr linkRefAddr = ref.get("openejb.link");
        if (linkRefAddr != null) {
            String ejbLink = linkRefAddr.getContent().toString();
            beanObj = (new InitialContext(env)).lookup(ejbLink);
        }
    }
    return beanObj;
}
Also used : RefAddr(javax.naming.RefAddr) Reference(javax.naming.Reference) EjbRef(org.apache.naming.EjbRef) Properties(java.util.Properties) InitialContext(javax.naming.InitialContext)

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