001 package org.apache.turbine.services.naming; 002 003 004 /* 005 * Licensed to the Apache Software Foundation (ASF) under one 006 * or more contributor license agreements. See the NOTICE file 007 * distributed with this work for additional information 008 * regarding copyright ownership. The ASF licenses this file 009 * to you under the Apache License, Version 2.0 (the 010 * "License"); you may not use this file except in compliance 011 * with the License. You may obtain a copy of the License at 012 * 013 * http://www.apache.org/licenses/LICENSE-2.0 014 * 015 * Unless required by applicable law or agreed to in writing, 016 * software distributed under the License is distributed on an 017 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 018 * KIND, either express or implied. See the License for the 019 * specific language governing permissions and limitations 020 * under the License. 021 */ 022 023 024 import java.util.HashMap; 025 import java.util.Iterator; 026 import java.util.Map; 027 import java.util.Properties; 028 029 import javax.naming.Context; 030 import javax.naming.InitialContext; 031 import javax.naming.NamingException; 032 033 import org.apache.commons.configuration.Configuration; 034 import org.apache.commons.logging.Log; 035 import org.apache.commons.logging.LogFactory; 036 import org.apache.turbine.Turbine; 037 import org.apache.turbine.services.InitializationException; 038 import org.apache.turbine.services.TurbineBaseService; 039 040 /** 041 * This class is the default implementation of NamingService, which 042 * provides JNDI naming contexts. 043 * 044 * @author <a href="mailto:greg@shwoop.com">Greg Ritter</a> 045 * @author <a href="mailto:colin.chalmers@maxware.nl">Colin Chalmers</a> 046 * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a> 047 * @author <a href="mailto:peter@courcoux.biz">Peter Courcoux</a> 048 * @version $Id: TurbineNamingService.java 1078552 2011-03-06 19:58:46Z tv $ 049 */ 050 public class TurbineNamingService 051 extends TurbineBaseService 052 implements NamingService 053 { 054 /** Logging */ 055 private static Log log = LogFactory.getLog(TurbineNamingService.class); 056 057 /** 058 * A global Map of Property objects which are initialised using 059 * parameters from the ResourcesFile 060 */ 061 private static Map<String, Properties> contextPropsList = null; 062 063 /** All initial contexts known to this service */ 064 private final Map<String, InitialContext> initialContexts = new HashMap<String, InitialContext>(); 065 066 /** 067 * Called the first time the Service is used.<br> 068 * 069 */ 070 @Override 071 public void init() 072 throws InitializationException 073 { 074 // Context properties are specified in lines in the properties 075 // file that begin with "context.contextname.", allowing 076 // multiple named contexts to be used. Everything after the 077 // "contextname." is the name of the property that will be 078 // used by the InitialContext class to create a new context 079 // instance. 080 081 Configuration conf = Turbine.getConfiguration(); 082 try 083 { 084 contextPropsList = new HashMap<String, Properties>(); 085 086 for (Iterator contextKeys = conf.subset("context").getKeys(); 087 contextKeys.hasNext();) 088 { 089 String key = (String) contextKeys.next(); 090 int end = key.indexOf("."); 091 092 if (end == -1) 093 { 094 continue; 095 } 096 097 String contextName = key.substring(0, end); 098 Properties contextProps = null; 099 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 (Iterator contextPropsKeys = contextPropsList.keySet().iterator(); 116 contextPropsKeys.hasNext();) 117 { 118 String key = (String) contextPropsKeys.next(); 119 Properties contextProps = contextPropsList.get(key); 120 InitialContext context = new InitialContext(contextProps); 121 initialContexts.put(key, context); 122 } 123 124 setInit(true); 125 } 126 catch (NamingException e) 127 { 128 log.error("Failed to initialize JDNI contexts!", e); 129 130 throw new InitializationException( 131 "Failed to initialize JDNI contexts!"); 132 } 133 } 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 Properties contextProps = null; 151 152 if (contextPropsList.containsKey(contextName)) 153 { 154 contextProps = contextPropsList.get(contextName); 155 } 156 else 157 { 158 contextProps = new Properties(); 159 } 160 161 // Construct a new context with the properties. 162 try 163 { 164 return new InitialContext(contextProps); 165 } 166 catch (Exception e) 167 { 168 return null; 169 } 170 } 171 }