Implemented Mapper for NameIdentifiers.
[java-idp.git] / src / edu / internet2 / middleware / shibboleth / common / NameMapper.java
1 /*
2  * The Shibboleth License, Version 1. Copyright (c) 2002 University Corporation
3  * for Advanced Internet Development, Inc. All rights reserved
4  * 
5  * 
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  * 
9  * Redistributions of source code must retain the above copyright notice, this
10  * list of conditions and the following disclaimer.
11  * 
12  * Redistributions in binary form must reproduce the above copyright notice,
13  * this list of conditions and the following disclaimer in the documentation
14  * and/or other materials provided with the distribution, if any, must include
15  * the following acknowledgment: "This product includes software developed by
16  * the University Corporation for Advanced Internet Development
17  * <http://www.ucaid.edu> Internet2 Project. Alternately, this acknowledegement
18  * may appear in the software itself, if and wherever such third-party
19  * acknowledgments normally appear.
20  * 
21  * Neither the name of Shibboleth nor the names of its contributors, nor
22  * Internet2, nor the University Corporation for Advanced Internet Development,
23  * Inc., nor UCAID may be used to endorse or promote products derived from this
24  * software without specific prior written permission. For written permission,
25  * please contact shibboleth@shibboleth.org
26  * 
27  * Products derived from this software may not be called Shibboleth, Internet2,
28  * UCAID, or the University Corporation for Advanced Internet Development, nor
29  * may Shibboleth appear in their name, without prior written permission of the
30  * University Corporation for Advanced Internet Development.
31  * 
32  * 
33  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
34  * AND WITH ALL FAULTS. ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
35  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
36  * PARTICULAR PURPOSE, AND NON-INFRINGEMENT ARE DISCLAIMED AND THE ENTIRE RISK
37  * OF SATISFACTORY QUALITY, PERFORMANCE, ACCURACY, AND EFFORT IS WITH LICENSEE.
38  * IN NO EVENT SHALL THE COPYRIGHT OWNER, CONTRIBUTORS OR THE UNIVERSITY
39  * CORPORATION FOR ADVANCED INTERNET DEVELOPMENT, INC. BE LIABLE FOR ANY
40  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
41  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
42  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
43  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
44  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
45  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
46  */
47
48 package edu.internet2.middleware.shibboleth.common;
49
50 import java.lang.reflect.Constructor;
51 import java.util.Collections;
52 import java.util.HashMap;
53 import java.util.Map;
54
55 import org.apache.log4j.Logger;
56 import org.w3c.dom.Element;
57
58 /**
59  * @author Walter Hoehn
60  */
61 public class NameMapper {
62
63         private static Logger log = Logger.getLogger(NameMapper.class.getName());
64         private Map byFormat = new HashMap();
65         private Map byId = new HashMap();
66         private static Map registeredMappingTypes = Collections.synchronizedMap(new HashMap());
67
68         static {
69                 try {
70                         registeredMappingTypes.put(
71                                 "CryptoHandleGenerator",
72                                 Class.forName("edu.internet2.middleware.shibboleth.common.CryptoHandleGenerator"));
73
74                         registeredMappingTypes.put(
75                                 "MemoryHandleGenerator",
76                                 Class.forName("edu.internet2.middleware.shibboleth.common.MemoryHandleGenerator"));
77
78                         registeredMappingTypes.put(
79                                 "PassThruNameIdentifier",
80                                 Class.forName("edu.internet2.middleware.shibboleth.common.PassThruNameIdentifier"));
81
82                 } catch (ClassNotFoundException e) {
83                         log.error("Unable to pre-register Name mapping implementation types.");
84                 }
85         }
86
87         public void addNameMapping(Element e) throws NameIdentifierMappingException {
88
89                 if (!e.getTagName().equals("NameMapping")) {
90                         throw new IllegalArgumentException();
91                 }
92
93                 String type = ((Element) e).getAttribute("type");
94                 String implementation = ((Element) e).getAttribute("class");
95                 if (type != null && (!type.equals("")) && implementation != null && (!implementation.equals(""))) {
96                         log.error("Name Mapping has both a \"type\" and a \"class\" attribute. Only \"type\" will take effect.");
97                 }
98
99                 if (type != null && (!type.equals(""))) {
100
101                         Class registeredImplementation = (Class) registeredMappingTypes.get(type);
102                         if (registeredImplementation == null) {
103                                 log.error("Name Mapping refers to an unregistered implementation type.");
104                                 throw new NameIdentifierMappingException("Invalid mapping implementation specified.");
105                         }
106
107                         log.debug(
108                                 "Found type ("
109                                         + type
110                                         + ") registered with an implementation class of ("
111                                         + registeredImplementation.getName()
112                                         + ").");
113                         addNameMapping(loadNameIdentifierMapping(registeredImplementation, e));
114
115                 } else if (implementation != null && (!implementation.equals(""))) {
116
117                         try {
118                                 Class implementorClass = Class.forName(implementation);
119                                 addNameMapping(loadNameIdentifierMapping(implementorClass, e));
120
121                         } catch (ClassNotFoundException cnfe) {
122                                 log.error("Name Mapping refers to an implementation class that cannot be loaded: " + cnfe);
123                                 throw new NameIdentifierMappingException("Invalid mapping implementation specified.");
124                         }
125
126                 } else {
127                         log.error("Name Mapping requires either a \"type\" or a \"class\" attribute.");
128                         throw new NameIdentifierMappingException("No mapping implementation specified.");
129                 }
130
131         }
132
133         public void addNameMapping(NameIdentifierMapping mapping) {
134                 byFormat.put(mapping.getNameIdentifierFormat(), mapping);
135
136                 /* Will probably bump this into an HS specific base class
137                 if (mapping.getId() != null && (!mapping.getId().equals(""))) {
138                         byId.put(mapping.getId(), mapping);
139                 }
140                 */
141         }
142
143         public NameIdentifierMapping getNameIdentifierMappingByFormat(String format) {
144                 return (NameIdentifierMapping) byFormat.get(format);
145         }
146
147         public NameIdentifierMapping getNameIdentifierMappingById(String id) {
148                 return (NameIdentifierMapping) byId.get(id);
149         }
150
151         protected NameIdentifierMapping loadNameIdentifierMapping(Class implementation, Element config)
152                 throws NameIdentifierMappingException {
153
154                 try {
155                         Class[] params = new Class[] { Element.class };
156                         Constructor implementorConstructor = implementation.getConstructor(params);
157                         Object[] args = new Object[] { config };
158                         log.debug("Initializing Name Identifier Mapping of type (" + implementation.getName() + ").");
159                         return (NameIdentifierMapping) implementorConstructor.newInstance(args);
160
161                 } catch (NoSuchMethodException nsme) {
162                         log.error(
163                                 "Failed to instantiate a Name Identifier Mapping: NameIdentifierMapping "
164                                         + "implementation must contain a constructor that accepts an Element object for "
165                                         + "configuration data.");
166                         throw new NameIdentifierMappingException("Failed to instantiate a Name Identifier Mapping.");
167
168                 } catch (Exception e) {
169                         log.error("Failed to instantiate a Name Identifier Mapping: " + e + ":" + e.getCause());
170                         throw new NameIdentifierMappingException("Failed to instantiate a Name Identifier Mapping: " + e);
171
172                 }
173
174         }
175
176 }