1 16 17 18 package org.apache.naming.config; 19 20 import java.io.IOException ; 21 import java.io.InputStream ; 22 import java.util.Iterator ; 23 import java.util.Set ; 24 25 import javax.naming.Context ; 26 import javax.naming.InitialContext ; 27 import javax.naming.Name ; 28 import javax.naming.NameClassPair ; 29 import javax.naming.NamingEnumeration ; 30 import javax.naming.NamingException ; 31 32 import org.apache.commons.digester.Digester; 33 import org.apache.commons.logging.Log; 34 import org.apache.commons.logging.LogFactory; 35 36 import org.xml.sax.SAXException ; 37 38 44 public class XmlConfigurator 45 { 46 private static final String COMP_CONTEXT_NAME = "java:comp"; 47 private static final String ENV_CONTEXT_NAME = "env"; 48 private static final String ROOT_ELEMENT = "naming"; 49 private static final String CONTEXT_ELEMENT = ROOT_ELEMENT + "/context"; 50 private static final String ENV_ELEMENT = CONTEXT_ELEMENT + "/environment"; 51 private static final String RES_ELEMENT = CONTEXT_ELEMENT + "/resource"; 52 private static final String RES_PARAM_ELEMENT = RES_ELEMENT + "/parameter"; 53 private static Context envContext = null; 54 55 private static final Log LOG = LogFactory.getLog(XmlConfigurator.class); 56 57 65 public static synchronized void setupInitialContext() throws NamingException { 66 System.setProperty(Context.INITIAL_CONTEXT_FACTORY, "org.apache.naming.java.javaURLContextFactory"); 67 System.setProperty(Context.URL_PKG_PREFIXES, "org.apache.naming"); 68 69 Context initialContext = new InitialContext (); 70 envContext = initialContext.createSubcontext(COMP_CONTEXT_NAME).createSubcontext(ENV_CONTEXT_NAME); 71 } 72 73 81 public static synchronized void destroyInitialContext() throws NamingException { 82 Context initialContext = new InitialContext (); 83 NamingEnumeration contexts = initialContext.list(""); 84 while (contexts.hasMore()) { 85 initialContext.destroySubcontext(((NameClassPair ) contexts.next()).getName()); 86 } 87 envContext = null; 88 initialContext = null; 89 } 90 91 98 public static synchronized void loadConfiguration(InputStream inputFile) throws NamingException , ParseException { 99 if (envContext == null) 100 { 101 setupInitialContext(); 102 } 103 104 Digester digester = new Digester(); 105 digester.addObjectCreate(ROOT_ELEMENT, Config.Naming.class); 107 digester.addObjectCreate(CONTEXT_ELEMENT, Config.Context.class); 108 digester.addSetProperties(CONTEXT_ELEMENT); 109 digester.addSetNext(CONTEXT_ELEMENT, "addContext"); 110 digester.addObjectCreate(ENV_ELEMENT, Config.Environment.class); 112 digester.addSetProperties(ENV_ELEMENT); 113 digester.addSetNext(ENV_ELEMENT, "addEnvironment"); 114 digester.addObjectCreate(RES_ELEMENT, Config.Resource.class); 115 digester.addSetProperties(RES_ELEMENT); 116 digester.addSetNext(RES_ELEMENT, "addResource"); 117 digester.addCallMethod(RES_PARAM_ELEMENT + "", "addParameter", 2); 118 digester.addCallParam(RES_PARAM_ELEMENT + "/name", 0); 119 digester.addCallParam(RES_PARAM_ELEMENT + "/value", 1); 120 121 try 122 { 123 Config.Naming naming = (Config.Naming) digester.parse(inputFile); 124 if (naming == null) { 125 throw new ParseException("Unable to find root element '" + ROOT_ELEMENT + "'"); 126 } 127 if (LOG.isDebugEnabled()) { 128 LOG.debug("XML configuration loaded: " + naming.toString()); 129 } 130 131 132 for (Iterator i = naming.getContextList().iterator(); i.hasNext();) 133 { 134 Config.Context ctx = (Config.Context) i.next(); 135 Context jndiCtx = envContext; 136 if (ctx.getName() != null) 137 { 138 destroyInitialContext(); 139 Context initialContext = new InitialContext (); 140 Name nm = initialContext.getNameParser("").parse(ctx.getName()); 141 envContext = initialContext.createSubcontext(nm.get(0)); 142 jndiCtx = envContext; 143 for (int k = 1; k < nm.size(); k++) { 144 jndiCtx = jndiCtx.createSubcontext(nm.get(k)); 145 } 146 } 147 precreateSubcontextTree(jndiCtx, naming.generateSortedSubcontextNameSet()); 148 149 for (Iterator j = ctx.getEnvironmentList().iterator(); j.hasNext();) 150 { 151 Config.Environment e = (Config.Environment) j.next(); 152 jndiCtx.rebind(e.getName(), e.createValue()); 153 } 154 155 for (Iterator j = ctx.getResourceList().iterator(); j.hasNext();) 156 { 157 Config.Resource r = (Config.Resource) j.next(); 158 jndiCtx.bind(r.getName(), r.createValue()); 159 } 160 } 161 } 162 catch (IOException e) 163 { 164 throw new ParseException("Error reading configuration file", e); 165 } 166 catch (SAXException e) 167 { 168 throw new ParseException("Error reading configuration file", e); 169 } 170 } 171 172 private static void precreateSubcontextTree(Context ctx, Set sortedSubcontextNameSet) throws NamingException 173 { 174 for (Iterator i = sortedSubcontextNameSet.iterator(); i.hasNext();) 176 { 177 String name = (String ) i.next(); 178 ctx.createSubcontext(name); 179 } 180 } 181 182 } 183 | Popular Tags |