View Javadoc

    1   /*
    2    * joey-gen and its relative products are published under the terms
    3    * of the Apache Software License.
    4    *
    5    * Created on 2004/12/20 12:59:01
    6    */
    7   package org.asyrinx.joey.gen.task;
    8
    9   import java.io.BufferedReader;
    10  import java.io.File;
    11  import java.io.FileInputStream;
    12  import java.io.FileNotFoundException;
    13  import java.io.FileOutputStream;
    14  import java.io.FileReader;
    15  import java.io.IOException;
    16  import java.io.InputStream;
    17  import java.util.ArrayList;
    18  import java.util.Iterator;
    19  import java.util.List;
    20  import java.util.Map;
    21  import java.util.Properties;
    22
    23  import ognl.OgnlRuntime;
    24
    25  import org.apache.commons.io.CopyUtils;
    26  import org.apache.commons.io.FileUtils;
    27  import org.apache.tools.ant.BuildException;
    28  import org.apache.tools.ant.DirectoryScanner;
    29  import org.apache.tools.ant.Project;
    30  import org.apache.tools.ant.types.FileSet;
    31  import org.apache.velocity.VelocityContext;
    32  import org.apache.velocity.context.Context;
    33  import org.apache.velocity.texen.Generator;
    34  import org.asyrinx.brownie.core.lang.StringUtils;
    35  import org.asyrinx.joey.gen.hibernate.HibernateUtils;
    36  import org.asyrinx.joey.gen.model.java.AppDomain;
    37  import org.asyrinx.joey.gen.model.rdb.Databases;
    38  import org.asyrinx.joey.gen.task.impl.S2ContainerLoader;
    39  import org.seasar.framework.container.S2Container;
    40  import org.xml.sax.SAXException;
    41
    42  /***
    43   * @author takeshi
    44   */
    45  public class JoeyGenerateTask extends MultiTargetTexenTask {
    46
    47      private S2Container container = S2ContainerLoader.getContainer();
    48
    49      public Context initControlContext() {
    50          if (filesets.isEmpty())
    51              throw new BuildException("There's no schema file. You must specify fileset of schema files!");
    52          try {
    53              return loadModels();
    54          } catch (Exception e) {
    55              throw new BuildException(e);
    56          }
    57      }
    58
    59      private Generator generator = null;
    60
    61      protected Generator initGenerator() {
    62          if (this.generator == null) {
    63              final Object gen = container.getComponent(JoeyVelocityGenerator.class);
    64              if (gen instanceof Generator)
    65                  this.generator = (Generator) gen;
    66              else
    67                  throw new BuildException("JoeyVelocityGenerator object must extends " + Generator.class.getName());
    68          }
    69          return this.generator;
    70      }
    71
    72      private SchemaBackup schemaBackup = new SchemaBackup();
    73
    74      //protected Context context;
    75
    76      private Context loadModels() throws IOException, SAXException {
    77          final ModelLoader modelLoader = (ModelLoader) container.getComponent(ModelLoader.class);
    78          final Databases databases = modelLoader.loadDatabaseModels(filesets, this.project);
    79          final AppDomain domain = modelLoader.loadAppDomainModel(databases, this.project);
    80          final Map rdb2Java = modelLoader.getRdb2Java();
    81          //
    82          schemaBackup.execute(this.project, filesets, lastSchemaDir);
    83          //
    84          final Context context = new VelocityContext();
    85          context.put("databases", databases);
    86          context.put("domain", domain);
    87          context.put("rdb2java", rdb2Java);
    88          context.put("helper", new VelocityHelper(context));
    89          context.put("ognl", new VelocityOgnlHelper(context));
    90          context.put("stringUtils", new org.asyrinx.brownie.core.lang.StringUtils());
    91          context.put("hibernateUtils", new HibernateUtils());
    92          OgnlRuntime.setPropertyAccessor(Map.class, new VelocityOgnlAccessor(context));
    93          return context;
    94      }
    95
    96      private List loadTargetFile() {
    97          final List result = new ArrayList();
    98          try {
    99              final BufferedReader reader = new BufferedReader(new FileReader(getTargetTextFile()));
    100             for (String line = reader.readLine(); line != null; line = reader.readLine()) {
    101                 line = line.trim();
    102                 if (StringUtils.isEmpty(line))
    103                     continue;
    104                 if (line.startsWith("#")) //'#' means comment
    105                     continue;
    106                 if (result.contains(line))
    107                     continue;
    108                 log.debug(getTargetTextFile() + " target: " + line);
    109                 result.add(line);
    110             }
    111         } catch (IOException e) {
    112             throw new BuildException(e);
    113         }
    114         return result;
    115     }
    116
    117     protected List initTargets() {
    118         final Properties properties = getDefaultProperties();
    119         final List source = loadTargetFile();
    120         final List result = new ArrayList();
    121         for (Iterator i = source.iterator(); i.hasNext();) {
    122             final String line = (String) i.next();
    123             final String controlKey = "joey-gen.template.control." + line;
    124             final String destKey = "joey-gen.template.dest." + line;
    125             final String controlTemplate = String.valueOf(properties.getProperty(controlKey));
    126             final String targetStr = String.valueOf(properties.getProperty(destKey));
    127             final String destDir = getDestDir(targetStr);
    128             final JoeyGenerateTarget target = new JoeyGenerateTarget(line, destDir, controlTemplate);
    129             result.add(target);
    130         }
    131         return result;
    132     }
    133
    134     private Properties getDefaultProperties() {
    135         final Properties properties = new Properties();
    136         final InputStream stream = this.getClass().getResourceAsStream("default.properties");
    137         try {
    138             properties.load(stream);
    139         } catch (IOException e) {
    140             throw new BuildException(e);
    141         }
    142         return properties;
    143     }
    144
    145     private String getDestDir(String targetStr) {
    146         if (targetStr == null)
    147             return getJavaSrcDir();
    148         if ("javasrc".equals(targetStr))
    149             return getJavaSrcDir();
    150         if ("testsrc".equals(targetStr))
    151             return getTestSrcDir();
    152         if ("proj".equals(targetStr))
    153             return getProjDir();
    154         if ("webapp".equals(targetStr))
    155             return getWebappDir();
    156         throw new BuildException("Illegal targetStr '" + targetStr + "'");
    157     }
    158
    159     protected List filesets = new ArrayList();
    160
    161     public void addFileset(FileSet set) {
    162         filesets.add(set);
    163     }
    164
    165     public void setContextProperties(String file) {
    166         super.setContextProperties(file);
    167         final Map env = super.getProject().getProperties();
    168         for (Iterator i = env.keySet().iterator(); i.hasNext();) {
    169             final String key = (String) i.next();
    170             if (key.startsWith("joey-gen.")) {
    171                 String newKey = toVelocityKey(key.substring("joey-gen.".length()));
    172                 contextProperties.setProperty(newKey, env.get(key));
    173                 log.debug("joey-gen property available: " + newKey + ":" + env.get(key));
    174             }
    175         }
    176         for (Iterator i = env.keySet().iterator(); i.hasNext();) {
    177             final String key = (String) i.next();
    178             if (key.startsWith("proj.")) {
    179                 String newKey = toVelocityKey(key);
    180                 contextProperties.setProperty(newKey, env.get(key));
    181                 log.debug("project property available: " + newKey + ":" + env.get(key));
    182             }
    183         }
    184     }
    185
    186     private String toVelocityKey(String newKey) {
    187         int j = newKey.indexOf(".");
    188         while (j != -1) {
    189             newKey = newKey.substring(0, j) + StringUtils.capitalize(newKey.substring(j + 1));
    190             j = newKey.indexOf(".");
    191         }
    192         return newKey;
    193     }
    194
    195     private String targetTextFile = null;
    196
    197     public String getTargetTextFile() {
    198         return targetTextFile;
    199     }
    200
    201     public void setTargetTextFile(File targetTextFile) {
    202         try {
    203             this.targetTextFile = targetTextFile.getCanonicalPath();
    204         } catch (IOException e) {
    205             throw new BuildException(e);
    206         }
    207     }
    208
    209     private String projDir = null;
    210
    211     private String javaSrcDir = null;
    212
    213     private String testSrcDir = null;
    214
    215     private String webappDir = null;
    216
    217     public String getJavaSrcDir() {
    218         return javaSrcDir;
    219     }
    220
    221     public String getProjDir() {
    222         return projDir;
    223     }
    224
    225     public String getTestSrcDir() {
    226         return testSrcDir;
    227     }
    228
    229     public String getWebappDir() {
    230         return webappDir;
    231     }
    232
    233     public void setJavaSrcDir(File javaSrcDir) {
    234         try {
    235             this.javaSrcDir = javaSrcDir.getCanonicalPath();
    236         } catch (IOException e) {
    237             throw new BuildException(e);
    238         }
    239     }
    240
    241     public void setProjDir(File projDir) {
    242         try {
    243             this.projDir = projDir.getCanonicalPath();
    244         } catch (IOException e) {
    245             throw new BuildException(e);
    246         }
    247     }
    248
    249     public void setTestSrcDir(File testSrcDir) {
    250         try {
    251             this.testSrcDir = testSrcDir.getCanonicalPath();
    252         } catch (IOException e) {
    253             throw new BuildException(e);
    254         }
    255     }
    256
    257     public void setWebappDir(File webappDir) {
    258         try {
    259             this.webappDir = webappDir.getCanonicalPath();
    260         } catch (IOException e) {
    261             throw new BuildException(e);
    262         }
    263     }
    264
    265     private File lastSchemaDir = null;
    266
    267     public File getLastSchemaDir() {
    268         return lastSchemaDir;
    269     }
    270
    271     public void setLastSchemaDir(File lastSchemaDir) {
    272         this.lastSchemaDir = lastSchemaDir;
    273     }
    274 }
    275
    276 class SchemaBackup {
    277
    278     public void execute(Project project, List sourceFileSets, File destDir) throws IOException {
    279         if (destDir.exists()) {
    280             FileUtils.cleanDirectory(destDir);
    281         } else {
    282             destDir.mkdirs();
    283         }
    284         copySchemaFiles(project, sourceFileSets, destDir);
    285     }
    286
    287     /***
    288      * @param project
    289      * @param sourceFileSets
    290      * @param destDir
    291      * @throws IOException
    292      * @throws FileNotFoundException
    293      */
    294     private void copySchemaFiles(Project project, List sourceFileSets, File destDir) throws IOException,
    295             FileNotFoundException {
    296         for (int i = 0; i < sourceFileSets.size(); i++) {
    297             final FileSet fs = (FileSet) sourceFileSets.get(i);
    298             final File dir = fs.getDir(project);
    299             final DirectoryScanner ds = fs.getDirectoryScanner(project);
    300             final String[] filenNmes = ds.getIncludedFiles();
    301             for (int j = 0; j < filenNmes.length; j++) {
    302                 final File src = new File(dir, filenNmes[j]);
    303                 final File dest = new File(destDir, filenNmes[j]);
    304                 CopyUtils.copy(new FileInputStream(src), new FileOutputStream(dest));
    305             }
    306         }
    307     }
    308
    309 }