KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > avalon > fortress > impl > handler > PerThreadComponentHandler


1 /*
2  * Copyright 2003-2004 The Apache Software Foundation
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
12  * implied.
13  *
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 package org.apache.avalon.fortress.impl.handler;
19
20 import java.util.Collections JavaDoc;
21 import java.util.Iterator JavaDoc;
22 import java.util.LinkedList JavaDoc;
23 import java.util.List JavaDoc;
24
25
26 /**
27  * The ThreadSafeComponentHandler to make sure components are initialized
28  * and destroyed correctly.
29  *
30  * @author <a HREF="mailto:dev@avalon.apache.org">Avalon Development Team</a>
31  * @version CVS $Revision: 1.9 $ $Date: 2004/02/28 15:16:25 $
32  * @since 4.0
33  */

34 public final class PerThreadComponentHandler
35     extends AbstractComponentHandler
36 {
37     private ThreadLocalComponent m_instance;
38     private List JavaDoc m_instances;
39
40     public void initialize()
41         throws Exception JavaDoc
42     {
43         super.initialize();
44         m_instance = new ThreadLocalComponent( this );
45         m_instances = Collections.synchronizedList(new LinkedList JavaDoc());
46     }
47
48     /**
49      * Get a reference of the desired Component
50      */

51     protected Object JavaDoc doGet()
52         throws Exception JavaDoc
53     {
54         final Object JavaDoc instance = m_instance.get();
55         if ( null == instance )
56         {
57             throw new IllegalStateException JavaDoc( "Instance is unavailable" );
58         }
59
60         return instance;
61     }
62
63     protected void doDispose()
64     {
65         Iterator JavaDoc it = m_instances.iterator();
66         while (it.hasNext())
67         {
68             disposeComponent( it.next() );
69             it.remove();
70         }
71         m_instance = null;
72         m_instances = null;
73     }
74
75     private static final class ThreadLocalComponent
76         extends ThreadLocal JavaDoc
77     {
78         private final PerThreadComponentHandler m_handler;
79
80         protected ThreadLocalComponent( final PerThreadComponentHandler handler )
81         {
82             m_handler = handler;
83         }
84
85         protected Object JavaDoc initialValue()
86         {
87             try
88             {
89                 Object JavaDoc component = m_handler.newComponent();
90                 m_handler.m_instances.add(component);
91                 return component;
92             }
93             catch ( final Exception JavaDoc e )
94             {
95                 return null;
96             }
97         }
98     }
99 }
100
Popular Tags