KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > Jt > ejb > JtServiceLocator


1 package Jt.ejb;
2
3 import javax.ejb.*;
4 import javax.naming.*;
5 import java.rmi.*;
6 import java.util.Properties JavaDoc;
7 import java.util.*;
8 import Jt.*;
9
10
11 /**
12  * Jt Implementation of the J2EE service locator pattern.
13  */

14
15 public class JtServiceLocator extends JtObject {
16
17   private String JavaDoc url = "t3://localhost:7001"; // Default value (Weblogic)
18
private String JavaDoc user = null;
19   private String JavaDoc password = null;
20   private String JavaDoc factory = "weblogic.jndi.WLInitialContextFactory"; // Default value (Weblogic)
21
private String JavaDoc jndiName = "JtSessionFacade"; // Default value
22
Class JavaDoc sclass = JtSessionFacadeHome.class; // Default value
23

24
25
26  /**
27   * Specifies the service class. EJBHome class in the case of EJBs
28   * @param sclass service class
29   */

30
31   public void setSclass (Class JavaDoc sclass) {
32      this.sclass = sclass;
33
34   }
35
36
37  /**
38    * Returns the service class.
39    */

40
41
42   public Class JavaDoc getSclass () {
43      return (sclass);
44   }
45
46
47  /**
48   * Specifies the URL (initial context).
49   * @param url url
50   */

51
52   public void setUrl (String JavaDoc url) {
53      this.url = url;
54
55   }
56
57  /**
58    * Returns the url.
59    */

60
61   public String JavaDoc getUrl () {
62      return (url);
63   }
64
65
66  /**
67   * Specifies the user (initial context).
68   * @param user user
69   */

70
71   public void setUser (String JavaDoc user) {
72      this.user = user;
73   }
74
75  /**
76    * Returns the user.
77    */

78
79   public String JavaDoc getUser () {
80      return (user);
81   }
82
83
84  /**
85   * Specifies the password (initial context).
86   * @param password password
87   */

88
89   public void setPassword (String JavaDoc password) {
90      this.password = password;
91   }
92
93
94  /**
95    * Returns the password.
96    */

97
98   public String JavaDoc getPassword () {
99      return (password);
100   }
101
102  /**
103   * Specifies the context factory (initial context).
104   * @param factory factory
105   */

106
107   public void setFactory (String JavaDoc factory) {
108      this.factory = factory;
109   }
110
111
112  /**
113    * Returns the context factory.
114    */

115
116
117   public String JavaDoc getFactory () {
118      return (factory);
119   }
120
121  /**
122   * Specifies the JNDI name.
123   * @param jndiName JNDI name
124   */

125
126   public void setJndiName (String JavaDoc jndiName) {
127      this.jndiName = jndiName;
128   }
129
130
131  /**
132    * Returns the JNDI name.
133    */

134
135   public String JavaDoc getJndiName () {
136      return (jndiName);
137   }
138
139
140   // Locate the service
141

142   private Object JavaDoc activate () {
143
144     Object JavaDoc dhome = null;
145     //parseArgs(args);
146

147     try {
148
149       Context ctx = getInitialContext();
150       //Context ctx = new InitialContext();
151
Object JavaDoc objref = ctx.lookup (jndiName);
152
153      dhome = (Object JavaDoc)
154         javax.rmi.PortableRemoteObject.narrow (objref, sclass);
155       
156
157     } catch (Exception JavaDoc e) {
158       handleException (e);
159     }
160     return (dhome);
161
162   }
163
164
165   private Object JavaDoc test () {
166
167     return (sendMessage (this, new JtMessage ("JtACTIVATE")));
168
169   }
170
171   /**
172     * Process object messages.
173     * <ul>
174     * <li> JtACTIVATE - Activates this object to locate a service.
175     * <li> JtTEST - Tests the messages processes by JtServiceLocator
176     * </ul>
177     * @param event message
178     */

179
180   public Object JavaDoc processMessage (Object JavaDoc event) {
181
182    String JavaDoc msgid = null;
183    JtMessage e = (JtMessage) event;
184    Object JavaDoc content;
185
186      if (e == null)
187     return null;
188
189      msgid = (String JavaDoc) e.getMsgId ();
190
191      if (msgid == null)
192     return null;
193
194      content = e.getMsgContent();
195
196      // Locate the service (JtACTIVATE)
197

198      if (msgid.equals ("JtACTIVATE")) {
199              
200         return (activate ());
201
202      }
203
204
205      if (msgid.equals ("JtTEST")) {
206              
207         return (test ());
208
209      }
210
211
212      if (msgid.equals ("JtREMOVE")) {
213              
214         return (null);
215
216      }
217           
218      handleError ("JtServiceLocator.processMessage: invalid message id:" + msgid);
219      return (null);
220
221   }
222
223
224   /**
225    * Gets an initial context.
226    *
227    */

228
229   private Context getInitialContext() throws Exception JavaDoc {
230     Properties JavaDoc p = new Properties JavaDoc();
231     p.put(Context.INITIAL_CONTEXT_FACTORY,
232           factory);
233     p.put(Context.PROVIDER_URL, url);
234     if (user != null) {
235       //System.out.println ("user: " + user);
236
p.put(Context.SECURITY_PRINCIPAL, user);
237       if (password == null)
238         password = "";
239       p.put(Context.SECURITY_CREDENTIALS, password);
240     }
241     return new InitialContext(p);
242   }
243
244
245  /**
246    * Unit tests all the messages processed by JtServiceLocator.
247    */

248
249   public static void main (String JavaDoc[] args) {
250     JtObject main;
251     JtServiceLocator locator;
252     Object JavaDoc jtservice;
253     //Object session = (Object) new Date ();
254

255
256     main = new JtObject ();
257
258
259     // Create an instance of JtServiceLocator
260

261     locator = (JtServiceLocator) main.createObject ("Jt.ejb.JtServiceLocator", "locator");
262
263     // Set the service locator attributes
264

265     main.setValue (locator, "jndiName", "JtSessionFacade");
266     main.setValue (locator, "sclass", JtSessionFacadeHome.class);
267
268     
269     // Activate the Service Locator (locate the Service)
270

271     jtservice = (Object JavaDoc) main.sendMessage ("locator", new JtMessage ("JtTEST"));
272
273
274     if (jtservice != null)
275     System.out.println ("JtServiceLocator: GO");
276     else
277     System.out.println ("JtServiceLocator: FAIL");
278
279
280     // Destroy the Service Locator
281

282     main.removeObject ("locator");
283
284
285   }
286 }
287
Popular Tags