View Javadoc

1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements. See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership. The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License. You may obtain a copy of the License at
9    *
10   * http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied. See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  
20  package org.apache.ws.security.saml;
21  
22  import org.apache.ws.security.WSSecurityException;
23  import org.apache.ws.security.util.Loader;
24  
25  import java.io.IOException;
26  import java.lang.reflect.Constructor;
27  import java.net.URL;
28  import java.util.Properties;
29  
30  /**
31   * CryptoFactory.
32   * <p/>
33   *
34   * @author Davanum Srinivas (dims@yahoo.com).
35   */
36  public abstract class SAMLIssuerFactory {
37      private static final org.apache.commons.logging.Log LOG = 
38          org.apache.commons.logging.LogFactory.getLog(SAMLIssuerFactory.class);
39      private static final Class<? extends SAMLIssuer> DEFAULT_SAML_CLASS = 
40          org.apache.ws.security.saml.SAMLIssuerImpl.class;
41  
42      /**
43       * getInstance
44       * <p/>
45       * Returns an instance of SAMLIssuer. This method uses the file
46       * <code>saml.properties</code> to determine which implementation to
47       * use. Thus the property <code>org.apache.ws.security.saml.issuerClass</code>
48       * must define the classname of the SAMLIssuer implementation. The file
49       * may contain other property definitions as well. These properties are
50       * handed over to the  SAMLIssuer implementation. The file
51       * <code>saml.properties</code> is loaded with the
52       * <code>Loader.getResource()</code> method.
53       * <p/>
54       *
55       * @return The SAMLIssuer implementation was defined
56       * @throws WSSecurityException if there is an error in loading the crypto properties
57       */
58      public static SAMLIssuer getInstance() throws WSSecurityException {
59          return getInstance("saml.properties");
60      }
61  
62      /**
63       * getInstance
64       * <p/>
65       * Returns an instance of SAMLIssuer. The properties are handed over the the SAMLIssuer
66       * implementation. The properties can be <code>null</code>. It is dependent on the
67       * SAMLIssuer implementation how the initialization is done in this case.
68       * <p/>
69       *
70       * @param samlClass     This is the SAMLIssuer implementation class. No default is
71       *                      provided here.
72       * @param properties    The Properties that are forwarded to the SAMLIssuer implementation.
73       *                      These properties are dependent on the SAMLIssuer implementation
74       * @return The SAMLIssuer implementation or null if no samlClassName was defined
75       * @throws WSSecurityException if there is an error in loading the crypto properties
76       */
77      public static SAMLIssuer getInstance(
78          Class<? extends SAMLIssuer> samlClass,
79          Properties properties
80      ) throws WSSecurityException {
81          return loadClass(samlClass, properties);
82      }
83  
84      /**
85       * getInstance
86       * <p/>
87       * Returns an instance of SAMLIssuer. This method uses the specified filename
88       * to load a property file. This file shall use the property
89       * <code>org.apache.ws.security.saml.issuerClass</code>
90       * to define the classname of the SAMLIssuer implementation. The file
91       * may contain other property definitions as well. These properties are
92       * handed over to the SAMLIssuer implementation. The specified file
93       * is loaded with the <code>Loader.getResource()</code> method.
94       * <p/>
95       *
96       * @param propFilename The name of the property file to load
97       * @return The SAMLIssuer implementation that was defined
98       * @throws WSSecurityException if there is an error in loading the crypto properties
99       */
100     public static SAMLIssuer getInstance(String propFilename) throws WSSecurityException {
101         Properties properties = getProperties(propFilename);
102         String samlClassName = 
103             properties.getProperty("org.apache.ws.security.saml.issuerClass");
104         Class<? extends SAMLIssuer> samlIssuerClass = null;
105         if (samlClassName == null 
106             || samlClassName.equals("org.apache.ws.security.saml.SAMLIssuerImpl")) {
107             samlIssuerClass = DEFAULT_SAML_CLASS;
108         } else {
109             try {
110                 // instruct the class loader to load the crypto implementation
111                 samlIssuerClass = Loader.loadClass(samlClassName, SAMLIssuer.class);
112             } catch (ClassNotFoundException ex) {
113                 if (LOG.isDebugEnabled()) {
114                     LOG.debug(ex.getMessage(), ex);
115                 }
116                 throw new WSSecurityException(samlClassName + " Not Found", ex);
117             }
118         }
119 
120         return loadClass(samlIssuerClass, properties);
121     }
122 
123     private static SAMLIssuer loadClass(
124         Class<? extends SAMLIssuer> samlIssuerClass, 
125         Properties properties
126     ) throws WSSecurityException {
127         SAMLIssuer samlIssuer = null;
128         if (LOG.isDebugEnabled()) {
129             LOG.debug("Using Crypto Engine [" + samlIssuerClass + "]");
130         }
131         try {
132             Class<?>[] classes = new Class<?>[]{Properties.class};
133             Constructor<? extends SAMLIssuer> c = samlIssuerClass.getConstructor(classes);
134             samlIssuer = c.newInstance(new Object[]{properties});
135             return samlIssuer;
136         } catch (java.lang.Exception ex) {
137             if (LOG.isDebugEnabled()) {
138                 LOG.debug(ex.getMessage(), ex);
139             }
140             throw new WSSecurityException(samlIssuerClass.getName() + " cannot create instance", ex);
141         }
142     }
143 
144     /**
145      * Gets the properties for SAML issuer.
146      * The functions loads the property file via
147      * {@link Loader.getResource(String)}, thus the property file
148      * should be accessible via the classpath
149      *
150      * @param propFilename the properties file to load
151      * @return a <code>Properties</code> object loaded from the filename
152      * @throws WSSecurityException if there is an error in loading the crypto properties
153      */
154     private static Properties getProperties(String propFilename) throws WSSecurityException {
155         Properties properties = new Properties();
156         try {
157             URL url = Loader.getResource(propFilename);
158             if (url == null) {
159                 throw new WSSecurityException(
160                     WSSecurityException.FAILURE, 
161                     "resourceNotFound",
162                     new Object[]{propFilename}
163                 );
164             }
165             properties.load(url.openStream());
166         } catch (IOException e) {
167             if (LOG.isDebugEnabled()) {
168                 LOG.debug("Cannot find resource: " + propFilename, e);
169             }
170             throw new WSSecurityException(
171                 WSSecurityException.FAILURE, 
172                 "resourceNotFound",
173                 new Object[]{propFilename},
174                 e
175             );
176         }
177         return properties;
178     }
179     
180 }