Create parser pool interface and move current pool to an implementation of this inter...
[java-idp.git] / src / edu / internet2 / middleware / shibboleth / idp / ProtocolHandlerFactory.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 java.lang.reflect.Constructor;
20 import java.lang.reflect.InvocationTargetException;
21
22 import org.apache.log4j.Logger;
23 import org.w3c.dom.Element;
24
25 import edu.internet2.middleware.shibboleth.common.ShibbolethConfigurationException;
26
27 /**
28  * Factory class for loading <code>ProtocolHandler</code> implementations based on xml configuration.
29  * 
30  * @author Walter Hoehn
31  */
32 public class ProtocolHandlerFactory {
33
34         private static Logger log = Logger.getLogger(ProtocolHandlerFactory.class.getName());
35
36         public static IdPProtocolHandler getInstance(Element config) throws ShibbolethConfigurationException {
37
38                 String implementation = config.getAttribute("implementation");
39                 if (implementation == null || implementation.equals("")) {
40                         log.error("No Protocol Handler implementation specified.  Attribute (implementation) is "
41                                         + "required with element <ProtocolHandler/>.");
42                         throw new ShibbolethConfigurationException("Invalid configuration data supplied.");
43
44                 } else {
45
46                         try {
47                                 log.debug("Loading Protocol Handler implementation: (" + implementation + ").");
48                                 Class implClass = Class.forName(implementation);
49                                 Constructor constructor = implClass.getConstructor(new Class[]{Element.class});
50                                 Object rawImpl = constructor.newInstance(new Object[]{config});
51
52                                 if (rawImpl instanceof IdPProtocolHandler) {
53                                         return (IdPProtocolHandler) rawImpl;
54                                 } else {
55                                         log.error("Invalid configuration, supplied implementation class for the Protocol Handler "
56                                                         + "does not properly implement the required IdPProtocolHandler interface.");
57                                         throw new ShibbolethConfigurationException("Invalid configuration data supplied.");
58                                 }
59
60                         } catch (ClassNotFoundException e) {
61                                 log.error("Invalid configuration, supplied implementation class for the Protocol Handler "
62                                                 + "could not be found: " + e.getMessage());
63                                 throw new ShibbolethConfigurationException("Invalid configuration data supplied.");
64
65                         } catch (NoSuchMethodException e) {
66                                 log.error("Invalid configuration, supplied implementation class for the Protocol Handler is "
67                                                 + "not valid.  A DOM Element constructor is required: " + e.getMessage());
68                                 throw new ShibbolethConfigurationException("Invalid configuration data supplied.");
69
70                         } catch (InvocationTargetException e) {
71                                 Throwable cause = e.getCause();
72                                 if (cause != null) {
73                                         log.error(cause.getMessage());
74                                 }
75                                 log.error("Invalid configuration, supplied implementation class for the Protocol Handler"
76                                                 + " could not be loaded: " + e.getMessage());
77                                 throw new ShibbolethConfigurationException("Invalid configuration data supplied.");
78                         } catch (Exception e) {
79                                 log.error("Invalid configuration, supplied implementation class for the Protocol Handler"
80                                                 + " could not be loaded: " + e.getMessage());
81                                 throw new ShibbolethConfigurationException("Invalid configuration data supplied.");
82                         }
83                 }
84         }
85
86 }