01: /*
02: * Copyright 2002-2006 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 effects the
27: * registration of custom {@link Scope Scope(s)} in a {@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: * @author Rick Evans
34: * @since 2.0
35: */
36: public class CustomScopeConfigurer implements BeanFactoryPostProcessor,
37: Ordered {
38:
39: private int order = Ordered.LOWEST_PRECEDENCE;
40:
41: private Map scopes;
42:
43: /**
44: * Specify the custom scopes that are to be registered.
45: * <p>The keys indicate the scope names (of type String); each value
46: * is expected to be the corresponding custom {@link Scope} instance.
47: */
48: public void setScopes(Map scopes) {
49: this .scopes = scopes;
50: }
51:
52: public void setOrder(int order) {
53: this .order = order;
54: }
55:
56: public int getOrder() {
57: return order;
58: }
59:
60: public void postProcessBeanFactory(
61: ConfigurableListableBeanFactory beanFactory)
62: throws BeansException {
63: if (this .scopes != null) {
64: for (Iterator it = this .scopes.entrySet().iterator(); it
65: .hasNext();) {
66: Map.Entry entry = (Map.Entry) it.next();
67: Object key = entry.getKey();
68: if (!(key instanceof String)) {
69: throw new IllegalArgumentException(
70: "Invalid scope key [" + key
71: + "]: only Strings allowed");
72: }
73: Object value = entry.getValue();
74: if (!(value instanceof Scope)) {
75: throw new IllegalArgumentException("Mapped value ["
76: + value + "] for scope key [" + key
77: + "] is not of required type ["
78: + Scope.class.getName() + "]");
79: }
80: beanFactory.registerScope((String) key, (Scope) value);
81: }
82: }
83: }
84:
85: }
|