KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas > ws > handler > FileWSDLHandler


1 /**
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 1999-2004 Bull S.A.
4  * Contact: jonas-team@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * --------------------------------------------------------------------------
22  * $Id: FileWSDLHandler.java,v 1.14 2004/11/23 14:26:44 sauthieg Exp $
23  * --------------------------------------------------------------------------
24 */

25
26 package org.objectweb.jonas.ws.handler;
27
28 import java.io.File JavaDoc;
29 import java.io.IOException JavaDoc;
30 import java.nio.charset.Charset JavaDoc;
31 import java.nio.charset.IllegalCharsetNameException JavaDoc;
32 import java.nio.charset.UnsupportedCharsetException JavaDoc;
33 import java.util.Properties JavaDoc;
34
35 import javax.wsdl.WSDLException;
36
37 import org.objectweb.jonas_ws.deployment.api.ServiceDesc;
38
39 import org.objectweb.jonas.common.Log;
40 import org.objectweb.jonas.ws.JDefinitionWriter;
41 import org.objectweb.jonas.ws.WSServiceException;
42
43 import org.objectweb.util.monolog.api.BasicLevel;
44 import org.objectweb.util.monolog.api.Logger;
45
46
47 /**
48  * In charge of published a given WSDL in a directory.
49  * properties :<br/>
50  * - <code>jonas.service.publish.file.directory</code> : directory name where WSDLs will be placed
51  *
52  * use the <code>wsdl-publish-directory</code> if provided in jonas descriptors.
53  * @author Xavier Delplanque
54  * @author Guillaume Sauthier
55  */

56 public class FileWSDLHandler implements WSDLHandler {
57
58     /** WSDL Directory */
59     private File JavaDoc location;
60
61     /** charset */
62     private Charset JavaDoc cs;
63
64     /** directory property name */
65     private static final String JavaDoc OUTPUT_DIRECTORY = "jonas.service.publish.file.directory";
66
67     /** encoding mode */
68     private static final String JavaDoc ENCODING = "jonas.service.publish.file.encoding";
69
70     /** logger */
71     private static Logger logger = Log.getLogger(Log.JONAS_WS_PREFIX);
72
73     /**
74      * Return a new instance of FileWSDLHandler.
75      *
76      * @param props Properties used to configure FileHandler
77      *
78      * @throws WSServiceException When Cannot create the directory
79      * (if it don't exist or not set in property file).
80      */

81     public FileWSDLHandler(Properties JavaDoc props) throws WSServiceException {
82
83         String JavaDoc directory = props.getProperty(OUTPUT_DIRECTORY);
84         String JavaDoc encoding = props.getProperty(ENCODING, "UTF-8");
85
86         try {
87             location = new File JavaDoc(directory).getCanonicalFile();
88
89             if (!location.exists()) {
90                 // if the given file doesn't exist, create it.
91
location.mkdirs();
92             }
93
94             cs = Charset.forName(encoding);
95
96         } catch (IOException JavaDoc ioe) {
97             throw new WSServiceException(
98                 "cannot find/create the publishing directory '" + directory + "'",
99                 ioe);
100         } catch (IllegalCharsetNameException JavaDoc icsne) {
101             throw new WSServiceException(
102                 "Illegal Charset '" + encoding + "'",
103                 icsne);
104         } catch (UnsupportedCharsetException JavaDoc ucse) {
105             throw new WSServiceException(
106                 "Charset '" + encoding + "' not supported on this platform.",
107                 ucse);
108         }
109     }
110
111     /**
112      * Publish the given WSDL to the directory location.
113      *
114      * @param sd the Service containing the WSDL file to publish.
115      *
116      * @throws WSServiceException When publication fails.
117      */

118     public void publish(ServiceDesc sd) throws WSServiceException {
119         // build the fully qualified file name for the published wsdl file
120
String JavaDoc filePath = sd.getWSDL().getName();
121
122         String JavaDoc[] pathElements = filePath.split("/");
123         if (pathElements.length <= 2) {
124             throw new WSServiceException("invalid filename");
125         }
126
127         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
128         for (int i = 2; i < pathElements.length; i++) {
129             buf.append(pathElements[i]);
130             if (i != (pathElements.length - 1)) {
131                 // last part is a filename
132
buf.append(File.separator);
133             }
134         }
135         // remove WEB-INF/wsdl/
136
// remove META-INF/wsdl/
137
String JavaDoc fileName = buf.toString();
138
139         logger.log(BasicLevel.DEBUG, "Attempting to publish '" + fileName + "'");
140
141         File JavaDoc sLoc = null;
142         File JavaDoc pubDirectory = sd.getPublicationDirectory();
143         if (pubDirectory == null) {
144             sLoc = new File JavaDoc(location, sd.getName());
145         } else {
146             sLoc = pubDirectory;
147         }
148
149         try {
150             sLoc = sLoc.getCanonicalFile();
151
152             logger.log(BasicLevel.DEBUG, "Publishing into directory '" + sLoc + "'");
153
154             createDirIfNeeded(sLoc);
155             // write the wsdl file in the directory
156
JDefinitionWriter jdw = new JDefinitionWriter(sd.getWSDL().getDefinition(), sLoc, cs, fileName);
157             jdw.write();
158         } catch (IOException JavaDoc ioe) {
159             throw new WSServiceException("Error with writer of file '"
160                                          + fileName + "'", ioe);
161         } catch (WSDLException we) {
162             throw new WSServiceException("Error with wsdl file '" + filePath
163                                          + "' publishing in directory '" + location
164                                          + "'", we);
165         }
166     }
167     /**
168      * Creates the parent of the given file as a directory
169      * @param file with a parent that must be a directory
170      * @throws IOException if directory cannot be created
171      */

172     private void createDirIfNeeded(File JavaDoc file) throws IOException JavaDoc {
173         if (!file.exists()) {
174             if (!file.mkdirs()) {
175                 // cannot create directory
176
throw new IOException JavaDoc("Cannot create directory " + file.getCanonicalPath());
177             }
178         } else if (!file.isDirectory()) {
179             // parent exists but is not a directory
180
throw new IOException JavaDoc("Parent " + file.getCanonicalPath() + " already exists but is not a directory.");
181         }
182     }
183
184
185 }
186
Popular Tags