1 16 17 package org.apache.axis.handlers; 18 19 import org.apache.axis.AxisFault; 20 import org.apache.axis.Constants; 21 import org.apache.axis.MessageContext; 22 import org.apache.axis.components.compiler.Compiler; 23 import org.apache.axis.components.compiler.CompilerError; 24 import org.apache.axis.components.compiler.CompilerFactory; 25 import org.apache.axis.components.logger.LogFactory; 26 import org.apache.axis.constants.Scope; 27 import org.apache.axis.handlers.soap.SOAPService; 28 import org.apache.axis.providers.java.RPCProvider; 29 import org.apache.axis.utils.ClassUtils; 30 import org.apache.axis.utils.ClasspathUtils; 31 import org.apache.axis.utils.JWSClassLoader; 32 import org.apache.axis.utils.Messages; 33 import org.apache.axis.utils.XMLUtils; 34 import org.apache.commons.logging.Log; 35 import org.w3c.dom.Document ; 36 import org.w3c.dom.Element ; 37 38 import java.io.File ; 39 import java.io.FileNotFoundException ; 40 import java.io.FileReader ; 41 import java.io.FileWriter ; 42 import java.util.HashMap ; 43 import java.util.List ; 44 45 52 public class JWSHandler extends BasicHandler 53 { 54 protected static Log log = 55 LogFactory.getLog(JWSHandler.class.getName()); 56 57 public final String OPTION_JWS_FILE_EXTENSION = "extension"; 58 public final String DEFAULT_JWS_FILE_EXTENSION = Constants.JWS_DEFAULT_FILE_EXTENSION; 59 60 protected static HashMap soapServices = new HashMap (); 61 62 65 public void invoke(MessageContext msgContext) throws AxisFault 66 { 67 if (log.isDebugEnabled()) { 68 log.debug("Enter: JWSHandler::invoke"); 69 } 70 71 try { 72 setupService(msgContext); 73 } catch (Exception e) { 74 log.error( Messages.getMessage("exception00"), e ); 75 throw AxisFault.makeFault(e); 76 } 77 } 78 79 86 protected void setupService(MessageContext msgContext) throws Exception { 87 String realpath = msgContext.getStrProp(Constants.MC_REALPATH); 89 String extension = (String )getOption(OPTION_JWS_FILE_EXTENSION); 90 if (extension == null) extension = DEFAULT_JWS_FILE_EXTENSION; 91 92 if ((realpath!=null) && (realpath.endsWith(extension))) { 93 94 95 96 String jwsFile = realpath; 97 String rel = msgContext.getStrProp(Constants.MC_RELATIVE_PATH); 98 99 File f2 = new File ( jwsFile ); 102 if (!f2.exists()) { 103 throw new FileNotFoundException (rel); 104 } 105 106 if (rel.charAt(0) == '/') { 107 rel = rel.substring(1); 108 } 109 110 int lastSlash = rel.lastIndexOf('/'); 111 String dir = null; 112 113 if (lastSlash > 0) { 114 dir = rel.substring(0, lastSlash); 115 } 116 117 String file = rel.substring(lastSlash + 1); 118 119 String outdir = msgContext.getStrProp( Constants.MC_JWS_CLASSDIR ); 120 if ( outdir == null ) outdir = "." ; 121 122 if (dir != null) { 129 outdir = outdir + File.separator + dir; 130 } 131 132 File outDirectory = new File (outdir); 136 if (!outDirectory.exists()) { 137 outDirectory.mkdirs(); 138 } 139 140 if (log.isDebugEnabled()) 141 log.debug("jwsFile: " + jwsFile ); 142 143 String jFile = outdir + File.separator + file.substring(0, file.length()-extension.length()+1) + 144 "java" ; 145 String cFile = outdir + File.separator + file.substring(0, file.length()-extension.length()+1) + 146 "class" ; 147 148 if (log.isDebugEnabled()) { 149 log.debug("jFile: " + jFile ); 150 log.debug("cFile: " + cFile ); 151 log.debug("outdir: " + outdir); 152 } 153 154 File f1 = new File ( cFile ); 155 156 157 158 String clsName = null ; 159 if ( clsName == null ) clsName = f2.getName(); 161 if ( clsName != null && clsName.charAt(0) == '/' ) 162 clsName = clsName.substring(1); 163 164 clsName = clsName.substring( 0, clsName.length()-extension.length() ); 165 clsName = clsName.replace('/', '.'); 166 167 if (log.isDebugEnabled()) 168 log.debug("ClsName: " + clsName ); 169 170 171 172 if ( !f1.exists() || f2.lastModified() > f1.lastModified() ) { 173 174 175 176 177 log.debug(Messages.getMessage("compiling00", jwsFile) ); 178 log.debug(Messages.getMessage("copy00", jwsFile, jFile) ); 179 FileReader fr = new FileReader ( jwsFile ); 180 FileWriter fw = new FileWriter ( jFile ); 181 char[] buf = new char[4096]; 182 int rc ; 183 while ( (rc = fr.read( buf, 0, 4095)) >= 0 ) 184 fw.write( buf, 0, rc ); 185 fw.close(); 186 fr.close(); 187 188 189 190 log.debug("javac " + jFile ); 191 Compiler compiler = CompilerFactory.getCompiler(); 194 195 compiler.setClasspath(ClasspathUtils.getDefaultClasspath(msgContext)); 196 compiler.setDestination(outdir); 197 compiler.addFile(jFile); 198 199 boolean result = compiler.compile(); 200 201 202 203 (new File (jFile)).delete(); 204 205 if ( !result ) { 206 207 208 209 210 (new File (cFile)).delete(); 211 212 Document doc = XMLUtils.newDocument(); 213 214 Element root = doc.createElementNS("", "Errors"); 215 StringBuffer message = new StringBuffer ("Error compiling "); 216 message.append(jFile); 217 message.append(":\n"); 218 219 List errors = compiler.getErrors(); 220 int count = errors.size(); 221 for (int i = 0; i < count; i++) { 222 CompilerError error = (CompilerError) errors.get(i); 223 if (i > 0) message.append("\n"); 224 message.append("Line "); 225 message.append(error.getStartLine()); 226 message.append(", column "); 227 message.append(error.getStartColumn()); 228 message.append(": "); 229 message.append(error.getMessage()); 230 } 231 root.appendChild( doc.createTextNode( message.toString() ) ); 232 throw new AxisFault( "Server.compileError", 233 Messages.getMessage("badCompile00", jFile), 234 null, new Element [] { root } ); 235 } 236 ClassUtils.removeClassLoader( clsName ); 237 soapServices.remove(clsName); 239 } 240 241 ClassLoader cl = ClassUtils.getClassLoader(clsName); 242 if (cl == null) { 243 cl = new JWSClassLoader(clsName, 244 msgContext.getClassLoader(), 245 cFile); 246 } 247 248 msgContext.setClassLoader(cl); 249 250 251 252 253 SOAPService rpc = (SOAPService)soapServices.get(clsName); 257 if (rpc == null) { 258 rpc = new SOAPService(new RPCProvider()); 259 rpc.setName(clsName); 260 rpc.setOption(RPCProvider.OPTION_CLASSNAME, clsName ); 261 rpc.setEngine(msgContext.getAxisEngine()); 262 263 String allowed = (String )getOption(RPCProvider.OPTION_ALLOWEDMETHODS); 265 if (allowed == null) allowed = "*"; 266 rpc.setOption(RPCProvider.OPTION_ALLOWEDMETHODS, allowed); 267 String scope = (String )getOption(RPCProvider.OPTION_SCOPE); 270 if (scope == null) scope = Scope.DEFAULT.getName(); 271 rpc.setOption(RPCProvider.OPTION_SCOPE, scope); 272 273 rpc.getInitializedServiceDesc(msgContext); 274 275 soapServices.put(clsName, rpc); 276 } 277 278 rpc.setEngine(msgContext.getAxisEngine()); 280 281 rpc.init(); 283 msgContext.setService( rpc ); 285 } 286 287 if (log.isDebugEnabled()) { 288 log.debug("Exit: JWSHandler::invoke"); 289 } 290 } 291 292 public void generateWSDL(MessageContext msgContext) throws AxisFault { 293 try { 294 setupService(msgContext); 295 } catch (Exception e) { 296 log.error( Messages.getMessage("exception00"), e ); 297 throw AxisFault.makeFault(e); 298 } 299 } 300 } 301 | Popular Tags |