KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > avalon > excalibur > component > ComponentHandler


1 /*
2  * Copyright (C) The Apache Software Foundation. All rights reserved.
3  *
4  * This software is published under the terms of the Apache Software License
5  * version 1.1, a copy of which has been included with this distribution in
6  * the LICENSE.txt file.
7  */

8 package org.apache.avalon.excalibur.component;
9
10 import org.apache.avalon.framework.activity.Disposable;
11 import org.apache.avalon.framework.activity.Initializable;
12 import org.apache.avalon.framework.component.Component;
13 import org.apache.avalon.framework.component.ComponentManager;
14 import org.apache.avalon.framework.configuration.Configuration;
15 import org.apache.avalon.framework.context.Context;
16 import org.apache.avalon.framework.logger.AbstractLoggable;
17 import org.apache.avalon.framework.thread.SingleThreaded;
18 import org.apache.avalon.framework.thread.ThreadSafe;
19 import org.apache.avalon.excalibur.pool.Poolable;
20 import org.apache.avalon.excalibur.logger.LogKitManager;
21
22 /**
23  * This class acts like a Factory to instantiate the correct version
24  * of the ComponentHandler that you need.
25  *
26  * @author <a HREF="mailto:bloritsch@apache.org">Berin Loritsch</a>
27  * @version CVS $Revision: 1.4 $ $Date: 2001/12/11 09:53:27 $
28  * @since 4.0
29  */

30 public abstract class ComponentHandler extends AbstractLoggable
31                                        implements Initializable, Disposable {
32
33     public static ComponentHandler getComponentHandler(
34                              final Class JavaDoc componentClass,
35                              final Configuration config,
36                              final ComponentManager manager,
37                              final Context context,
38                              final RoleManager roles,
39                              final LogKitManager logkit )
40     throws Exception JavaDoc
41     {
42         int numInterfaces = 0;
43
44         if (SingleThreaded.class.isAssignableFrom(componentClass))
45         {
46             numInterfaces++;
47         }
48
49         if (ThreadSafe.class.isAssignableFrom(componentClass))
50         {
51             numInterfaces++;
52         }
53
54         if (Poolable.class.isAssignableFrom(componentClass))
55         {
56             numInterfaces++;
57         }
58
59         if (numInterfaces > 1)
60         {
61             throw new Exception JavaDoc("[CONFLICT] lifestyle interfaces: " + componentClass.getName());
62         }
63
64         if (Poolable.class.isAssignableFrom(componentClass))
65         {
66             return new PoolableComponentHandler(componentClass,
67                                                 config,
68                                                 manager,
69                                                 context,
70                                                 roles,
71                                                 logkit);
72         }
73         else if (ThreadSafe.class.isAssignableFrom(componentClass))
74         {
75             return new ThreadSafeComponentHandler(componentClass,
76                                                   config,
77                                                   manager,
78                                                   context,
79                                                   roles,
80                                                   logkit);
81         }
82         else // This is a SingleThreaded component
83
{
84             return new DefaultComponentHandler(componentClass,
85                                                config,
86                                                manager,
87                                                context,
88                                                roles,
89                                                logkit);
90         }
91     }
92
93     public static ComponentHandler getComponentHandler(
94                              final Component componentInstance )
95     throws Exception JavaDoc
96     {
97         int numInterfaces = 0;
98
99         if (SingleThreaded.class.isAssignableFrom(componentInstance.getClass()))
100         {
101             numInterfaces++;
102         }
103
104         if (ThreadSafe.class.isAssignableFrom(componentInstance.getClass()))
105         {
106             numInterfaces++;
107         }
108
109         if (Poolable.class.isAssignableFrom(componentInstance.getClass()))
110         {
111             numInterfaces++;
112         }
113
114         if (numInterfaces > 1)
115         {
116             throw new Exception JavaDoc("[CONFLICT] lifestyle interfaces: " + componentInstance.getClass().getName());
117         }
118
119         return new ThreadSafeComponentHandler(componentInstance);
120     }
121
122     public abstract Component get() throws Exception JavaDoc;
123
124     public abstract void put(Component component) throws Exception JavaDoc;
125 }
126
Popular Tags