blob: f7be0c0a43c33431a7d68504fc71c9df1cba5030 [file] [log] [blame]
<?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> &gt; <a href="index.source.html" class="el_package">org.apache.turbine.services.naming</a> &gt; <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
* &quot;License&quot;); 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
* &quot;AS IS&quot; 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 &lt;a href=&quot;mailto:greg@shwoop.com&quot;&gt;Greg Ritter&lt;/a&gt;
* @author &lt;a href=&quot;mailto:colin.chalmers@maxware.nl&quot;&gt;Colin Chalmers&lt;/a&gt;
* @author &lt;a href=&quot;mailto:hps@intermeta.de&quot;&gt;Henning P. Schmiedehausen&lt;/a&gt;
* @author &lt;a href=&quot;mailto:peter@courcoux.biz&quot;&gt;Peter Courcoux&lt;/a&gt;
* @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&lt;String, Properties&gt; contextPropsList = null;</span>
/** All initial contexts known to this service */
<span class="nc" id="L64"> private final Map&lt;String, InitialContext&gt; initialContexts = new HashMap&lt;&gt;();</span>
/**
* Called the first time the Service is used.&lt;br&gt;
*
*/
@Override
public void init()
throws InitializationException
{
// Context properties are specified in lines in the properties
// file that begin with &quot;context.contextname.&quot;, allowing
// multiple named contexts to be used. Everything after the
// &quot;contextname.&quot; 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&lt;&gt;();</span>
<span class="nc" id="L86"> for (Iterator&lt;String&gt; contextKeys = conf.subset(&quot;context&quot;).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(&quot;.&quot;);</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&lt;String, Properties&gt; 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(&quot;Failed to initialize JDNI contexts!&quot;, e);</span>
<span class="nc" id="L129"> throw new InitializationException(</span>
&quot;Failed to initialize JDNI contexts!&quot;);
<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
* &quot;services.servicename.properties.name.&quot;.
*
* @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>