use of org.apache.cxf.tools.common.ToolException in project cxf by apache.
the class WSDLToJavaContainer method createClientJar.
private void createClientJar(File tmpDirectory, JarOutputStream jarout) {
try {
URI parentFile = new File((String) context.get(ToolConstants.CFG_CLASSDIR)).toURI();
File[] files = tmpDirectory.listFiles();
if (files != null) {
for (File file : files) {
URI relativePath = parentFile.relativize(file.toURI());
String name = relativePath.toString();
if (file.isDirectory()) {
if (!StringUtils.isEmpty(name)) {
if (!name.endsWith("/")) {
name += "/";
}
JarEntry entry = new JarEntry(name);
entry.setTime(file.lastModified());
jarout.putNextEntry(entry);
jarout.closeEntry();
}
createClientJar(file, jarout);
continue;
}
JarEntry entry = new JarEntry(name);
entry.setTime(file.lastModified());
jarout.putNextEntry(entry);
InputStream input = new BufferedInputStream(Files.newInputStream(file.toPath()));
IOUtils.copy(input, jarout);
input.close();
jarout.closeEntry();
}
}
} catch (Exception e) {
Message msg = new Message("FAILED_ADD_JARENTRY", LOG);
throw new ToolException(msg, e);
}
}
use of org.apache.cxf.tools.common.ToolException in project cxf by apache.
the class WSDLToJavaContainer method generateLocalWSDL.
@SuppressWarnings("unchecked")
private void generateLocalWSDL(ToolContext context) {
String outputdir = (String) context.get(ToolConstants.CFG_CLASSDIR);
File wsdlFile = new File(outputdir, (String) context.get(ToolConstants.CFG_WSDLLOCATION));
Definition def = context.get(Definition.class);
try {
// get imported schemas
int xsdCount = 0;
SchemaCollection schemas = (SchemaCollection) context.get(ToolConstants.XML_SCHEMA_COLLECTION);
Map<String, String> sourceMap = new HashMap<>();
for (XmlSchema imp : schemas.getXmlSchemas()) {
if (imp.getSourceURI() != null && !imp.getSourceURI().contains(".wsdl#")) {
String schemaFileName = "schema" + (++xsdCount) + ".xsd";
sourceMap.put(imp.getTargetNamespace(), schemaFileName);
}
}
// get imported wsdls
int wsdlImportCount = 0;
List<Definition> defs = (List<Definition>) context.get(ToolConstants.IMPORTED_DEFINITION);
Map<String, String> importWSDLMap = new HashMap<>();
for (Definition importDef : defs) {
File importedWsdlFile = null;
if (!StringUtils.isEmpty(importDef.getDocumentBaseURI())) {
importedWsdlFile = new File(importDef.getDocumentBaseURI());
} else {
importedWsdlFile = new File(importDef.getQName().getLocalPart() + ".wsdl");
}
if (!FileUtils.isValidFileName(importedWsdlFile.getName())) {
importedWsdlFile = new File("import" + (++wsdlImportCount) + ".wsdl");
}
importWSDLMap.put(importDef.getTargetNamespace(), importedWsdlFile.getName());
}
OutputStreamCreator outputStreamCreator = null;
if (context.get(OutputStreamCreator.class) != null) {
outputStreamCreator = context.get(OutputStreamCreator.class);
} else {
outputStreamCreator = new OutputStreamCreator();
context.put(OutputStreamCreator.class, outputStreamCreator);
}
Writer os = null;
for (XmlSchema imp : schemas.getXmlSchemas()) {
if (imp.getSourceURI() != null && !imp.getSourceURI().contains(".wsdl#")) {
String schemaFileName = sourceMap.get(imp.getTargetNamespace());
File impfile = new File(wsdlFile.getParentFile(), schemaFileName);
Element el = imp.getSchemaDocument().getDocumentElement();
updateImports(el, sourceMap);
os = new FileWriterUtil(impfile.getParent(), context.get(OutputStreamCreator.class)).getWriter(impfile, StandardCharsets.UTF_8.name());
StaxUtils.writeTo(el, os, 2);
os.close();
}
}
// change the import location in wsdl file
OutputStream wsdloutput = new BufferedOutputStream(Files.newOutputStream(wsdlFile.toPath()));
WSDLWriter wsdlWriter = WSDLFactory.newInstance().newWSDLWriter();
LoadingByteArrayOutputStream bout = new LoadingByteArrayOutputStream();
wsdlWriter.writeWSDL(def, bout);
Element defEle = StaxUtils.read(bout.createInputStream()).getDocumentElement();
List<Element> xsdElements = DOMUtils.findAllElementsByTagNameNS(defEle, WSDLConstants.NS_SCHEMA_XSD, "schema");
for (Element xsdEle : xsdElements) {
updateImports(xsdEle, sourceMap);
}
updateWSDLImports(defEle, importWSDLMap);
StaxUtils.writeTo(defEle, wsdloutput);
wsdloutput.close();
for (Definition importDef : defs) {
File importWsdlFile = new File(outputdir, importWSDLMap.get(importDef.getTargetNamespace()));
OutputStream wsdlOs = new BufferedOutputStream(Files.newOutputStream(importWsdlFile.toPath()));
bout = new LoadingByteArrayOutputStream();
wsdlWriter.writeWSDL(importDef, bout);
Element importEle = StaxUtils.read(bout.createInputStream()).getDocumentElement();
xsdElements = DOMUtils.findAllElementsByTagNameNS(importEle, WSDLConstants.NS_SCHEMA_XSD, "schema");
for (Element xsdEle : xsdElements) {
updateImports(xsdEle, sourceMap);
}
updateWSDLImports(importEle, importWSDLMap);
StaxUtils.writeTo(importEle, wsdlOs);
wsdlOs.close();
}
} catch (Exception ex) {
LOG.log(Level.SEVERE, "FAILED_TO_GEN_LOCAL_WSDL", ex);
Message msg = new Message("FAILED_TO_GEN_LOCAL_WSDL", LOG);
throw new ToolException(msg, ex);
}
}
use of org.apache.cxf.tools.common.ToolException in project cxf by apache.
the class PluginLoader method init.
private void init() {
try {
jaxbContext = JAXBContext.newInstance("org.apache.cxf.tools.plugin");
loadPlugins(ClassLoaderUtils.getResources(PLUGIN_FILE_NAME, getClass()));
} catch (JAXBException e) {
Message msg = new Message("JAXB_CONTEXT_INIT_FAIL", LOG);
LOG.log(Level.SEVERE, msg.toString());
throw new ToolException(msg);
} catch (IOException ioe) {
Message msg = new Message("LOAD_PLUGIN_EXCEPTION", LOG);
LOG.log(Level.SEVERE, msg.toString());
throw new ToolException(msg);
}
}
use of org.apache.cxf.tools.common.ToolException in project cxf by apache.
the class PluginLoader method loadContainerClass.
private Class<? extends ToolContainer> loadContainerClass(String fullClzName) {
Class<?> clz = null;
try {
clz = ClassLoaderUtils.loadClass(fullClzName, getClass());
} catch (Exception e) {
Message msg = new Message("LOAD_CONTAINER_CLASS_FAILED", LOG, fullClzName);
LOG.log(Level.SEVERE, msg.toString());
throw new ToolException(msg, e);
}
if (!ToolContainer.class.isAssignableFrom(clz)) {
Message message = new Message("CLZ_SHOULD_IMPLEMENT_INTERFACE", LOG, clz.getName());
LOG.log(Level.SEVERE, message.toString());
throw new ToolException(message);
}
return clz.asSubclass(ToolContainer.class);
}
use of org.apache.cxf.tools.common.ToolException in project cxf by apache.
the class PluginLoader method getPlugin.
protected Plugin getPlugin(String resource) throws JAXBException, FileNotFoundException {
Plugin plugin = plugins.get(resource);
if (plugin == null) {
InputStream is = null;
if (new File(resource).exists()) {
is = new BufferedInputStream(new FileInputStream(new File(resource)));
} else {
is = getClass().getResourceAsStream(resource);
}
if (is == null) {
Message msg = new Message("PLUGIN_MISSING", LOG, resource);
LOG.log(Level.SEVERE, msg.toString());
throw new ToolException(msg);
}
plugin = getPlugin(is);
if (plugin == null || StringUtils.isEmpty(plugin.getName())) {
Message msg = new Message("PLUGIN_LOAD_FAIL", LOG, resource);
LOG.log(Level.SEVERE, msg.toString());
throw new ToolException(msg);
}
plugins.put(resource, plugin);
}
return plugin;
}
Aggregations