Validate Signatures
[java-idp.git] / src / edu / internet2 / middleware / shibboleth / idp / ProtocolHandlerFactory.java
1 /*
2  * The Shibboleth License, Version 1. Copyright (c) 2002 University Corporation for Advanced Internet Development, Inc.
3  * All rights reserved Redistribution and use in source and binary forms, with or without modification, are permitted
4  * provided that the following conditions are met: Redistributions of source code must retain the above copyright
5  * notice, this list of conditions and the following disclaimer. Redistributions in binary form must reproduce the above
6  * copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials
7  * provided with the distribution, if any, must include the following acknowledgment: "This product includes software
8  * developed by the University Corporation for Advanced Internet Development <http://www.ucaid.edu> Internet2 Project.
9  * Alternately, this acknowledegement may appear in the software itself, if and wherever such third-party
10  * acknowledgments normally appear. Neither the name of Shibboleth nor the names of its contributors, nor Internet2, nor
11  * the University Corporation for Advanced Internet Development, Inc., nor UCAID may be used to endorse or promote
12  * products derived from this software without specific prior written permission. For written permission, please contact
13  * shibboleth@shibboleth.org Products derived from this software may not be called Shibboleth, Internet2, UCAID, or the
14  * University Corporation for Advanced Internet Development, nor may Shibboleth appear in their name, without prior
15  * written permission of the University Corporation for Advanced Internet Development. THIS SOFTWARE IS PROVIDED BY THE
16  * COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND WITH ALL FAULTS. ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND NON-INFRINGEMENT ARE
18  * DISCLAIMED AND THE ENTIRE RISK OF SATISFACTORY QUALITY, PERFORMANCE, ACCURACY, AND EFFORT IS WITH LICENSEE. IN NO
19  * EVENT SHALL THE COPYRIGHT OWNER, CONTRIBUTORS OR THE UNIVERSITY CORPORATION FOR ADVANCED INTERNET DEVELOPMENT, INC.
20  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
21  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
23  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */
25
26 package edu.internet2.middleware.shibboleth.idp;
27
28 import java.lang.reflect.Constructor;
29 import java.lang.reflect.InvocationTargetException;
30
31 import org.apache.log4j.Logger;
32 import org.w3c.dom.Element;
33
34 import edu.internet2.middleware.shibboleth.common.ShibbolethConfigurationException;
35
36 /**
37  * Factory class for loading <code>ProtocolHandler</code> implementations based on xml configuration.
38  * 
39  * @author Walter Hoehn
40  */
41 public class ProtocolHandlerFactory {
42
43         private static Logger log = Logger.getLogger(ProtocolHandlerFactory.class.getName());
44
45         public static IdPProtocolHandler getInstance(Element config) throws ShibbolethConfigurationException {
46
47                 String implementation = config.getAttribute("implementation");
48                 if (implementation == null || implementation.equals("")) {
49                         log.error("No Protocol Handler implementation specified.  Attribute (implementation) is "
50                                         + "required with element <ProtocolHandler/>.");
51                         throw new ShibbolethConfigurationException("Invalid configuration data supplied.");
52
53                 } else {
54
55                         try {
56                                 log.debug("Loading Protocol Handler implementation: (" + implementation + ").");
57                                 Class implClass = Class.forName(implementation);
58                                 Constructor constructor = implClass.getConstructor(new Class[]{Element.class});
59                                 Object rawImpl = constructor.newInstance(new Object[]{config});
60
61                                 if (rawImpl instanceof IdPProtocolHandler) {
62                                         return (IdPProtocolHandler) rawImpl;
63                                 } else {
64                                         log.error("Invalid configuration, supplied implementation class for the Protocol Handler "
65                                                         + "does not properly implement the required IdPProtocolHandler interface.");
66                                         throw new ShibbolethConfigurationException("Invalid configuration data supplied.");
67                                 }
68
69                         } catch (ClassNotFoundException e) {
70                                 log.error("Invalid configuration, supplied implementation class for the Protocol Handler "
71                                                 + "could not be found: " + e.getMessage());
72                                 throw new ShibbolethConfigurationException("Invalid configuration data supplied.");
73
74                         } catch (NoSuchMethodException e) {
75                                 log.error("Invalid configuration, supplied implementation class for the Protocol Handler is "
76                                                 + "not valid.  A DOM Element constructor is required: " + e.getMessage());
77                                 throw new ShibbolethConfigurationException("Invalid configuration data supplied.");
78
79                         } catch (InvocationTargetException e) {
80                                 Throwable cause = e.getCause();
81                                 if (cause != null) {
82                                         log.error(cause.getMessage());
83                                 }
84                                 log.error("Invalid configuration, supplied implementation class for the Protocol Handler"
85                                                 + " could not be loaded: " + e.getMessage());
86                                 throw new ShibbolethConfigurationException("Invalid configuration data supplied.");
87                         } catch (Exception e) {
88                                 log.error("Invalid configuration, supplied implementation class for the Protocol Handler"
89                                                 + " could not be loaded: " + e.getMessage());
90                                 throw new ShibbolethConfigurationException("Invalid configuration data supplied.");
91                         }
92                 }
93         }
94
95 }