001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */package org.apache.cxf.maven_plugin;
019:
020: import java.io.File;
021: import java.util.ArrayList;
022: import java.util.Iterator;
023: import java.util.List;
024:
025: import org.apache.maven.plugin.AbstractMojo;
026: import org.apache.maven.plugin.MojoExecutionException;
027: import org.apache.maven.project.MavenProject;
028: import org.apache.tools.ant.ExitException;
029: import org.apache.tools.ant.util.optional.NoExitSecurityManager;
030:
031: /**
032: * @goal xsdtojava
033: * @description CXF XSD To Java Tool
034: */
035: public class XSDToJavaMojo extends AbstractMojo {
036: /**
037: * @parameter
038: */
039: String testSourceRoot;
040:
041: /**
042: * @parameter expression="${project.build.directory}/generated/src/main/java"
043: * @required
044: */
045: String sourceRoot;
046:
047: /**
048: * @parameter expression="${project}"
049: * @required
050: */
051: MavenProject project;
052:
053: /**
054: * @parameter
055: */
056: XsdOption xsdOptions[];
057:
058: public void execute() throws MojoExecutionException {
059: String outputDir = testSourceRoot == null ? sourceRoot
060: : testSourceRoot;
061: File outputDirFile = new File(outputDir);
062: outputDirFile.mkdirs();
063:
064: long timestamp = CodegenUtils.getCodegenTimestamp();
065: boolean result = true;
066:
067: if (xsdOptions == null) {
068: throw new MojoExecutionException("Must specify xsdOptions");
069: }
070:
071: for (int x = 0; x < xsdOptions.length; x++) {
072: String[] args = getArguments(xsdOptions[x], outputDir);
073:
074: File file = new File(xsdOptions[x].getXsd());
075: File doneFile = new File(outputDirFile, "."
076: + file.getName() + ".DONE");
077: boolean doWork = timestamp > doneFile.lastModified();
078: if (!doneFile.exists()) {
079: doWork = true;
080: } else if (file.lastModified() > doneFile.lastModified()) {
081: doWork = true;
082: } else {
083: File files[] = xsdOptions[x].getDependencies();
084: if (files != null) {
085: for (int z = 0; z < files.length; ++z) {
086: if (files[z].lastModified() > doneFile
087: .lastModified()) {
088: doWork = true;
089: }
090: }
091: }
092: }
093:
094: if (doWork) {
095: SecurityManager oldSm = System.getSecurityManager();
096: try {
097: try {
098: System
099: .setSecurityManager(new NoExitSecurityManager());
100:
101: com.sun.tools.xjc.Driver.main(args);
102:
103: } catch (ExitException e) {
104: if (e.getStatus() == 0) {
105: doneFile.delete();
106: doneFile.createNewFile();
107: } else {
108: throw e;
109: }
110: } finally {
111: System.setSecurityManager(oldSm);
112: File dirs[] = xsdOptions[x].getDeleteDirs();
113: if (dirs != null) {
114: for (int idx = 0; idx < dirs.length; ++idx) {
115: result = result && deleteDir(dirs[idx]);
116: }
117: }
118: }
119: } catch (Exception e) {
120: e.printStackTrace();
121: throw new MojoExecutionException(e.getMessage(), e);
122: }
123: }
124:
125: if (!result) {
126: throw new MojoExecutionException(
127: "Could not delete redundant dirs");
128: }
129: }
130:
131: if (project != null && sourceRoot != null) {
132: project.addCompileSourceRoot(sourceRoot);
133: }
134: if (project != null && testSourceRoot != null) {
135: project.addTestCompileSourceRoot(testSourceRoot);
136: }
137: }
138:
139: private String[] getArguments(XsdOption option, String outputDir) {
140: List<String> list = new ArrayList<String>();
141: if (option.getPackagename() != null) {
142: list.add("-p");
143: list.add(option.getPackagename());
144: }
145: if (option.getBindingFile() != null) {
146: list.add("-b");
147: list.add(option.getBindingFile());
148: }
149: if (option.getCatalog() != null) {
150: list.add("-catalog");
151: list.add(option.getCatalog());
152: }
153: if (option.isExtension()) {
154: list.add("-extension");
155: }
156: if (option.getExtensionArgs() != null) {
157: Iterator it = option.getExtensionArgs().iterator();
158: while (it.hasNext()) {
159: list.add(it.next().toString());
160: }
161: }
162: list.add("-quiet");
163: list.add("-d");
164: list.add(outputDir);
165: list.add(option.getXsd());
166:
167: return list.toArray(new String[list.size()]);
168:
169: }
170:
171: private boolean deleteDir(File f) {
172: if (f.isDirectory()) {
173: File files[] = f.listFiles();
174: for (int idx = 0; idx < files.length; ++idx) {
175: deleteDir(files[idx]);
176: }
177: }
178:
179: if (f.exists()) {
180: return f.delete();
181: }
182:
183: return true;
184: }
185: }
|