KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > deployment > util > ModuleContentValidator


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 package com.sun.enterprise.deployment.util;
25
26 import java.util.logging.Level JavaDoc;
27
28 import java.io.*;
29 import java.net.URL JavaDoc;
30 import com.sun.enterprise.deployment.deploy.shared.AbstractArchive;
31 import com.sun.enterprise.deployment.BundleDescriptor;
32 import com.sun.enterprise.deployment.ServiceReferenceDescriptor;
33 import com.sun.enterprise.deployment.WebService;
34 import com.sun.enterprise.deployment.util.DOLUtils;
35 import com.sun.enterprise.util.LocalStringManagerImpl;
36
37 /**
38  * Allows validation of module content that might involve actually
39  * reading the bytes themselves from the module. Called after
40  * descriptor has been loaded but before module-specific archivist
41  * is closed.
42  *
43  * @author Kenneth Saks
44  */

45 public class ModuleContentValidator extends DefaultDOLVisitor {
46
47     private AbstractArchive archive_;
48     
49     // resources...
50
private static LocalStringManagerImpl localStrings =
51         new LocalStringManagerImpl(ModuleContentValidator.class);
52
53     public ModuleContentValidator(AbstractArchive archive) {
54         archive_ = archive;
55     }
56
57     public void accept(ServiceReferenceDescriptor serviceRef) {
58         if( serviceRef.hasWsdlFile() ) {
59             String JavaDoc wsdlFileUri = serviceRef.getWsdlFileUri();
60             // For JAXWS based clients, the user need not package WSDL and not refer to it
61
// in @WebServiceRef; in this case we rely on @WebClient in generated files to get
62
// wsdl location; this wsdl location can be URL or absolute path; in these cases
63
// ensure that the file is present and return
64
URL JavaDoc url = null;
65             try {
66                 url = new URL JavaDoc(wsdlFileUri);
67             } catch(java.net.MalformedURLException JavaDoc e) {
68                 // don't care, will eventuall fail below
69
}
70             if (url!=null) {
71                 if (url.getProtocol().equals("http") || url.getProtocol().equals("https"))
72                     return;
73             }
74             File tmpFile = new File(wsdlFileUri);
75             if(tmpFile.isAbsolute() && tmpFile.exists()) {
76                 return;
77             }
78             try {
79                 InputStream wsdlFileInputStream =
80                     archive_.getEntry(wsdlFileUri);
81                 if( wsdlFileInputStream != null ) {
82                     wsdlFileInputStream.close();
83                 } else {
84                     String JavaDoc msg = localStrings.getLocalString(
85                    "enterprise.deployment.util.wsdlfilenotfound",
86                            "wsdl file {0} does not exist for service-ref {1}",
87                            new Object JavaDoc[] {wsdlFileUri, serviceRef.getName()});
88                     DOLUtils.getDefaultLogger().severe(msg);
89                     throw new RuntimeException JavaDoc(msg);
90                 }
91             } catch(IOException ioe) {
92                     String JavaDoc msg = localStrings.getLocalString(
93                    "enterprise.deployment.util.wsdlfilenotreadable",
94                            "wsdl file {0} for service-ref {1} cannot be opened : {2}",
95                            new Object JavaDoc[] {wsdlFileUri, serviceRef.getName(), ioe.getMessage()});
96                     DOLUtils.getDefaultLogger().severe(msg);
97                     throw new RuntimeException JavaDoc(ioe);
98             }
99         }
100         
101         if( serviceRef.hasMappingFile() ) {
102             String JavaDoc mappingFileUri = serviceRef.getMappingFileUri();
103             try {
104                 InputStream mappingFileInputStream =
105                     archive_.getEntry(mappingFileUri);
106                 if( mappingFileInputStream != null ) {
107                     mappingFileInputStream.close();
108                 } else {
109                     String JavaDoc msg = localStrings.getLocalString(
110                    "enterprise.deployment.util.mappingfilenotfound",
111                            "mapping file {0} does not exist for service-ref {1}",
112                            new Object JavaDoc[] {mappingFileUri, serviceRef.getName()});
113                     DOLUtils.getDefaultLogger().severe(msg);
114                     throw new RuntimeException JavaDoc(msg);
115                 }
116             } catch(IOException ioe) {
117                     String JavaDoc msg = localStrings.getLocalString(
118                    "enterprise.deployment.util.mappingfilenotreadable",
119                            "mapping file {0} for service-ref {1} cannot be opened : {2}",
120                            new Object JavaDoc[] {mappingFileUri, serviceRef.getName(), ioe.getMessage()});
121                     DOLUtils.getDefaultLogger().severe(msg);
122                     throw new RuntimeException JavaDoc(ioe);
123             }
124         }
125     }
126
127     public void accept(WebService webService) {
128         
129         try {
130             
131             String JavaDoc wsdlFileUri = webService.getWsdlFileUri();
132             if (!webService.hasWsdlFile()) {
133                 // no wsdl was specified in the annotation or deployment descritor,
134
//it will be generated at deployment.
135
return;
136             }
137             try {
138                 URL JavaDoc url = new URL JavaDoc(wsdlFileUri);
139                 if (url.getProtocol()!=null && !url.getProtocol().equals("file"))
140                     return;
141             } catch(java.net.MalformedURLException JavaDoc e) {
142                 // ignore it could be a relative uri
143
}
144             InputStream wsdlFileInputStream = archive_.getEntry(wsdlFileUri);
145
146             if( wsdlFileInputStream != null ) {
147                 
148                 wsdlFileInputStream.close();
149                 BundleDescriptor bundle = webService.getBundleDescriptor();
150                 if( !isWsdlContent(wsdlFileUri, bundle) ) {
151                     String JavaDoc msg = localStrings.getLocalString(
152                         "enterprise.deployment.util.wsdlpackagedinwrongservicelocation",
153                         "wsdl file {0} for web service {1} must be packaged in or below {2}",
154                         new Object JavaDoc[] {wsdlFileUri, webService.getName(), bundle.getWsdlDir()});
155                     DOLUtils.getDefaultLogger().severe(msg);
156                     throw new RuntimeException JavaDoc(msg);
157                 }
158             } else {
159                 // let's look in the wsdl directory
160
String JavaDoc fullFileUri = webService.getBundleDescriptor().getWsdlDir() + "/" + wsdlFileUri;
161                 wsdlFileInputStream = archive_.getEntry(fullFileUri);
162
163                 if( wsdlFileInputStream != null ) {
164                     // found it, let's update the DOL to not have to recalculate this again
165
wsdlFileInputStream.close();
166                     webService.setWsdlFileUri(fullFileUri);
167                 } else {
168                     // this time I give up, no idea where this WSDL is
169
String JavaDoc msg = localStrings.getLocalString(
170                    "enterprise.deployment.util.servicewsdlfilenotfound",
171                            "wsdl file {0} does not exist for web service {1}",
172                            new Object JavaDoc[] {wsdlFileUri, webService.getName()});
173                     DOLUtils.getDefaultLogger().severe(msg);
174                     throw new RuntimeException JavaDoc(msg);
175                 }
176             }
177         } catch(IOException ioe) {
178                     String JavaDoc msg = localStrings.getLocalString(
179                    "enterprise.deployment.util.servicewsdlfilenotreadable",
180                            "wsdl file {0} for service-ref {1} cannot be opened : {2}",
181                            new Object JavaDoc[] {webService.getWsdlFileUri(), webService.getName(), ioe.getMessage()});
182                     DOLUtils.getDefaultLogger().severe(msg);
183                     throw new RuntimeException JavaDoc(ioe);
184         }
185         
186         // For JAXRPC-2.0 based webservice, there is no model file
187
// XXX - TODO - This check should be changed to checking the version
188
// once the 2.0 DTDs/Schemas are available
189
if(webService.getMappingFileUri() == null) {
190             return;
191         }
192
193         try {
194             InputStream mappingFileInputStream =
195                 archive_.getEntry(webService.getMappingFileUri());
196             if( mappingFileInputStream != null ) {
197                 mappingFileInputStream.close();
198             } else {
199                     String JavaDoc msg = localStrings.getLocalString(
200                    "enterprise.deployment.util.servicemappingfilenotfound",
201                            "Web Service mapping file {0} for web service {1} not found",
202                            new Object JavaDoc[] {webService.getMappingFileUri(), webService.getName()});
203                     DOLUtils.getDefaultLogger().severe(msg);
204                     throw new RuntimeException JavaDoc(msg);
205             }
206         } catch(IOException ioe) {
207                     String JavaDoc msg = localStrings.getLocalString(
208                    "enterprise.deployment.util.servicemappingfilenotreadable",
209                            "Web Service mapping file {0} for web service {1} not found {2} ",
210                            new Object JavaDoc[] {webService.getMappingFileUri(), webService.getName(), ioe});
211                     DOLUtils.getDefaultLogger().severe(msg);
212                     throw new RuntimeException JavaDoc(ioe);
213         }
214     }
215
216     /**
217      * All wsdl files and wsdl imported files live under a well-known
218      * wsdl directory.
219      * @param uri module uri
220      */

221     public boolean isWsdlContent(String JavaDoc uri, BundleDescriptor bundle) {
222         String JavaDoc wsdlDir = bundle.getWsdlDir();
223         return (uri != null) && uri.startsWith(wsdlDir);
224     }
225 }
226
Popular Tags