001 /*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017
018 package org.apache.commons.proxy.provider.remoting;
019
020 import org.apache.commons.proxy.ObjectProvider;
021 import org.apache.commons.proxy.exception.ObjectProviderException;
022
023 import java.rmi.NotBoundException;
024 import java.rmi.RemoteException;
025 import java.rmi.registry.LocateRegistry;
026 import java.rmi.registry.Registry;
027 import java.rmi.server.RMIClientSocketFactory;
028
029 /**
030 * Provides an object by looking it up in an RMI registry.
031 *
032 * @author James Carman
033 * @since 1.0
034 */
035 public class RmiProvider implements ObjectProvider
036 {
037 //**********************************************************************************************************************
038 // Fields
039 //**********************************************************************************************************************
040
041 private String host = "localhost";
042 private int port = Registry.REGISTRY_PORT;
043 private RMIClientSocketFactory clientSocketFactory;
044 private String name;
045
046 //**********************************************************************************************************************
047 // Constructors
048 //**********************************************************************************************************************
049
050 public RmiProvider()
051 {
052 }
053
054 public RmiProvider( String name )
055 {
056 setName(name);
057 }
058
059 public RmiProvider( String host, String name )
060 {
061 setHost(host);
062 setName(name);
063 }
064
065 public RmiProvider( String host, int port, String name )
066 {
067 setHost(host);
068 setName(name);
069 setPort(port);
070 }
071
072 public RmiProvider( String host, int port, RMIClientSocketFactory clientSocketFactory, String name )
073 {
074 setHost(host);
075 setPort(port);
076 setClientSocketFactory(clientSocketFactory);
077 setName(name);
078 }
079
080 //**********************************************************************************************************************
081 // ObjectProvider Implementation
082 //**********************************************************************************************************************
083
084 public Object getObject()
085 {
086 Registry reg = null;
087 try
088 {
089 reg = getRegistry();
090 return reg.lookup(name);
091 }
092 catch( NotBoundException e )
093 {
094 throw new ObjectProviderException("Name " + name + " not found in registry at " + host + ":" + port + ".",
095 e);
096 }
097 catch( RemoteException e )
098 {
099 throw new ObjectProviderException(
100 "Unable to lookup service named " + name + " in registry at " + host + ":" + port + ".", e);
101 }
102 }
103
104 //**********************************************************************************************************************
105 // Getter/Setter Methods
106 //**********************************************************************************************************************
107
108 public void setClientSocketFactory( RMIClientSocketFactory clientSocketFactory )
109 {
110 this.clientSocketFactory = clientSocketFactory;
111 }
112
113 public void setHost( String host )
114 {
115 this.host = host;
116 }
117
118 public void setName( String name )
119 {
120 this.name = name;
121 }
122
123 public void setPort( int port )
124 {
125 this.port = port;
126 }
127
128 //**********************************************************************************************************************
129 // Other Methods
130 //**********************************************************************************************************************
131
132 private Registry getRegistry()
133 {
134 try
135 {
136 if( clientSocketFactory != null )
137 {
138 return LocateRegistry.getRegistry(host, port, clientSocketFactory);
139 }
140 else
141 {
142 return LocateRegistry.getRegistry(host, port);
143 }
144 }
145 catch( RemoteException e )
146 {
147 throw new ObjectProviderException("Unable to locate registry at " + host + ":" + port + ".", e);
148 }
149 }
150 }
151