00001
00026 package org.objectweb.jonas.ws.handler;
00027
00028 import java.io.File;
00029 import java.io.IOException;
00030 import java.nio.charset.Charset;
00031 import java.nio.charset.IllegalCharsetNameException;
00032 import java.nio.charset.UnsupportedCharsetException;
00033 import java.util.Properties;
00034
00035 import javax.wsdl.WSDLException;
00036
00037 import org.objectweb.jonas_ws.deployment.api.ServiceDesc;
00038
00039 import org.objectweb.jonas.common.Log;
00040 import org.objectweb.jonas.ws.JDefinitionWriter;
00041 import org.objectweb.jonas.ws.WSServiceException;
00042
00043 import org.objectweb.util.monolog.api.BasicLevel;
00044 import org.objectweb.util.monolog.api.Logger;
00045
00046
00056 public class FileWSDLHandler implements WSDLHandler {
00057
00059 private File location;
00060
00062 private Charset cs;
00063
00065 private static final String OUTPUT_DIRECTORY = "jonas.service.publish.file.directory";
00066
00068 private static final String ENCODING = "jonas.service.publish.file.encoding";
00069
00071 private static Logger logger = Log.getLogger(Log.JONAS_WS_PREFIX);
00072
00081 public FileWSDLHandler(Properties props) throws WSServiceException {
00082
00083 String directory = props.getProperty(OUTPUT_DIRECTORY);
00084 String encoding = props.getProperty(ENCODING, "UTF-8");
00085
00086 try {
00087 location = new File(directory).getCanonicalFile();
00088
00089 if (!location.exists()) {
00090
00091 location.mkdirs();
00092 }
00093
00094 cs = Charset.forName(encoding);
00095
00096 } catch (IOException ioe) {
00097 throw new WSServiceException(
00098 "cannot find/create the publishing directory '" + directory + "'",
00099 ioe);
00100 } catch (IllegalCharsetNameException icsne) {
00101 throw new WSServiceException(
00102 "Illegal Charset '" + encoding + "'",
00103 icsne);
00104 } catch (UnsupportedCharsetException ucse) {
00105 throw new WSServiceException(
00106 "Charset '" + encoding + "' not supported on this platform.",
00107 ucse);
00108 }
00109 }
00110
00118 public void publish(ServiceDesc sd) throws WSServiceException {
00119
00120 String filePath = sd.getWSDL().getName();
00121
00122 String[] pathElements = filePath.split("/");
00123 if (pathElements.length <= 2) {
00124 throw new WSServiceException("invalid filename");
00125 }
00126
00127 StringBuffer buf = new StringBuffer();
00128 for (int i = 2; i < pathElements.length; i++) {
00129 buf.append(pathElements[i]);
00130 if (i != (pathElements.length - 1)) {
00131
00132 buf.append(File.separator);
00133 }
00134 }
00135
00136
00137 String fileName = buf.toString();
00138
00139 logger.log(BasicLevel.DEBUG, "Attempting to publish '" + fileName + "'");
00140
00141 File sLoc = null;
00142 File pubDirectory = sd.getPublicationDirectory();
00143 if (pubDirectory == null) {
00144 sLoc = new File(location, sd.getName());
00145 } else {
00146 sLoc = pubDirectory;
00147 }
00148
00149 try {
00150 sLoc = sLoc.getCanonicalFile();
00151
00152 logger.log(BasicLevel.DEBUG, "Publishing into directory '" + sLoc + "'");
00153
00154 createDirIfNeeded(sLoc);
00155
00156 JDefinitionWriter jdw = new JDefinitionWriter(sd.getWSDL().getDefinition(), sLoc, cs, fileName);
00157 jdw.write();
00158 } catch (IOException ioe) {
00159 throw new WSServiceException("Error with writer of file '"
00160 + fileName + "'", ioe);
00161 } catch (WSDLException we) {
00162 throw new WSServiceException("Error with wsdl file '" + filePath
00163 + "' publishing in directory '" + location
00164 + "'", we);
00165 }
00166 }
00172 private void createDirIfNeeded(File file) throws IOException {
00173 if (!file.exists()) {
00174 if (!file.mkdirs()) {
00175
00176 throw new IOException("Cannot create directory " + file.getCanonicalPath());
00177 }
00178 } else if (!file.isDirectory()) {
00179
00180 throw new IOException("Parent " + file.getCanonicalPath() + " already exists but is not a directory.");
00181 }
00182 }
00183
00184
00185 }