Author: dblevins
Date: Thu Nov 29 19:26:28 2007
New Revision: 599690

URL: http://svn.apache.org/viewvc?rev=599690&view=rev
Log:
OPENEJB-668: Client JNDI Context.list method support

Added:
    
openejb/trunk/openejb3/server/openejb-client/src/main/java/org/apache/openejb/client/NameClassPairEnumeration.java
    
openejb/trunk/openejb3/server/openejb-ejbd/src/test/java/org/apache/openejb/JndiTest.java
Modified:
    
openejb/trunk/openejb3/server/openejb-client/src/main/java/org/apache/openejb/client/JNDIContext.java
    
openejb/trunk/openejb3/server/openejb-client/src/main/java/org/apache/openejb/client/JNDIResponse.java
    
openejb/trunk/openejb3/server/openejb-ejbd/src/main/java/org/apache/openejb/server/ejbd/JndiRequestHandler.java

Modified: 
openejb/trunk/openejb3/server/openejb-client/src/main/java/org/apache/openejb/client/JNDIContext.java
URL: 
http://svn.apache.org/viewvc/openejb/trunk/openejb3/server/openejb-client/src/main/java/org/apache/openejb/client/JNDIContext.java?rev=599690&r1=599689&r2=599690&view=diff
==============================================================================
--- 
openejb/trunk/openejb3/server/openejb-client/src/main/java/org/apache/openejb/client/JNDIContext.java
 (original)
+++ 
openejb/trunk/openejb3/server/openejb-client/src/main/java/org/apache/openejb/client/JNDIContext.java
 Thu Nov 29 19:26:28 2007
@@ -19,12 +19,22 @@
 import org.omg.CORBA.ORB;
 
 import java.io.Serializable;
+import java.io.Externalizable;
+import java.io.ObjectOutput;
+import java.io.IOException;
+import java.io.ObjectInput;
 import java.net.URI;
 import java.net.URISyntaxException;
 import java.net.ConnectException;
 import java.rmi.RemoteException;
 import java.util.Hashtable;
 import java.util.Properties;
+import java.util.Enumeration;
+import java.util.Vector;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Iterator;
+import java.util.Collections;
 import java.lang.reflect.Constructor;
 import javax.naming.AuthenticationException;
 import javax.naming.ConfigurationException;
@@ -37,6 +47,7 @@
 import javax.naming.NamingException;
 import javax.naming.OperationNotSupportedException;
 import javax.naming.ServiceUnavailableException;
+import javax.naming.NameClassPair;
 import javax.naming.spi.InitialContextFactory;
 import javax.sql.DataSource;
 
@@ -322,7 +333,49 @@
     }
 
     public NamingEnumeration list(String name) throws NamingException {
-        throw new OperationNotSupportedException("TODO: Needs to be 
implemented");
+        if (name == null) throw new InvalidNameException("The name cannot be 
null");
+        else if (name.startsWith("java:")) name = name.replaceFirst("^java:", 
"");
+        else if (!name.startsWith("/")) name = tail + name;
+
+        JNDIRequest req = new JNDIRequest(RequestMethodConstants.JNDI_LIST, 
name);
+        req.setModuleId(moduleId);
+
+        JNDIResponse res = null;
+        try {
+            res = request(req);
+        } catch (Exception e) {
+            if (e instanceof RemoteException && e.getCause() instanceof 
ConnectException) {
+                e = (Exception) e.getCause();
+                throw (ServiceUnavailableException) new 
ServiceUnavailableException("Cannot list '" + name + "'.").initCause(e);
+            }
+            throw (NamingException) new NamingException("Cannot list '" + name 
+ "'.").initCause(e);
+        }
+
+        switch (res.getResponseCode()) {
+
+            case ResponseCodes.JNDI_OK:
+                return null;
+
+            case ResponseCodes.JNDI_ENUMERATION:
+                return (NamingEnumeration) res.getResult();
+
+            case ResponseCodes.JNDI_NOT_FOUND:
+                throw new NameNotFoundException(name);
+
+            case ResponseCodes.JNDI_NAMING_EXCEPTION:
+                Throwable throwable = ((ThrowableArtifact) 
res.getResult()).getThrowable();
+                if (throwable instanceof NamingException) {
+                    throw (NamingException) throwable;
+                }
+                throw (NamingException) new 
NamingException().initCause(throwable);
+
+            case ResponseCodes.JNDI_ERROR:
+                throw (Error) res.getResult();
+
+            default:
+                throw new RuntimeException("Invalid response from server :" + 
res.getResponseCode());
+        }
+
     }
 
     public NamingEnumeration list(Name name) throws NamingException {
@@ -330,7 +383,7 @@
     }
 
     public NamingEnumeration listBindings(String name) throws NamingException {
-        throw new OperationNotSupportedException("TODO: Needs to be 
implemented");
+        throw new OperationNotSupportedException("Use list(String) instead");
     }
 
     public NamingEnumeration listBindings(Name name) throws NamingException {

Modified: 
openejb/trunk/openejb3/server/openejb-client/src/main/java/org/apache/openejb/client/JNDIResponse.java
URL: 
http://svn.apache.org/viewvc/openejb/trunk/openejb3/server/openejb-client/src/main/java/org/apache/openejb/client/JNDIResponse.java?rev=599690&r1=599689&r2=599690&view=diff
==============================================================================
--- 
openejb/trunk/openejb3/server/openejb-client/src/main/java/org/apache/openejb/client/JNDIResponse.java
 (original)
+++ 
openejb/trunk/openejb3/server/openejb-client/src/main/java/org/apache/openejb/client/JNDIResponse.java
 Thu Nov 29 19:26:28 2007
@@ -85,6 +85,11 @@
                 WsMetaData ws = (WsMetaData) in.readObject();
                 result = ws;
                 break;
+            case ResponseCodes.JNDI_ENUMERATION:
+                NameClassPairEnumeration ncpe = new NameClassPairEnumeration();
+                ncpe.readExternal(in);
+                result = ncpe;
+                break;
         }
     }
 
@@ -121,6 +126,10 @@
             case ResponseCodes.JNDI_WEBSERVICE:
                 WsMetaData ws = (WsMetaData) result;
                 out.writeObject(ws);
+                break;
+            case ResponseCodes.JNDI_ENUMERATION:
+                NameClassPairEnumeration ncpe = (NameClassPairEnumeration) 
result;
+                ncpe.writeExternal(out);
                 break;
         }
     }

