1 25 26 package org.objectweb.easybeans.container.archive; 27 28 import java.lang.reflect.ParameterizedType ; 29 import java.lang.reflect.Type ; 30 import java.util.HashMap ; 31 import java.util.Map ; 32 33 import org.objectweb.easybeans.api.EZBArchive; 34 import org.objectweb.easybeans.api.EZBArchiveFactory; 35 import org.objectweb.easybeans.log.JLog; 36 import org.objectweb.easybeans.log.JLogFactory; 37 38 42 public final class ArchiveManager { 43 44 47 private static JLog logger = JLogFactory.getLog(ArchiveManager.class); 48 49 52 private static ArchiveManager archiveManager; 53 54 58 private Map <Class , EZBArchiveFactory> factories = new HashMap <Class , EZBArchiveFactory>(); 59 60 63 private ArchiveManager() { 64 addFactory(new DefaultArchiveFactory()); 65 } 66 67 71 public static ArchiveManager getInstance() { 72 if (archiveManager == null) { 73 archiveManager = new ArchiveManager(); 74 } 75 return archiveManager; 76 } 77 78 83 public void addFactory(final EZBArchiveFactory factory) { 84 85 Class factoryClass = factory.getClass(); 86 Class argumentClass = getSupportedType(factoryClass); 87 88 logger.debug("Adding factory '" + factoryClass.getName() + "' for class '" + argumentClass + "'."); 89 90 factories.put(argumentClass, factory); 92 93 } 94 95 102 private Class getSupportedType(final Class factoryClass) { 103 104 Type [] generics = factoryClass.getGenericInterfaces(); 106 107 if (generics.length != 1) { 109 throw new IllegalArgumentException ("Invalid class, supports only class with a single generic"); 110 } 111 112 Type factoryClassType = generics[0]; 114 ParameterizedType parameterType = null; 115 116 if (factoryClassType instanceof ParameterizedType ) { 117 parameterType = (ParameterizedType ) factoryClassType; 118 } else { 119 throw new IllegalArgumentException ("Object '" + factoryClassType + "' is not a ParameterizedType"); 120 } 121 122 Type [] arguments = parameterType.getActualTypeArguments(); 123 if (arguments.length != 1) { 124 throw new IllegalArgumentException ("Invalid class, supports only class with a single generic"); 125 } 126 return (Class ) arguments[0]; 127 } 128 129 134 public void removeFactory(final EZBArchiveFactory factory) { 135 136 Class factoryClass = factory.getClass(); 137 Class argumentClass = getSupportedType(factoryClass); 138 139 if (factories.containsKey(argumentClass)) { 140 logger.debug("Removing factory '" + factoryClass.getName() + "' for class '" + argumentClass + "'."); 141 142 factories.remove(argumentClass); 144 } else { 145 logger.debug("Factory '" + factoryClass.getName() + "' was not registered in the ArchiveManager."); 146 } 147 } 148 149 154 @SuppressWarnings ("unchecked") 155 public EZBArchive getArchive(final Object o) { 156 Class objectClass = o.getClass(); 158 159 EZBArchiveFactory factory = factories.get(objectClass); 161 162 if (factory == null) { 163 164 for (Class cls : factories.keySet()) { 166 if (cls.isAssignableFrom(objectClass) && factory == null) { 167 factory = factories.get(cls); 169 } 170 } 171 172 if (factory == null) { 174 throw new IllegalArgumentException ("No factory found for the type '" + objectClass + "'."); 175 } 176 } 177 178 return factory.create(o); 179 } 180 181 } 182 | Popular Tags |