Source code: com/mjh/switchrmi/RmiContextImpl.java
1 //
2 // SwitchRMI Framework
3 // Copyright (c) 2000-2002 by Michael J. Henderson & Associates.
4 //
5 // Michael Henderson
6 // http://switchrmi.sf.net
7 // mailto:mikehenderson@dunelm.org.uk
8 //
9 // This library is free software.
10 //
11 // You may redistribute it and/or modify it under the terms of the GNU
12 // Lesser General Public License as published by the Free Software Foundation.
13 //
14 // Version 2.1 of the license should be included with this distribution in
15 // the file LICENSE, as well as License.html. If the license is not
16 // included with this distribution, you may find a copy at the FSF web
17 // site at 'www.gnu.org' or 'www.fsf.org', or you may write to the
18 // Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139 USA.
19 //
20 // This library is distributed in the hope that it will be useful,
21 // but WITHOUT ANY WARRANTY; without even the implied waranty of
22 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23 // Lesser General Public License for more details.
24 //
25 // $Id: RmiContextImpl.java,v 1.1 2002/11/11 22:19:48 mikehenderson Exp $
26 package com.mjh.switchrmi;
27
28 import com.mjh.util.*;
29
30 import java.net.*;
31
32 import java.util.*;
33
34 import javax.naming.*;
35
36 import org.apache.log4j.*;
37
38 public class RmiContextImpl implements RmiContext
39 {
40 private static final Logger log =
41 Logger.getLogger(RmiContext.class.getName());
42 private String targetUrl;
43 private RmiTransport transport;
44 private RmiProtocol protocol;
45 private UrlInfo urlInfo;
46 private Object sessionID;
47 private Object target;
48 private Context jndiContext;
49 boolean isService;
50
51 public RmiContextImpl()
52 {
53 }
54
55 public RmiContextImpl(boolean isService)
56 {
57 this.isService = isService;
58 }
59
60 public RmiContextImpl(boolean isService, String url, Context ctx)
61 throws Exception
62 {
63 this(isService, url, ctx, null);
64 }
65
66 public RmiContextImpl(boolean isService, String url, Context ctx,
67 String protocolName)
68 throws Exception
69 {
70 this.isService = isService;
71 setUrl(url);
72 urlInfo = new RmiUrlInfo(url);
73
74 if (log.isDebugEnabled())
75 {
76 log.debug("urlInfo = " + urlInfo);
77 }
78
79 jndiContext = ctx;
80
81 String string = (isService()) ? "service/" : "client/";
82 String jndiName = "/switchrmi/" + string + "transport/"
83 + getTransportName();
84
85 if (log.isDebugEnabled())
86 {
87 log.debug("jndiName = " + jndiName);
88 }
89
90 transport = (RmiTransport) ctx.lookup(jndiName);
91
92 if (log.isDebugEnabled())
93 {
94 log.debug("transport = " + transport);
95 }
96
97 if (log.isDebugEnabled())
98 {
99 log.debug("protocolName = " + protocolName);
100 log.debug("getProtocolName() = " + getProtocolName());
101 }
102
103 String name = (protocolName == null
104 || protocolName.trim().length() == 0)
105 ? getProtocolName() : protocolName;
106
107 jndiName = "/switchrmi/" + string + "protocol/" + name;
108
109 if (log.isDebugEnabled())
110 {
111 log.debug("jndiName = " + jndiName);
112 }
113
114 protocol = (RmiProtocol) ctx.lookup(jndiName);
115
116 if (log.isDebugEnabled())
117 {
118 log.debug("protocol = " + protocol);
119 }
120 }
121
122 public boolean isClient()
123 {
124 return !isService;
125 }
126
127 public boolean isService()
128 {
129 return isService;
130 }
131
132 public void setUrl(String url)
133 {
134 targetUrl = url;
135 }
136
137 public String getUrl()
138 {
139 return targetUrl;
140 }
141
142 public void setTransport(RmiTransport transport)
143 {
144 this.transport = transport;
145 }
146
147 public RmiTransport getTransport()
148 {
149 return transport;
150 }
151
152 public void setProtocol(RmiProtocol protocol)
153 {
154 this.protocol = protocol;
155 }
156
157 public RmiProtocol getProtocol()
158 {
159 return protocol;
160 }
161
162 public String getObjectName()
163 {
164 return ((RmiUrlInfo) urlInfo).getObjectName();
165 }
166
167 public String getTransportName()
168 {
169 return ((RmiUrlInfo) urlInfo).getTransportName();
170 }
171
172 public String getProtocolName()
173 {
174 return ((RmiUrlInfo) urlInfo).getProtocolName();
175 }
176
177 public void setTarget(Object target)
178 {
179 this.target = target;
180 }
181
182 public Object lookup(String name)
183 throws Exception
184 {
185 return jndiContext.lookup(name);
186 }
187
188 public Context getJndiContext()
189 {
190 return jndiContext;
191 }
192
193 public Object getTarget()
194 {
195 return target;
196 }
197
198 public void setSessionIdentifier(Object identifier)
199 {
200 sessionID = identifier;
201
202 if (log.isDebugEnabled())
203 {
204 log.debug("context.setSessionIdentifier() sessionID = " + sessionID);
205 }
206 }
207
208 public Object getSessionIdentifier()
209 {
210 if (log.isDebugEnabled())
211 {
212 log.debug("context.getSessionIdentifier() sessionID = " + sessionID);
213 }
214
215 return sessionID;
216 }
217
218 public UrlInfo getUrlInfo()
219 throws MalformedURLException
220 {
221 return getTransport().getUrlInfo(this);
222 }
223 }