1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18 package org.apache.commons.proxy.provider.remoting;
19
20 import org.apache.commons.proxy.ObjectProvider;
21 import org.apache.commons.proxy.exception.ObjectProviderException;
22
23 import java.rmi.NotBoundException;
24 import java.rmi.RemoteException;
25 import java.rmi.registry.LocateRegistry;
26 import java.rmi.registry.Registry;
27 import java.rmi.server.RMIClientSocketFactory;
28
29 /**
30 * Provides an object by looking it up in an RMI registry.
31 *
32 * @author James Carman
33 * @since 1.0
34 */
35 public class RmiProvider implements ObjectProvider
36 {
37 //----------------------------------------------------------------------------------------------------------------------
38 // Fields
39 //----------------------------------------------------------------------------------------------------------------------
40
41 private String host = "localhost";
42 private int port = Registry.REGISTRY_PORT;
43 private RMIClientSocketFactory clientSocketFactory;
44 private String name;
45
46 //----------------------------------------------------------------------------------------------------------------------
47 // Constructors
48 //----------------------------------------------------------------------------------------------------------------------
49
50 public RmiProvider()
51 {
52 }
53
54 public RmiProvider( String name )
55 {
56 setName( name );
57 }
58
59 public RmiProvider( String host, String name )
60 {
61 setHost( host );
62 setName( name );
63 }
64
65 public RmiProvider( String host, int port, String name )
66 {
67 setHost( host );
68 setName( name );
69 setPort( port );
70 }
71
72 public RmiProvider( String host, int port, RMIClientSocketFactory clientSocketFactory, String name )
73 {
74 setHost( host );
75 setPort( port );
76 setClientSocketFactory( clientSocketFactory );
77 setName( name );
78 }
79
80 //----------------------------------------------------------------------------------------------------------------------
81 // ObjectProvider Implementation
82 //----------------------------------------------------------------------------------------------------------------------
83
84 public Object getObject()
85 {
86 Registry reg = null;
87 try
88 {
89 reg = getRegistry();
90 return reg.lookup( name );
91 }
92 catch( NotBoundException e )
93 {
94 throw new ObjectProviderException( "Name " + name + " not found in registry at " + host + ":" + port + ".",
95 e );
96 }
97 catch( RemoteException e )
98 {
99 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 setName( String name )
109 {
110 this.name = name;
111 }
112
113 public void setClientSocketFactory( RMIClientSocketFactory clientSocketFactory )
114 {
115 this.clientSocketFactory = clientSocketFactory;
116 }
117
118 public void setHost( String host )
119 {
120 this.host = host;
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