1 package org.ozoneDB.tools.OPP; 7 8 import org.apache.tools.ant.*; 9 import org.apache.tools.ant.util.LoaderUtils; 10 import org.apache.tools.ant.types.FileSet; 11 import org.apache.tools.ant.types.Path; 12 import org.ozoneDB.tools.OPP.OPPBean; 13 import org.ozoneDB.tools.OPP.srcgen.ClassDirector; 14 import org.ozoneDB.tools.OPP.srcgen.ClassBuilder; 15 import org.ozoneDB.tools.OPP.srcgen.BuilderException; 16 import org.ozoneDB.tools.OPP.message.MessageWriter; 17 18 import java.io.*; 19 import java.util.StringTokenizer ; 20 21 public class OPPTask extends Task { 22 private FileSet source; 23 File output; 24 boolean quiet = false; 25 private final OPPBean oppBean = new OPPBean(); 26 private final MessageWriter genListener = new AntLogOPPGenerationeventListener(); 27 private Path classPath = null; 28 29 public void setClasspath(Path classpath) { 30 this.classPath = classpath; 31 } 32 33 public Path createClasspath() { 34 if (classPath == null) { 35 classPath = new Path(project); 36 } 37 return classPath.createPath(); 38 } 39 40 public void addConfiguredSource(FileSet source) { 41 this.source = source; 42 } 43 44 public void setOutput(File output) { 45 this.output = output; 46 } 47 48 private String convertFileToClassName(String file) { 49 if ((file == null) || file.equals("")) 50 return ""; 51 String token; 52 StringBuffer className = new StringBuffer (); 53 for (StringTokenizer st = new StringTokenizer (file, File.separator); st.hasMoreTokens();) { 54 token = st.nextToken(); 55 if (st.hasMoreTokens()) { 56 className.append(token + '.'); 57 } else { 58 className.append(token.substring(0, token.lastIndexOf('.'))); 59 } 60 } 61 return className.toString(); 62 } 63 64 private void verifyParameters() { 65 if (output == null) { 66 output = getProject().getBaseDir(); 67 } 68 if (source == null) { 69 throw new BuildException("resolver must be specified"); 70 } 71 } 72 73 public void setCache(boolean cache) { 74 oppBean.setCache(cache); 75 } 76 77 81 82 public void setUpdateExpression(String updateExpression) { 83 oppBean.setUpdateExpression(updateExpression); 84 } 85 86 90 91 95 96 public void setPrintStackTrace(boolean printStackTrace) { 97 oppBean.setPrintStackTrace(printStackTrace); 98 } 99 100 void setGenerate_ocd(boolean value) { 101 oppBean.setGenerateOcd(value); 102 } 103 104 void setGenerate_factory(boolean value) { 105 oppBean.setGenerateFactory(value); 106 } 107 108 void setGenerate_proxy(boolean value) { 109 oppBean.setGenerateProxy(value); 110 } 111 112 void setUse_ocd(boolean value) { 113 oppBean.setUseOcd(value); 114 } 115 116 void setUse_source(boolean value) { 117 oppBean.setUseSource(value); 118 } 119 120 public void execute() throws BuildException { 121 oppBean.addGenrationEventListener(genListener); 122 verifyParameters(); 123 DirectoryScanner scanner = source.getDirectoryScanner(project); 124 scanner.scan(); 125 String files[] = scanner.getIncludedFiles(); 126 127 ClassDirector cd = oppBean.createDirector(scanner.getBasedir()); 128 ClassBuilder cb = oppBean.createBuilder(output, "Factory", "_Proxy"); 129 130 for (int i = 0; i < files.length; i++) { 131 String className = convertFileToClassName(files[i]); 132 final ClassLoader loader = getConfiguredClassLoader(); 133 final ClassLoader oldLoader = LoaderUtils.getContextClassLoader(); 134 log("Loader is set"); 135 try { 136 LoaderUtils.setContextClassLoader(loader); 137 try { 138 139 log("Begin build"); 140 cd.build(className, cb); 141 log("End build"); 142 } finally { 143 LoaderUtils.setContextClassLoader(oldLoader); 144 } 145 } catch (BuilderException e) { 146 throw new BuildException(e); 147 } 148 } 149 } 150 151 private ClassLoader getConfiguredClassLoader() { 152 if (classPath == null) { 153 return this.getClass().getClassLoader(); 154 } else { 155 return new AntClassLoader(this.getClass().getClassLoader(), getProject(), classPath, false); 156 } 157 } 158 159 class AntLogOPPGenerationeventListener implements MessageWriter { 160 public void startGeneration(String object) { 161 } 163 164 public void error(String message) { 165 log(message, Project.MSG_ERR); 166 } 167 168 public void warning(String message) { 169 log(message, Project.MSG_WARN); 170 } 171 172 public void warning(String filename, int row, String message) { 173 log(filename + " [" + row + "]: " + message, Project.MSG_WARN); 174 } 175 176 public void info(String message) { 177 log(message, Project.MSG_INFO); 178 } 179 180 public void debug(String message) { 181 log(message, Project.MSG_DEBUG); 182 } 183 184 public void endGeneration() { 185 } 186 } 187 } 188 189 | Popular Tags |