001: /**
002: * JOnAS: Java(TM) Open Application Server
003: * Copyright (C) 1999-2004 Bull S.A.
004: * Contact: jonas-team@objectweb.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
019: * USA
020: *
021: * --------------------------------------------------------------------------
022: * $Id: FileWSDLHandler.java 7339 2005-09-06 15:28:31Z sauthieg $
023: * --------------------------------------------------------------------------
024: */package org.objectweb.jonas.ws.handler;
025:
026: import java.io.File;
027: import java.io.IOException;
028: import java.nio.charset.Charset;
029: import java.nio.charset.IllegalCharsetNameException;
030: import java.nio.charset.UnsupportedCharsetException;
031: import java.util.Properties;
032:
033: import javax.wsdl.WSDLException;
034:
035: import org.objectweb.jonas_ws.deployment.api.ServiceDesc;
036:
037: import org.objectweb.jonas.common.Log;
038: import org.objectweb.jonas.ws.JDefinitionWriter;
039: import org.objectweb.jonas.ws.WSServiceException;
040:
041: import org.objectweb.util.monolog.api.BasicLevel;
042: import org.objectweb.util.monolog.api.Logger;
043: import org.objectweb.jonas.common.JProp;
044:
045: /**
046: * In charge of published a given WSDL in a directory.
047: * properties :<br/>
048: * - <code>jonas.service.publish.file.directory</code> : directory name where WSDLs will be placed (default to JONAS_BASE/wsdls)<br/>
049: * - <code>jonas.service.publish.file.encoding</code> : file encoding (default to UTF-8)<br/>
050: *
051: * use the <code>wsdl-publish-directory</code> if provided in jonas descriptors.
052: * @author Xavier Delplanque
053: * @author Guillaume Sauthier
054: */
055: public class FileWSDLHandler implements WSDLHandler {
056:
057: /** WSDL Directory */
058: private File location;
059:
060: /** charset */
061: private Charset cs;
062:
063: /** directory property name */
064: private static final String OUTPUT_DIRECTORY = "jonas.service.publish.file.directory";
065:
066: /** encoding mode */
067: private static final String ENCODING = "jonas.service.publish.file.encoding";
068:
069: /** logger */
070: private static Logger logger = Log.getLogger(Log.JONAS_WS_PREFIX);
071:
072: /**
073: * Return a new instance of FileWSDLHandler.
074: *
075: * @param props Properties used to configure FileHandler
076: *
077: * @throws WSServiceException When Cannot create the directory
078: * (if it don't exist or not set in property file).
079: */
080: public FileWSDLHandler(Properties props) throws WSServiceException {
081:
082: String directory = props.getProperty(OUTPUT_DIRECTORY);
083:
084: if (directory == null) {
085: // no directory specified
086: // use the default one : JONAS_BASE/wsdls
087: String jonasBase = JProp.getJonasBase();
088: directory = new File(jonasBase, "wsdls").getPath();
089: }
090:
091: String encoding = props.getProperty(ENCODING, "UTF-8");
092:
093: try {
094: location = new File(directory).getCanonicalFile();
095:
096: if (!location.exists()) {
097: // if the given file doesn't exist, create it.
098: location.mkdirs();
099: }
100:
101: cs = Charset.forName(encoding);
102:
103: } catch (IOException ioe) {
104: throw new WSServiceException(
105: "cannot find/create the publishing directory '"
106: + directory + "'", ioe);
107: } catch (IllegalCharsetNameException icsne) {
108: throw new WSServiceException("Illegal Charset '" + encoding
109: + "'", icsne);
110: } catch (UnsupportedCharsetException ucse) {
111: throw new WSServiceException("Charset '" + encoding
112: + "' not supported on this platform.", ucse);
113: }
114: }
115:
116: /**
117: * Publish the given WSDL to the directory location.
118: *
119: * @param sd the Service containing the WSDL file to publish.
120: *
121: * @throws WSServiceException When publication fails.
122: */
123: public void publish(ServiceDesc sd) throws WSServiceException {
124: // build the fully qualified file name for the published wsdl file
125: String filePath = sd.getWSDL().getName();
126:
127: String[] pathElements = filePath.split("/");
128: if (pathElements.length <= 2) {
129: throw new WSServiceException("invalid filename");
130: }
131:
132: StringBuffer buf = new StringBuffer();
133: for (int i = 2; i < pathElements.length; i++) {
134: buf.append(pathElements[i]);
135: if (i != (pathElements.length - 1)) {
136: // last part is a filename
137: buf.append(File.separator);
138: }
139: }
140: // remove WEB-INF/wsdl/
141: // remove META-INF/wsdl/
142: String fileName = buf.toString();
143:
144: logger.log(BasicLevel.DEBUG, "Attempting to publish '"
145: + fileName + "'");
146:
147: File sLoc = null;
148: File pubDirectory = sd.getPublicationDirectory();
149: if (pubDirectory == null) {
150: sLoc = new File(location, sd.getName());
151: } else {
152: sLoc = pubDirectory;
153: }
154:
155: try {
156: sLoc = sLoc.getCanonicalFile();
157:
158: logger.log(BasicLevel.DEBUG, "Publishing into directory '"
159: + sLoc + "'");
160:
161: createDirIfNeeded(sLoc);
162: // write the wsdl file in the directory
163: JDefinitionWriter jdw = new JDefinitionWriter(sd.getWSDL()
164: .getDefinition(), sLoc, cs, fileName);
165: jdw.write();
166: } catch (IOException ioe) {
167: throw new WSServiceException("Error with writer of file '"
168: + fileName + "'", ioe);
169: } catch (WSDLException we) {
170: throw new WSServiceException("Error with wsdl file '"
171: + filePath + "' publishing in directory '"
172: + location + "'", we);
173: }
174: }
175:
176: /**
177: * Creates the parent of the given file as a directory
178: * @param file with a parent that must be a directory
179: * @throws IOException if directory cannot be created
180: */
181: private void createDirIfNeeded(File file) throws IOException {
182: if (!file.exists()) {
183: if (!file.mkdirs()) {
184: // cannot create directory
185: throw new IOException("Cannot create directory "
186: + file.getCanonicalPath());
187: }
188: } else if (!file.isDirectory()) {
189: // parent exists but is not a directory
190: throw new IOException("Parent " + file.getCanonicalPath()
191: + " already exists but is not a directory.");
192: }
193: }
194:
195: }
|