1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
31
32
33
34
35 public class RmiProvider implements ObjectProvider
36 {
37
38
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
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
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
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
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