00001
00026 package org.objectweb.jonas_ws.wsgen.generator.axis;
00027
00028 import java.io.File;
00029
00030 import org.w3c.dom.Document;
00031 import org.w3c.dom.Element;
00032
00033 import org.apache.velocity.VelocityContext;
00034
00035 import org.objectweb.jonas_lib.I18n;
00036 import org.objectweb.jonas_lib.genbase.GenBaseException;
00037 import org.objectweb.jonas_lib.genbase.archive.Archive;
00038 import org.objectweb.jonas_lib.genbase.archive.EjbJar;
00039 import org.objectweb.jonas_lib.genbase.archive.J2EEArchive;
00040 import org.objectweb.jonas_lib.genbase.archive.WebApp;
00041 import org.objectweb.jonas_lib.genbase.generator.Config;
00042 import org.objectweb.jonas_lib.genbase.utils.XMLUtils;
00043 import org.objectweb.jonas_lib.loader.AbsModuleClassLoader;
00044
00045 import org.objectweb.jonas_ws.deployment.api.MappingFile;
00046 import org.objectweb.jonas_ws.deployment.api.ServiceRefDesc;
00047 import org.objectweb.jonas_ws.wsgen.WsGenException;
00048 import org.objectweb.jonas_ws.wsgen.ddmodifier.WsClientDDModifier;
00049 import org.objectweb.jonas_ws.wsgen.generator.WsClientGenerator;
00050 import org.objectweb.jonas_ws.wsgen.generator.axis.wsdl2java.JOnASWSDL2Java;
00051
00052 import org.objectweb.util.monolog.api.BasicLevel;
00053
00061 public class AxisWsClientGenerator extends WsClientGenerator {
00062
00064 private static JVelocity jvelocity = null;
00065
00069 private static final String CLIENT_CONFIG = "axis.clientConfigFile";
00070
00074 private static final String WSDD_SUFFIX = ".wsdd";
00075
00079 private static final String WSDD_PREFIX = "deploy-client-";
00080
00082 private static int count = 0;
00083
00085 private File generated = null;
00086
00090 private static final String WEB_PREFIX = "WEB-INF/";
00091
00095 private static I18n i18n = I18n.getInstance(AxisWsClientGenerator.class);
00096
00106 public AxisWsClientGenerator(Config config, ServiceRefDesc srd, WsClientDDModifier ddm, Archive archive)
00107 throws GenBaseException, WsGenException {
00108 super(config, srd, ddm, archive);
00109
00110
00111 if (jvelocity == null) {
00112 jvelocity = new JVelocity("deploy_client.vm");
00113 }
00114 }
00115
00120 public void generate() throws WsGenException {
00121
00122
00123
00124 if ((getRef().getWsdlFileName() != null)
00125 && !getRef().getServiceInterface().getName().equals("javax.xml.rpc.Service")) {
00126
00127 try {
00128
00129 J2EEArchive j2eeArchive = (J2EEArchive) getArchive();
00130 AbsModuleClassLoader cl = (AbsModuleClassLoader) j2eeArchive.getModuleClassloader();
00131 getConfig().setClasspath(getConfig().getClasspath() + cl.getClasspath());
00132
00133
00134 JOnASWSDL2Java jWsdl2Java = new JOnASWSDL2Java();
00135 jWsdl2Java.run(this);
00136 getLogger().log(BasicLevel.INFO, "Web Services Classes successfully generated by Axis.");
00137 } catch (Exception e) {
00138 String err = getI18n().getMessage("AxisWsClientGenerator.generate.WSDL2Java");
00139 e.printStackTrace(System.err);
00140 throw new WsGenException(err, e);
00141 }
00142 }
00143
00144
00145
00146
00147
00148
00149 if (hasClientConfigFile(getRef())) {
00150
00151
00152
00153 String filename = WSDD_PREFIX + (count++) + WSDD_SUFFIX;
00154
00155 VelocityContext vctx = VContextFactory.getContext(getRef());
00156
00157 getLogger().log(BasicLevel.DEBUG, "Creating '" + filename + "'");
00158 generated = new File(getSources(), filename);
00159
00160 jvelocity.generate(generated, vctx);
00161
00162 }
00163
00164 }
00165
00171 private boolean hasClientConfigFile(ServiceRefDesc ref) {
00172
00173 if (getConfig().isNoConfig()) {
00174 return false;
00175 }
00176
00177 MappingFile mf = ref.getMappingFile();
00178
00179 if (mf != null) {
00180
00181 if (mf.getXmlTypeMappings().hasNext()) {
00182
00183 return true;
00184 } else {
00185
00186
00187 return (ref.getHandlerRefs().size() != 0);
00188 }
00189 } else {
00190
00191
00192 return (ref.getHandlerRefs().size() != 0);
00193 }
00194 }
00195
00201 public void addFiles(Archive archive) throws WsGenException {
00202 if (archive instanceof WebApp) {
00203 archive.addDirectoryIn("WEB-INF/classes/", getClasses());
00204
00205 if (generated != null) {
00206 archive.addFileIn("WEB-INF/", generated);
00207
00208
00209 if (!getModifier().hasJonasServiceRef()) {
00210 if (!getArchive().getContainedFiles().contains("WEB-INF/jonas-web.xml")) {
00211
00212 createEmptyJonasWeb((J2EEArchive) archive);
00213 }
00214 Element jsr = getModifier().createJonasServiceRef(getRef().getServiceRefName());
00215
00216 getModifier().setElement(jsr);
00217
00218 }
00219
00220
00221 getModifier().addJonasInitParam(CLIENT_CONFIG, WEB_PREFIX + generated.getName());
00222 }
00223 } else if (archive instanceof EjbJar) {
00224 archive.addDirectory(getClasses());
00225
00226 if (generated != null) {
00227 archive.addFileIn("META-INF/", generated);
00228
00229
00230 if (!getModifier().hasJonasServiceRef()) {
00231 Element jsr = getModifier().createJonasServiceRef(getRef().getServiceRefName());
00232
00233 getModifier().setElement(jsr);
00234 }
00235
00236
00237 getModifier().addJonasInitParam(CLIENT_CONFIG, "META-INF/" + generated.getName());
00238 }
00239 } else {
00240 archive.addDirectory(getClasses());
00241
00242 if (generated != null) {
00243 archive.addFileIn("META-INF/", generated);
00244
00245
00246 if (!getModifier().hasJonasServiceRef()) {
00247 if (!getArchive().getContainedFiles().contains("META-INF/jonas-client.xml")) {
00248
00249 createEmptyJonasClient((J2EEArchive) archive);
00250 }
00251 Element jsr = getModifier().createJonasServiceRef(getRef().getServiceRefName());
00252
00253 getModifier().setElement(jsr);
00254
00255 }
00256
00257
00258 getModifier().addJonasInitParam(CLIENT_CONFIG, "META-INF/" + generated.getName());
00259 }
00260 }
00261 }
00262
00267 private void createEmptyJonasWeb(J2EEArchive archive) {
00268 Document doc = XMLUtils.newJonasWeb();
00269 archive.getDescriptors().put("WEB-INF/jonas-web.xml", doc);
00270 getModifier().setDocument(doc);
00271 }
00272
00277 private void createEmptyJonasClient(J2EEArchive archive) {
00278 Document doc = XMLUtils.newJonasClient();
00279 archive.getDescriptors().put("META-INF/jonas-client.xml", doc);
00280 getModifier().setDocument(doc);
00281 }
00282
00286 public static I18n getI18n() {
00287 return i18n;
00288 }
00289 }