1 25 26 package org.objectweb.jonas.ws.handler; 27 28 import java.io.File ; 29 import java.io.IOException ; 30 import java.nio.charset.Charset ; 31 import java.nio.charset.IllegalCharsetNameException ; 32 import java.nio.charset.UnsupportedCharsetException ; 33 import java.util.Properties ; 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 56 public class FileWSDLHandler implements WSDLHandler { 57 58 59 private File location; 60 61 62 private Charset cs; 63 64 65 private static final String OUTPUT_DIRECTORY = "jonas.service.publish.file.directory"; 66 67 68 private static final String ENCODING = "jonas.service.publish.file.encoding"; 69 70 71 private static Logger logger = Log.getLogger(Log.JONAS_WS_PREFIX); 72 73 81 public FileWSDLHandler(Properties props) throws WSServiceException { 82 83 String directory = props.getProperty(OUTPUT_DIRECTORY); 84 String encoding = props.getProperty(ENCODING, "UTF-8"); 85 86 try { 87 location = new File (directory).getCanonicalFile(); 88 89 if (!location.exists()) { 90 location.mkdirs(); 92 } 93 94 cs = Charset.forName(encoding); 95 96 } catch (IOException ioe) { 97 throw new WSServiceException( 98 "cannot find/create the publishing directory '" + directory + "'", 99 ioe); 100 } catch (IllegalCharsetNameException icsne) { 101 throw new WSServiceException( 102 "Illegal Charset '" + encoding + "'", 103 icsne); 104 } catch (UnsupportedCharsetException ucse) { 105 throw new WSServiceException( 106 "Charset '" + encoding + "' not supported on this platform.", 107 ucse); 108 } 109 } 110 111 118 public void publish(ServiceDesc sd) throws WSServiceException { 119 String filePath = sd.getWSDL().getName(); 121 122 String [] pathElements = filePath.split("/"); 123 if (pathElements.length <= 2) { 124 throw new WSServiceException("invalid filename"); 125 } 126 127 StringBuffer buf = new StringBuffer (); 128 for (int i = 2; i < pathElements.length; i++) { 129 buf.append(pathElements[i]); 130 if (i != (pathElements.length - 1)) { 131 buf.append(File.separator); 133 } 134 } 135 String fileName = buf.toString(); 138 139 logger.log(BasicLevel.DEBUG, "Attempting to publish '" + fileName + "'"); 140 141 File sLoc = null; 142 File pubDirectory = sd.getPublicationDirectory(); 143 if (pubDirectory == null) { 144 sLoc = new File (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 JDefinitionWriter jdw = new JDefinitionWriter(sd.getWSDL().getDefinition(), sLoc, cs, fileName); 157 jdw.write(); 158 } catch (IOException 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 172 private void createDirIfNeeded(File file) throws IOException { 173 if (!file.exists()) { 174 if (!file.mkdirs()) { 175 throw new IOException ("Cannot create directory " + file.getCanonicalPath()); 177 } 178 } else if (!file.isDirectory()) { 179 throw new IOException ("Parent " + file.getCanonicalPath() + " already exists but is not a directory."); 181 } 182 } 183 184 185 } 186 | Popular Tags |