KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ejtools > jndi > browser > model > Server


1 /*
2  * EJTools, the Enterprise Java Tools
3  *
4  * Distributable under LGPL license.
5  * See terms of license at www.gnu.org.
6  */

7 package org.ejtools.jndi.browser.model;
8
9 import java.lang.reflect.Constructor JavaDoc;
10 import java.util.Collection JavaDoc;
11 import java.util.Iterator JavaDoc;
12 import java.util.Properties JavaDoc;
13 import java.util.ResourceBundle JavaDoc;
14 import java.util.Vector JavaDoc;
15
16 import javax.naming.Context JavaDoc;
17 import javax.naming.InitialContext JavaDoc;
18 import javax.naming.LinkRef JavaDoc;
19 import javax.naming.NameClassPair JavaDoc;
20 import javax.naming.NamingEnumeration JavaDoc;
21 import javax.naming.NamingException JavaDoc;
22
23 import org.apache.log4j.Logger;
24 import org.ejtools.jndi.browser.model.service.ConnectionMetaData;
25 import org.ejtools.util.service.Profile;
26 import org.ejtools.util.service.ProfileHolder;
27
28 /**
29  * Description of the Class
30  *
31  * @author letiemble
32  * @created 13 d?cembre 2001
33  * @version $Revision: 1.2 $
34  * @todo Javadoc to complete
35  * @todo Add log4j logs
36  * @javabean:class displayName="JNDI Server"
37  * shortDescription="JNDI Server"
38  * @javabean:icons color16="/toolbarButtonGraphics/development/Server16.gif"
39  * @javabean:property name="name"
40  * class="java.lang.String"
41  * displayName="Connection"
42  * shortDescription="Connection Name"
43  * @javabean:property name="factory"
44  * class="java.lang.String"
45  * displayName="Factory"
46  * shortDescription="JNDI context factory"
47  * @javabean:property name="packages"
48  * class="java.lang.String"
49  * displayName="Packages"
50  * shortDescription="Packages for context"
51  * @javabean:property name="url"
52  * class="java.lang.String"
53  * displayName="URL"
54  * shortDescription="JNDI Server URL"
55  * @javabean:property name="context"
56  * class="java.lang.String"
57  * displayName="Context"
58  * shortDescription="Initial JNDI context"
59  * @javabean:property name="principal"
60  * class="java.lang.String"
61  * displayName="Principal"
62  * shortDescription="Principal to connect"
63  * @javabean:property name="credentials"
64  * class="java.lang.String"
65  * displayName="Credentials"
66  * shortDescription="Credentials to connect"
67  */

