use of org.apache.cxf.tools.util.ClassCollector in project cxf by apache.
the class JavaScriptContainer method execute.
@SuppressWarnings("unchecked")
public void execute() throws ToolException {
if (hasInfoOption()) {
return;
}
buildToolContext();
validate(context);
WSDLConstants.WSDLVersion version = getWSDLVersion();
String wsdlURL = (String) context.get(ToolConstants.CFG_WSDLURL);
List<ServiceInfo> serviceList = (List<ServiceInfo>) context.get(ToolConstants.SERVICE_LIST);
if (serviceList == null) {
serviceList = new ArrayList<>();
PluginLoader pluginLoader = PluginLoader.newInstance();
// for JavaScript generation, we always use JAX-WS.
FrontEndProfile frontend = pluginLoader.getFrontEndProfile("jaxws");
// Build the ServiceModel from the WSDLModel
if (version == WSDLConstants.WSDLVersion.WSDL11) {
AbstractWSDLBuilder builder = frontend.getWSDLBuilder();
builder.setContext(context);
builder.setBus(getBus());
context.put(Bus.class, getBus());
builder.build(URIParserUtil.getAbsoluteURI(wsdlURL));
builder.customize();
Definition definition = builder.getWSDLModel();
context.put(Definition.class, definition);
builder.validate(definition);
WSDLServiceBuilder serviceBuilder = new WSDLServiceBuilder(getBus());
String serviceName = (String) context.get(ToolConstants.CFG_SERVICENAME);
if (serviceName != null) {
List<ServiceInfo> services = serviceBuilder.buildServices(definition, getServiceQName(definition));
serviceList.addAll(services);
} else if (definition.getServices().size() > 0) {
serviceList = serviceBuilder.buildServices(definition);
} else {
serviceList = serviceBuilder.buildMockServices(definition);
}
} else {
// TODO: wsdl2.0 support
throw new ToolException("Only WSDL 1.1 supported");
}
}
if (serviceList.isEmpty()) {
throw new ToolException("Did not find any services in WSDL");
}
Map<String, InterfaceInfo> interfaces = new LinkedHashMap<>();
ServiceInfo service0 = serviceList.get(0);
SchemaCollection schemaCollection = service0.getXmlSchemaCollection();
context.put(ToolConstants.XML_SCHEMA_COLLECTION, schemaCollection);
context.put(ToolConstants.PORTTYPE_MAP, interfaces);
context.put(ClassCollector.class, new ClassCollector());
WSDLToJavaScriptProcessor processor = new WSDLToJavaScriptProcessor();
for (ServiceInfo service : serviceList) {
context.put(ServiceInfo.class, service);
validate(service);
processor.setEnvironment(context);
processor.process();
}
}
use of org.apache.cxf.tools.util.ClassCollector in project cxf by apache.
the class WSDLToJavaContainer method processClientJar.
private void processClientJar(ToolContext context) {
ClassCollector oldCollector = context.get(ClassCollector.class);
ClassCollector newCollector = new ClassCollector();
String oldClassDir = (String) context.get(ToolConstants.CFG_CLASSDIR);
File tmpDir = FileUtils.createTmpDir();
context.put(ToolConstants.CFG_CLASSDIR, tmpDir.getAbsolutePath());
newCollector.setTypesClassNames(oldCollector.getTypesClassNames());
newCollector.setSeiClassNames(oldCollector.getSeiClassNames());
newCollector.setExceptionClassNames(oldCollector.getExceptionClassNames());
newCollector.setServiceClassNames(oldCollector.getServiceClassNames());
context.put(ClassCollector.class, newCollector);
new ClassUtils().compile(context);
generateLocalWSDL(context);
File clientJarFile = new File((String) context.get(ToolConstants.CFG_OUTPUTDIR), (String) context.get(ToolConstants.CFG_CLIENT_JAR));
try (JarOutputStream jarout = new JarOutputStream(Files.newOutputStream(clientJarFile.toPath()), new Manifest())) {
createClientJar(tmpDir, jarout);
} catch (Exception e) {
LOG.log(Level.SEVERE, "FAILED_TO_CREAT_CLIENTJAR", e);
Message msg = new Message("FAILED_TO_CREAT_CLIENTJAR", LOG);
throw new ToolException(msg, e);
}
context.put(ToolConstants.CFG_CLASSDIR, oldClassDir);
context.put(ClassCollector.class, oldCollector);
}
use of org.apache.cxf.tools.util.ClassCollector in project cxf by apache.
the class ImplGenerator method mapClassName.
private String mapClassName(String packageName, String name, ToolContext context) {
ClassCollector collector = context.get(ClassCollector.class);
int count = 0;
String checkName = name;
while (collector.containImplClass(packageName, checkName)) {
checkName = name + (++count);
}
collector.addImplClassName(packageName, checkName, packageName + "." + checkName);
return checkName;
}
use of org.apache.cxf.tools.util.ClassCollector in project cxf by apache.
the class JAXRSContainerTest method testOnewayMethod.
@Test
public void testOnewayMethod() throws Exception {
JAXRSContainer container = new JAXRSContainer(null);
final String onewayMethod = "deleteRepository";
ToolContext context = new ToolContext();
context.put(WadlToolConstants.CFG_OUTPUTDIR, output.getCanonicalPath());
context.put(WadlToolConstants.CFG_WADLURL, getLocation("/wadl/test.xml"));
context.put(WadlToolConstants.CFG_COMPILE, "true");
context.put(WadlToolConstants.CFG_ONEWAY, onewayMethod);
container.setContext(context);
container.execute();
assertNotNull(output.list());
ClassCollector cc = context.get(ClassCollector.class);
assertEquals(1, cc.getServiceClassNames().size());
try (URLClassLoader loader = new URLClassLoader(new URL[] { output.toURI().toURL() })) {
final Class<?> generatedClass = loader.loadClass(cc.getServiceClassNames().values().iterator().next());
Method m = generatedClass.getMethod(onewayMethod, String.class);
assertNotNull(m.getAnnotation(Oneway.class));
}
}
use of org.apache.cxf.tools.util.ClassCollector in project cxf by apache.
the class JAXRSContainerTest method testThrows.
@Test
public void testThrows() throws Exception {
JAXRSContainer container = new JAXRSContainer(null);
ToolContext context = new ToolContext();
context.put(WadlToolConstants.CFG_OUTPUTDIR, output.getCanonicalPath());
context.put(WadlToolConstants.CFG_WADLURL, getLocation("/wadl/test.xml"));
context.put(WadlToolConstants.CFG_COMPILE, Boolean.TRUE);
context.put(WadlToolConstants.CFG_INTERFACE, Boolean.TRUE);
context.put(WadlToolConstants.CFG_IMPL, Boolean.TRUE);
context.put(WadlToolConstants.CFG_CREATE_JAVA_DOCS, Boolean.TRUE);
container.setContext(context);
container.execute();
assertNotNull(output.list());
List<File> javaFiles = FileUtils.getFilesRecurseUsingSuffix(output, ".java");
assertEquals(2, javaFiles.size());
for (File f : javaFiles) {
if (!f.getName().endsWith("Impl.java")) {
assertTrue(Files.readAllLines(f.toPath()).contains(" * @throws IOException if something going wrong"));
}
}
ClassCollector cc = context.get(ClassCollector.class);
assertEquals(2, cc.getServiceClassNames().size());
final Map<String, Class<?>[]> methods = new HashMap<>();
methods.put("listRepositories", new Class<?>[] {});
methods.put("createRepository", new Class<?>[] { java.io.IOException.class });
methods.put("deleteRepository", new Class<?>[] { javax.ws.rs.NotFoundException.class, java.io.IOException.class });
methods.put("postThename", new Class<?>[] { java.io.IOException.class, java.lang.NoSuchMethodException.class });
try (URLClassLoader loader = new URLClassLoader(new URL[] { output.toURI().toURL() })) {
for (String className : cc.getServiceClassNames().values()) {
final Class<?> generatedClass = loader.loadClass(className);
for (Map.Entry<String, Class<?>[]> entry : methods.entrySet()) {
Method m;
try {
m = generatedClass.getMethod(entry.getKey(), String.class);
} catch (NoSuchMethodException e) {
m = generatedClass.getMethod(entry.getKey(), String.class, String.class);
}
assertArrayEquals(entry.getValue(), m.getExceptionTypes());
}
}
}
}
Aggregations