View Javadoc

    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  public class TurbineNamingService
    51          extends TurbineBaseService
    52          implements NamingService
    53  {
    54      /** Logging */
    55      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      private static Map<String, Properties> contextPropsList = null;
    62
    63      /** All initial contexts known to this service */
    64      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          Configuration conf = Turbine.getConfiguration();
    82          try
    83          {
    84              contextPropsList = new HashMap<String, Properties>();
    85
    86              for (Iterator<String> contextKeys = conf.subset("context").getKeys();
    87                   contextKeys.hasNext();)
    88              {
    89                  String key = contextKeys.next();
    90                  int end = key.indexOf(".");
    91
    92                  if (end == -1)
    93                  {
    94                      continue;
    95                  }
    96
    97                  String contextName = key.substring(0, end);
    98                  Properties contextProps = null;
    99
    100                 if (contextPropsList.containsKey(contextName))
    101                 {
    102                     contextProps = contextPropsList.get(contextName);
    103                 }
    104                 else
    105                 {
    106                     contextProps = new Properties();
    107                 }
    108
    109                 contextProps.put(key.substring(end + 1),
    110                         conf.getString(key));
    111
    112                 contextPropsList.put(contextName, contextProps);
    113             }
    114
    115             for (Map.Entry<String, Properties> entry : contextPropsList.entrySet())
    116             {
    117                 String key = entry.getKey();
    118                 Properties contextProps = entry.getValue();
    119                 InitialContext context = new InitialContext(contextProps);
    120                 initialContexts.put(key, context);
    121             }
    122
    123             setInit(true);
    124         }
    125         catch (NamingException 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     /**
    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         Properties contextProps = null;
    150
    151         if (contextPropsList.containsKey(contextName))
    152         {
    153             contextProps = contextPropsList.get(contextName);
    154         }
    155         else
    156         {
    157             contextProps = new Properties();
    158         }
    159
    160         // Construct a new context with the properties.
    161         try
    162         {
    163             return new InitialContext(contextProps);
    164         }
    165         catch (Exception e)
    166         {
    167             return null;
    168         }
    169     }
    170 }