Patch from Noah. Push up refactoring for parsing of origin config file.
authorwassa <wassa@ab3bd59b-922f-494d-bb5f-6f0a3c29deca>
Wed, 4 Feb 2004 19:03:47 +0000 (19:03 +0000)
committerwassa <wassa@ab3bd59b-922f-494d-bb5f-6f0a3c29deca>
Wed, 4 Feb 2004 19:03:47 +0000 (19:03 +0000)
git-svn-id: https://subversion.switch.ch/svn/shibboleth/java-idp/trunk@876 ab3bd59b-922f-494d-bb5f-6f0a3c29deca

src/edu/internet2/middleware/shibboleth/aa/AAServlet.java
src/edu/internet2/middleware/shibboleth/common/OriginComponent.java
src/edu/internet2/middleware/shibboleth/hs/HandleServlet.java

index 426f958..6f37159 100755 (executable)
@@ -71,6 +71,7 @@ import org.opensaml.SAMLStatement;
 import org.opensaml.SAMLSubject;
 import org.w3c.dom.Element;
 import org.w3c.dom.NodeList;
+import org.w3c.dom.Document;
 import org.xml.sax.InputSource;
 import org.xml.sax.SAXException;
 
@@ -128,34 +129,17 @@ public class AAServlet extends OriginComponent {
                        throw new UnavailableException("Attribute Authority failed to initialize.");
                }
        }
