KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > carol > util > configuration > ProtocolConfigurationImpl


1 /**
2  * Copyright (C) 2005 - Bull S.A.
3  *
4  * CAROL: Common Architecture for RMI ObjectWeb Layer
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * --------------------------------------------------------------------------
22  * $Id: ProtocolConfigurationImpl.java,v 1.3 2005/04/28 11:37:26 benoitf Exp $
23  * --------------------------------------------------------------------------
24  */

25 package org.objectweb.carol.util.configuration;
26
27 import java.util.ArrayList JavaDoc;
28 import java.util.Hashtable JavaDoc;
29 import java.util.List JavaDoc;
30 import java.util.Properties JavaDoc;
31 import java.util.StringTokenizer JavaDoc;
32
33 import javax.naming.Context JavaDoc;
34 import javax.naming.InitialContext JavaDoc;
35 import javax.naming.NameClassPair JavaDoc;
36 import javax.naming.NamingEnumeration JavaDoc;
37 import javax.naming.NamingException JavaDoc;
38
39 /**
40  * This class manage a rmi configuration used by carol.<br>
41  * The configuration is based on a protocol which contains required values and non modified values
42  */

43 public class ProtocolConfigurationImpl implements ProtocolConfiguration, ProtocolConfigurationImplMBean {
44
45     /**
46      * Name of this configuration
47      */

48     private String JavaDoc name = null;
49
50     /**
51      * Protocol used by this configuration
52      */

53     private Protocol protocol = null;
54
55     /**
56      * Properties of this configuration
57      */

58     private Properties JavaDoc properties = null;
59
60     /**
61      * JNDI env
62      */

63     private Hashtable JavaDoc jndiEnv = null;
64
65     /**
66      * Host for this protocol name service
67      */

68     private String JavaDoc host = null;
69
70     /**
71      * port number for this protocol name servce
72      */

73     private int port = 0;
74
75     /**
76      * Object name
77      */

78     private String JavaDoc objectName = null;
79
80     /**
81      * Build a new configuration with given parameters
82      * @param name the name of this protocol configuration
83      * @param protocol the protocol object used by this configuration
84      * @param properties for this object
85      * @throws ConfigurationException if configuration is not correct
86      */

87     public ProtocolConfigurationImpl(String JavaDoc name, Protocol protocol, Properties JavaDoc properties) throws ConfigurationException {
88         this.name = name;
89         this.protocol = protocol;
90
91         configure(properties);
92     }
93
94
95     /**
96      * Configure this configuration with a given properties object
97      * @param properties given properties
98      * @throws ConfigurationException if the given config is invalid
99      */

100     public void configure(Properties JavaDoc properties) throws ConfigurationException {
101         if (properties == null) {
102             throw new ConfigurationException("Cannot build a configuration with a null properties object");
103         }
104         this.properties = properties;
105
106
107         // extract JNDI env for InitialContext() creation
108
extractJNDIProperties();
109
110         // extract host and port of URL
111
parseURL();
112     }
113
114     /**
115      * Extract JNDI properties of properties
116      * @throws ConfigurationException if properties are missing
117      */

118     protected void extractJNDIProperties() throws ConfigurationException {
119         jndiEnv = new Hashtable JavaDoc();
120         String JavaDoc prefixProtocol = CarolDefaultValues.CAROL_PREFIX + "." + protocol.getName() + ".";
121
122         jndiEnv.put(Context.PROVIDER_URL, getValue(prefixProtocol + CarolDefaultValues.URL_PREFIX));
123         jndiEnv.put(Context.INITIAL_CONTEXT_FACTORY, protocol.getInitialContextFactoryClassName());
124     }
125
126
127     /**
128      * Parse URL to extract host and port
129      * @throws ConfigurationException if URL is not correct
130      */

131     protected void parseURL() throws ConfigurationException {
132         String JavaDoc url = getProviderURL();
133         port = getPortOfUrl(url);
134         host = getHostOfUrl(url);
135     }
136
137     /**
138      * Build an initial context with the given environment using our configuration
139      * @param env parameters for the initial context
140      * @return an InitialContext
141      * @throws NamingException if the context is not created
142      */

143     public Context JavaDoc getInitialContext(Hashtable JavaDoc env) throws NamingException JavaDoc {
144         Hashtable JavaDoc newEnv = (Hashtable JavaDoc) env.clone();
145         newEnv.putAll(jndiEnv);
146         return new InitialContext JavaDoc(newEnv);
147     }
148
149     /**
150      * @return the protocol used by this configuration.
151      */

152     public Protocol getProtocol() {
153         return protocol;
154     }
155
156     /**
157      * @return the name of this configuration
158      */

159     public String JavaDoc getName() {
160         return name;
161     }
162
163     /**
164      * @return a copy of the properties of this configuration
165      */

166     public Properties JavaDoc getProperties() {
167         return (Properties JavaDoc) properties.clone();
168     }
169
170     /**
171      * @return the host.
172      */

173     public String JavaDoc getHost() {
174         return host;
175     }
176
177     /**
178      * @return the port for this protocol name service
179      */

180     public int getPort() {
181         return port;
182     }
183
184     /**
185      * @return the Provider URL attribute
186      */

187     public String JavaDoc getProviderURL() {
188         return (String JavaDoc) jndiEnv.get(Context.PROVIDER_URL);
189     }
190
191     /**
192      * Parses the given url, and returns the port number. 0 is given in error
193      * case)
194      * @param url given url on which extract port number
195      * @return port number of the url
196      * @throws ConfigurationException if URL is invalid
197      */

198     protected int getPortOfUrl(String JavaDoc url) throws ConfigurationException {
199         int portNumber = 0;
200         try {
201             StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(url, ":");
202             st.nextToken();
203             st.nextToken();
204             if (st.hasMoreTokens()) {
205                 StringTokenizer JavaDoc lastst = new StringTokenizer JavaDoc(st.nextToken(), "/");
206                 String JavaDoc pts = lastst.nextToken().trim();
207                 int i = pts.indexOf(',');
208                 if (i > 0) {
209                     pts = pts.substring(0, i);
210                 }
211                 portNumber = new Integer JavaDoc(pts).intValue();
212             }
213             return portNumber;
214         } catch (Exception JavaDoc e) {
215             // don't rethrow original exception. only URL name is important
216
throw new ConfigurationException("Invalid URL '" + url + "'. It should be on the format <protocol>://<hostname>:<port>");
217         }
218     }
219
220     /**
221      * Parses the given url, and returns the hostname
222      * If not found, returns localhost
223      * @param url given url on which extract hostname
224      * @return hostname of the url
225      * @throws ConfigurationException if URL is invalid
226      */

227     protected String JavaDoc getHostOfUrl(String JavaDoc url) throws ConfigurationException {
228         String JavaDoc host = null;
229         // this would be simpler with a regexp :)
230
try {
231             // url is of the form protocol://<hostname>:<port>
232
String JavaDoc[] tmpSplitStr = url.split(":");
233
234             // array should be of length = 3
235
// get 2nd element (should be //<hostname>)
236
String JavaDoc tmpHost = tmpSplitStr[1];
237
238             // remove //
239
String JavaDoc[] tmpSplitHost = tmpHost.split("/");
240
241             // Get last element of the array to get hostname
242
host = tmpSplitHost[tmpSplitHost.length - 1];
243         } catch (Exception JavaDoc e) {
244             // don't rethrow original exception. only URL name is important
245
throw new ConfigurationException("Invalid URL '" + url + "'. It should be on the format <protocol>://<hostname>:<port>");
246         }
247         return host;
248     }
249
250
251     /**
252      * Gets value of properties object
253      * @param key the key of the properties
254      * @return value stored in a property object
255      * @throws ConfigurationException if properties are missing
256      */

257     protected String JavaDoc getValue(String JavaDoc key) throws ConfigurationException {
258         // properties cannot be null, check in constructor
259
String JavaDoc s = properties.getProperty(key);
260         if (s == null) {
261             throw new ConfigurationException("Property '" + key + "' was not found in the properties object of the protocol, properties are :'" + properties + "'");
262         }
263         return s;
264     }
265
266
267
268     /**
269      * @return the jndiEnv (used for InitialContext).
270      */

271     public Hashtable JavaDoc getJndiEnv() {
272         return jndiEnv;
273     }
274
275     /**
276      * MBean method : Gets the InitialContextFactory classname (Context.INITIAL_CONTEXT_FACTORY)
277      * @return the InitialContextFactory classname
278      */

279     public String JavaDoc getInitialContextFactoryClassName() {
280         return protocol.getInitialContextFactoryClassName();
281     }
282
283     /**
284      * Gets JNDI names of the context with this configuration
285      * @return JNDI names
286      * @throws NamingException if the names cannot be listed
287      */

288     public List JavaDoc getNames() throws NamingException JavaDoc {
289         Context JavaDoc ctx = getInitialContext(getJndiEnv());
290
291         List JavaDoc names = new ArrayList JavaDoc();
292         NamingEnumeration JavaDoc ne = ctx.list("");
293         String JavaDoc n = null;
294         while (ne.hasMore()) {
295             NameClassPair JavaDoc ncp = (NameClassPair JavaDoc) ne.next();
296             n = ncp.getName();
297             names.add(n);
298         }
299         return names;
300     }
301
302     /**
303      * @return Object Name
304      */

305     public String JavaDoc getobjectName() {
306         return objectName;
307     }
308
309     /**
310      * Sets the object name of this mbean
311      * @param name the Object Name
312      */

313     public void setobjectName(String JavaDoc name) {
314         this.objectName = name;
315     }
316
317     /**
318      * @return true if it is an event provider
319      */

320     public boolean iseventProvider() {
321         return false;
322     }
323
324     /**
325      * @return true if this managed object implements J2EE State Management
326      * Model
327      */

328     public boolean isstateManageable() {
329         return false;
330     }
331
332     /**
333      * @return true if this managed object implements the J2EE StatisticProvider
334      * Model
335      */

336     public boolean isstatisticsProvider() {
337         return false;
338     }
339
340 }
341
Popular Tags