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.Collections;
20 import java.util.Map;
21 import java.util.Set;
22
23 import org.opensaml.xml.security.credential.Credential;
24 import org.opensaml.xml.util.LazyMap;
25
26 /**
27 * A manager for named sets of {@link KeyInfoGeneratorFactory} instances. Each name key serves as an index to an
28 * instance of {@link KeyInfoGeneratorManager}.
29 */
30 public class NamedKeyInfoGeneratorManager {
31
32 /** The set of named factory managers. */
33 private Map<String, KeyInfoGeneratorManager> managers;
34
35 /** The default manager for unnamed factories. */
36 private KeyInfoGeneratorManager defaultManager;
37
38 /** Flag indicating whether the default (unnamed) factory manager will be used to
39 * lookup factories for credentials. */
40 private boolean useDefaultManager;
41
42 /** Constructor. */
43 public NamedKeyInfoGeneratorManager() {
44 managers = new LazyMap<String, KeyInfoGeneratorManager>();
45 defaultManager = new KeyInfoGeneratorManager();
46 useDefaultManager = true;
47 }
48
49 /**
50 * Set the option as to whether the default (unnamed) manager will be used to lookup factories
51 * for credentials if there is no appropriate named factory for the credential type.
52 *
53 * @param newValue the new option value
54 */
55 public void setUseDefaultManager(boolean newValue) {
56 useDefaultManager = newValue;
57 }
58
59 /**
60 * Get the (unmodifiable) set of names of factory managers currently available.
61 *
62 * @return the set of all manager names currently configured
63 */
64 public Set<String> getManagerNames() {
65 return Collections.unmodifiableSet(managers.keySet());
66 }
67
68 /**
69 * Get the named factory manager. If it doesn't exist yet, one will be created.
70 *
71 * @param name the name of the manager to obtain
72 * @return the named manager
73 */
74 public KeyInfoGeneratorManager getManager(String name) {
75 KeyInfoGeneratorManager manager = managers.get(name);
76 if (manager == null) {
77 manager = new KeyInfoGeneratorManager();
78 managers.put(name, manager);
79 }
80 return manager;
81 }
82
83 /**
84 * Remove the named factory manager, and all its managed factories.
85 *
86 * @param name the name of the manager to remove
87 */
88 public void removeManager(String name) {
89 managers.remove(name);
90 }
91
92 /**
93 * Register a factory within the specified named manager. If that
94 * named manager does not currently exist, it will be created.
95 *
96 * @param name the name of the factory manager
97 * @param factory the factory to register
98 */
99 public void registerFactory(String name, KeyInfoGeneratorFactory factory) {
100 KeyInfoGeneratorManager manager = getManager(name);
101 manager.registerFactory(factory);
102 }
103
104 /**
105 * De-register a factory within the specified named manager.
106 *
107 * @param name the name of the factory manager
108 * @param factory the factory to de-register
109 */
110 public void deregisterFactory(String name, KeyInfoGeneratorFactory factory) {
111 KeyInfoGeneratorManager manager = managers.get(name);
112 if (manager == null) {
113 throw new IllegalArgumentException("Manager with name '" + name + "' does not exist");
114 }
115
116 manager.deregisterFactory(factory);
117 }
118
119 /**
120 * Register a factory with the default (unnamed) manager.
121 *
122 * @param factory the factory to register
123 */
124 public void registerDefaultFactory(KeyInfoGeneratorFactory factory) {
125 defaultManager.registerFactory(factory);
126 }
127
128 /**
129 * De-register a factory with the default (unnamed) manager.
130 *
131 * @param factory the factory to de-register
132 */
133 public void deregisterDefaultFactory(KeyInfoGeneratorFactory factory) {
134 defaultManager.deregisterFactory(factory);
135 }
136
137 /**
138 * Get the default (unnamed) factory manager.
139 *
140 * @return the default factory manager
141 */
142 public KeyInfoGeneratorManager getDefaultManager() {
143 return defaultManager;
144 }
145
146 /**
147 * Lookup and return the named generator factory for the type of the credential specified.
148 *
149 * @param name the name of the factory manger
150 * @param credential the credential to evaluate
151 * @return a factory for generators appropriate for the specified credential
152 */
153 public KeyInfoGeneratorFactory getFactory(String name, Credential credential) {
154 KeyInfoGeneratorManager manager = managers.get(name);
155 if (manager == null) {
156 throw new IllegalArgumentException("Manager with name '" + name + "' does not exist");
157 }
158
159 KeyInfoGeneratorFactory factory = manager.getFactory(credential);
160 if (factory == null) {
161 if (useDefaultManager) {
162 factory = defaultManager.getFactory(credential);
163 }
164 }
165 return factory;
166 }
167
168 }