##### Coverage Report - org.apache.turbine.services.naming.TurbineNamingService

Classes in this File | Line Coverage | Branch Coverage | Complexity
TurbineNamingService |
0% | 0/39
0% | 0/10
| 5.5;5,5

1 |  |
     package org.apache.turbine.services.naming;
2 |  |
3 |  |
4 |  |
     /*
5 |  |
      * Licensed to the Apache Software Foundation (ASF) under one
6 |  |
      * or more contributor license agreements.  See the NOTICE file
7 |  |
      * distributed with this work for additional information
8 |  |
      * regarding copyright ownership.  The ASF licenses this file
9 |  |
      * to you under the Apache License, Version 2.0 (the
10 |  |
      * "License"); you may not use this file except in compliance
11 |  |
      * with the License.  You may obtain a copy of the License at
12 |  |
      *
13 |  |
      *   http://www.apache.org/licenses/LICENSE-2.0
14 |  |
      *
15 |  |
      * Unless required by applicable law or agreed to in writing,
16 |  |
      * software distributed under the License is distributed on an
17 |  |
      * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
18 |  |
      * KIND, either express or implied.  See the License for the
19 |  |
      * specific language governing permissions and limitations
20 |  |
      * under the License.
21 |  |
      */
22 |  |
23 |  |
24 |  |
     import java.util.HashMap;
25 |  |
     import java.util.Iterator;
26 |  |
     import java.util.Map;
27 |  |
     import java.util.Properties;
28 |  |
29 |  |
     import javax.naming.Context;
30 |  |
     import javax.naming.InitialContext;
31 |  |
     import javax.naming.NamingException;
32 |  |
33 |  |
     import org.apache.commons.configuration.Configuration;
34 |  |
     import org.apache.commons.logging.Log;
35 |  |
     import org.apache.commons.logging.LogFactory;
36 |  |
     import org.apache.turbine.Turbine;
37 |  |
     import org.apache.turbine.services.InitializationException;
38 |  |
     import org.apache.turbine.services.TurbineBaseService;
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 |  |
      * @author <a href="mailto:peter@courcoux.biz">Peter Courcoux</a>
48 |  |
      * @version $Id: TurbineNamingService.java 1521104 2013-09-09 13:40:27Z tv $
49 |  |
      */
50 | 0 |
     public class TurbineNamingService
51 |  |
             extends TurbineBaseService
52 |  |
             implements NamingService
53 |  |
     {
54 |  |
         /** Logging */
55 | 0 |
         private static Log log = LogFactory.getLog(TurbineNamingService.class);
56 |  |
57 |  |
         /**
58 |  |
          * A global Map of Property objects which are initialised using
59 |  |
          * parameters from the ResourcesFile
60 |  |
          */
61 | 0 |
         private static Map<String, Properties> contextPropsList = null;
62 |  |
63 |  |
         /** All initial contexts known to this service */
64 | 0 |
         private final Map<String, InitialContext> initialContexts = new HashMap<String, InitialContext>();
65 |  |
66 |  |
         /**
67 |  |
          * Called the first time the Service is used.<br>
68 |  |
          *
69 |  |
          */
70 |  |
         @Override
71 |  |
         public void init()
72 |  |
                 throws InitializationException
73 |  |
         {
74 |  |
             // Context properties are specified in lines in the properties
75 |  |
             // file that begin with "context.contextname.", allowing
76 |  |
             // multiple named contexts to be used.  Everything after the
77 |  |
             // "contextname."  is the name of the property that will be
78 |  |
             // used by the InitialContext class to create a new context
79 |  |
             // instance.
80 |  |
81 | 0 |
             Configuration conf = Turbine.getConfiguration();
82 |  |
             try
83 |  |
             {
84 | 0 |
                 contextPropsList = new HashMap<String, Properties>();
85 |  |
86 | 0 |
                 for (Iterator<String> contextKeys = conf.subset("context").getKeys();
87 | 0 |
                      contextKeys.hasNext();)
88 |  |
                 {
89 | 0 |
                     String key = contextKeys.next();
90 | 0 |
                     int end = key.indexOf(".");
91 |  |
92 | 0 |
                     if (end == -1)
93 |  |
                     {
94 | 0 |
                         continue;
95 |  |
                     }
96 |  |
97 | 0 |
                     String contextName = key.substring(0, end);
98 | 0 |
                     Properties contextProps = null;
99 |  |
100 | 0 |
                     if (contextPropsList.containsKey(contextName))
101 |  |
                     {
102 | 0 |
                         contextProps = contextPropsList.get(contextName);
103 |  |
                     }
104 |  |
                     else
105 |  |
                     {
106 | 0 |
                         contextProps = new Properties();
107 |  |
                     }
108 |  |
109 | 0 |
                     contextProps.put(key.substring(end + 1),
110 |  |
                             conf.getString(key));
111 |  |
112 | 0 |
                     contextPropsList.put(contextName, contextProps);
113 | 0 |
                 }
114 |  |
115 | 0 |
                 for (Map.Entry<String, Properties> entry : contextPropsList.entrySet())
116 |  |
                 {
117 | 0 |
                     String key = entry.getKey();
118 | 0 |
                     Properties contextProps = entry.getValue();
119 | 0 |
                     InitialContext context = new InitialContext(contextProps);
120 | 0 |
                     initialContexts.put(key, context);
121 | 0 |
                 }
122 |  |
123 | 0 |
                 setInit(true);
124 |  |
             }
125 | 0 |
             catch (NamingException e)
126 |  |
             {
127 | 0 |
                 log.error("Failed to initialize JDNI contexts!", e);
128 |  |
129 | 0 |
                 throw new InitializationException(
130 |  |
                         "Failed to initialize JDNI contexts!");
131 | 0 |
             }
132 | 0 |
         }
133 |  |
134 |  |
135 |  |
         /**
136 |  |
          * Return the Context with the specified name.  The Context is
137 |  |
          * constructed using the properties for the context with the
138 |  |
          * specified name; ie. those properties that start with
139 |  |
          * "services.servicename.properties.name.".
140 |  |
          *
141 |  |
          * @param contextName The name of the context.
142 |  |
          * @return The context with the specified name, or null if no
143 |  |
          * context exists with that name.
144 |  |
          */
145 |  |
         public Context getContext(String contextName)
146 |  |
         {
147 |  |
             // Get just the properties for the context with the specified
148 |  |
             // name.
149 | 0 |
             Properties contextProps = null;
150 |  |
151 | 0 |
             if (contextPropsList.containsKey(contextName))
152 |  |
             {
153 | 0 |
                 contextProps = contextPropsList.get(contextName);
154 |  |
             }
155 |  |
             else
156 |  |
             {
157 | 0 |
                 contextProps = new Properties();
158 |  |
             }
159 |  |
160 |  |
             // Construct a new context with the properties.
161 |  |
             try
162 |  |
             {
163 | 0 |
                 return new InitialContext(contextProps);
164 |  |
             }
165 | 0 |
             catch (Exception e)
166 |  |
             {
167 | 0 |
                 return null;
168 |  |
             }
169 |  |
         }
170 |  |
     }