KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > beehive > netui > pageflow > internal > AnnotationReader


1 /*
2  * Copyright 2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  * $Header:$
17  */

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 JavaDoc;
30 import java.io.InputStream JavaDoc;
31 import java.io.IOException JavaDoc;
32 import java.io.Serializable JavaDoc;
33 import java.lang.reflect.Member JavaDoc;
34
35 /**
36  * Utility for reading XML files that describe annotations in classes. These files are generated during Page Flow build.
37  */

38 public class AnnotationReader
39         implements Serializable JavaDoc
40 {
41     private static final Logger _log = Logger.getInstance( AnnotationReader.class );
42     private static final String JavaDoc CACHE_ATTR = InternalConstants.ATTR_PREFIX + "annCache";
43     
44     private ProcessedAnnotationsDocument.ProcessedAnnotations _annotations;
45     
46     public static AnnotationReader getAnnotationReader( Class JavaDoc type, ServletContext JavaDoc 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 JavaDoc type, ServletContext JavaDoc servletContext )
68     {
69         String JavaDoc annotationsXml =
70                 PageFlowConstants.PAGEFLOW_MODULE_CONFIG_GEN_DIR + "/jpf-annotations-"
71                 + type.getName().replace( '.', '-' ) + ".xml";
72         InputStream JavaDoc 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 JavaDoc 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 JavaDoc e )
96                 {
97                     _log.error( "Could not close input stream for " + annotationsXml, e );
98                 }
99             }
100         }
101     }
102     
103     public ProcessedAnnotation getAnnotation( String JavaDoc declarationName, String JavaDoc 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                 // For now, we can be sure that there's only one element in this array.
115
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 JavaDoc member, String JavaDoc annotationTypeName )
125     {
126         return getAnnotation( member.getName(), InternalConstants.ANNOTATION_QUALIFIER + annotationTypeName );
127     }
128     
129     public ProcessedAnnotation getJpfAnnotation( Class JavaDoc type, String JavaDoc annotationTypeName )
130     {
131         return getAnnotation( type.getName(), InternalConstants.ANNOTATION_QUALIFIER + annotationTypeName );
132     }
133     
134     public static String JavaDoc getStringAttribute( ProcessedAnnotation ann, String JavaDoc 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 JavaDoc 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 JavaDoc 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