68 public class Server extends JNDIContext implements ProfileHolder
69 {
70    /** Description of the Field */
71    protected Profile profile = new Profile();
72    /** Description of the Field */
73    private static Logger logger = Logger.getLogger(Server.class);
74    /** Description of the Field */
75    private static Vector JavaDoc proxies = new Vector JavaDoc();
76    /** Bundle for I18N */
77    private static ResourceBundle JavaDoc resources = ResourceBundle.getBundle("org.ejtools.jndi.browser.Resources");
78
79
80    /**
81     * Constructor for the JndiServer object
82     *
83     * @todo I18N to complete
84     */

85    public Server()
86    {
87       super();
88       this.setName(resources.getString("connection.text.untitled"));
89    }
90
91
92    /**
93     * Gets the context attribute of the JNDIServer object
94     *
95     * @return The context value
96     */

97    public String JavaDoc getContext()
98    {
99       return this.profile.getProperty(ConnectionMetaData.CONTEXT);
100    }
101
102
103    /**
104     * Returns the credentials.
105     *
106     * @return String
107     */

108    public String JavaDoc getCredentials()
109    {
110       return this.profile.getProperty(ConnectionMetaData.CREDENTIALS);
111    }
112
113
114    /**
115     * Gets the provider attribute of the JndiServer object
116     *
117     * @return The provider value
118     */

119    public String JavaDoc getFactory()
120    {
121       return this.profile.getProperty(ConnectionMetaData.FACTORY);
122    }
123
124
125    /**
126     * Gets the packages attribute of the JndiServer object
127     *
128     * @return The packages value
129     */

130    public String JavaDoc getPackages()
131    {
132       return this.profile.getProperty(ConnectionMetaData.PACKAGES);
133    }
134
135
136
137    /**
138     * Returns the principal.
139     *
140     * @return String
141     */

142    public String JavaDoc getPrincipal()
143    {
144       return this.profile.getProperty(ConnectionMetaData.PRINCIPAL);
145    }
146
147
148    /**
149     * Gets the url attribute of the JndiServer object
150     *
151     * @return The url value
152     */

153    public String JavaDoc getUrl()
154    {
155       return this.profile.getProperty(ConnectionMetaData.URL);
156    }
157
158
159    /**
160     * Description of the Method
161     *
162     * @javabean:method name="refresh" displayName="Refresh" shortDescription="Refresh the JNDI tree"
163     */

164    public void refresh()
165    {
166       logger.debug("Cleaning JNDI tree...");
167       Iterator JavaDoc iterator = iterator();
168       while (iterator.hasNext())
169       {
170          remove(iterator.next());
171       }
172
173       try
174       {
175          Context JavaDoc context = null;
176          if (this.getFactory() != null)
177          {
178             Properties JavaDoc props = new Properties JavaDoc();
179             props.put(Context.INITIAL_CONTEXT_FACTORY, this.getFactory());
180             props.put(Context.URL_PKG_PREFIXES, this.getPackages());
181             props.put(Context.PROVIDER_URL, this.getUrl());
182             logger.debug("Initial context with " + props);
183             context = new InitialContext JavaDoc(props);
184          }
185          else
186          {
187             context = new InitialContext JavaDoc();
188          }
189
190          if (!("".equals(this.getContext())))
191          {
192             logger.debug("Context =<" + this.getContext() + ">");
193             context = (Context JavaDoc) context.lookup(this.getContext());
194          }
195
196          logger.debug("Populating JNDI tree...");
197          this.list(context, this);
198       }
199       catch (Exception JavaDoc e)
200       {
201          e.printStackTrace();
202          logger.error("Exception " + e.getMessage());
203       }
204    }
205
206
207    /**
208     * Sets the context attribute of the JNDIServer object
209     *
210     * @param context The new context value
211     */

212    public void setContext(String JavaDoc context)
213    {
214       this.profile.setProperty(ConnectionMetaData.CONTEXT, context);
215    }
216
217
218
219    /**
220     * Sets the credentials.
221     *
222     * @param credentials The credentials to set
223     */

224    public void setCredentials(String JavaDoc credentials)
225    {
226       this.profile.setProperty(ConnectionMetaData.CREDENTIALS, credentials);
227    }
228
229
230    /**
231     * Sets the provider attribute of the JndiServer object
232     *
233     * @param factory The new factory value
234     */

235    public void setFactory(String JavaDoc factory)
236    {
237       this.profile.setProperty(ConnectionMetaData.FACTORY, factory);
238    }
239
240
241    /**
242     * Sets the name of this server
243     *
244     * @param name The name
245     */

246    public void setName(String JavaDoc name)
247    {
248       super.setName(name);
249    }
250
251
252    /**
253     * Sets the packages attribute of the JndiServer object
254     *
255     * @param packages The new packages value
256     */

257    public void setPackages(String JavaDoc packages)
258    {
259       this.profile.setProperty(ConnectionMetaData.PACKAGES, packages);
260    }
261
262
263
264    /**
265     * Sets the principal.
266     *
267     * @param principal The principal to set
268     */

269    public void setPrincipal(String JavaDoc principal)
270    {
271       this.profile.setProperty(ConnectionMetaData.PRINCIPAL, principal);
272    }
273
274
275    /**
276     * Sets the profile.
277     *
278     * @param profile The profile to set
279     */

280    public void setProfile(Profile profile)
281    {
282       this.profile = profile;
283    }
284
285
286    /**
287     * Sets the url attribute of the JndiServer object
288     *
289     * @param url The new url value
290     */

291    public void setUrl(String JavaDoc url)
292    {
293       this.profile.setProperty(ConnectionMetaData.URL, url);
294    }
295
296
297    /**
298     * Description of the Method
299     *
300     * @param context Description of the Parameter
301     * @param jndiName Description of the Parameter
302     * @return Description of the Return Value
303     */

304    protected JNDIContext createNode(Context JavaDoc context, String JavaDoc jndiName)
305    {
306       for (int i = 0; i < proxies.size(); i++)
307       {
308          Class JavaDoc clazz = (Class JavaDoc) proxies.elementAt(i);
309
310          try
311          {
312             Constructor JavaDoc c = clazz.getConstructor(new Class JavaDoc[]{javax.naming.Context JavaDoc.class, java.lang.String JavaDoc.class});
313             JNDIContext node = (JNDIContext) c.newInstance(new Object JavaDoc[]{context, jndiName});
314             if (node != null)
315             {
316                return node;
317             }
318          }
319          catch (Exception JavaDoc e)
320          {
321          }
322       }
323
324       return null;
325    }
326
327
328    /**
329     * Description of the Method
330     *
331     * @param ctx Description of the Parameter
332     * @param node Description of the Parameter
333     */

334    protected void list(Context JavaDoc ctx, Collection JavaDoc node)
335    {
336       ClassLoader JavaDoc loader = Thread.currentThread().getContextClassLoader();
337       try
338       {
339          NamingEnumeration JavaDoc ne = ctx.list("");
340          while (ne.hasMore())
341          {
342             JNDIContext newNode = null;
343
344             NameClassPair JavaDoc pair = (NameClassPair JavaDoc) ne.next();
345
346             String JavaDoc name = pair.getName();
347             String JavaDoc className = pair.getClassName();
348             boolean recursive = false;
349             boolean isLinkRef = false;
350
351             Class JavaDoc c = null;
352             try
353             {
354                c = loader.loadClass(className);
355
356                if (Context JavaDoc.class.isAssignableFrom(c))
357                {
358                   recursive = true;
359                }
360                if (LinkRef JavaDoc.class.isAssignableFrom(c))
361                {
362                   isLinkRef = true;
363                }
364             }
365             catch (ClassNotFoundException JavaDoc cnfe)
366             {
367                logger.warn("ClassNotFoundException " + cnfe.getMessage());
368             }
369             catch (Exception JavaDoc e)
370             {
371                logger.warn("Exception " + e.getMessage());
372             }
373
374             if (isLinkRef)
375             {
376                try
377                {
378                   newNode = new JNDILinkRef(ctx, name);
379                   node.add(newNode);
380                }
381                catch (Exception JavaDoc e)
382                {
383                }
384             }
385
386             if (recursive)
387             {
388                newNode = new JNDIContext();
389                newNode.setName(name);
390                newNode.setClassName(className);
391
392                try
393                {
394                   Object JavaDoc value = ctx.lookup(name);
395
396                   if (value instanceof Context JavaDoc)
397                   {
398                      Context JavaDoc subctx = (Context JavaDoc) value;
399                      list(subctx, newNode);
400                   }
401                }
402                catch (Throwable JavaDoc t)
403                {
404                   logger.warn("Throwable " + t);
405                }
406             }
407
408             // Try to cast to a predefined object
409
if (newNode == null)
410             {
411                newNode = createNode(ctx, name);
412             }
413
414             // If no match, create a JNDIEntry
415
if (newNode == null)
416             {
417                newNode = new JNDIEntry();
418                newNode.setName(name);
419                newNode.setClassName(className);
420             }
421
422             node.add(newNode);
423          }
424          ne.close();
425       }
426       catch (NamingException JavaDoc ne)
427       {
428          logger.error("error while listing context " + ctx.toString() + ": " + ne.toString(true));
429       }
430    }
431
432    /** Proxies to load */
433    static
434    {
435       proxies.add(org.ejtools.jndi.browser.model.JNDIEnvEntry.class);
436       proxies.add(org.ejtools.jndi.browser.model.ejb.EJBHomeProxy.class);
437       proxies.add(org.ejtools.jndi.browser.model.ejb.EJBLocalHomeProxy.class);
438       proxies.add(org.ejtools.jndi.browser.model.jms.QueueProxy.class);
439       proxies.add(org.ejtools.jndi.browser.model.jms.TopicProxy.class);
440       proxies.add(org.ejtools.jndi.browser.model.jms.ConnectionFactoryProxy.class);
441       proxies.add(org.ejtools.jndi.browser.model.mail.SessionProxy.class);
442       proxies.add(org.ejtools.jndi.browser.model.sql.DataSourceProxy.class);
443       proxies.add(org.ejtools.jndi.browser.model.Proxy.class);
444    }
445 }
446
Popular Tags