Added: 
openejb/trunk/openejb3/server/openejb-client/src/main/java/org/apache/openejb/client/NameClassPairEnumeration.java
URL: 
http://svn.apache.org/viewvc/openejb/trunk/openejb3/server/openejb-client/src/main/java/org/apache/openejb/client/NameClassPairEnumeration.java?rev=599690&view=auto
==============================================================================
--- 
openejb/trunk/openejb3/server/openejb-client/src/main/java/org/apache/openejb/client/NameClassPairEnumeration.java
 (added)
+++ 
openejb/trunk/openejb3/server/openejb-client/src/main/java/org/apache/openejb/client/NameClassPairEnumeration.java
 Thu Nov 29 19:26:28 2007
@@ -0,0 +1,94 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ */
+package org.apache.openejb.client;
+
+import javax.naming.NameClassPair;
+import javax.naming.NamingEnumeration;
+import java.io.Externalizable;
+import java.io.ObjectOutput;
+import java.io.IOException;
+import java.io.ObjectInput;
+import java.util.List;
+import java.util.Iterator;
+import java.util.Collections;
+import java.util.ArrayList;
+
+/**
+ * The product of a javax.naming.Context.list() method
+ */
+public class NameClassPairEnumeration implements 
NamingEnumeration<NameClassPair>, Externalizable {
+
+    private List<NameClassPair> list;
+    private Iterator<NameClassPair> iterator;
+
+    public NameClassPairEnumeration(List<NameClassPair> list){
+        this.list = list;
+        this.iterator = list.iterator();
+    }
+
+    public NameClassPairEnumeration() {
+        list = Collections.EMPTY_LIST;
+        iterator = list.iterator();
+    }
+
+    public void close() {
+        iterator = null;
+    }
+
+    public boolean hasMore() {
+        return iterator.hasNext();
+    }
+
+    public boolean hasMoreElements() {
+        return hasMore();
+    }
+
+    public NameClassPair next() {
+        return iterator.next();
+    }
+
+    public NameClassPair nextElement() {
+        return next();
+    }
+
+    public void writeExternal(ObjectOutput out) throws IOException {
+        // write out the version of the serialized data for future use
+        out.writeByte(1);
+
+        out.writeInt(list.size());
+        for (NameClassPair pair : list) {
+            out.writeObject(pair.getName());
+            out.writeObject(pair.getClassName());
+        }
+    }
+
+    public void readExternal(ObjectInput in) throws IOException, 
ClassNotFoundException {
+        byte version = in.readByte(); // future use
+
+        int size = in.readInt();
+
+        list = new ArrayList<NameClassPair>(size);
+
+        for (; size > 0; size--) {
+            String name = (String) in.readObject();
+            String className = (String) in.readObject();
+            list.add(new NameClassPair(name, className));
+        }
+
+        iterator = list.iterator();
+    }
+}

Modified: 
openejb/trunk/openejb3/server/openejb-ejbd/src/main/java/org/apache/openejb/server/ejbd/JndiRequestHandler.java
URL: 
http://svn.apache.org/viewvc/openejb/trunk/openejb3/server/openejb-ejbd/src/main/java/org/apache/openejb/server/ejbd/JndiRequestHandler.java?rev=599690&r1=599689&r2=599690&view=diff
==============================================================================
--- 
openejb/trunk/openejb3/server/openejb-ejbd/src/main/java/org/apache/openejb/server/ejbd/JndiRequestHandler.java
 (original)
+++ 
openejb/trunk/openejb3/server/openejb-ejbd/src/main/java/org/apache/openejb/server/ejbd/JndiRequestHandler.java
 Thu Nov 29 19:26:28 2007
@@ -22,11 +22,15 @@
 import java.util.Set;
 import java.util.Map;
 import java.util.HashMap;
+import java.util.ArrayList;
+import java.util.Collections;
 import java.lang.reflect.Method;
 
 import javax.naming.Context;
 import javax.naming.NameNotFoundException;
 import javax.naming.NamingException;
+import javax.naming.NameClassPair;
+import javax.naming.NamingEnumeration;
 import javax.jms.ConnectionFactory;
 import javax.xml.namespace.QName;
 
@@ -37,6 +41,7 @@
 import org.apache.openejb.util.Logger;
 import org.apache.openejb.util.LogCategory;
 import org.apache.openejb.core.ivm.BaseEjbProxyHandler;
