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 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));
JarOutputStream jarout = null;
try {
jarout = new JarOutputStream(Files.newOutputStream(clientJarFile.toPath()), new Manifest());
createClientJar(tmpDir, jarout);
jarout.close();
} 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 ClassUtils method compile.
public void compile(ToolContext context) throws ToolException {
Compiler compiler = (Compiler) context.get(ToolConstants.COMPILER);
if (compiler == null) {
compiler = new Compiler();
}
if (context.isVerbose()) {
compiler.setVerbose(true);
}
compiler.setEncoding((String) context.get(ToolConstants.CFG_ENCODING));
if (context.get(ToolConstants.CFG_CLASSDIR) != null) {
compiler.setOutputDir((String) context.get(ToolConstants.CFG_CLASSDIR));
}
String javaClasspath = System.getProperty("java.class.path");
if (javaClasspath != null) {
if (context.get(ToolConstants.CFG_OUTPUTDIR) != null) {
compiler.setClassPath(javaClasspath + File.pathSeparatorChar + context.get(ToolConstants.CFG_OUTPUTDIR));
} else {
compiler.setClassPath(javaClasspath);
}
}
String outPutDir = (String) context.get(ToolConstants.CFG_OUTPUTDIR);
Set<String> dirSet = new HashSet<>();
ClassCollector classCollector = context.get(ClassCollector.class);
List<String> fileList = new ArrayList<>();
Iterator<String> ite = classCollector.getGeneratedFileInfo().iterator();
while (ite.hasNext()) {
String fileName = ite.next();
fileName = fileName.replace('.', File.separatorChar);
String dirName = fileName.substring(0, fileName.lastIndexOf(File.separator) + 1);
String path = outPutDir + File.separator + dirName;
if (!dirSet.contains(path)) {
dirSet.add(path);
File file = new File(path);
if (file.isDirectory() && file.list() != null) {
for (String str : file.list()) {
if (str.endsWith("java")) {
fileList.add(path + str);
} else {
// copy generated xml file or others to class directory
File otherFile = new File(path + File.separator + str);
if (otherFile.isFile() && str.toLowerCase().endsWith("xml") && context.get(ToolConstants.CFG_CLASSDIR) != null) {
String targetDir = (String) context.get(ToolConstants.CFG_CLASSDIR);
File targetFile = new File(targetDir + File.separator + dirName + File.separator + str);
try {
copyXmlFile(otherFile, targetFile);
} catch (IOException e) {
Message msg = new Message("FAIL_TO_COPY_GENERATED_RESOURCE_FILE", LOG);
throw new ToolException(msg, e);
}
}
}
}
// JAXB plugins will generate extra files under the runtime directory
// Those files can not be allocated into the ClassCollector
File jaxbRuntime = new File(path, "runtime");
if (jaxbRuntime.isDirectory() && jaxbRuntime.exists()) {
List<File> files = FileUtils.getFiles(jaxbRuntime, ".+\\.java$");
for (File f : files) {
fileList.add(f.toString());
}
}
}
}
}
if (!compiler.compileFiles(fileList.toArray(new String[fileList.size()]))) {
Message msg = new Message("FAIL_TO_COMPILE_GENERATE_CODES", LOG);
throw new ToolException(msg);
}
}
use of org.apache.cxf.tools.util.ClassCollector in project cxf by apache.
the class JAXBDataBinding method initialize.
public void initialize(ToolContext c) throws ToolException {
this.context = c;
checkEncoding(c);
SchemaCompiler schemaCompiler = XJC.createSchemaCompiler();
Bus bus = context.get(Bus.class);
OASISCatalogManager catalog = bus.getExtension(OASISCatalogManager.class);
Options opts = null;
opts = getOptions(schemaCompiler);
hackInNewInternalizationLogic(schemaCompiler, catalog, opts);
ClassCollector classCollector = context.get(ClassCollector.class);
ClassNameAllocatorImpl allocator = new ClassNameAllocatorImpl(classCollector, c.optionSet(ToolConstants.CFG_AUTORESOLVE));
schemaCompiler.setClassNameAllocator(allocator);
listener = new JAXBBindErrorListener(context.isVerbose(), context.getErrorListener());
schemaCompiler.setErrorListener(listener);
// Collection<SchemaInfo> schemas = serviceInfo.getSchemas();
List<InputSource> jaxbBindings = context.getJaxbBindingFile();
SchemaCollection schemas = (SchemaCollection) context.get(ToolConstants.XML_SCHEMA_COLLECTION);
List<String> args = new ArrayList<>();
if (context.get(ToolConstants.CFG_NO_ADDRESS_BINDING) == null) {
// hard code to enable jaxb extensions
args.add("-extension");
String name = "/org/apache/cxf/tools/common/jaxb/W3CEPRJaxbBinding.xml";
if (isJAXB22()) {
name = "/org/apache/cxf/tools/common/jaxb/W3CEPRJaxbBinding_jaxb22.xml";
}
URL bindingFileUrl = getClass().getResource(name);
InputSource ins = new InputSource(bindingFileUrl.toString());
opts.addBindFile(ins);
}
if (context.get(ToolConstants.CFG_XJC_ARGS) != null) {
Object o = context.get(ToolConstants.CFG_XJC_ARGS);
if (o instanceof String) {
o = new String[] { (String) o };
}
String[] xjcArgss = (String[]) o;
for (String xjcArgs : xjcArgss) {
StringTokenizer tokenizer = new StringTokenizer(xjcArgs, ",", false);
while (tokenizer.hasMoreTokens()) {
String arg = tokenizer.nextToken();
args.add(arg);
LOG.log(Level.FINE, "xjc arg:" + arg);
}
}
}
if (context.get(ToolConstants.CFG_NO_ADDRESS_BINDING) == null || context.get(ToolConstants.CFG_XJC_ARGS) != null) {
try {
// keep parseArguments happy, supply dummy required command-line
// opts
opts.addGrammar(new InputSource("null"));
opts.parseArguments(args.toArray(new String[args.size()]));
} catch (BadCommandLineException e) {
StringBuilder msg = new StringBuilder("XJC reported 'BadCommandLineException' for -xjc argument:");
for (String arg : args) {
msg.append(arg + " ");
}
LOG.log(Level.FINE, msg.toString(), e);
if (opts != null) {
String pluginUsage = getPluginUsageString(opts);
msg.append(System.getProperty("line.separator"));
if (args.contains("-X")) {
throw new ToolException(pluginUsage, e);
}
msg.append(pluginUsage);
}
throw new ToolException(msg.toString(), e);
}
}
if (context.optionSet(ToolConstants.CFG_MARK_GENERATED)) {
// '-mark-generated' attribute to jaxb xjc.
try {
opts.parseArgument(new String[] { "-mark-generated" }, 0);
} catch (BadCommandLineException e) {
LOG.log(Level.SEVERE, e.getMessage());
throw new ToolException(e);
}
}
addSchemas(opts, schemaCompiler, schemas);
addBindingFiles(opts, jaxbBindings, schemas);
parseSchemas(schemaCompiler);
rawJaxbModelGenCode = schemaCompiler.bind();
addedEnumClassToCollector(schemas, allocator);
if (context.get(ToolConstants.CFG_DEFAULT_VALUES) != null) {
String cname = (String) context.get(ToolConstants.CFG_DEFAULT_VALUES);
if (StringUtils.isEmpty(cname)) {
defaultValues = new RandomValueProvider();
} else {
if (cname.charAt(0) == '=') {
cname = cname.substring(1);
}
try {
defaultValues = (DefaultValueProvider) Class.forName(cname).newInstance();
} catch (Exception e) {
LOG.log(Level.SEVERE, e.getMessage());
throw new ToolException(e);
}
}
}
initialized = true;
}
use of org.apache.cxf.tools.util.ClassCollector in project cxf by apache.
the class JAXBDataBinding method generate.
public void generate(ToolContext c) throws ToolException {
if (!initialized) {
initialize(c);
}
if (rawJaxbModelGenCode == null) {
return;
}
if (c.getErrorListener().getErrorCount() > 0) {
return;
}
try {
String dir = (String) context.get(ToolConstants.CFG_OUTPUTDIR);
TypesCodeWriter fileCodeWriter = new TypesCodeWriter(new File(dir), context.getExcludePkgList(), (String) context.get(ToolConstants.CFG_ENCODING), context.get(OutputStreamCreator.class));
S2JJAXBModel schem2JavaJaxbModel = rawJaxbModelGenCode;
ClassCollector classCollector = context.get(ClassCollector.class);
for (JClass cls : schem2JavaJaxbModel.getAllObjectFactories()) {
classCollector.getTypesPackages().add(cls._package().name());
}
JCodeModel jcodeModel = schem2JavaJaxbModel.generateCode(null, null);
if (!isSuppressCodeGen()) {
jcodeModel.build(fileCodeWriter);
}
context.put(JCodeModel.class, jcodeModel);
for (String str : fileCodeWriter.getExcludeFileList()) {
context.getExcludeFileList().add(str);
}
return;
} catch (IOException e) {
Message msg = new Message("FAIL_TO_GENERATE_TYPES", LOG);
throw new ToolException(msg, e);
}
}
Aggregations