View Javadoc

1   package org.apache.turbine.services.naming;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.util.HashMap;
23  import java.util.Iterator;
24  import java.util.Map;
25  import java.util.Properties;
26  
27  import javax.naming.Context;
28  import javax.naming.InitialContext;
29  
30  import org.apache.commons.configuration.Configuration;
31  
32  import org.apache.commons.logging.Log;
33  import org.apache.commons.logging.LogFactory;
34  
35  import org.apache.turbine.Turbine;
36  import org.apache.turbine.services.InitializationException;
37  import org.apache.turbine.services.TurbineBaseService;
38  import org.apache.turbine.util.RunData;
39  
40  /***
41   * This class is the default implementation of NamingService, which
42   * provides JNDI naming contexts.
43   *
44   * @author <a href="mailto:greg@shwoop.com">Greg Ritter</a>
45   * @author <a href="mailto:colin.chalmers@maxware.nl">Colin Chalmers</a>
46   * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
47   * @version $Id: TurbineNamingService.java 534527 2007-05-02 16:10:59Z tv $
48   */
49  public class TurbineNamingService
50          extends TurbineBaseService
51          implements NamingService
52  {
53      /*** Logging */
54      private static Log log = LogFactory.getLog(TurbineNamingService.class);
55  
56      /***
57       * A global Map of Property objects which are initialised using
58       * parameters from the ResourcesFile
59       */
60      private Map contextPropsList = null;
61  
62      /*** All initial contexts known to this service */
63      private Map initialContexts = new HashMap();
64  
65      /***
66       * Called the first time the Service is used.<br>
67       *
68       */
69      public void init()
70              throws InitializationException
71      {
72          // Context properties are specified in lines in the properties
73          // file that begin with "context.contextname.", allowing
74          // multiple named contexts to be used.  Everything after the
75          // "contextname."  is the name of the property that will be
76          // used by the InitialContext class to create a new context
77          // instance.
78  
79          Configuration conf = Turbine.getConfiguration();
80          try
81          {
82              contextPropsList = new HashMap();
83  
84              for (Iterator contextKeys = conf.subset("context").getKeys();
85                   contextKeys.hasNext();)
86              {
87                  String key = (String) contextKeys.next();
88                  int end = key.indexOf(".");
89  
90                  if (end == -1)
91                  {
92                      continue;
93                  }
94  
95                  String contextName = key.substring(0, end);
96                  Properties contextProps = null;
97  
98                  if (contextPropsList.containsKey(contextName))
99                  {
100                     contextProps = (Properties)
101                             contextPropsList.get(contextName);
102                 }
103                 else
104                 {
105                     contextProps = new Properties();
106                 }
107 
108                 contextProps.put(key.substring(end + 1),
109                         conf.getString(key));
110 
111                 contextPropsList.put(contextName, contextProps);
112             }
113 
114             for (Iterator contextPropsKeys = contextPropsList.keySet().iterator();
115                  contextPropsKeys.hasNext();)
116             {
117                 String key = (String) contextPropsKeys.next();
118                 Properties contextProps = (Properties) contextPropsList.get(key);
119                 InitialContext context = new InitialContext(contextProps);
120                 initialContexts.put(key, context);
121             }
122 
123             setInit(true);
124         }
125         catch (Exception e)
126         {
127             log.error("Failed to initialize JDNI contexts!", e);
128 
129             throw new InitializationException(
130                     "Failed to initialize JDNI contexts!");
131         }
132     }
133 
134     /***
135      * Places the contexts defined in the TurbineResources instance
136      * (if any) into the data.contexts Map.
137      *
138      * @param data The RunData object for the current request.
139      * @exception InitializationException, if there was a problem
140      * during initialization.
141      * @deprecated This should never have been here. No replacement.
142      */
143     public void init(RunData data) throws InitializationException
144     {
145         try
146         {
147             if (contextPropsList == null)
148             {
149                 init();
150             }
151 
152             for (Iterator it = contextPropsList.keySet().iterator(); it.hasNext();)
153             {
154                 String key = (String) it.next();
155                 Properties contextProps =
156                         (Properties) contextPropsList.get(key);
157                 InitialContext context = new InitialContext(contextProps);
158                 data.getJNDIContexts().put(key, context);
159             }
160         }
161         catch (Exception e)
162         {
163             log.error("Failed to initialize JDNI contexts!", e);
164 
165             throw new InitializationException(
166                     "Failed to initialize JDNI contexts!");
167         }
168     }
169 
170     /***
171      * Return the Context with the specified name.  The Context is
172      * constructed using the properties for the context with the
173      * specified name; ie. those properties that start with
174      * "services.servicename.properties.name.".
175      *
176      * @param contextName The name of the context.
177      * @return The context with the specified name, or null if no
178      * context exists with that name.
179      */
180     public Context getContext(String contextName)
181     {
182         // Get just the properties for the context with the specified
183         // name.
184         Properties contextProps = null;
185 
186         if (contextPropsList.containsKey(contextName))
187         {
188             contextProps = (Properties) contextPropsList.get(contextName);
189         }
190         else
191         {
192             contextProps = new Properties();
193         }
194 
195         // Construct a new context with the properties.
196         try
197         {
198             return new InitialContext(contextProps);
199         }
200         catch (Exception e)
201         {
202             return null;
203         }
204     }
205 }