KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > beehive > wsm > processor > apt > WsmAnnotationProcessor


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

20
21 import java.util.HashSet JavaDoc;
22 import java.util.Set JavaDoc;
23
24 import com.sun.mirror.apt.AnnotationProcessorEnvironment;
25 import com.sun.mirror.apt.Messager;
26
27 import com.sun.mirror.declaration.AnnotationTypeDeclaration;
28 import com.sun.mirror.declaration.ClassDeclaration;
29 import com.sun.mirror.declaration.Declaration;
30 import com.sun.mirror.declaration.InterfaceDeclaration;
31 import com.sun.mirror.declaration.TypeDeclaration;
32
33 import javax.jws.WebService;
34
35 import org.apache.beehive.wsm.model.BeehiveWsTypeMetadata;
36 import org.apache.beehive.wsm.model.jsr181.Jsr181ObjectModelStore;
37 import org.apache.beehive.wsm.model.jsr181.Jsr181TypeMetadataImpl;
38
39
40 /**
41  * WsmAnnotationProcessor provides an API for IDEs and other applications to
42  * perform code validation.
43  * Currently, check() and generate() are not implemented. Since the
44  * WsmAnnotationProcessor does not generate any artifacts but class files,
45  * process() is equivalent with check() and generate() is void.
46  */

47 public class WsmAnnotationProcessor extends TwoPhaseAnnotationProcessor {
48
49     private Set JavaDoc<TypeDeclaration> handledDecls = new HashSet JavaDoc<TypeDeclaration>();
50     private Jsr181ObjectModelStore oms;
51     
52     /**
53      * Constructor.
54      */

55     public WsmAnnotationProcessor(Set JavaDoc<AnnotationTypeDeclaration> atds, AnnotationProcessorEnvironment env) {
56         super (atds, env);
57         oms = new Jsr181ObjectModelStore(env);
58     }
59
60     /**
61      * Checks whether a source file complies with JSR-181.
62      * @see TwoPhaseAnnotationProcessor
63      * todo: should this always run all tests whenever called for any supported annotation?
64      */

65     @Override JavaDoc
66     public void check(Declaration _decl) {
67
68         Messager messager = _env.getMessager();
69
70         try {
71
72             // check if we've already handled this declaration
73
if (handledDecls.contains(_decl)) {
74                 return;
75             }
76
77             // check if we're interested in declaration
78
if (! (_decl instanceof TypeDeclaration)) {
79                 return;
80             }
81             WebService wsAnnotation = _decl.getAnnotation(WebService.class);
82             if (null == wsAnnotation) {
83                 messager.printError(_decl.getPosition(), "@WebService annotation missing; unable to process: " + ((TypeDeclaration) _decl).getQualifiedName());
84             }
85
86             // store declaration so we don't handle it multiple times
87
handledDecls.add((TypeDeclaration) _decl);
88         
89             // service implementation bean
90
if (_decl instanceof ClassDeclaration) {
91                 ClassDeclaration classDecl = (ClassDeclaration) _decl;
92                 messager.printNotice("processing service implementation bean: " + classDecl.getQualifiedName());
93                 
94                 BeehiveWsTypeMetadata om = null;
95                 String JavaDoc endpointInterface = wsAnnotation.endpointInterface().trim();
96                 
97                 // start from endpoint interface
98
if (null != endpointInterface && 0 < endpointInterface.length()) {
99
100                     // get object model for service endpoint interface
101
om = getEndpointInterfaceObjectModel(endpointInterface);
102                     
103                     // merge abstract and concrete object models
104
om.merge(new MirrorTypeInfo(classDecl, _env));
105                 }
106                 
107                 // create object model from scratch
108
else {
109                     om = new Jsr181TypeMetadataImpl(new MirrorTypeInfo(classDecl, _env));
110                 }
111
112                 // check if we have an object model
113
if (null == om) {
114                     return;
115                 }
116
117                 // persist object model
118
oms.store(om);
119             }
120             
121             // service endpoint interface
122
else if (_decl instanceof InterfaceDeclaration) {
123                 InterfaceDeclaration interfaceDecl = (InterfaceDeclaration) _decl;
124                 
125                 messager.printNotice("processing service endpoint interface: " + interfaceDecl.getQualifiedName());
126                 
127                 // create object model
128
BeehiveWsTypeMetadata om = new Jsr181TypeMetadataImpl(new MirrorTypeInfo(interfaceDecl, _env));
129                 if (null == om) {
130                     return;
131                 }
132
133                 // store the object model
134
oms.store(om);
135             }
136
137             // @WebService annotation on unknown/unsupported type definition
138
else {
139                 messager.printError(_decl.getPosition(), "found unsupported type of TypeDeclaration:" + _decl.getSimpleName());
140             }
141         }
142
143         // if an exception or error ocurred log it and return
144
catch (Throwable JavaDoc t) {
145             messager.printError(_decl.getPosition(), t.getMessage());
146         }
147     }
148     
149     /**
150      * @see TwoPhaseAnnotationProcessor
151      */

152     @Override JavaDoc
153     public void generate(Declaration _decl) {
154             
155         // todo: persist all object models that have been created [by check()]
156
}
157
158     private BeehiveWsTypeMetadata getEndpointInterfaceObjectModel(String JavaDoc endpointInterface) {
159
160         BeehiveWsTypeMetadata om = null;
161
162         // search for persistent object model
163
try {
164             om = Jsr181ObjectModelStore.load(getClass().getClassLoader().getResourceAsStream(Jsr181ObjectModelStore.getLocation(endpointInterface).toString()));
165         }
166         catch (Throwable JavaDoc t) { }
167
168         // try to generate object model
169
if (null == om) {
170             try {
171                 _env.getMessager().printNotice("-> loading object model for required endpoint interface:" + endpointInterface);
172                 check(_env.getTypeDeclaration(endpointInterface));
173                 om = Jsr181ObjectModelStore.load(getClass().getClassLoader().getResourceAsStream(Jsr181ObjectModelStore.getLocation(endpointInterface).toString()));
174             }
175             catch (Throwable JavaDoc t) { }
176         }
177         
178         return om;
179     }
180 }
181
Popular Tags