| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| TurbineNamingService |
|
| 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 1078552 2011-03-06 19:58:46Z 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 contextKeys = conf.subset("context").getKeys(); |
| 87 | 0 | contextKeys.hasNext();) |
| 88 | { | |
| 89 | 0 | String key = (String) 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 (Iterator contextPropsKeys = contextPropsList.keySet().iterator(); |
| 116 | 0 | contextPropsKeys.hasNext();) |
| 117 | { | |
| 118 | 0 | String key = (String) contextPropsKeys.next(); |
| 119 | 0 | Properties contextProps = contextPropsList.get(key); |
| 120 | 0 | InitialContext context = new InitialContext(contextProps); |
| 121 | 0 | initialContexts.put(key, context); |
| 122 | 0 | } |
| 123 | ||
| 124 | 0 | setInit(true); |
| 125 | } | |
| 126 | 0 | catch (NamingException e) |
| 127 | { | |
| 128 | 0 | log.error("Failed to initialize JDNI contexts!", e); |
| 129 | ||
| 130 | 0 | throw new InitializationException( |
| 131 | "Failed to initialize JDNI contexts!"); | |
| 132 | 0 | } |
| 133 | 0 | } |
| 134 | ||
| 135 | ||
| 136 | /** | |
| 137 | * Return the Context with the specified name. The Context is | |
| 138 | * constructed using the properties for the context with the | |
| 139 | * specified name; ie. those properties that start with | |
| 140 | * "services.servicename.properties.name.". | |
| 141 | * | |
| 142 | * @param contextName The name of the context. | |
| 143 | * @return The context with the specified name, or null if no | |
| 144 | * context exists with that name. | |
| 145 | */ | |
| 146 | public Context getContext(String contextName) | |
| 147 | { | |
| 148 | // Get just the properties for the context with the specified | |
| 149 | // name. | |
| 150 | 0 | Properties contextProps = null; |
| 151 | ||
| 152 | 0 | if (contextPropsList.containsKey(contextName)) |
| 153 | { | |
| 154 | 0 | contextProps = contextPropsList.get(contextName); |
| 155 | } | |
| 156 | else | |
| 157 | { | |
| 158 | 0 | contextProps = new Properties(); |
| 159 | } | |
| 160 | ||
| 161 | // Construct a new context with the properties. | |
| 162 | try | |
| 163 | { | |
| 164 | 0 | return new InitialContext(contextProps); |
| 165 | } | |
| 166 | 0 | catch (Exception e) |
| 167 | { | |
| 168 | 0 | return null; |
| 169 | } | |
| 170 | } | |
| 171 | } |