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.tools.common;
019:
020: import java.io.File;
021: import java.io.FileInputStream;
022: import java.io.FileOutputStream;
023: import java.util.ArrayList;
024: import java.util.HashSet;
025: import java.util.Iterator;
026: import java.util.List;
027: import java.util.Set;
028: import java.util.logging.Logger;
029:
030: import org.apache.cxf.common.i18n.Message;
031: import org.apache.cxf.common.logging.LogUtils;
032: import org.apache.cxf.tools.util.ClassCollector;
033: import org.apache.cxf.tools.util.Compiler;
034:
035: public class ClassUtils {
036:
037: protected static final Logger LOG = LogUtils
038: .getL7dLogger(ClassUtils.class);
039:
040: public void compile(ToolContext context) throws ToolException {
041: List<String> argList = new ArrayList<String>();
042:
043: String javaClasspath = System.getProperty("java.class.path");
044: // hard code cxf.jar
045: boolean classpathSetted = javaClasspath != null ? true : false;
046: // && (javaClasspath.indexOf("cxf.jar") >= 0);
047: if (context.isVerbose()) {
048: argList.add("-verbose");
049: }
050:
051: if (context.get(ToolConstants.CFG_CLASSDIR) != null) {
052: argList.add("-d");
053: String classDir = (String) context
054: .get(ToolConstants.CFG_CLASSDIR);
055: argList.add(classDir.replace(File.pathSeparatorChar, '/'));
056: }
057:
058: if (!classpathSetted) {
059: argList.add("-extdirs");
060: argList.add(getClass().getClassLoader().getResource(".")
061: .getFile()
062: + "../lib/");
063: } else {
064: argList.add("-classpath");
065: argList.add(javaClasspath);
066: }
067:
068: String outPutDir = (String) context
069: .get(ToolConstants.CFG_OUTPUTDIR);
070:
071: Set<String> dirSet = new HashSet<String>();
072: ClassCollector classCollector = context
073: .get(ClassCollector.class);
074: List<String> fileList = new ArrayList<String>();
075: Iterator ite = classCollector.getGeneratedFileInfo().iterator();
076: while (ite.hasNext()) {
077: String fileName = (String) ite.next();
078: fileName = fileName.replace('.', File.separatorChar);
079: String dirName = fileName.substring(0, fileName
080: .lastIndexOf(File.separator) + 1);
081:
082: String path = outPutDir + File.separator + dirName;
083: if (!dirSet.contains(path)) {
084:
085: dirSet.add(path);
086: File file = new File(path);
087: if (file.isDirectory()) {
088: for (String str : file.list()) {
089: if (str.endsWith("java")) {
090: fileList.add(path + str);
091: } else {
092: // copy generated xml file or others to class directory
093: File otherFile = new File(path
094: + File.separator + str);
095: if (otherFile.isFile()
096: && str.toLowerCase()
097: .endsWith("xml")
098: && context
099: .get(ToolConstants.CFG_CLASSDIR) != null) {
100: String targetDir = (String) context
101: .get(ToolConstants.CFG_CLASSDIR);
102:
103: File targetFile = new File(targetDir
104: + File.separator + dirName
105: + File.separator + str);
106: copyXmlFile(otherFile, targetFile);
107: }
108: }
109: }
110: }
111: }
112:
113: }
114: //Jaxb's bug . Jaxb ClassNameCollecotr may not be invoked when generated class is an enum.
115: //So we need recheck whether we add all generated source files to fileList
116:
117: String[] arguments = new String[argList.size()
118: + fileList.size() + 1];
119: arguments[0] = "javac";
120:
121: int i = 1;
122:
123: for (Object obj : argList.toArray()) {
124: String arg = (String) obj;
125: arguments[i] = arg;
126: i++;
127: }
128:
129: int srcFileIndex = i;
130:
131: for (Object o : fileList.toArray()) {
132: String file = (String) o;
133: arguments[i] = file;
134: i++;
135: }
136:
137: Compiler compiler = new Compiler();
138:
139: if (!compiler.internalCompile(arguments, srcFileIndex)) {
140: Message msg = new Message("FAIL_TO_COMPILE_GENERATE_CODES",
141: LOG);
142: throw new ToolException(msg);
143: }
144: }
145:
146: private void copyXmlFile(File from, File to) throws ToolException {
147:
148: try {
149: String dir = to.getCanonicalPath().substring(0,
150: to.getCanonicalPath().lastIndexOf(File.separator));
151: File dirFile = new File(dir);
152: dirFile.mkdirs();
153: FileInputStream input = new FileInputStream(from);
154: FileOutputStream output = new FileOutputStream(to);
155: byte[] b = new byte[1024 * 3];
156: int len = 0;
157: while (len != -1) {
158: len = input.read(b);
159: if (len != -1) {
160: output.write(b, 0, len);
161: }
162: }
163: output.flush();
164: output.close();
165: input.close();
166: } catch (Exception e) {
167: Message msg = new Message(
168: "FAIL_TO_COPY_GENERATED_RESOURCE_FILE", LOG);
169: throw new ToolException(msg, e);
170: }
171: }
172: }
|