1 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 30 public abstract class ComponentHandler extends AbstractLoggable 31 implements Initializable, Disposable { 32 33 public static ComponentHandler getComponentHandler( 34 final Class componentClass, 35 final Configuration config, 36 final ComponentManager manager, 37 final Context context, 38 final RoleManager roles, 39 final LogKitManager logkit ) 40 throws Exception 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 ("[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 { 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 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 ("[CONFLICT] lifestyle interfaces: " + componentInstance.getClass().getName()); 117 } 118 119 return new ThreadSafeComponentHandler(componentInstance); 120 } 121 122 public abstract Component get() throws Exception ; 123 124 public abstract void put(Component component) throws Exception ; 125 } 126 | Popular Tags |