001: package com.bm.ejb3metadata.annotations.metadata;
002:
003: import java.util.Map;
004:
005: import javax.persistence.EntityManager;
006:
007: import com.bm.cfg.Ejb3UnitCfg;
008: import com.bm.creators.DynamicDIModuleCreator;
009: import com.bm.creators.MockedDIModuleCreator;
010: import com.bm.ejb3guice.inject.Module;
011: import com.bm.ejb3metadata.MetadataAnalyzer;
012:
013: public final class MetaDataCache {
014:
015: /**
016: * Contains the meta data information off all jars. GuardedBy MetaDataCache
017: * class lock.
018: */
019: private static final EjbJarAnnotationMetadata META_DATA = new EjbJarAnnotationMetadata();
020:
021: /**
022: * Constructor.
023: */
024: private MetaDataCache() {
025:
026: }
027:
028: /**
029: * Adds explicit matadate form annotated classes to the ejb3unit framework
030: * @param classes the list of classes
031: */
032: public static void addClasses(String... classes) {
033: for (String s : classes) {
034: EjbJarAnnotationMetadata newJar = MetadataAnalyzer
035: .initialize(s);
036: mergeJarIntoCache(newJar);
037: }
038: }
039:
040: /**
041: * Returns the implementation for the class
042: *
043: * @author Daniel Wiese
044: * @since Jul 19, 2007
045: * @param toInspect
046: * the interface.
047: * @return the implementation
048: */
049: public static String getBeanImplementationForInterface(
050: Class<?> toInspect) {
051: getMetaData(toInspect);
052: final String name = toInspect.getName();
053: String implFound = null;
054: // add mappings to the creator
055: Map<String, String> interface2implemantation = getInterface2implemantation();
056: implFound = interface2implemantation
057: .get(name.replace('.', '/'));
058:
059: if (implFound == null) {
060: EjbJarAnnotationMetadata newJar = MetadataAnalyzer
061: .initialize(toInspect);
062: interface2implemantation = newJar
063: .getInterface2implemantation();
064: implFound = interface2implemantation.get(name.replace('.',
065: '/'));
066: if (implFound != null) {
067: mergeJarIntoCache(newJar);
068: } else {
069: throw new IllegalArgumentException(
070: "Can't find any meta data information for the class ("
071: + toInspect.getName() + ")");
072: }
073: }
074:
075: return implFound;
076: }
077:
078: /**
079: * Returns the {@link ClassAnnotationMetadata} for the given class name
080: *
081: * @param className
082: * The name of the class in the form com.bla.class
083: * @return Metadata if present otherwise NULL
084: */
085: public static synchronized ClassAnnotationMetadata getClassAnnotationMetaData(
086: String className) {
087: return META_DATA.getClassAnnotationMetadata(className);
088: }
089:
090: /**
091: * Returns the Module {@link Module} for guice dependency injection with
092: * valid ejb3mapping definition.
093: *
094: * @param conf
095: * the configuration
096: * @param manager
097: * the entity manager instance which should be used for the
098: * binding
099: * @param toCheck
100: * the class wich identifyes the module
101: * @return the module creator.
102: */
103: public static DynamicDIModuleCreator getDynamicModuleCreator(
104: Ejb3UnitCfg conf, EntityManager manager, Class toCheck) {
105: // ensure the metadata is resolved for this jar (where toCheck is in)
106: getMetaData(toCheck);
107: final DynamicDIModuleCreator dynamicDIModuleCreator = new DynamicDIModuleCreator(
108: conf, manager);
109: // add mappings to the creator
110: dynamicDIModuleCreator
111: .addInteface2ImplMap(getInterface2implemantation());
112:
113: return dynamicDIModuleCreator;
114: }
115:
116: /**
117: * Returns the metadate mapping for the jar where the class file is in.
118: *
119: * @author Daniel Wiese
120: * @since Jul 19, 2007
121: * @param toCheck
122: * the class wich identifyes the module
123: * @return metat data
124: */
125: public static ClassAnnotationMetadata getMetaData(Class toCheck) {
126: ClassAnnotationMetadata classMeta = getClassAnnotationMetaData(toCheck
127: .getName().replace('.', '/'));
128:
129: if (classMeta == null) {
130: // still not found
131: EjbJarAnnotationMetadata newJar = MetadataAnalyzer
132: .initialize(toCheck);
133: classMeta = newJar.getClassAnnotationMetadata(toCheck
134: .getName().replace('.', '/'));
135: if (classMeta == null) {
136: throw new IllegalArgumentException(
137: "Can't find any meta data information for the class ("
138: + toCheck.getName() + ")");
139: } else {
140: mergeJarIntoCache(newJar);
141: }
142: }
143:
144: return classMeta;
145: }
146:
147: /**
148: * Returns the Module {@link Module} for guice dependency injection with
149: * valid ejb3mapping definition. Will inject mock controlls.
150: *
151: * @param toCheck
152: * the class to check
153: * @return the module creator.
154: */
155: public static MockedDIModuleCreator getMockModuleCreator(
156: Class toCheck) {
157: // ensure the metadata is resolved for this jar (where toCheck is in)
158: getMetaData(toCheck);
159: final MockedDIModuleCreator mockedDIModuleCreator = new MockedDIModuleCreator();
160: // add mappings to the creator
161: mockedDIModuleCreator
162: .addInteface2ImplMap(getInterface2implemantation());
163: return mockedDIModuleCreator;
164: }
165:
166: private static synchronized Map<String, String> getInterface2implemantation() {
167: return META_DATA.getInterface2implemantation();
168: }
169:
170: private static synchronized void mergeJarIntoCache(
171: EjbJarAnnotationMetadata ejbJarAnnotationMetadata) {
172: META_DATA
173: .mergeClassAnnotationMetadata(ejbJarAnnotationMetadata);
174: }
175:
176: }
|