01: /*
02: * Copyright 2002-2007 the original author or authors.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
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.springframework.beans.factory.config;
18:
19: import java.util.Iterator;
20: import java.util.Map;
21:
22: import org.springframework.beans.BeansException;
23: import org.springframework.core.Ordered;
24:
25: /**
26: * Simple {@link BeanFactoryPostProcessor} implementation that registers
27: * custom {@link Scope Scope(s)} with the containing {@link ConfigurableBeanFactory}.
28: *
29: * <p>Will register all of the supplied {@link #setScopes(java.util.Map) scopes}
30: * with the {@link ConfigurableListableBeanFactory} that is passed to the
31: * {@link #postProcessBeanFactory(ConfigurableListableBeanFactory)} method.
32: *
33: * <p>This class allows for <i>declarative</i> registration of custom scopes.
34: * Alternatively, consider implementing a custom {@link BeanFactoryPostProcessor}
35: * that calls {@link ConfigurableBeanFactory#registerScope} programmatically.
36: *
37: * @author Rick Evans
38: * @author Juergen Hoeller
39: * @since 2.0
40: * @see ConfigurableBeanFactory#registerScope
41: */
42: public class CustomScopeConfigurer implements BeanFactoryPostProcessor,
43: Ordered {
44:
45: private int order = Ordered.LOWEST_PRECEDENCE;
46:
47: private Map scopes;
48:
49: /**
50: * Specify the custom scopes that are to be registered.
51: * <p>The keys indicate the scope names (of type String); each value
52: * is expected to be the corresponding custom {@link Scope} instance.
53: */
54: public void setScopes(Map scopes) {
55: this .scopes = scopes;
56: }
57:
58: public void setOrder(int order) {
59: this .order = order;
60: }
61:
62: public int getOrder() {
63: return this .order;
64: }
65:
66: public void postProcessBeanFactory(
67: ConfigurableListableBeanFactory beanFactory)
68: throws BeansException {
69: if (this .scopes != null) {
70: for (Iterator it = this .scopes.entrySet().iterator(); it
71: .hasNext();) {
72: Map.Entry entry = (Map.Entry) it.next();
73: Object key = entry.getKey();
74: if (!(key instanceof String)) {
75: throw new IllegalArgumentException(
76: "Invalid scope key [" + key
77: + "]: only Strings allowed");
78: }
79: Object value = entry.getValue();
80: if (!(value instanceof Scope)) {
81: throw new IllegalArgumentException("Mapped value ["
82: + value + "] for scope key [" + key
83: + "] is not of required type ["
84: + Scope.class.getName() + "]");
85: }
86: beanFactory.registerScope((String) key, (Scope) value);
87: }
88: }
89: }
90:
91: }
|