| <?xml version="1.0" encoding="UTF-8"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml" lang=""><head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/><link rel="stylesheet" href="../jacoco-resources/report.css" type="text/css"/><link rel="shortcut icon" href="../jacoco-resources/report.gif" type="image/gif"/><title>TurbineNamingService.java</title><link rel="stylesheet" href="../jacoco-resources/prettify.css" type="text/css"/><script type="text/javascript" src="../jacoco-resources/prettify.js"></script></head><body onload="window['PR_TAB_WIDTH']=4;prettyPrint()"><div class="breadcrumb" id="breadcrumb"><span class="info"><a href="../jacoco-sessions.html" class="el_session">Sessions</a></span><a href="../index.html" class="el_report">Apache Turbine</a> > <a href="index.source.html" class="el_package">org.apache.turbine.services.naming</a> > <span class="el_source">TurbineNamingService.java</span></div><h1>TurbineNamingService.java</h1><pre class="source lang-java linenums">package org.apache.turbine.services.naming; |
| |
| |
| /* |
| * Licensed to the Apache Software Foundation (ASF) under one |
| * or more contributor license agreements. See the NOTICE file |
| * distributed with this work for additional information |
| * regarding copyright ownership. The ASF licenses this file |
| * to you under the Apache License, Version 2.0 (the |
| * "License"); you may not use this file except in compliance |
| * with the License. You may obtain a copy of the License at |
| * |
| * http://www.apache.org/licenses/LICENSE-2.0 |
| * |
| * Unless required by applicable law or agreed to in writing, |
| * software distributed under the License is distributed on an |
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| * KIND, either express or implied. See the License for the |
| * specific language governing permissions and limitations |
| * under the License. |
| */ |
| |
| |
| import java.util.HashMap; |
| import java.util.Iterator; |
| import java.util.Map; |
| import java.util.Properties; |
| |
| import javax.naming.Context; |
| import javax.naming.InitialContext; |
| import javax.naming.NamingException; |
| |
| import org.apache.commons.configuration2.Configuration; |
| import org.apache.logging.log4j.Logger; |
| import org.apache.logging.log4j.LogManager; |
| import org.apache.turbine.Turbine; |
| import org.apache.turbine.services.InitializationException; |
| import org.apache.turbine.services.TurbineBaseService; |
| |
| /** |
| * This class is the default implementation of NamingService, which |
| * provides JNDI naming contexts. |
| * |
| * @author <a href="mailto:greg@shwoop.com">Greg Ritter</a> |
| * @author <a href="mailto:colin.chalmers@maxware.nl">Colin Chalmers</a> |
| * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a> |
| * @author <a href="mailto:peter@courcoux.biz">Peter Courcoux</a> |
| * @version $Id$ |
| */ |
| <span class="nc" id="L50">public class TurbineNamingService</span> |
| extends TurbineBaseService |
| implements NamingService |
| { |
| /** Logging */ |
| <span class="nc" id="L55"> private static Logger log = LogManager.getLogger(TurbineNamingService.class);</span> |
| |
| /** |
| * A global Map of Property objects which are initialised using |
| * parameters from the ResourcesFile |
| */ |
| <span class="nc" id="L61"> private static Map<String, Properties> contextPropsList = null;</span> |
| |
| /** All initial contexts known to this service */ |
| <span class="nc" id="L64"> private final Map<String, InitialContext> initialContexts = new HashMap<>();</span> |
| |
| /** |
| * Called the first time the Service is used.<br> |
| * |
| */ |
| @Override |
| public void init() |
| throws InitializationException |
| { |
| // Context properties are specified in lines in the properties |
| // file that begin with "context.contextname.", allowing |
| // multiple named contexts to be used. Everything after the |
| // "contextname." is the name of the property that will be |
| // used by the InitialContext class to create a new context |
| // instance. |
| |
| <span class="nc" id="L81"> Configuration conf = Turbine.getConfiguration();</span> |
| try |
| { |
| <span class="nc" id="L84"> contextPropsList = new HashMap<>();</span> |
| |
| <span class="nc" id="L86"> for (Iterator<String> contextKeys = conf.subset("context").getKeys();</span> |
| <span class="nc bnc" id="L87" title="All 2 branches missed."> contextKeys.hasNext();)</span> |
| { |
| <span class="nc" id="L89"> String key = contextKeys.next();</span> |
| <span class="nc" id="L90"> int end = key.indexOf(".");</span> |
| |
| <span class="nc bnc" id="L92" title="All 2 branches missed."> if (end == -1)</span> |
| { |
| <span class="nc" id="L94"> continue;</span> |
| } |
| |
| <span class="nc" id="L97"> String contextName = key.substring(0, end);</span> |
| <span class="nc" id="L98"> Properties contextProps = null;</span> |
| |
| <span class="nc bnc" id="L100" title="All 2 branches missed."> if (contextPropsList.containsKey(contextName))</span> |
| { |
| <span class="nc" id="L102"> contextProps = contextPropsList.get(contextName);</span> |
| } |
| else |
| { |
| <span class="nc" id="L106"> contextProps = new Properties();</span> |
| } |
| |
| <span class="nc" id="L109"> contextProps.put(key.substring(end + 1),</span> |
| <span class="nc" id="L110"> conf.getString(key));</span> |
| |
| <span class="nc" id="L112"> contextPropsList.put(contextName, contextProps);</span> |
| <span class="nc" id="L113"> }</span> |
| |
| <span class="nc bnc" id="L115" title="All 2 branches missed."> for (Map.Entry<String, Properties> entry : contextPropsList.entrySet())</span> |
| { |
| <span class="nc" id="L117"> String key = entry.getKey();</span> |
| <span class="nc" id="L118"> Properties contextProps = entry.getValue();</span> |
| <span class="nc" id="L119"> InitialContext context = new InitialContext(contextProps);</span> |
| <span class="nc" id="L120"> initialContexts.put(key, context);</span> |
| <span class="nc" id="L121"> }</span> |
| |
| <span class="nc" id="L123"> setInit(true);</span> |
| } |
| <span class="nc" id="L125"> catch (NamingException e)</span> |
| { |
| <span class="nc" id="L127"> log.error("Failed to initialize JDNI contexts!", e);</span> |
| |
| <span class="nc" id="L129"> throw new InitializationException(</span> |
| "Failed to initialize JDNI contexts!"); |
| <span class="nc" id="L131"> }</span> |
| <span class="nc" id="L132"> }</span> |
| |
| |
| /** |
| * Return the Context with the specified name. The Context is |
| * constructed using the properties for the context with the |
| * specified name; ie. those properties that start with |
| * "services.servicename.properties.name.". |
| * |
| * @param contextName The name of the context. |
| * @return The context with the specified name, or null if no |
| * context exists with that name. |
| */ |
| @Override |
| public Context getContext(String contextName) |
| { |
| // Get just the properties for the context with the specified |
| // name. |
| <span class="nc" id="L150"> Properties contextProps = null;</span> |
| |
| <span class="nc bnc" id="L152" title="All 2 branches missed."> if (contextPropsList.containsKey(contextName))</span> |
| { |
| <span class="nc" id="L154"> contextProps = contextPropsList.get(contextName);</span> |
| } |
| else |
| { |
| <span class="nc" id="L158"> contextProps = new Properties();</span> |
| } |
| |
| // Construct a new context with the properties. |
| try |
| { |
| <span class="nc" id="L164"> return new InitialContext(contextProps);</span> |
| } |
| <span class="nc" id="L166"> catch (Exception e)</span> |
| { |
| <span class="nc" id="L168"> return null;</span> |
| } |
| } |
| } |
| </pre><div class="footer"><span class="right">Created with <a href="http://www.jacoco.org/jacoco">JaCoCo</a> 0.8.12.202403310830</span></div></body></html> |