KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ejbca > core > ejb > InitialContextBuilder


1 /*************************************************************************
2  * *
3  * EJBCA: The OpenSource Certificate Authority *
4  * *
5  * This software 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 any later version. *
9  * *
10  * See terms of license at gnu.org. *
11  * *
12  *************************************************************************/

13
14 package org.ejbca.core.ejb;
15
16 import java.io.IOException JavaDoc;
17 import java.io.InputStream JavaDoc;
18 import java.util.Hashtable JavaDoc;
19 import java.util.Properties JavaDoc;
20
21 import javax.naming.InitialContext JavaDoc;
22 import javax.naming.NamingException JavaDoc;
23
24 /**
25  * Utility class to manage creation of InitialContext.
26  * <p/>
27  *
28  * @version $Id: InitialContextBuilder.java,v 1.1 2006/01/17 20:30:04 anatom Exp $
29  */

30
31 public class InitialContextBuilder {
32     /** Filename where find out jndi properties to apply */
33     private final static String JavaDoc PROPERTIES = "ejbca.properties";
34
35     /** Singleton instance */
36     static private InitialContextBuilder instance = null;
37
38     /** Cached properties found in ajbca.properties */
39     private Properties JavaDoc cacheEnv = null;
40
41     /** Cached context */
42     private InitialContext JavaDoc cacheCtx = null;
43
44     /**
45      * Return the only instance permited of itself. It follow a Singleton design pattern.
46      *
47      * @return the instance
48      */

49     static public InitialContextBuilder getInstance() {
50         if( instance == null ) {
51             instance = new InitialContextBuilder();
52         }
53         return instance;
54     }
55
56     /**
57      * Private constructor to avoid instance this class.
58      */

59     private InitialContextBuilder() {
60         // try to load ejbca.properties into cacheEnv
61
// it could be in any part of classpath
62
try {
63             ClassLoader JavaDoc cl = ClassLoader.getSystemClassLoader();
64             cacheEnv = new Properties JavaDoc();
65             InputStream JavaDoc inStream = cl.getResourceAsStream(InitialContextBuilder.PROPERTIES);
66             cacheEnv.load( inStream );
67             try { inStream.close(); } catch ( IOException JavaDoc ioex ) {}
68         } catch (Exception JavaDoc ex2) {
69             cacheEnv = null;
70         }
71     }
72
73     /**
74      * Return the configured context
75      *
76      * @return the requested context
77      * @throws NamingException if there is an error creating the context
78      */

79     public InitialContext JavaDoc getInitialContext() throws NamingException JavaDoc {
80         return getInitialContext(null);
81     }
82
83     /**
84      * Return the requested context.
85      * <p/>
86      * Try to use env to configure the context, if env is null,
87      * try to use cacheEnv and if it is null too try to use jndi.propertied
88      *
89      * @param env the properties to configure the context.
90      * @return the requested context
91      * @throws NamingException if there is an error creating the datasource
92      */

93     public InitialContext JavaDoc getInitialContext(Hashtable JavaDoc env) throws NamingException JavaDoc {
94         if( cacheCtx == null ) {
95             if( env != null ) {
96                 // try the environmento given by the user
97
cacheCtx = new InitialContext JavaDoc(env);
98             } else if ( cacheEnv != null ) {
99                 // try ejbca.properties
100
cacheCtx = new InitialContext JavaDoc(cacheEnv);
101             } else {
102                 // try jndi.properties
103
cacheCtx = new InitialContext JavaDoc();
104             }
105         }
106         return cacheCtx;
107     }
108 }
109
110 // vi:ts=4 syntax=off
Popular Tags