+import org.apache.openejb.core.ivm.naming.IvmContext;
 import org.apache.openejb.core.webservices.ServiceRefData;
 import org.apache.openejb.core.webservices.HandlerChainData;
 import org.apache.openejb.core.webservices.HandlerData;
@@ -57,6 +62,8 @@
 import org.apache.openejb.client.CallbackMetaData;
 import org.apache.openejb.client.PortRefMetaData;
 import org.apache.openejb.client.ThrowableArtifact;
+import org.apache.openejb.client.RequestMethodConstants;
+import org.apache.openejb.client.NameClassPairEnumeration;
 import org.apache.commons.dbcp.BasicDataSource;
 import org.omg.CORBA.ORB;
 
@@ -103,223 +110,262 @@
         }
 
         try {
-            String name = req.getRequestString();
-            if (name.startsWith("/")) name = name.substring(1);
+            if (req.getRequestString().startsWith("/")) {
+                req.setRequestString(req.getRequestString().substring(1));
+            }
+            Context context = getContext(req);
 
-            Object object;
-            try {
-                if (req.getModuleId() != null && 
req.getModuleId().equals("openejb/Deployment")){
+            switch(req.getRequestMethod()){
+                case RequestMethodConstants.JNDI_LOOKUP: doLookup(req, res, 
context); break;
+                case RequestMethodConstants.JNDI_LIST: doList(req, res, 
context); break;
+            }
 
-                    object = deploymentsJndiTree.lookup(name);
+        } catch (Throwable e) {
+            res.setResponseCode(ResponseCodes.JNDI_NAMING_EXCEPTION);
+            NamingException namingException = new NamingException("Unknown 
error in container");
+            namingException.setRootCause(e);
+            res.setResult(new ThrowableArtifact(namingException));
+        } finally {
 
-                } else if (req.getModuleId() != null && clientJndiTree != 
null) {
+            if (logger.isDebugEnabled()){
+                try {
+                    logger.debug("JNDI REQUEST: "+req+" -- RESPONSE: " + res);
+                } catch (Exception justInCase) {}
+            }
 
-                    Context moduleContext = (Context) 
clientJndiTree.lookup(req.getModuleId());
+            try {
+                res.writeExternal(out);
+            } catch (Throwable e) {
+                logger.fatal("Couldn't write JndiResponse to output stream", 
e);
+            }
+        }
+    }
 
-                    if (name.startsWith("comp/env/")) {
+    private Context getContext(JNDIRequest req) throws NamingException {
+        Context context;
+        if (req.getModuleId() != null && 
req.getModuleId().equals("openejb/Deployment")){
+            context = deploymentsJndiTree;
+        } else if (req.getModuleId() != null && clientJndiTree != null) {
+            context = (Context) clientJndiTree.lookup(req.getModuleId());
+        } else {
+            context = ejbJndiTree;
+        }
+        return context;
+    }
 
-                        Context ctx = (Context) moduleContext.lookup("comp");
-                        ctx = (Context) ctx.lookup("env");
-                        name = name.replaceFirst("comp/env/", "");
-                        object = ctx.lookup(name);
+    private void doLookup(JNDIRequest req, JNDIResponse res, Context context) {
+        Object object;
+        String name = req.getRequestString();
 
-                    } else if (name.equals("comp/injections")) {
+        try {
 
-                        //noinspection unchecked
-                        List<Injection> injections = (List<Injection>) 
moduleContext.lookup(name);
-                        InjectionMetaData metaData = new InjectionMetaData();
-                        for (Injection injection : injections) {
-                            
metaData.addInjection(injection.getTarget().getName(), injection.getName(), 
injection.getJndiName());
-                        }
-                        res.setResponseCode(ResponseCodes.JNDI_INJECTIONS);
-                        res.setResult(metaData);
-                        return;
-                    } else {
-                        object = moduleContext.lookup(name);
-                    }
+            if (name.equals("comp/injections")) {
 
-                } else {
-                    object = ejbJndiTree.lookup(name);
+                //noinspection unchecked
+                List<Injection> injections = (List<Injection>) 
context.lookup(name);
+                InjectionMetaData metaData = new InjectionMetaData();
+                for (Injection injection : injections) {
+                    metaData.addInjection(injection.getTarget().getName(), 
injection.getName(), injection.getJndiName());
                 }
+                res.setResponseCode(ResponseCodes.JNDI_INJECTIONS);
+                res.setResult(metaData);
+                return;
+            } else {
+                object = context.lookup(name);
+            }
 
-                if (object instanceof Context) {
-                    res.setResponseCode(ResponseCodes.JNDI_CONTEXT);
-                    return;
-                } else if (object == null) {
-                    throw new NullPointerException("lookup of '"+name+"' 
returned null");
-                } else if (object instanceof BasicDataSource){
-                    BasicDataSource cf = (BasicDataSource) object;
-                    DataSourceMetaData dataSourceMetaData = new 
DataSourceMetaData(cf.getDriverClassName(), cf.getUrl(), cf.getUsername(), 
cf.getPassword());
-                    res.setResponseCode(ResponseCodes.JNDI_DATA_SOURCE);
-                    res.setResult(dataSourceMetaData);
-                    return;
-                } else if (object instanceof ConnectionFactory){
-                    res.setResponseCode(ResponseCodes.JNDI_RESOURCE);
-                    res.setResult(ConnectionFactory.class.getName());
-                    return;
-                } else if (object instanceof ORB){
-                    res.setResponseCode(ResponseCodes.JNDI_RESOURCE);
-                    res.setResult(ORB.class.getName());
-                    return;
-                }
-
-                ServiceRefData serviceRef;
-                if (object instanceof ServiceRefData) {
-                    serviceRef = (ServiceRefData) object;
-                } else {
-                    serviceRef = ServiceRefData.getServiceRefData(object);
-                }
+            if (object instanceof Context) {
+                res.setResponseCode(ResponseCodes.JNDI_CONTEXT);
+                return;
+            } else if (object == null) {
+                throw new NullPointerException("lookup of '"+name+"' returned 
null");
+            } else if (object instanceof BasicDataSource){
+                BasicDataSource cf = (BasicDataSource) object;
+                DataSourceMetaData dataSourceMetaData = new 
DataSourceMetaData(cf.getDriverClassName(), cf.getUrl(), cf.getUsername(), 
cf.getPassword());
+                res.setResponseCode(ResponseCodes.JNDI_DATA_SOURCE);
+                res.setResult(dataSourceMetaData);
+                return;
+            } else if (object instanceof ConnectionFactory){
+                res.setResponseCode(ResponseCodes.JNDI_RESOURCE);
+                res.setResult(ConnectionFactory.class.getName());
+                return;
+            } else if (object instanceof ORB){
+                res.setResponseCode(ResponseCodes.JNDI_RESOURCE);
+                res.setResult(ORB.class.getName());
+                return;
+            }
 
-                if (serviceRef != null) {
-                    WsMetaData serviceMetaData = new WsMetaData();
+            ServiceRefData serviceRef;
+            if (object instanceof ServiceRefData) {
+                serviceRef = (ServiceRefData) object;
+            } else {
+                serviceRef = ServiceRefData.getServiceRefData(object);
+            }
 
-                    // service class
-                    String serviceClassName = null;
-                    if (serviceRef.getServiceClass() != null) {
-                        serviceClassName = 
serviceRef.getServiceClass().getName();
-                    }
-                    serviceMetaData.setServiceClassName(serviceClassName);
+            if (serviceRef != null) {
+                WsMetaData serviceMetaData = new WsMetaData();
 
-                    // reference class
-                    String referenceClassName = null;
-                    if (serviceRef.getReferenceClass() != null) {
-                        referenceClassName = 
serviceRef.getReferenceClass().getName();
-                    }
-                    serviceMetaData.setReferenceClassName(referenceClassName);
+                // service class
+                String serviceClassName = null;
+                if (serviceRef.getServiceClass() != null) {
+                    serviceClassName = serviceRef.getServiceClass().getName();
+                }
+                serviceMetaData.setServiceClassName(serviceClassName);
 
-                    // set service qname
-                    if (serviceRef.getServiceQName() != null) {
-                        
serviceMetaData.setServiceQName(serviceRef.getServiceQName().toString());
-                    }
+                // reference class
+                String referenceClassName = null;
+                if (serviceRef.getReferenceClass() != null) {
+                    referenceClassName = 
serviceRef.getReferenceClass().getName();
+                }
+                serviceMetaData.setReferenceClassName(referenceClassName);
 
-                    // get the port addresses for this service
-                    PortAddressRegistry portAddressRegistry = 
SystemInstance.get().getComponent(PortAddressRegistry.class);
-                    Set<PortAddress> portAddresses = null;
-                    if (portAddressRegistry != null) {
-                        portAddresses = 
portAddressRegistry.getPorts(serviceRef.getId(), serviceRef.getServiceQName(), 
referenceClassName);
-                    }
+                // set service qname
+                if (serviceRef.getServiceQName() != null) {
+                    
serviceMetaData.setServiceQName(serviceRef.getServiceQName().toString());
+                }
 
-                    // resolve the wsdl url
-                    if (serviceRef.getWsdlURL() != null) {
-                        
serviceMetaData.setWsdlUrl(serviceRef.getWsdlURL().toExternalForm());
-                    }
-                    if (portAddresses.size() == 1) {
-                        PortAddress portAddress = 
portAddresses.iterator().next();
-                        serviceMetaData.setWsdlUrl(portAddress.getAddress() + 
"?wsdl");
-                    }
+                // get the port addresses for this service
+                PortAddressRegistry portAddressRegistry = 
SystemInstance.get().getComponent(PortAddressRegistry.class);
+                Set<PortAddress> portAddresses = null;
+                if (portAddressRegistry != null) {
+                    portAddresses = 
portAddressRegistry.getPorts(serviceRef.getId(), serviceRef.getServiceQName(), 
referenceClassName);
+                }
 
-                    // add handler chains
-                    for (HandlerChainData handlerChain : 
serviceRef.getHandlerChains()) {
-                        HandlerChainMetaData handlerChainMetaData = new 
HandlerChainMetaData();
-                        
handlerChainMetaData.setServiceNamePattern(handlerChain.getServiceNamePattern());
-                        
handlerChainMetaData.setPortNamePattern(handlerChain.getPortNamePattern());
-                        
handlerChainMetaData.getProtocolBindings().addAll(handlerChain.getProtocolBindings());
-                        for (HandlerData handler : handlerChain.getHandlers()) 
{
-                            HandlerMetaData handlerMetaData = new 
HandlerMetaData();
-                            
handlerMetaData.setHandlerClass(handler.getHandlerClass().getName());
-                            for (Method method : handler.getPostConstruct()) {
-                                CallbackMetaData callbackMetaData = new 
CallbackMetaData();
-                                
callbackMetaData.setClassName(method.getDeclaringClass().getName());
-                                callbackMetaData.setMethod(method.getName());
-                                
handlerMetaData.getPostConstruct().add(callbackMetaData);
-                            }
-                            for (Method method : handler.getPreDestroy()) {
-                                CallbackMetaData callbackMetaData = new 
CallbackMetaData();
-                                
callbackMetaData.setClassName(method.getDeclaringClass().getName());
-                                callbackMetaData.setMethod(method.getName());
-                                
handlerMetaData.getPreDestroy().add(callbackMetaData);
-                            }
-                            
handlerChainMetaData.getHandlers().add(handlerMetaData);
+                // resolve the wsdl url
+                if (serviceRef.getWsdlURL() != null) {
+                    
serviceMetaData.setWsdlUrl(serviceRef.getWsdlURL().toExternalForm());
+                }
+                if (portAddresses.size() == 1) {
+                    PortAddress portAddress = portAddresses.iterator().next();
+                    serviceMetaData.setWsdlUrl(portAddress.getAddress() + 
"?wsdl");
+                }
+
+                // add handler chains
+                for (HandlerChainData handlerChain : 
serviceRef.getHandlerChains()) {
+                    HandlerChainMetaData handlerChainMetaData = new 
HandlerChainMetaData();
+                    
handlerChainMetaData.setServiceNamePattern(handlerChain.getServiceNamePattern());
+                    
handlerChainMetaData.setPortNamePattern(handlerChain.getPortNamePattern());
+                    
handlerChainMetaData.getProtocolBindings().addAll(handlerChain.getProtocolBindings());
+                    for (HandlerData handler : handlerChain.getHandlers()) {
+                        HandlerMetaData handlerMetaData = new 
HandlerMetaData();
+                        
handlerMetaData.setHandlerClass(handler.getHandlerClass().getName());
+                        for (Method method : handler.getPostConstruct()) {
+                            CallbackMetaData callbackMetaData = new 
CallbackMetaData();
+                            
callbackMetaData.setClassName(method.getDeclaringClass().getName());
+                            callbackMetaData.setMethod(method.getName());
+                            
handlerMetaData.getPostConstruct().add(callbackMetaData);
+                        }
+                        for (Method method : handler.getPreDestroy()) {
+                            CallbackMetaData callbackMetaData = new 
CallbackMetaData();
+                            
callbackMetaData.setClassName(method.getDeclaringClass().getName());
+                            callbackMetaData.setMethod(method.getName());
+                            
handlerMetaData.getPreDestroy().add(callbackMetaData);
                         }
-                        
serviceMetaData.getHandlerChains().add(handlerChainMetaData);
+                        
handlerChainMetaData.getHandlers().add(handlerMetaData);
                     }
+                    
serviceMetaData.getHandlerChains().add(handlerChainMetaData);
+                }
 
-                    // add port refs
-                    Map<QName,PortRefMetaData> portsByQName = new 
HashMap<QName,PortRefMetaData>();
-                    for (PortRefData portRef : serviceRef.getPortRefs()) {
-                        PortRefMetaData portRefMetaData = new 
PortRefMetaData();
-                        portRefMetaData.setQName(portRef.getQName());
-                        
portRefMetaData.setServiceEndpointInterface(portRef.getServiceEndpointInterface());
-                        portRefMetaData.setEnableMtom(portRef.isEnableMtom());
-                        
portRefMetaData.getProperties().putAll(portRef.getProperties());
-                        
portRefMetaData.getAddresses().addAll(portRef.getAddresses());
-                        if (portRef.getQName() != null) {
-                            portsByQName.put(portRef.getQName(), 
portRefMetaData);
-                        }
-                        serviceMetaData.getPortRefs().add(portRefMetaData);
+                // add port refs
+                Map<QName,PortRefMetaData> portsByQName = new 
HashMap<QName,PortRefMetaData>();
+                for (PortRefData portRef : serviceRef.getPortRefs()) {
+                    PortRefMetaData portRefMetaData = new PortRefMetaData();
+                    portRefMetaData.setQName(portRef.getQName());
+                    
portRefMetaData.setServiceEndpointInterface(portRef.getServiceEndpointInterface());
+                    portRefMetaData.setEnableMtom(portRef.isEnableMtom());
+                    
portRefMetaData.getProperties().putAll(portRef.getProperties());
+                    
portRefMetaData.getAddresses().addAll(portRef.getAddresses());
+                    if (portRef.getQName() != null) {
+                        portsByQName.put(portRef.getQName(), portRefMetaData);
                     }
+                    serviceMetaData.getPortRefs().add(portRefMetaData);
+                }
 
-                    // add PortRefMetaData for any portAddress not added above
-                    for (PortAddress portAddress : portAddresses) {
-                        PortRefMetaData portRefMetaData = 
portsByQName.get(portAddress.getPortQName());
-                        if (portRefMetaData == null) {
-                            portRefMetaData = new PortRefMetaData();
-                            
portRefMetaData.setQName(portAddress.getPortQName());
+                // add PortRefMetaData for any portAddress not added above
+                for (PortAddress portAddress : portAddresses) {
+                    PortRefMetaData portRefMetaData = 
portsByQName.get(portAddress.getPortQName());
+                    if (portRefMetaData == null) {
+                        portRefMetaData = new PortRefMetaData();
+                        portRefMetaData.setQName(portAddress.getPortQName());
+                        
portRefMetaData.setServiceEndpointInterface(portAddress.getServiceEndpointInterface());
+                        
portRefMetaData.getAddresses().add(portAddress.getAddress());
+                        serviceMetaData.getPortRefs().add(portRefMetaData);
+                    } else {
+                        
portRefMetaData.getAddresses().add(portAddress.getAddress());
+                        if (portRefMetaData.getServiceEndpointInterface() == 
null) {
                             
portRefMetaData.setServiceEndpointInterface(portAddress.getServiceEndpointInterface());
-                            
portRefMetaData.getAddresses().add(portAddress.getAddress());
-                            serviceMetaData.getPortRefs().add(portRefMetaData);
-                        } else {
-                            
portRefMetaData.getAddresses().add(portAddress.getAddress());
-                            if (portRefMetaData.getServiceEndpointInterface() 
== null) {
-                                
portRefMetaData.setServiceEndpointInterface(portAddress.getServiceEndpointInterface());
-                            }
                         }
                     }
-
-                    res.setResponseCode(ResponseCodes.JNDI_WEBSERVICE);
-                    res.setResult(serviceMetaData);
-                    return;
                 }
-            } catch (NameNotFoundException e) {
-                res.setResponseCode(ResponseCodes.JNDI_NOT_FOUND);
-                return;
-            } catch (NamingException e) {
-                res.setResponseCode(ResponseCodes.JNDI_NAMING_EXCEPTION);
-                res.setResult(new ThrowableArtifact(e));
+
+                res.setResponseCode(ResponseCodes.JNDI_WEBSERVICE);
+                res.setResult(serviceMetaData);
                 return;
             }
+        } catch (NameNotFoundException e) {
+            res.setResponseCode(ResponseCodes.JNDI_NOT_FOUND);
+            return;
+        } catch (NamingException e) {
+            res.setResponseCode(ResponseCodes.JNDI_NAMING_EXCEPTION);
+            res.setResult(new ThrowableArtifact(e));
+            return;
+        }
 
 
-            BaseEjbProxyHandler handler;
-            try {
-                handler = (BaseEjbProxyHandler) 
ProxyManager.getInvocationHandler(object);
-            } catch (Exception e) {
-                // Not a proxy.  See if it's serializable and send it
-                if (object instanceof java.io.Serializable){
-                    res.setResponseCode(ResponseCodes.JNDI_OK);
-                    res.setResult(object);
-                    return;
-                } else {
-                    res.setResponseCode(ResponseCodes.JNDI_NAMING_EXCEPTION);
-                    NamingException namingException = new 
NamingException("Expected an ejb proxy, found unknown object: type=" + 
object.getClass().getName() + ", toString=" + object);
-                    res.setResult(new ThrowableArtifact(namingException));
-                    return;
-                }
+        BaseEjbProxyHandler handler;
+        try {
+            handler = (BaseEjbProxyHandler) 
ProxyManager.getInvocationHandler(object);
+        } catch (Exception e) {
+            // Not a proxy.  See if it's serializable and send it
+            if (object instanceof java.io.Serializable){
+                res.setResponseCode(ResponseCodes.JNDI_OK);
+                res.setResult(object);
+                return;
+            } else {
+                res.setResponseCode(ResponseCodes.JNDI_NAMING_EXCEPTION);
+                NamingException namingException = new 
NamingException("Expected an ejb proxy, found unknown object: type=" + 
object.getClass().getName() + ", toString=" + object);
+                res.setResult(new ThrowableArtifact(namingException));
+                return;
             }
+        }
 
-            ProxyInfo proxyInfo = handler.getProxyInfo();
-            DeploymentInfo deployment = proxyInfo.getDeploymentInfo();
-            String deploymentID = deployment.getDeploymentID().toString();
-
-            switch(proxyInfo.getInterfaceType()){
-                case EJB_HOME: {
-                    res.setResponseCode(ResponseCodes.JNDI_EJBHOME);
-                    EJBMetaDataImpl metaData = new 
EJBMetaDataImpl(deployment.getHomeInterface(),
-                            deployment.getRemoteInterface(),
-                            deployment.getPrimaryKeyClass(),
-                            deployment.getComponentType().toString(),
-                            deploymentID,
-                            -1, null);
-                    res.setResult(metaData);
-                    break;
-                }
-                case EJB_LOCAL_HOME: {
-                    res.setResponseCode(ResponseCodes.JNDI_NAMING_EXCEPTION);
-                    NamingException namingException = new NamingException("Not 
remotable: '" + name + "'. EJBLocalHome interfaces are not remotable as per the 
EJB specification.");
-                    res.setResult(new ThrowableArtifact(namingException));
-                    break;
-                }
-                case BUSINESS_REMOTE: {
+        ProxyInfo proxyInfo = handler.getProxyInfo();
+        DeploymentInfo deployment = proxyInfo.getDeploymentInfo();
+        String deploymentID = deployment.getDeploymentID().toString();
+
+        switch(proxyInfo.getInterfaceType()){
+            case EJB_HOME: {
+                res.setResponseCode(ResponseCodes.JNDI_EJBHOME);
+                EJBMetaDataImpl metaData = new 
EJBMetaDataImpl(deployment.getHomeInterface(),
+                        deployment.getRemoteInterface(),
+                        deployment.getPrimaryKeyClass(),
+                        deployment.getComponentType().toString(),
+                        deploymentID,
+                        -1, null);
+                res.setResult(metaData);
+                break;
+            }
+            case EJB_LOCAL_HOME: {
+                res.setResponseCode(ResponseCodes.JNDI_NAMING_EXCEPTION);
+                NamingException namingException = new NamingException("Not 
remotable: '" + name + "'. EJBLocalHome interfaces are not remotable as per the 
EJB specification.");
+                res.setResult(new ThrowableArtifact(namingException));
+                break;
+            }
+            case BUSINESS_REMOTE: {
+                res.setResponseCode(ResponseCodes.JNDI_BUSINESS_OBJECT);
+                EJBMetaDataImpl metaData = new EJBMetaDataImpl(null,
+                        null,
+                        deployment.getPrimaryKeyClass(),
+                        deployment.getComponentType().toString(),
+                        deploymentID,
+                        -1, proxyInfo.getInterfaces());
+                metaData.setPrimaryKey(proxyInfo.getPrimaryKey());
+                res.setResult(metaData);
+                break;
+            }
+            case BUSINESS_LOCAL: {
+                String property = 
SystemInstance.get().getProperty("openejb.remotable.businessLocals", "false");
+                if (property.equalsIgnoreCase("true")) {
                     res.setResponseCode(ResponseCodes.JNDI_BUSINESS_OBJECT);
                     EJBMetaDataImpl metaData = new EJBMetaDataImpl(null,
                             null,
@@ -329,51 +375,47 @@
                             -1, proxyInfo.getInterfaces());
                     metaData.setPrimaryKey(proxyInfo.getPrimaryKey());
                     res.setResult(metaData);
-                    break;
-                }
-                case BUSINESS_LOCAL: {
-                    String property = 
SystemInstance.get().getProperty("openejb.remotable.businessLocals", "false");
-                    if (property.equalsIgnoreCase("true")) {
-                        
res.setResponseCode(ResponseCodes.JNDI_BUSINESS_OBJECT);
-                        EJBMetaDataImpl metaData = new EJBMetaDataImpl(null,
-                                null,
-                                deployment.getPrimaryKeyClass(),
-                                deployment.getComponentType().toString(),
-                                deploymentID,
-                                -1, proxyInfo.getInterfaces());
-                        metaData.setPrimaryKey(proxyInfo.getPrimaryKey());
-                        res.setResult(metaData);
-                    } else {
-                        
res.setResponseCode(ResponseCodes.JNDI_NAMING_EXCEPTION);
-                        NamingException namingException = new 
NamingException("Not remotable: '" + name + "'. Business Local interfaces are 
not remotable as per the EJB specification.  To disable this restriction, set 
the system property 'openejb.remotable.businessLocals=true' in the server.");
-                        res.setResult(new ThrowableArtifact(namingException));
-                    }
-                    break;
-                }
-                default: {
+                } else {
                     res.setResponseCode(ResponseCodes.JNDI_NAMING_EXCEPTION);
-                    NamingException namingException = new NamingException("Not 
remotable: '" + name + "'.");
+                    NamingException namingException = new NamingException("Not 
remotable: '" + name + "'. Business Local interfaces are not remotable as per 
the EJB specification.  To disable this restriction, set the system property 
'openejb.remotable.businessLocals=true' in the server.");
                     res.setResult(new ThrowableArtifact(namingException));
                 }
+                break;
             }
-        } catch (Throwable e) {
-            res.setResponseCode(ResponseCodes.JNDI_NAMING_EXCEPTION);
-            NamingException namingException = new NamingException("Unknown 
error in container");
-            namingException.setRootCause(e);
-            res.setResult(new ThrowableArtifact(namingException));
-        } finally {
-
-            if (logger.isDebugEnabled()){
-                try {
-                    logger.debug("JNDI REQUEST: "+req+" -- RESPONSE: " + res);
-                } catch (Exception justInCase) {}
+            default: {
+                res.setResponseCode(ResponseCodes.JNDI_NAMING_EXCEPTION);
+                NamingException namingException = new NamingException("Not 
remotable: '" + name + "'.");
+                res.setResult(new ThrowableArtifact(namingException));
             }
+        }
 
-            try {
-                res.writeExternal(out);
-            } catch (Throwable e) {
-                logger.fatal("Couldn't write JndiResponse to output stream", 
e);
+    }
+
+    private void doList(JNDIRequest req, JNDIResponse res, Context context) {
+        String name = req.getRequestString();
+        try {
+            NamingEnumeration<NameClassPair> namingEnumeration = 
context.list(name);
+            if (namingEnumeration == null){
+                res.setResponseCode(ResponseCodes.JNDI_OK);
+                res.setResult(null);
+            } else {
+                res.setResponseCode(ResponseCodes.JNDI_ENUMERATION);
+                ArrayList<NameClassPair> list = 
Collections.list(namingEnumeration);
+                for (NameClassPair pair : list) {
+                    if 
(pair.getClassName().equals(IvmContext.class.getName())){
+                        
pair.setClassName(javax.naming.Context.class.getName());
+                    }
+                }
+                res.setResult(new NameClassPairEnumeration(list));
             }
+        } catch (NameNotFoundException e) {
+            res.setResponseCode(ResponseCodes.JNDI_NOT_FOUND);
+            return;
+        } catch (NamingException e) {
+            res.setResponseCode(ResponseCodes.JNDI_NAMING_EXCEPTION);
+            res.setResult(new ThrowableArtifact(e));
+            return;
         }
     }
+
 }

Added: 
openejb/trunk/openejb3/server/openejb-ejbd/src/test/java/org/apache/openejb/JndiTest.java
URL: 
http://svn.apache.org/viewvc/openejb/trunk/openejb3/server/openejb-ejbd/src/test/java/org/apache/openejb/JndiTest.java?rev=599690&view=auto
==============================================================================
--- 
openejb/trunk/openejb3/server/openejb-ejbd/src/test/java/org/apache/openejb/JndiTest.java
 (added)
+++ 
openejb/trunk/openejb3/server/openejb-ejbd/src/test/java/org/apache/openejb/JndiTest.java
 Thu Nov 29 19:26:28 2007
@@ -0,0 +1,115 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ */
+package org.apache.openejb;
+
+import junit.framework.TestCase;
+import org.apache.openejb.server.ejbd.EjbServer;
+import org.apache.openejb.server.ServiceDaemon;
+import org.apache.openejb.core.ServerFederation;
+import org.apache.openejb.loader.SystemInstance;
+import org.apache.openejb.assembler.classic.Assembler;
+import org.apache.openejb.config.ConfigurationFactory;
+import org.apache.openejb.jee.EjbJar;
+import org.apache.openejb.jee.StatelessBean;
+
+import javax.naming.Context;
+import javax.naming.InitialContext;
+import javax.naming.NamingException;
+import javax.naming.NameClassPair;
+import javax.naming.NamingEnumeration;
+import javax.ejb.Remote;
+import java.util.Properties;
+import java.util.Collections;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * @version $Rev$ $Date$
+ */
+public class JndiTest extends TestCase {
+    public void test() throws Exception {
+        EjbServer ejbServer = new EjbServer();
+
+        Properties initProps = new Properties();
+        initProps.setProperty("openejb.deployments.classpath.include", "");
+        
initProps.setProperty("openejb.deployments.classpath.filter.descriptors", 
"true");
+        OpenEJB.init(initProps, new ServerFederation());
+        ejbServer.init(new Properties());
+
+        ServiceDaemon serviceDaemon = new ServiceDaemon(ejbServer, 0, 
"localhost");
+        serviceDaemon.start();
+
+        int port = serviceDaemon.getPort();
+
+        Assembler assembler = 
SystemInstance.get().getComponent(Assembler.class);
+        ConfigurationFactory config = new ConfigurationFactory();
+
+        EjbJar ejbJar = new EjbJar();
+        ejbJar.addEnterpriseBean(new StatelessBean("Orange", Fruit.class));
+        ejbJar.addEnterpriseBean(new StatelessBean("Apple", Fruit.class));
+        ejbJar.addEnterpriseBean(new StatelessBean("Peach", Fruit.class));
+        ejbJar.addEnterpriseBean(new StatelessBean("Pear", Fruit.class));
+        ejbJar.addEnterpriseBean(new StatelessBean("Plum", Fruit.class));
+        ejbJar.addEnterpriseBean(new StatelessBean("ejb/Orange", Fruit.class));
+        ejbJar.addEnterpriseBean(new StatelessBean("ejb/Apple", Fruit.class));
+        ejbJar.addEnterpriseBean(new StatelessBean("ejb/Peach", Fruit.class));
+        ejbJar.addEnterpriseBean(new StatelessBean("ejb/Pear", Fruit.class));
+        ejbJar.addEnterpriseBean(new StatelessBean("ejb/Plum", Fruit.class));
+        assembler.createApplication(config.configureApplication(ejbJar));
+
+        try {
+
+            // good creds
+            Properties props = new Properties();
+            props.put("java.naming.factory.initial", 
"org.apache.openejb.client.RemoteInitialContextFactory");
+            props.put("java.naming.provider.url", "ejbd://127.0.0.1:" + port);
+            Context context = new InitialContext(props);
+
+            assertNameClassPair(context.list(""));
+            assertNameClassPair(context.list("ejb"));
+
+        } finally {
+            serviceDaemon.stop();
+            OpenEJB.destroy();
+        }
+
+    }
+
+    private void assertNameClassPair(NamingEnumeration<NameClassPair> 
namingEnumeration) {
+        assertNotNull("namingEnumeration", namingEnumeration);
+
+        Map<String, String> map = new HashMap<String, String>();
+        while (namingEnumeration.hasMoreElements()) {
+            NameClassPair pair = namingEnumeration.nextElement();
+            map.put(pair.getName(), pair.getClassName());
+        }
+
+        assertTrue("OrangeRemote", map.containsKey("OrangeRemote"));
+        assertTrue("AppleRemote", map.containsKey("AppleRemote"));
+        assertTrue("PeachRemote", map.containsKey("PeachRemote"));
+        assertTrue("PearRemote", map.containsKey("PearRemote"));
+        assertTrue("PlumRemote", map.containsKey("PlumRemote"));
+    }
+
+    @Remote
+    public static interface FruitRemote {
+    }
+
+    public static class Fruit implements FruitRemote {
+    }
+}


Reply via email to