KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > Jt > jndi > JtJNDIAdapter


1 package Jt.jndi;
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 JNDI Adapter
13  */

14
15 public class JtJNDIAdapter extends JtAdapter {
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 boolean initted = false;
22   private transient Context ctx = null;
23
24  /**
25   * Specifies the URL (initial context).
26   * @param url url
27   */

28
29   public void setUrl (String JavaDoc url) {
30      this.url = url;
31
32   }
33
34  /**
35    * Returns the url.
36    */

37
38   public String JavaDoc getUrl () {
39      return (url);
40   }
41
42
43  /**
44   * Specifies the user (initial context).
45   * @param user user
46   */

47
48   public void setUser (String JavaDoc user) {
49      this.user = user;
50   }
51
52  /**
53    * Returns the user.
54    */

55
56   public String JavaDoc getUser () {
57      return (user);
58   }
59
60
61  /**
62   * Specifies the password (initial context).
63   * @param password password
64   */

65
66   public void setPassword (String JavaDoc password) {
67      this.password = password;
68   }
69
70
71  /**
72    * Returns the password.
73    */

74
75   public String JavaDoc getPassword () {
76      return (password);
77   }
78
79  /**
80   * Specifies the context factory (initial context).
81   * @param factory factory
82   */

83
84   public void setFactory (String JavaDoc factory) {
85      this.factory = factory;
86   }
87
88
89  /**
90    * Returns the context factory.
91    */

92
93
94   public String JavaDoc getFactory () {
95      return (factory);
96   }
97
98
99
100   // lookup
101

102   private Object JavaDoc lookup (String JavaDoc jndiName) {
103
104     Object JavaDoc objref = null;
105
106      
107     try {
108
109       if (!initted) {
110         initted = true;
111         ctx = getInitialContext();
112       }
113
114       objref = ctx.lookup (jndiName);
115       
116
117     } catch (Exception JavaDoc e) {
118       handleException (e);
119     }
120     return (objref);
121
122   }
123
124
125   private Object JavaDoc test () {
126
127     JtMessage msg = new JtMessage ("JtLOOKUP");
128
129     msg.setMsgContent ("JtSessionFacade");
130
131     return (processMessage (msg));
132
133   }
134
135   /**
136     * Process object messages.
137     * <ul>
138     * <li> JtLOOKUP - retrieves the named object specified by msgContent.
139     * </ul>
140     * @param event message
141     */

142
143   public Object JavaDoc processMessage (Object JavaDoc event) {
144
145    String JavaDoc msgid = null;
146    JtMessage e = (JtMessage) event;
147    Object JavaDoc content;
148
149      if (e == null)
150     return null;
151
152      msgid = (String JavaDoc) e.getMsgId ();
153
154      if (msgid == null)
155     return null;
156
157      content = e.getMsgContent();
158
159      // Locate the service (JtACTIVATE)
160

161      if (msgid.equals ("JtLOOKUP")) {
162              
163         return (lookup ((String JavaDoc) content));
164
165      }
166
167
168      if (msgid.equals ("JtTEST")) {
169              
170         return (test ());
171
172      }
173
174
175      if (msgid.equals ("JtREMOVE")) {
176              
177         return (null);
178
179      }
180           
181      handleError ("processMessage: invalid message id:" + msgid);
182      return (null);
183
184   }
185
186
187   /**
188    * Gets an initial context.
189    *
190    */

191
192   private Context getInitialContext() throws Exception JavaDoc {
193     Properties JavaDoc p = new Properties JavaDoc();
194     p.put(Context.INITIAL_CONTEXT_FACTORY,
195           factory);
196     p.put(Context.PROVIDER_URL, url);
197     if (user != null) {
198       //System.out.println ("user: " + user);
199
p.put(Context.SECURITY_PRINCIPAL, user);
200       if (password == null)
201         password = "";
202       p.put(Context.SECURITY_CREDENTIALS, password);
203     }
204     return new InitialContext(p);
205   }
206
207
208  /**
209    * Unit tests all the messages processed by JtJNDIAdapter.
210    */

211
212   public static void main (String JavaDoc[] args) {
213     JtObject main;
214     JtJNDIAdapter adapter;
215     Object JavaDoc entry;
216
217     main = new JtObject ();
218
219
220     // Create an instance of JtJNDIAdapter
221

222     adapter = (JtJNDIAdapter) main.createObject ("Jt.jndi.JtJNDIAdapter", "adapter");
223     
224     // Send a TEST message to the adapter
225

226     entry = (Object JavaDoc) main.sendMessage ("adapter", new JtMessage ("JtTEST"));
227
228
229     if (entry != null)
230     System.out.println ("JtJNDIAdapter: GO");
231     else
232     System.out.println ("JtJNDIAdapter: FAIL");
233
234
235     // Destroy the JtJNDIAdapter instance
236

237     main.removeObject ("adapter");
238
239
240   }
241 }
242
Popular Tags