KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > aop > framework > autoproxy > metadata > AttributesThreadLocalTargetSourceCreator


1 /*
2  * Copyright 2002-2005 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.aop.framework.autoproxy.metadata;
18
19 import java.util.Collection JavaDoc;
20
21 import org.springframework.aop.framework.autoproxy.target.AbstractBeanFactoryBasedTargetSourceCreator;
22 import org.springframework.aop.target.AbstractBeanFactoryBasedTargetSource;
23 import org.springframework.aop.target.ThreadLocalTargetSource;
24 import org.springframework.metadata.Attributes;
25
26 /**
27  * PrototypeTargetSourceCreator driven by metadata. Creates a ThreadLocalTargetSource
28  * only if there's a ThreadLocalAttribute associated with the class.
29  *
30  * @author Rod Johnson
31  * @see org.springframework.aop.target.ThreadLocalTargetSource
32  */

33 public class AttributesThreadLocalTargetSourceCreator extends AbstractBeanFactoryBasedTargetSourceCreator {
34
35     /**
36      * Underlying Attributes implementation that we're using.
37      */

38     private Attributes attributes;
39
40     /**
41      * Create a new AttributesThreadLocalTargetSourceCreator.
42      * @see #setAttributes
43      */

44     public AttributesThreadLocalTargetSourceCreator() {
45     }
46
47     /**
48      * Create a new AttributesThreadLocalTargetSourceCreator.
49      * @param attributes the Attributes implementation to use
50      */

51     public AttributesThreadLocalTargetSourceCreator(Attributes attributes) {
52         if (attributes == null) {
53             throw new IllegalArgumentException JavaDoc("Attributes is required");
54         }
55         this.attributes = attributes;
56     }
57
58     /**
59      * Set the Attributes implementation to use.
60      */

61     public void setAttributes(Attributes attributes) {
62         this.attributes = attributes;
63     }
64
65     public void afterPropertiesSet() {
66         if (this.attributes == null) {
67             throw new IllegalArgumentException JavaDoc("'attributes' is required");
68         }
69     }
70
71     protected AbstractBeanFactoryBasedTargetSource createBeanFactoryBasedTargetSource(
72             Class JavaDoc beanClass, String JavaDoc beanName) {
73
74         // See if there's a pooling attribute.
75
Collection JavaDoc atts = this.attributes.getAttributes(beanClass, ThreadLocalAttribute.class);
76         if (atts.isEmpty()) {
77             // No pooling attribute: don't create a custom TargetSource.
78
return null;
79         }
80         else {
81             return new ThreadLocalTargetSource();
82         }
83     }
84
85 }
86
Popular Tags