View Javadoc

1   /*
2    * Copyright [2007] [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 org.opensaml.xml.security.keyinfo;
18  
19  import java.util.Collection;
20  import java.util.Collections;
21  import java.util.HashMap;
22  import java.util.Map;
23  
24  import org.opensaml.xml.security.credential.Credential;
25  
26  /**
27   * A manager for {@link KeyInfoGeneratorFactory} instances.  Factories are uniquely indexed according to the 
28   * value returned by {@link KeyInfoGeneratorFactory#getCredentialType()}.
29   */
30  public class KeyInfoGeneratorManager {
31      
32      /** The factories being managed, indexed by credential type. */
33      private Map<Class<? extends Credential>, KeyInfoGeneratorFactory> factories;
34      
35      /** Constructor. */
36      public KeyInfoGeneratorManager() {
37          factories = new HashMap<Class<? extends Credential>, KeyInfoGeneratorFactory>(5);
38      }
39  
40      /**
41       * Register a factory within this manager instance. If a factory already exists
42       * for that credential type, it will be replaced.
43       * 
44       * @param factory the factory to register
45       */
46      public void registerFactory(KeyInfoGeneratorFactory factory) {
47          factories.put(factory.getCredentialType(), factory);
48      }
49      
50      /**
51       * De-register a factory within this manager instance.
52       * 
53       * @param factory the factory to de-register
54       */
55      public void deregisterFactory(KeyInfoGeneratorFactory factory) {
56          factories.remove(factory.getCredentialType());
57      }
58      
59      /**
60       * Get the (unmodifiable) collection of all factories managed by this manager.
61       * 
62       * @return the collection of managed factories
63       */
64      public Collection<KeyInfoGeneratorFactory> getFactories() {
65          return Collections.unmodifiableCollection(factories.values());
66      }
67      
68      /**
69       * Get the factory which produces KeyInfoGenerators which can handle
70       * the specified credential.
71       * 
72       * @param credential the credential for which to locate a factory
73       * @return a KeyInfoGeneratorFactory instance appropriate for the credential
74       */
75      public KeyInfoGeneratorFactory getFactory(Credential credential) {
76          return factories.get(credential.getCredentialType());
77      }
78  
79  }