-       protected void loadConfiguration() throws ShibbolethConfigurationException {
-
-               DOMParser parser = loadParser(true);
-
-               String originConfigFile = getInitParameter("OriginConfigFile");
-               if (originConfigFile == null) {
-                       originConfigFile = "/conf/origin.xml";
-               }
-
-               log.debug("Loading Configuration from (" + originConfigFile + ").");
 
-               try {
-                       parser.parse(new InputSource(new ShibResource(originConfigFile, this.getClass()).getInputStream()));
+       protected void loadConfiguration() throws ShibbolethConfigurationException {
 
-               } catch (SAXException e) {
-                       log.error("Error while parsing origin configuration: " + e);
-                       throw new ShibbolethConfigurationException("Error while parsing origin configuration.");
-               } catch (IOException e) {
-                       log.error("Could not load origin configuration: " + e);
-                       throw new ShibbolethConfigurationException("Could not load origin configuration.");
-               }
+               Document originConfig = getOriginConfig();
 
                //Load global configuration properties
-               configuration = new AAConfig(parser.getDocument().getDocumentElement());
+               configuration = new AAConfig(originConfig.getDocumentElement());
 
                //Load name mappings
                NodeList itemElements =
-                       parser.getDocument().getDocumentElement().getElementsByTagNameNS(
+                       originConfig.getDocumentElement().getElementsByTagNameNS(
                                NameIdentifierMapping.mappingNamespace,
                                "NameMapping");
 
@@ -169,7 +153,7 @@ public class AAServlet extends OriginComponent {
 
                //Load relying party config
                try {
-                       targetMapper = new AAServiceProviderMapper(parser.getDocument().getDocumentElement(), configuration);
+                       targetMapper = new AAServiceProviderMapper(originConfig.getDocumentElement(), configuration);
                } catch (ServiceProviderMapperException e) {
                        log.error("Could not load origin configuration: " + e);
                        throw new ShibbolethConfigurationException("Could not load origin configuration.");
@@ -182,7 +166,7 @@ public class AAServlet extends OriginComponent {
                        //Startup ARP Engine
                        ArpEngine arpEngine = null;
                        itemElements =
-                               parser.getDocument().getDocumentElement().getElementsByTagNameNS(
+                               originConfig.getDocumentElement().getElementsByTagNameNS(
                                        ShibbolethOriginConfig.originConfigNamespace,
                                        "ReleasePolicyEngine");
 
index d3ba484..0fbec9d 100644 (file)
@@ -60,6 +60,7 @@ import org.xml.sax.ErrorHandler;
 import org.xml.sax.InputSource;
 import org.xml.sax.SAXException;
 import org.xml.sax.SAXParseException;
+import org.w3c.dom.Document;
 
 /**
  * @author Walter Hoehn
@@ -67,62 +68,85 @@ import org.xml.sax.SAXParseException;
 public abstract class OriginComponent extends HttpServlet {
 
        private static Logger log = Logger.getLogger(OriginComponent.class.getName());
+        private static Document originConfig = null;
 
-       protected DOMParser loadParser(boolean schemaChecking) throws ShibbolethConfigurationException {
-
-               DOMParser parser = new DOMParser();
-
-               if (!schemaChecking) {
-                       return parser;
-               }
-
-               try {
-                       parser.setFeature("http://xml.org/sax/features/validation", true);
-                       parser.setFeature("http://apache.org/xml/features/validation/schema", true);
-
-                       parser.setEntityResolver(new EntityResolver() {
-                               public InputSource resolveEntity(String publicId, String systemId) throws SAXException {
-                                       log.debug("Resolving entity for System ID: " + systemId);
-                                       if (systemId != null) {
-                                               StringTokenizer tokenString = new StringTokenizer(systemId, "/");
-                                               String xsdFile = "";
-                                               while (tokenString.hasMoreTokens()) {
-                                                       xsdFile = tokenString.nextToken();
-                                               }
-                                               if (xsdFile.endsWith(".xsd")) {
-                                                       InputStream stream;
-                                                       try {
-                                                               stream = new ShibResource("/schemas/" + xsdFile, this.getClass()).getInputStream();
-                                                       } catch (IOException ioe) {
-                                                               log.error("Error loading schema: " + xsdFile + ": " + ioe);
-                                                               return null;
-                                                       }
-                                                       if (stream != null) {
-                                                               return new InputSource(stream);
-                                                       }
-                                               }
-                                       }
-                                       return null;
-                               }
-                       });
-
-                       parser.setErrorHandler(new ErrorHandler() {
-                               public void error(SAXParseException arg0) throws SAXException {
-                                       throw new SAXException("Error parsing xml file: " + arg0);
-                               }
-                               public void fatalError(SAXParseException arg0) throws SAXException {
-                                       throw new SAXException("Error parsing xml file: " + arg0);
-                               }
-                               public void warning(SAXParseException arg0) throws SAXException {
-                                       throw new SAXException("Error parsing xml file: " + arg0);
-                               }
-                       });
-
-               } catch (SAXException e) {
-                       log.error("Unable to setup a workable XML parser: " + e);
-                       throw new ShibbolethConfigurationException("Unable to setup a workable XML parser.");
-               }
-               return parser;
-       }
+        /* synchronized to make sure only one thread attempts to parse the config file */
+        protected synchronized Document getOriginConfig () throws ShibbolethConfigurationException
+        {
+            if (originConfig != null)
+            {
+                return originConfig;
+            }
 
+            DOMParser parser = new DOMParser();
+
+            try {
+                parser.setFeature("http://xml.org/sax/features/validation", true);
+                parser.setFeature("http://apache.org/xml/features/validation/schema", true);
+
+                parser.setEntityResolver(
+                        new EntityResolver() {
+                            public InputSource resolveEntity(String publicId, String systemId) throws SAXException {
+                                log.debug("Resolving entity for System ID: " + systemId);
+                                if (systemId != null) {
+                                    StringTokenizer tokenString = new StringTokenizer(systemId, "/");
+                                    String xsdFile = "";
+                                    while (tokenString.hasMoreTokens()) {
+                                        xsdFile = tokenString.nextToken();
+                                    }
+                                    if (xsdFile.endsWith(".xsd")) {
+                                        InputStream stream;
+                                        try {
+                                            stream = new ShibResource("/schemas/" + xsdFile, this.getClass()).getInputStream();
+                                        } catch (IOException ioe) {
+                                            log.error("Error loading schema: " + xsdFile + ": " + ioe);
+                                            return null;
+                                        }
+                                        if (stream != null) {
+                                            return new InputSource(stream);
+                                        }
+                                    }
+                                }
+                                return null;
+                            }
+                        });
+
+                parser.setErrorHandler(new ErrorHandler() {
+                    public void error(SAXParseException arg0) throws SAXException {
+                        throw new SAXException("Error parsing xml file: " + arg0);
+                    }
+                    public void fatalError(SAXParseException arg0) throws SAXException {
+                        throw new SAXException("Error parsing xml file: " + arg0);
+                    }
+                    public void warning(SAXParseException arg0) throws SAXException {
+                        throw new SAXException("Error parsing xml file: " + arg0);
+                    }
+                });
+
+            } catch (SAXException e) {
+                log.error("Unable to setup a workable XML parser: " + e);
+                throw new ShibbolethConfigurationException("Unable to setup a workable XML parser.");
+            }
+
+            String originConfigFile = getInitParameter("OriginConfigFile");
+            if (originConfigFile == null) {
+                originConfigFile = "/conf/origin.xml";
+            }
+
+            log.debug("Loading Configuration from (" + originConfigFile + ").");
+
+            try {
+                parser.parse(new InputSource(new ShibResource(originConfigFile, this.getClass()).getInputStream()));
+            } catch (SAXException e) {
+                log.error("Error while parsing origin configuration: " + e);
+                throw new ShibbolethConfigurationException("Error while parsing origin configuration.");
+            } catch (IOException e) {
+                log.error("Could not load origin configuration: " + e);
+                throw new ShibbolethConfigurationException("Could not load origin configuration.");
+            }
+
+            originConfig = parser.getDocument();
+
+            return originConfig;
+        }
 }
index aa89058..59c7a60 100644 (file)
@@ -60,6 +60,7 @@ import org.opensaml.SAMLNameIdentifier;
 import org.opensaml.SAMLResponse;
 import org.w3c.dom.Element;
 import org.w3c.dom.NodeList;
+import org.w3c.dom.Document;
 import org.xml.sax.InputSource;
 import org.xml.sax.SAXException;
 
@@ -88,31 +89,14 @@ public class HandleServlet extends OriginComponent {
 
        protected void loadConfiguration() throws ShibbolethConfigurationException {
 
-               DOMParser parser = loadParser(true);
-
-               String originConfigFile = getInitParameter("OriginConfigFile");
-               if (originConfigFile == null) {
-                       originConfigFile = "/conf/origin.xml";
-               }
-               log.debug("Loading Configuration from (" + originConfigFile + ").");
-
-               try {
-                       parser.parse(new InputSource(new ShibResource(originConfigFile, this.getClass()).getInputStream()));
-
-               } catch (SAXException e) {
-                       log.error("Error while parsing origin configuration: " + e);
-                       throw new ShibbolethConfigurationException("Error while parsing origin configuration.");
-               } catch (IOException e) {
-                       log.error("Could not load origin configuration: " + e);
-                       throw new ShibbolethConfigurationException("Could not load origin configuration.");
-               }
+               Document originConfig = getOriginConfig();
 
                //Load global configuration properties
-               configuration = new HSConfig(parser.getDocument().getDocumentElement());
+               configuration = new HSConfig(originConfig.getDocumentElement());
 
                //Load signing credentials
                NodeList itemElements =
-                       parser.getDocument().getDocumentElement().getElementsByTagNameNS(
+                       originConfig.getDocumentElement().getElementsByTagNameNS(
                                Credentials.credentialsNamespace,
                                "Credentials");
                if (itemElements.getLength() < 1) {
@@ -128,7 +112,7 @@ public class HandleServlet extends OriginComponent {
 
                //Load name mappings
                itemElements =
-                       parser.getDocument().getDocumentElement().getElementsByTagNameNS(
+                       originConfig.getDocumentElement().getElementsByTagNameNS(
                                NameIdentifierMapping.mappingNamespace,
                                "NameMapping");
 
@@ -144,7 +128,7 @@ public class HandleServlet extends OriginComponent {
                try {
                        targetMapper =
                                new HSServiceProviderMapper(
-                                       parser.getDocument().getDocumentElement(),
+                                       originConfig.getDocumentElement(),
                                        configuration,
                                        credentials,
                                        nameMapper);