1 18 package org.apache.beehive.netui.pageflow.internal; 19 20 import org.apache.beehive.netui.pageflow.PageFlowConstants; 21 import org.apache.beehive.netui.compiler.schema.annotations.ProcessedAnnotationsDocument; 22 import org.apache.beehive.netui.compiler.schema.annotations.ProcessedAnnotation; 23 import org.apache.beehive.netui.compiler.schema.annotations.AnnotatedElement; 24 import org.apache.beehive.netui.compiler.schema.annotations.AnnotationAttribute; 25 import org.apache.beehive.netui.util.logging.Logger; 26 import org.apache.beehive.netui.util.internal.concurrent.InternalConcurrentHashMap; 27 import org.apache.xmlbeans.XmlException; 28 29 import javax.servlet.ServletContext ; 30 import java.io.InputStream ; 31 import java.io.IOException ; 32 import java.io.Serializable ; 33 import java.lang.reflect.Member ; 34 35 38 public class AnnotationReader 39 implements Serializable 40 { 41 private static final Logger _log = Logger.getInstance( AnnotationReader.class ); 42 private static final String CACHE_ATTR = InternalConstants.ATTR_PREFIX + "annCache"; 43 44 private ProcessedAnnotationsDocument.ProcessedAnnotations _annotations; 45 46 public static AnnotationReader getAnnotationReader( Class type, ServletContext servletContext ) 47 { 48 InternalConcurrentHashMap cache = ( InternalConcurrentHashMap ) servletContext.getAttribute( CACHE_ATTR ); 49 50 if ( cache == null ) 51 { 52 cache = new InternalConcurrentHashMap(); 53 servletContext.setAttribute( CACHE_ATTR, cache ); 54 } 55 56 AnnotationReader reader = ( AnnotationReader ) cache.get( type ); 57 58 if ( reader == null ) 59 { 60 reader = new AnnotationReader( type, servletContext ); 61 cache.put( type, reader ); 62 } 63 64 return reader; 65 } 66 67 private AnnotationReader( Class type, ServletContext servletContext ) 68 { 69 String annotationsXml = 70 PageFlowConstants.PAGEFLOW_MODULE_CONFIG_GEN_DIR + "/jpf-annotations-" 71 + type.getName().replace( '.', '-' ) + ".xml"; 72 InputStream in = servletContext.getResourceAsStream( annotationsXml ); 73 74 if ( in != null ) 75 { 76 try 77 { 78 ProcessedAnnotationsDocument doc = ProcessedAnnotationsDocument.Factory.parse( in ); 79 _annotations = doc.getProcessedAnnotations(); 80 } 81 catch ( XmlException e ) 82 { 83 _log.error( "Error while parsing annotations XML file " + annotationsXml, e ); 84 } 85 catch ( IOException e ) 86 { 87 _log.error( "Error while reading annotations XML file " + annotationsXml, e ); 88 } 89 finally 90 { 91 try 92 { 93 in.close(); 94 } 95 catch ( IOException e ) 96 { 97 _log.error( "Could not close input stream for " + annotationsXml, e ); 98 } 99 } 100 } 101 } 102 103 public ProcessedAnnotation getAnnotation( String declarationName, String annotationTypeName ) 104 { 105 if ( _annotations == null ) return null; 106 107 AnnotatedElement[] elements = _annotations.getAnnotatedElementArray(); 108 109 for ( int i = 0; i < elements.length; i++ ) 110 { 111 AnnotatedElement element = elements[i]; 112 if ( element.getElementName().equals( declarationName ) ) 113 { 114 assert element.getAnnotationArray().length == 1 : element.getAnnotationArray().length; 116 ProcessedAnnotation pa = element.getAnnotationArray( 0 ); 117 return pa.getAnnotationName().equals( annotationTypeName ) ? pa : null; 118 } 119 } 120 121 return null; 122 } 123 124 public ProcessedAnnotation getJpfAnnotation( Member member, String annotationTypeName ) 125 { 126 return getAnnotation( member.getName(), InternalConstants.ANNOTATION_QUALIFIER + annotationTypeName ); 127 } 128 129 public ProcessedAnnotation getJpfAnnotation( Class type, String annotationTypeName ) 130 { 131 return getAnnotation( type.getName(), InternalConstants.ANNOTATION_QUALIFIER + annotationTypeName ); 132 } 133 134 public static String getStringAttribute( ProcessedAnnotation ann, String attrName ) 135 { 136 AnnotationAttribute[] attrs = ann.getAnnotationAttributeArray(); 137 138 for ( int i = 0; i < attrs.length; i++ ) 139 { 140 AnnotationAttribute attr = attrs[i]; 141 142 if ( attr.getAttributeName().equals( attrName ) ) 143 { 144 String value = attr.getStringValue1(); 145 assert value != null : "attribute " + attrName + " did not have a String value"; 146 return value; 147 } 148 } 149 150 return null; 151 } 152 153 public static ProcessedAnnotation[] getAnnotationArrayAttribute( ProcessedAnnotation ann, String attrName ) 154 { 155 AnnotationAttribute[] attrs = ann.getAnnotationAttributeArray(); 156 157 for ( int i = 0; i < attrs.length; i++ ) 158 { 159 AnnotationAttribute attr = attrs[i]; 160 161 if ( attr.getAttributeName().equals( attrName ) ) 162 { 163 ProcessedAnnotation[] array = attr.getAnnotationValueArray(); 164 assert array != null : "attribute " + attrName + " did not have an array of annotations."; 165 return array; 166 } 167 } 168 169 return null; 170 } 171 } 172 | Popular Tags |