KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > rift > coad > lib > naming > ContextManager


1 /*
2  * CoadunationLib: The coaduntion implementation library.
3  * Copyright (C) 2006 Rift IT Contracting
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18  *
19  * ContextManager.java
20  *
21  * This object managers a context.
22  */

23
24 // package path
25
package com.rift.coad.lib.naming;
26
27 // logging import
28
import org.apache.log4j.Logger;
29
30 // java imports
31
import java.util.StringTokenizer JavaDoc;
32 import javax.naming.Context JavaDoc;
33 import javax.naming.InitialContext JavaDoc;
34 import javax.naming.CompositeName JavaDoc;
35
36 /**
37  * This object managers a context.
38  *
39  * @author Brett Chaldecott
40  */

41 public class ContextManager {
42     
43     // the class log variable
44
protected Logger log =
45         Logger.getLogger(ContextManager.class.getName());
46     
47     // the classes private member variables.
48
private Context JavaDoc baseContext = null;
49     private String JavaDoc base = null;
50     private String JavaDoc contextPath = null;
51     
52     /**
53      * Creates a new instance of ContextManager
54      *
55      * @param base The base string context.
56      * @exception NamingException
57      */

58     public ContextManager(String JavaDoc base) throws NamingException {
59         this.base = base;
60         if (base.endsWith("/")) {
61             this.base = base.substring(0,base.length() - 1);
62         }
63         baseContext = initContext(this.base);
64     }
65     
66     
67     /**
68      * This method will bind the object to the context.
69      *
70      * @param name The name of the context.
71      * @param ref The reference to the object.
72      * @exception NamingException
73      */

74     public void bind(String JavaDoc name, Object JavaDoc ref) throws NamingException {
75         try {
76             if (ref == null) {
77                 throw new NamingException("The reference to bind is null");
78             }
79             String JavaDoc bindName = name;
80             if (contextPath != null) {
81                 bindName = contextPath + "/" + name;
82             }
83             log.info("Binding [" + bindName + "]");
84             baseContext.bind(bindName,ref);
85         } catch (NamingException ex) {
86             throw ex;
87         } catch (Exception JavaDoc ex) {
88             log.error("Failed to bind to the context : " + ex.getMessage(),ex);
89             throw new NamingException(
90                     "Failed to bind the object to the context : " +
91                     ex.getMessage(),ex);
92         }
93     }
94     
95     
96     /**
97      * This method will un bind the object from the context.
98      *
99      * @param name The name of the context.
100      * @exception NamingException
101      */

102     public void unbind(String JavaDoc name) throws NamingException {
103         try {
104             String JavaDoc bindName = name;
105             if (contextPath != null) {
106                 bindName = contextPath + "/" + name;
107             }
108             baseContext.unbind(bindName);
109             log.info("Un-bound [" + base + "/" + name + "]");
110         } catch (Exception JavaDoc ex) {
111             log.error("Failed to unbind to the context : " + ex.getMessage(),ex);
112             throw new NamingException(
113                     "Failed to unbind the object to the context : " +
114                     ex.getMessage(),ex);
115         }
116     }
117     
118     
119     /**
120      * This method setups the initial context.
121      *
122      * @return The reference to the base context.
123      * @param base The base context.
124      */

125     private Context JavaDoc initContext(String JavaDoc base) throws NamingException {
126         try {
127             Context JavaDoc context = new InitialContext JavaDoc();
128             StringTokenizer JavaDoc stringTok = new StringTokenizer JavaDoc(base,"/");
129             while(stringTok.hasMoreTokens()) {
130                 String JavaDoc entry = stringTok.nextToken();
131                 Context JavaDoc subContext = null;
132                 try {
133                     subContext = (Context JavaDoc)context.lookup(entry);
134                 } catch (Exception JavaDoc exc) {
135                     // nothing
136
} finally {
137                     if (subContext == null) {
138                         try {
139                             subContext = context.createSubcontext(entry);
140                         } catch (javax.naming.OperationNotSupportedException JavaDoc ex) {
141                             // this is to deal with some JNDI implementations
142
// not supporting createSubcontext
143
contextPath = base;
144                             return context;
145                         } catch (javax.naming.NameAlreadyBoundException JavaDoc ex) {
146                             contextPath = base;
147                             return context;
148                         }
149                     }
150                 }
151                 context = subContext;
152             }
153             return context;
154         } catch (Exception JavaDoc ex) {
155             log.error("Failed to init the context : " + ex.getMessage(),ex);
156             throw new NamingException("Failed to init the context : " +
157                     ex.getMessage(),ex);
158         }
159     }
160 }
161
Popular Tags