1 package org.apache.turbine.services.naming; 2 3 18 19 import java.util.HashMap ; 20 import java.util.Iterator ; 21 import java.util.Map ; 22 import java.util.Properties ; 23 24 import javax.naming.Context ; 25 import javax.naming.InitialContext ; 26 27 import org.apache.commons.configuration.Configuration; 28 29 import org.apache.commons.logging.Log; 30 import org.apache.commons.logging.LogFactory; 31 32 import org.apache.turbine.Turbine; 33 import org.apache.turbine.services.InitializationException; 34 import org.apache.turbine.services.TurbineBaseService; 35 import org.apache.turbine.util.RunData; 36 37 46 public class TurbineNamingService 47 extends TurbineBaseService 48 implements NamingService 49 { 50 51 private static Log log = LogFactory.getLog(TurbineNamingService.class); 52 53 57 private static Map contextPropsList = null; 58 59 60 private Map initialContexts = new HashMap (); 61 62 66 public void init() 67 throws InitializationException 68 { 69 76 Configuration conf = Turbine.getConfiguration(); 77 try 78 { 79 contextPropsList = new HashMap (); 80 81 for (Iterator contextKeys = conf.subset("context").getKeys(); 82 contextKeys.hasNext();) 83 { 84 String key = (String ) contextKeys.next(); 85 int end = key.indexOf("."); 86 87 if (end == -1) 88 { 89 continue; 90 } 91 92 String contextName = key.substring(0, end); 93 Properties contextProps = null; 94 95 if (contextPropsList.containsKey(contextName)) 96 { 97 contextProps = (Properties ) 98 contextPropsList.get(contextName); 99 } 100 else 101 { 102 contextProps = new Properties (); 103 } 104 105 contextProps.put(key.substring(end + 1), 106 conf.getString(key)); 107 108 contextPropsList.put(contextName, contextProps); 109 } 110 111 for (Iterator contextPropsKeys = contextPropsList.keySet().iterator(); 112 contextPropsKeys.hasNext();) 113 { 114 String key = (String ) contextPropsKeys.next(); 115 Properties contextProps = (Properties ) contextPropsList.get(key); 116 InitialContext context = new InitialContext (contextProps); 117 initialContexts.put(key, context); 118 } 119 120 setInit(true); 121 } 122 catch (Exception e) 123 { 124 log.error("Failed to initialize JDNI contexts!", e); 125 126 throw new InitializationException( 127 "Failed to initialize JDNI contexts!"); 128 } 129 } 130 131 140 public void init(RunData data) throws InitializationException 141 { 142 try 143 { 144 if (contextPropsList == null) 145 { 146 init(); 147 } 148 149 for (Iterator it = contextPropsList.keySet().iterator(); it.hasNext();) 150 { 151 String key = (String ) it.next(); 152 Properties contextProps = 153 (Properties ) contextPropsList.get(key); 154 InitialContext context = new InitialContext (contextProps); 155 data.getJNDIContexts().put(key, context); 156 } 157 } 158 catch (Exception e) 159 { 160 log.error("Failed to initialize JDNI contexts!", e); 161 162 throw new InitializationException( 163 "Failed to initialize JDNI contexts!"); 164 } 165 } 166 167 177 public Context getContext(String contextName) 178 { 179 Properties contextProps = null; 182 183 if (contextPropsList.containsKey(contextName)) 184 { 185 contextProps = (Properties ) contextPropsList.get(contextName); 186 } 187 else 188 { 189 contextProps = new Properties (); 190 } 191 192 try 194 { 195 return new InitialContext (contextProps); 196 } 197 catch (Exception e) 198 { 199 return null; 200 } 201 } 202 } 203 | Popular Tags |