View Javadoc

1   /*
2    * Copyright 2007 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 net.sf.snamespaces.extensions;
18  
19  import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
20  import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
21  import org.springframework.beans.factory.config.Scope;
22  import org.springframework.core.Ordered;
23  
24  /**
25   * TODO make doc
26   * 
27   * @author Jonhnny Weslley
28   * @version 1.00, 13/09/2007
29   * @since 1.0
30   */
31  public class SimpleCustomScopeConfigurer implements BeanFactoryPostProcessor, Ordered {
32  
33  	public static final String SCOPE_BEAN_NAME_PREFIX = "scope:";
34  
35  	private int order = LOWEST_PRECEDENCE;
36  
37  	public void setOrder(final int order) {
38  		this.order = order;
39  	}
40  
41  	public int getOrder() {
42  		return order;
43  	}
44  
45  	public void postProcessBeanFactory(
46  			final ConfigurableListableBeanFactory beanFactory) {
47  		final String[] customScopeBeanNames = beanFactory.getBeanNamesForType(Scope.class);
48  		for (final String beanName : customScopeBeanNames) {
49  			if (beanName.startsWith(SCOPE_BEAN_NAME_PREFIX)) {
50  				registerCustomScope(beanFactory, beanName);
51  			}
52  		}
53  	}
54  
55  	private void registerCustomScope(
56  			final ConfigurableListableBeanFactory beanFactory, final String beanName) {
57  		final String scopeName = beanName.substring(SCOPE_BEAN_NAME_PREFIX.length());
58  		final Object scope = beanFactory.getBean(beanName);
59  		beanFactory.registerScope(scopeName, (Scope) scope);
60  	}
61  
62  }