KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sape > carbon > services > ejb > InitialContextUtility


1 /*
2  * The contents of this file are subject to the Sapient Public License
3  * Version 1.0 (the "License"); you may not use this file except in compliance
4  * with the License. You may obtain a copy of the License at
5  * http://carbon.sf.net/License.html.
6  *
7  * Software distributed under the License is distributed on an "AS IS" basis,
8  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
9  * the specific language governing rights and limitations under the License.
10  *
11  * The Original Code is The Carbon Component Framework.
12  *
13  * The Initial Developer of the Original Code is Sapient Corporation
14  *
15  * Copyright (C) 2003 Sapient Corporation. All Rights Reserved.
16  */

17
18 package org.sape.carbon.services.ejb;
19
20
21 import java.util.Hashtable JavaDoc;
22
23 import javax.naming.Context JavaDoc;
24 import javax.naming.InitialContext JavaDoc;
25 import javax.naming.NamingException JavaDoc;
26
27 /**
28  * <p>This utility class aids in the construction of well-formed
29  * <code>InitialContext</code> objects, as required by the EJB service
30  * home factories for JNDI lookups.</p>
31  * <p>Copyright 2002 Sapient</p>
32  * @stereotype utility
33  * @since carbon 1.0
34  * @author Erik M Gottesman, June 2002
35  * @version $Revision: 1.6 $($Author: dvoet $ / $Date: 2003/05/05 21:21:27 $)
36  */

37 public class InitialContextUtility {
38     /**
39      * Private constructor for utility class to block instantiation.
40      */

41     private InitialContextUtility() {
42     }
43
44
45     /**
46      * <p>Utility method that creates the InitialContext onto the JNDI tree
47      * based upon the configured properties.</p>
48      * @param initialContextFactory String representation of the class name to
49      * be used as the <code>InitialContextFactory</code>; this
50      * parameter is analagous to
51      * <code>Context.INITIAL_CONTEXT_FACTORY</code>
52      * @param providerUrl The JNDI provider URL as a Stringl this parameter is
53      * analagous to <code>Context.PROVIDER_URL</code>
54      * @param principal The security principal as a String; this parameter is
55      * analagous to <code>Context.SECURITY_PRINCIPAL</code>
56      * @param credentials The credentials associated with the security principal
57      * as a String; this parameter is analagous to the
58      * <code>Context.SECURITY_CREDENTIALS</code>
59      * @return a new Context based on the given environment.
60      *
61      * @throws NamingException indicates an error building the Context based
62      * on the given environment.
63      */

64     public static Context JavaDoc getInitialContext(String JavaDoc initialContextFactory,
65         String JavaDoc providerUrl,
66         String JavaDoc principal,
67         String JavaDoc credentials)
68         throws NamingException JavaDoc {
69
70         // Local variables to hold the Context and parameters for creating it
71
Context JavaDoc context = null;
72         Hashtable JavaDoc environment = new Hashtable JavaDoc();
73
74         // Go through the four possible parameters for creating the
75
// InitialContext and add all non-null values to the environment
76
// Hashtable.
77
if (null != initialContextFactory) {
78             environment.put(Context.INITIAL_CONTEXT_FACTORY,
79                 initialContextFactory);
80         }
81         if (null != providerUrl) {
82             environment.put(Context.PROVIDER_URL, providerUrl);
83         }
84         if (null != principal) {
85             environment.put(Context.SECURITY_PRINCIPAL, principal);
86         }
87         if (null != credentials) {
88             environment.put(Context.SECURITY_CREDENTIALS, credentials);
89         }
90
91         // If there were any parameters then call the factory method with the
92
// parameters; otherwise, just call the no-arg factory method.
93
if (environment.size() > 0) {
94             context = new InitialContext JavaDoc(environment);
95         } else {
96             context = new InitialContext JavaDoc();
97         }
98
99         // Return the context that was made
100
return context;
101     }
102 }
103
Popular Tags