KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > webservice > apt > WebServiceRefAp


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 /*
25  * ServiceRefAp.java
26  *
27  * Created on June 15, 2005, 9:00 AM
28  *
29  * To change this template, choose Tools | Options and locate the template under
30  * the Source Creation and Management node. Right-click the template and choose
31  * Open. You can then make changes to the template in the Source Editor.
32  */

33
34 package com.sun.enterprise.webservice.apt;
35
36 import javax.xml.ws.WebServiceRef;
37 import java.io.File JavaDoc;
38 import java.lang.Runtime JavaDoc;
39 import java.lang.Process JavaDoc;
40 import java.util.logging.Logger JavaDoc;
41 import java.util.logging.Level JavaDoc;
42
43 import com.sun.mirror.declaration.TypeDeclaration;
44 import com.sun.mirror.declaration.ClassDeclaration;
45 import com.sun.mirror.declaration.FieldDeclaration;
46 import com.sun.mirror.apt.AnnotationProcessorEnvironment;
47 import com.sun.mirror.apt.AnnotationProcessor;
48 import com.sun.mirror.util.SimpleDeclarationVisitor;
49
50 import static com.sun.mirror.util.DeclarationVisitors.*;
51
52 import com.sun.enterprise.deployment.backend.ProcessWatcher;
53 import com.sun.enterprise.deployment.backend.DeploymentLogger;
54
55 /**
56  *
57  * @author dochez
58  */

59 public class WebServiceRefAp implements AnnotationProcessor {
60     
61     private final AnnotationProcessorEnvironment env;
62     // should be set in the context of the appserver, but not in the
63
// apt context
64
public static Logger JavaDoc logger = null;
65     
66     public WebServiceRefAp(AnnotationProcessorEnvironment env) {
67         this.env = env;
68     }
69     
70     public void process() {
71         for (TypeDeclaration typeDecl : env.getSpecifiedTypeDeclarations())
72             typeDecl.accept(getDeclarationScanner(new ServiceRefVisitor(),
73                 NO_OP));
74     }
75     
76     private class ServiceRefVisitor extends SimpleDeclarationVisitor {
77         public void visitClassDeclaration(ClassDeclaration d) {
78             if (logger!=null) {
79                 logger.fine("Processing " + d.getQualifiedName());
80             }
81         }
82         
83         public void visitFieldDeclaration(FieldDeclaration f) {
84             
85             WebServiceRef ref = f.getAnnotation(WebServiceRef.class);
86             if (ref==null) {
87                 return;
88             } else {
89                 if (logger!=null && logger.isLoggable(Level.FINE)) {
90                     logger.fine("Found " + f.getSimpleName() + " annotated with WebServiceRef");
91                     logger.fine("Name is " + ref.name());
92                     logger.fine("WSDL is " + ref.wsdlLocation());
93                     logger.fine("of type " + f.getType().toString());
94                 }
95             }
96                 
97             // no wsdl specified, nothing we can do at this point...
98
if (ref.wsdlLocation()==null || ref.wsdlLocation().length()==0)
99                 return;
100             
101             File JavaDoc server = new File JavaDoc(System.getProperty("com.sun.aas.installRoot"));
102             server = new File JavaDoc(server, "bin");
103             File JavaDoc wsimport = new File JavaDoc(server, "wsimport");
104             
105             if (wsimport.exists()) {
106                 
107                 File JavaDoc classesDir = new File JavaDoc(System.getProperty("user.dir"));
108                 String JavaDoc outputDir = env.getOptions().get("-d");
109                 if (outputDir!=null) {
110                     if (!(new File JavaDoc(outputDir).isAbsolute())) {
111                         classesDir = new File JavaDoc(classesDir, outputDir);
112                     }
113                     classesDir.mkdirs();
114                 }
115                 
116                 String JavaDoc wsc = wsimport.getAbsolutePath();
117                 String JavaDoc wscompileArgs =
118                         " -keep -d " + classesDir.getAbsolutePath() +
119                         " " + ref.wsdlLocation();
120                 if (logger!=null) {
121                     logger.log(Level.INFO, "Invoking wsimport with " + ref.wsdlLocation());
122                 } else {
123                     System.out.println("Invoking " + wsimport.getAbsolutePath() + " with " + ref.wsdlLocation() + "in " + classesDir.getAbsolutePath());
124                 }
125                 String JavaDoc command = wsc + " " + wscompileArgs;
126                 if (logger!=null && logger.isLoggable(Level.FINE))
127                     logger.fine("Command " + command);
128                 
129                 int exitValue=-1;
130                 try {
131                     Process JavaDoc p = Runtime.getRuntime().exec(command);
132                     ProcessWatcher pw = new ProcessWatcher(p);
133                     exitValue = pw.watch();
134                 } catch(Exception JavaDoc e) {
135                     e.printStackTrace();
136                 }
137                 if (exitValue==0) {
138                     if (logger!=null)
139                         logger.log(Level.INFO, "wsimport successful");
140                 } else {
141                     if (logger!=null)
142                         logger.log(Level.SEVERE, "wsimport failed");
143                     return;
144                 }
145                 
146             } else {
147                 if (logger!=null) {
148                     logger.log(Level.SEVERE, "Cannot find wsimport tool");
149                 } else {
150                      System.out.println("Cannot find wsimport");
151                 }
152                 return;
153             }
154         }
155             
156     }
157 }
158
Popular Tags