Print idp configuration loading problem to STDERR, because we don't have a logger...
[java-idp.git] / src / edu / internet2 / middleware / shibboleth / idp / IdPConfigLoader.java
1 /*
2  * Copyright [2005] [University Corporation for Advanced Internet Development, Inc.]
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package edu.internet2.middleware.shibboleth.idp;
18
19 import javax.servlet.ServletContext;
20
21 import org.apache.log4j.Logger;
22 import org.w3c.dom.Document;
23
24 import edu.internet2.middleware.shibboleth.common.ShibbolethConfigurationException;
25 import edu.internet2.middleware.shibboleth.xml.Parser;
26
27 /**
28  * Constructs a DOM tree for the IdP configuration XML file.
29  * 
30  * @author Walter Hoehn
31  * @author Noah Levitt
32  */
33 public class IdPConfigLoader {
34
35         private static Logger log = Logger.getLogger(IdPConfigLoader.class);
36         private static Document idpConfig = null;
37         private static String idpConfigFile = null;
38
39         /**
40          * Returnes the location of the configuration file.
41          * 
42          * @param context
43          *            the context of the IdP servlet
44          * @return the location of the configuration file
45          */
46         private static String getIdPConfigFile(ServletContext context) {
47
48                 if (context.getInitParameter("IdPConfigFile") != null) {
49                         return context.getInitParameter("IdPConfigFile");
50                 } else {
51                         return "/conf/idp.xml";
52                 }
53         }
54
55         /**
56          * Loads the IdP Configuration file into a DOM tree.
57          * 
58          * @param configFileLocation
59          *            URL of the configuration file
60          * @return the DOM Document
61          * @throws ShibbolethConfigurationException
62          *             if there was an error loading the file
63          */
64         public static synchronized Document getIdPConfig(String configFileLocation) throws ShibbolethConfigurationException {
65
66                 if (log.isDebugEnabled()) {
67                         log.debug("Getting IdP configuration file: " + configFileLocation);
68                 }
69
70                 if (configFileLocation.equals(idpConfigFile)) {
71                         return idpConfig;
72
73                 } else if (idpConfigFile == null) {
74                         idpConfigFile = configFileLocation;
75
76                 } else {
77                         log.error("Previously read IdP configuration from (" + idpConfigFile + "), re-reading from ("
78                                         + configFileLocation + "). This probably indicates a bug in shibboleth.");
79                         idpConfigFile = configFileLocation;
80                 }
81
82                 try {
83                         idpConfig = Parser.loadDom(configFileLocation, true);
84
85                         if (idpConfig == null) { throw new Exception("IdP configuration could not be loaded from (" + idpConfigFile
86                                         + ")."); }
87
88                         if (log.isDebugEnabled()) {
89                                 log.debug("IdP configuration file " + configFileLocation + " successfully read and cached.");
90                         }
91                 } catch (Exception e) {
92                         System.err.println("Unable to parse Shibboleth Identity Provider configuration file: " + e);
93                         throw new ShibbolethConfigurationException(
94                                         "Encountered an error while parsing Shibboleth Identity Provider configuration file: " + e);
95                 }
96                 return idpConfig;
97         }
98
99         /**
100          * Loads the IdP Configuration file into a DOM tree.
101          * 
102          * @param context
103          *            {@link ServletContext}from which to figure out the location of IdP
104          * @return the DOM Document
105          * @throws ShibbolethConfigurationException
106          *             if there was an error loading the file
107          */
108         public static Document getIdPConfig(ServletContext context) throws ShibbolethConfigurationException {
109
110                 return getIdPConfig(getIdPConfigFile(context));
111
112         }
113 }