KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > naming > config > Config


1 /*
2  * Copyright 1999,2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.naming.config;
18
19 import java.util.Collection JavaDoc;
20 import java.util.Collections JavaDoc;
21 import java.util.HashMap JavaDoc;
22 import java.util.Iterator JavaDoc;
23 import java.util.LinkedList JavaDoc;
24 import java.util.Map JavaDoc;
25 import java.util.Set JavaDoc;
26 import java.util.TreeSet JavaDoc;
27
28 import javax.naming.CompositeName JavaDoc;
29 import javax.naming.InvalidNameException JavaDoc;
30 import javax.naming.StringRefAddr JavaDoc;
31
32 import org.apache.commons.lang.builder.ToStringBuilder;
33 import org.apache.commons.lang.builder.ToStringStyle;
34 import org.apache.naming.ResourceRef;
35
36 /**
37  * Configuration classes.
38  *
39  * @author <a HREF="brett@apache.org">Brett Porter</a>
40  * @version $Id: Config.java,v 1.2 2003/12/01 02:02:45 brett Exp $
41  */

42 public final class Config
43 {
44     /**
45      * Naming context configuration.
46      */

47     public static final class Naming
48     {
49         /** list of context configurations */
50         private final Collection JavaDoc contextList = new LinkedList JavaDoc();
51
52         /**
53          * Adds a new Context configuration to the context list.
54          *
55          * @param context Context configuration to add.
56          */

57         public void addContext(Context context)
58         {
59             contextList.add(context);
60         }
61         
62         /**
63          * Returns the context list.
64          *
65          * @return context list.
66          */

67         public Collection JavaDoc getContextList()
68         {
69             return Collections.unmodifiableCollection(contextList);
70         }
71
72         /**
73          * Generates and returns a sorted list of all configured names.
74          *
75          * @return configured names
76          * @throws InvalidNameException if an invalid name is encountered
77          */

78         public Set JavaDoc generateSortedSubcontextNameSet() throws InvalidNameException JavaDoc
79         {
80             Set JavaDoc sortedSubcontextNameSet = new TreeSet JavaDoc();
81             for (Iterator JavaDoc i = contextList.iterator(); i.hasNext();)
82             {
83                 Context context = (Context) i.next();
84                 context.addSubContextNames(sortedSubcontextNameSet);
85             }
86             return Collections.unmodifiableSet(sortedSubcontextNameSet);
87         }
88
89         /**
90          * Returns a string representation of the context list.
91          *
92          * @return context list as a string.
93          */

94         public String JavaDoc toString()
95         {
96             return new ToStringBuilder(this, ToStringStyle.MULTI_LINE_STYLE)
97                 .append("contextList", contextList)
98                 .toString();
99         }
100     }
101
102     /**
103      * Configuration for a Context. Contexts contain lists of
104      * {@link org.apache.naming.config.Config$Environment} entries and
105      * {@link org.apache.naming.config.Config$Resource} references.
106      */

107     public static final class Context
108     {
109         private String JavaDoc name;
110         private final Collection JavaDoc environmentList = new LinkedList JavaDoc();
111         private final Collection JavaDoc resourceList = new LinkedList JavaDoc();
112
113         /**
114          * Adds an Environment configuration to the environment list.
115          *
116          * @param environment environment configuration to add.
117          */

118         public void addEnvironment(Environment environment)
119         {
120             environmentList.add(environment);
121         }
122
123         /**
124          * Adds the subcontext names in this Context to the input set.
125          *
126          * @param sortedSubcontextNameSet set to be augmented with names from
127          * this context.
128          * @throws InvalidNameException if the configured string name of a
129          * Resource or Environment in this context is not a valid JNDI name.
130          */

131         public void addSubContextNames(Set JavaDoc sortedSubcontextNameSet) throws InvalidNameException JavaDoc
132         {
133             if ((name != null) && !environmentList.iterator().hasNext() && !resourceList.iterator().hasNext())
134             {
135                 sortedSubcontextNameSet.add(name);
136             }
137             for (Iterator JavaDoc i = environmentList.iterator(); i.hasNext();)
138             {
139                 Environment e = (Environment) i.next();
140                 CompositeName JavaDoc name = new CompositeName JavaDoc(e.getName());
141                 addSubContextNames(name, sortedSubcontextNameSet);
142             }
143             for (Iterator JavaDoc i = resourceList.iterator(); i.hasNext();)
144             {
145                 Resource r = (Resource) i.next();
146                 CompositeName JavaDoc name = new CompositeName JavaDoc(r.getName());
147                 addSubContextNames(name, sortedSubcontextNameSet);
148             }
149         }
150
151         private void addSubContextNames(CompositeName JavaDoc name, Set JavaDoc sortedSubcontextNameSet) {
152             for (int j = 1; j <= name.size() - 1; j++) {
153                 sortedSubcontextNameSet.add(name.getPrefix(j).toString());
154             }
155         }
156
157         /**
158          * Adds a Resource configuration to the resource list.
159          *
160          * @param resource resource configuration to add.
161          */

162         public void addResource(Resource resource)
163         {
164             resourceList.add(resource);
165         }
166         
167         /**
168          * Returns the environment list.
169          *
170          * @return list of Environment configurations in the Context
171          */

172         public Collection JavaDoc getEnvironmentList()
173         {
174             return Collections.unmodifiableCollection(environmentList);
175         }
176
177         /**
178          * Returns the name of this context.
179          *
180          * @return context name
181          */

182         public String JavaDoc getName()
183         {
184             return name;
185         }
186
187         /**
188          * Sets the name of this context.
189          *
190          * @param name the name
191          */

192         public void setName(String JavaDoc name)
193         {
194             this.name = name;
195         }
196
197         /**
198          * Returns the resource list.
199          *
200          * @return list of Resource configurations in the Context.
201          */

202         public Collection JavaDoc getResourceList()
203         {
204             return Collections.unmodifiableCollection(resourceList);
205         }
206
207         /**
208          * Returns a string representation of the name, environment list and
209          * resource list of this context.
210          *
211          * @return string representation of this context.
212          */

213         public String JavaDoc toString()
214         {
215             return new ToStringBuilder(this, ToStringStyle.MULTI_LINE_STYLE)
216                 .append("name", name)
217                 .append("environmentList", environmentList)
218                 .append("resourceList", resourceList)
219                 .toString();
220         }
221     }
222
223     /**
224      * Configuration for an Environment entry. Environment entries represent
225      * JNDI environment properties that take values that are primitive java
226      * types. The Environment configuration includes the type, the value and
227      * the JNDI name as a string, relative to the initial context.
228      */

229     public static final class Environment
230     {
231         private String JavaDoc name;
232         private String JavaDoc value;
233         private String JavaDoc type;
234         
235         /**
236          * Gets the name of this environment.
237          *
238          * @return name the name
239          */

240         public String JavaDoc getName()
241         {
242             return name;
243         }
244
245         /**
246          * Sets the name of this environment.
247          *
248          * @param name the name
249          */

250         public void setName(String JavaDoc name)
251         {
252             this.name = name;
253         }
254
255         /**
256          * Returns the class name of this environment entry.
257          *
258          * @return Environment entry class name
259          */

260         public String JavaDoc getType()
261         {
262             return type;
263         }
264
265         /**
266          * Sets the class name of this environment entry.
267          *
268          * @param type class name
269          */

270         public void setType(String JavaDoc type)
271         {
272             this.type = type;
273         }
274
275         /**
276          * Returns the value of this environment entry as a String.
277          *
278          * @return String representation of the value
279          */

280         public String JavaDoc getValue()
281         {
282             return value;
283         }
284
285         /**
286          * Sets the (String) value of this environment entry.
287          *
288          * @param value
289          */

290         public void setValue(String JavaDoc value)
291         {
292             this.value = value;
293         }
294
295         /**
296          * Returns the JNDI name, type and value of this environment entry as
297          * as String.
298          *
299          * @return String representation of this environment entry.
300          */

301         public String JavaDoc toString()
302         {
303             return new ToStringBuilder(this, ToStringStyle.MULTI_LINE_STYLE)
304                 .append("name", name)
305                 .append("type", type)
306                 .append("value", value)
307                 .toString();
308         }
309
310         /**
311          * Tries to create an instance of type <code>this.type</code> using
312          * <code>this.value</code>.
313          * <p>
314          * Only primitive types are currently supported. Wrapper object
315          * <code>valueOf</code> methods are used and runtime exceptions
316          * are not handled. If <code>this.type</code> is not a primitive type,
317          * <code>null</code> is returned.
318          *
319          * @return object instance
320          */

321         public Object JavaDoc createValue()
322         //TODO: handle / rethrow exceptions, support more types?
323
{
324             if (type.equals(String JavaDoc.class.getName()))
325             {
326                 return value;
327             }
328             else if (type.equals(Boolean JavaDoc.class.getName()))
329             {
330                 return Boolean.valueOf(value);
331             }
332             else if (type.equals(Integer JavaDoc.class.getName()))
333             {
334                 return Integer.valueOf(value);
335             }
336             else if (type.equals(Short JavaDoc.class.getName()))
337             {
338                 return Short.valueOf(value);
339             }
340             else if (type.equals(Character JavaDoc.class.getName()))
341             {
342                 return new Character JavaDoc(value.charAt(0));
343             }
344             else if (type.equals(Double JavaDoc.class.getName()))
345             {
346                 return Double.valueOf(value);
347             }
348             else if (type.equals(Float JavaDoc.class.getName()))
349             {
350                 return Float.valueOf(value);
351             }
352             else if (type.equals(Byte JavaDoc.class.getName()))
353             {
354                 return Byte.valueOf(value);
355             }
356             else if (type.equals(Long JavaDoc.class.getName()))
357             {
358                 return Long.valueOf(value);
359             }
360             return null;
361         }
362     }
363
364     /**
365     * Configuration for an JNDI resource reference. Resource references
366     * include the type of the resource, the parameters to be used in creating
367     * the resource instance and the JNDI name of the resource as a string,
368     * relative to the initial context.
369     */

370     public static final class Resource
371     {
372         private String JavaDoc name;
373         private String JavaDoc type;
374         private final Map JavaDoc parameters = new HashMap JavaDoc();
375
376         /**
377          * Adds a name-value pair to the parameters associated with this resource.
378          *
379          * @param name parameter name
380          * @param value parameter value
381          */

382         public void addParameter(String JavaDoc name, String JavaDoc value)
383         {
384             parameters.put(name, value);
385         }
386         
387         /**
388          * Returns the name of this resource.
389          *
390          * @return name
391          */

392         public String JavaDoc getName()
393         {
394             return name;
395         }
396
397         /**
398          * Sets the name of this resource.
399          *
400          * @param name name.
401          */

402         public void setName(String JavaDoc name)
403         {
404             this.name = name;
405         }
406
407         /**
408          * Returns the parameters associated with this resource as a Map.
409          * The keys of the map are the parameter names.
410          *
411          * @return parameters
412          */

413         public Map JavaDoc getParameters()
414         {
415             return parameters;
416         }
417
418         /**
419          * Returns the type of this resource.
420          *
421          * @return class name
422          */

423         public String JavaDoc getType()
424         {
425             return type;
426         }
427
428         /**
429          * Sets the type of this resource.
430          *
431          * @param type class name.
432          */

433         public void setType(String JavaDoc type)
434         {
435             this.type = type;
436         }
437
438        /**
439         * Creates a {@link ResourceRef} based on the configuration
440         * properties of this resource.
441         *
442         * @return ResourceRef instance.
443         */

444         public Object JavaDoc createValue()
445         //TODO: exceptions?
446
{
447             ResourceRef ref = new ResourceRef(type, null, null, null);
448             for (Iterator JavaDoc i = parameters.keySet().iterator(); i.hasNext();)
449             {
450                 String JavaDoc name = (String JavaDoc) i.next();
451                 String JavaDoc value = (String JavaDoc) parameters.get(name);
452                 ref.add(new StringRefAddr JavaDoc(name, value));
453             }
454             return ref;
455         }
456
457         /**
458          * Returns the name, type and parameter list as a String.
459          *
460          * @return String representation of this resource reference configuration.
461          */

462         public String JavaDoc toString()
463         {
464             return new ToStringBuilder(this, ToStringStyle.MULTI_LINE_STYLE)
465                 .append("name", name)
466                 .append("type", type)
467                 .append("parameters", parameters)
468                 .toString();
469         }
470     }
471 }
472
473
Popular Tags