All tests pass (with some commented out to get a clean compile)
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/proxy/branches/version-2.0-work@965009 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/core/src/main/java/org/apache/commons/proxy/provider/remoting/JaxRpcProvider.java b/core/src/main/java/org/apache/commons/proxy/provider/remoting/JaxRpcProvider.java
new file mode 100644
index 0000000..014c363
--- /dev/null
+++ b/core/src/main/java/org/apache/commons/proxy/provider/remoting/JaxRpcProvider.java
@@ -0,0 +1,171 @@
+/*
+ * 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.commons.proxy.provider.remoting;
+
+import org.apache.commons.proxy.ObjectProvider;
+import org.apache.commons.proxy.exception.ObjectProviderException;
+
+import javax.xml.namespace.QName;
+import javax.xml.rpc.Service;
+import javax.xml.rpc.ServiceException;
+import javax.xml.rpc.ServiceFactory;
+import java.net.MalformedURLException;
+import java.net.URL;
+
+/**
+ * Returns a proxy for a JAX-RPC-based service.
+ * <p/>
+ * <p>
+ * <b>Dependencies</b>:
+ * <ul>
+ * <li>A JAX-RPC implementation</li>
+ * </ul>
+ * </p>
+ *
+ * @author James Carman
+ * @since 1.0
+ */
+public class JaxRpcProvider<T> implements ObjectProvider<T>
+{
+//**********************************************************************************************************************
+// Fields
+//**********************************************************************************************************************
+
+ private Class<T> serviceInterface;
+ private String wsdlUrl;
+ private String serviceNamespaceUri;
+ private String serviceLocalPart;
+ private String servicePrefix;
+ private String portNamespaceUri;
+ private String portLocalPart;
+ private String portPrefix;
+
+//**********************************************************************************************************************
+// Constructors
+//**********************************************************************************************************************
+
+ public JaxRpcProvider()
+ {
+ }
+
+ public JaxRpcProvider( Class<T> serviceInterface )
+ {
+ this.serviceInterface = serviceInterface;
+ }
+
+//**********************************************************************************************************************
+// ObjectProvider Implementation
+//**********************************************************************************************************************
+
+ public T getObject()
+ {
+ try
+ {
+ final Service service = ( wsdlUrl == null ?
+ ServiceFactory.newInstance().createService(getServiceQName()) : ServiceFactory
+ .newInstance().createService(new URL(wsdlUrl), getServiceQName()) );
+ final QName portQName = getPortQName();
+ return serviceInterface.cast(portQName == null ? service.getPort(serviceInterface) :
+ service.getPort(portQName, serviceInterface));
+ }
+ catch( ServiceException e )
+ {
+ throw new ObjectProviderException("Unable to create JAX-RPC service proxy.", e);
+ }
+ catch( MalformedURLException e )
+ {
+ throw new ObjectProviderException("Invalid URL given.", e);
+ }
+ }
+
+//**********************************************************************************************************************
+// Getter/Setter Methods
+//**********************************************************************************************************************
+
+ public void setPortLocalPart( String portLocalPart )
+ {
+ this.portLocalPart = portLocalPart;
+ }
+
+ public void setPortNamespaceUri( String portNamespaceUri )
+ {
+ this.portNamespaceUri = portNamespaceUri;
+ }
+
+ public void setPortPrefix( String portPrefix )
+ {
+ this.portPrefix = portPrefix;
+ }
+
+ public void setServiceInterface( Class<T> serviceInterface )
+ {
+ this.serviceInterface = serviceInterface;
+ }
+
+ public void setServiceLocalPart( String serviceLocalPart )
+ {
+ this.serviceLocalPart = serviceLocalPart;
+ }
+
+ public void setServiceNamespaceUri( String serviceNamespaceUri )
+ {
+ this.serviceNamespaceUri = serviceNamespaceUri;
+ }
+
+ public void setServicePrefix( String servicePrefix )
+ {
+ this.servicePrefix = servicePrefix;
+ }
+
+ public void setWsdlUrl( String wsdlUrl )
+ {
+ this.wsdlUrl = wsdlUrl;
+ }
+
+//**********************************************************************************************************************
+// Other Methods
+//**********************************************************************************************************************
+
+ private QName getPortQName()
+ {
+ return getQName(portNamespaceUri, portLocalPart, portPrefix);
+ }
+
+ private QName getQName( String namespaceUri, String localPart, String prefix )
+ {
+ if( namespaceUri != null && localPart != null && prefix != null )
+ {
+ return new QName(namespaceUri, localPart, prefix);
+ }
+ else if( namespaceUri != null && localPart != null )
+ {
+ return new QName(namespaceUri, localPart);
+ }
+ else if( localPart != null )
+ {
+ return new QName(localPart);
+ }
+ return null;
+ }
+
+ private QName getServiceQName()
+ {
+ return getQName(serviceNamespaceUri, serviceLocalPart, servicePrefix);
+ }
+}
+
diff --git a/core/src/main/java/org/apache/commons/proxy/provider/remoting/RmiProvider.java b/core/src/main/java/org/apache/commons/proxy/provider/remoting/RmiProvider.java
new file mode 100644
index 0000000..a538711
--- /dev/null
+++ b/core/src/main/java/org/apache/commons/proxy/provider/remoting/RmiProvider.java
@@ -0,0 +1,152 @@
+/*
+ * 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.commons.proxy.provider.remoting;
+
+import org.apache.commons.proxy.ObjectProvider;
+import org.apache.commons.proxy.exception.ObjectProviderException;
+
+import java.rmi.NotBoundException;
+import java.rmi.RemoteException;
+import java.rmi.registry.LocateRegistry;
+import java.rmi.registry.Registry;
+import java.rmi.server.RMIClientSocketFactory;
+
+/**
+ * Provides an object by looking it up in an RMI registry.
+ *
+ * @author James Carman
+ * @since 1.0
+ */
+public class RmiProvider<T> implements ObjectProvider<T>
+{
+//**********************************************************************************************************************
+// Fields
+//**********************************************************************************************************************
+
+ private String host = "localhost";
+ private int port = Registry.REGISTRY_PORT;
+ private RMIClientSocketFactory clientSocketFactory;
+ private String name;
+
+//**********************************************************************************************************************
+// Constructors
+//**********************************************************************************************************************
+
+ public RmiProvider()
+ {
+ }
+
+ public RmiProvider( String name )
+ {
+ setName(name);
+ }
+
+ public RmiProvider( String host, String name )
+ {
+ setHost(host);
+ setName(name);
+ }
+
+ public RmiProvider( String host, int port, String name )
+ {
+ setHost(host);
+ setName(name);
+ setPort(port);
+ }
+
+ public RmiProvider( String host, int port, RMIClientSocketFactory clientSocketFactory, String name )
+ {
+ setHost(host);
+ setPort(port);
+ setClientSocketFactory(clientSocketFactory);
+ setName(name);
+ }
+
+//**********************************************************************************************************************
+// ObjectProvider Implementation
+//**********************************************************************************************************************
+
+ @SuppressWarnings("unchecked")
+ public T getObject()
+ {
+ Registry reg;
+ try
+ {
+ reg = getRegistry();
+ return (T)reg.lookup(name);
+ }
+ catch( NotBoundException e )
+ {
+ throw new ObjectProviderException("Name " + name + " not found in registry at " + host + ":" + port + ".",
+ e);
+ }
+ catch( RemoteException e )
+ {
+ throw new ObjectProviderException(
+ "Unable to lookup service named " + name + " in registry at " + host + ":" + port + ".", e);
+ }
+ }
+
+//**********************************************************************************************************************
+// Getter/Setter Methods
+//**********************************************************************************************************************
+
+ public void setClientSocketFactory( RMIClientSocketFactory clientSocketFactory )
+ {
+ this.clientSocketFactory = clientSocketFactory;
+ }
+
+ public void setHost( String host )
+ {
+ this.host = host;
+ }
+
+ public void setName( String name )
+ {
+ this.name = name;
+ }
+
+ public void setPort( int port )
+ {
+ this.port = port;
+ }
+
+//**********************************************************************************************************************
+// Other Methods
+//**********************************************************************************************************************
+
+ private Registry getRegistry()
+ {
+ try
+ {
+ if( clientSocketFactory != null )
+ {
+ return LocateRegistry.getRegistry(host, port, clientSocketFactory);
+ }
+ else
+ {
+ return LocateRegistry.getRegistry(host, port);
+ }
+ }
+ catch( RemoteException e )
+ {
+ throw new ObjectProviderException("Unable to locate registry at " + host + ":" + port + ".", e);
+ }
+ }
+}
+
diff --git a/core/src/main/java/org/apache/commons/proxy/provider/remoting/SessionBeanProvider.java b/core/src/main/java/org/apache/commons/proxy/provider/remoting/SessionBeanProvider.java
new file mode 100644
index 0000000..971d0a6
--- /dev/null
+++ b/core/src/main/java/org/apache/commons/proxy/provider/remoting/SessionBeanProvider.java
@@ -0,0 +1,103 @@
+/*
+ * 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.commons.proxy.provider.remoting;
+
+import org.apache.commons.proxy.ObjectProvider;
+import org.apache.commons.proxy.ProxyUtils;
+import org.apache.commons.proxy.exception.ObjectProviderException;
+
+import javax.naming.InitialContext;
+import javax.naming.NamingException;
+import javax.rmi.PortableRemoteObject;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+import java.util.Properties;
+
+/**
+ * Provides a reference to a session bean by looking up the home object and calling (via reflection) the no-argument
+ * create() method. This will work for both local and remote session beans.
+ *
+ * @author James Carman
+ * @since 1.0
+ */
+public class SessionBeanProvider<T> implements ObjectProvider<T>
+{
+//**********************************************************************************************************************
+// Fields
+//**********************************************************************************************************************
+
+ private final String jndiName;
+ private final Class homeInterface;
+ private final Properties properties;
+
+//**********************************************************************************************************************
+// Constructors
+//**********************************************************************************************************************
+
+ public SessionBeanProvider( String jndiName, Class homeInterface )
+ {
+ this.jndiName = jndiName;
+ this.homeInterface = homeInterface;
+ this.properties = null;
+ }
+
+ public SessionBeanProvider( String jndiName, Class homeInterface, Properties properties )
+ {
+ this.jndiName = jndiName;
+ this.homeInterface = homeInterface;
+ this.properties = properties;
+ }
+
+//**********************************************************************************************************************
+// ObjectProvider Implementation
+//**********************************************************************************************************************
+
+ @SuppressWarnings("unchecked")
+ public T getObject()
+ {
+ try
+ {
+ final InitialContext initialContext = properties == null ? new InitialContext() :
+ new InitialContext(properties);
+ Object homeObject = PortableRemoteObject.narrow(initialContext.lookup(jndiName), homeInterface);
+ final Method createMethod = homeObject.getClass().getMethod("create", ProxyUtils.EMPTY_ARGUMENT_TYPES);
+ return (T)createMethod.invoke(homeObject, ProxyUtils.EMPTY_ARGUMENTS);
+ }
+ catch( NoSuchMethodException e )
+ {
+ throw new ObjectProviderException(
+ "Unable to find no-arg create() method on home interface " + homeInterface.getName() + ".", e);
+ }
+ catch( IllegalAccessException e )
+ {
+ throw new ObjectProviderException(
+ "No-arg create() method on home interface " + homeInterface.getName() + " is not accessible.",
+ e); // Should never happen!
+ }
+ catch( NamingException e )
+ {
+ throw new ObjectProviderException("Unable to lookup EJB home object in JNDI.", e);
+ }
+ catch( InvocationTargetException e )
+ {
+ throw new ObjectProviderException(
+ "No-arg create() method on home interface " + homeInterface.getName() + " threw an exception.", e);
+ }
+ }
+}
+
diff --git a/core/src/main/java/org/apache/commons/proxy/provider/remoting/package.html b/core/src/main/java/org/apache/commons/proxy/provider/remoting/package.html
new file mode 100644
index 0000000..e906038
--- /dev/null
+++ b/core/src/main/java/org/apache/commons/proxy/provider/remoting/package.html
@@ -0,0 +1,25 @@
+<!--
+ ~ 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.
+ -->
+
+<html>
+<body>
+<p>
+ This package contains some useful <a href="../../ObjectProvider.html">ObjectProvider</a> implementations for use
+ in remoting situations (EJB, RMI, Burlap, Hessian, JAX-RPC, etc).
+</p>
+</body>
+</html>