KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > beans > factory > config > CustomScopeConfigurer


1 /*
2  * Copyright 2002-2006 the original author or authors.
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.springframework.beans.factory.config;
18
19 import java.util.Iterator JavaDoc;
20 import java.util.Map JavaDoc;
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, Ordered {
37
38     private int order = Ordered.LOWEST_PRECEDENCE;
39
40     private Map JavaDoc scopes;
41
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 JavaDoc 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
61     public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
62         if (this.scopes != null) {
63             for (Iterator JavaDoc it = this.scopes.entrySet().iterator(); it.hasNext();) {
64                 Map.Entry JavaDoc entry = (Map.Entry JavaDoc) it.next();
65                 Object JavaDoc key = entry.getKey();
66                 if (!(key instanceof String JavaDoc)) {
67                     throw new IllegalArgumentException JavaDoc(
68                             "Invalid scope key [" + key + "]: only Strings allowed");
69                 }
70                 Object JavaDoc value = entry.getValue();
71                 if (!(value instanceof Scope)) {
72                     throw new IllegalArgumentException JavaDoc("Mapped value [" + value + "] for scope key [" +
73                             key + "] is not of required type [" + Scope.class.getName() + "]");
74                 }
75                 beanFactory.registerScope((String JavaDoc) key, (Scope) value);
76             }
77         }
78     }
79
80 }
81
Popular Tags