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.FileReader;
    12  import java.io.IOException;
    13  import java.io.InputStream;
    14  import java.util.ArrayList;
    15  import java.util.Collections;
    16  import java.util.Iterator;
    17  import java.util.List;
    18  import java.util.Map;
    19  import java.util.Properties;
    20
    21  import ognl.OgnlRuntime;
    22
    23  import org.apache.commons.logging.Log;
    24  import org.apache.commons.logging.LogFactory;
    25  import org.apache.tools.ant.BuildException;
    26  import org.apache.tools.ant.types.FileSet;
    27  import org.apache.velocity.VelocityContext;
    28  import org.apache.velocity.context.Context;
    29  import org.asyrinx.brownie.core.lang.StringUtils;
    30  import org.asyrinx.brownie.seasar.aop.CacheInterceptor;
    31  import org.asyrinx.joey.gen.command.rdb2java.Rdb2JavaBuilder;
    32  import org.asyrinx.joey.gen.command.rdb2java.standard.BasicBuilder;
    33  import org.asyrinx.joey.gen.hibernate.HibernateUtils;
    34  import org.asyrinx.joey.gen.model.java.AppDomain;
    35  import org.asyrinx.joey.gen.model.rdb.Databases;
    36  import org.asyrinx.joey.gen.model.rdb.xml.DatabasesLoader;
    37  import org.asyrinx.joey.gen.model.rdb.xml.DatabasesLoaderImpl;
    38  import org.asyrinx.joey.gen.model.rdb.xml.XmlToRdbImpl;
    39  import org.seasar.framework.container.S2Container;
    40  import org.seasar.framework.container.impl.AspectDefImpl;
    41  import org.seasar.framework.container.impl.ComponentDefImpl;
    42  import org.seasar.framework.container.impl.S2ContainerImpl;
    43  import org.xml.sax.SAXException;
    44
    45  /***
    46   * @author takeshi
    47   */
    48  public class JoeyGenerateTask extends MultiTargetTexenTask {
    49
    50      private List loadTargetFile() {
    51          final List result = new ArrayList();
    52          try {
    53              final BufferedReader reader = new BufferedReader(new FileReader(getTargetTextFile()));
    54              for (String line = reader.readLine(); line != null; line = reader.readLine()) {
    55                  line = line.trim();
    56                  if (StringUtils.isEmpty(line))
    57                      continue;
    58                  if (line.startsWith("#")) //'#' means comment
    59                      continue;
    60                  if (result.contains(line))
    61                      continue;
    62                  log.debug(getTargetTextFile() + " target: " + line);
    63                  result.add(line);
    64              }
    65          } catch (IOException e) {
    66              throw new BuildException(e);
    67          }
    68          return result;
    69      }
    70
    71      protected List initTargets() {
    72          final Properties properties = getDefaultProperties();
    73          final List source = loadTargetFile();
    74          final List result = new ArrayList();
    75          for (Iterator i = source.iterator(); i.hasNext();) {
    76              final String line = (String) i.next();
    77              final String controlKey = "joey-gen.template.control." + line;
    78              final String destKey = "joey-gen.template.dest." + line;
    79              final String controlTemplate = String.valueOf(properties.getProperty(controlKey));
    80              final String targetStr = String.valueOf(properties.getProperty(destKey));
    81              final String destDir = getDestDir(targetStr);
    82              final JoeyGenerateTarget target = new JoeyGenerateTarget(line, destDir, controlTemplate);
    83              result.add(target);
    84          }
    85          return result;
    86      }
    87
    88      /***
    89       * @return
    90       */
    91      private Properties getDefaultProperties() {
    92          final Properties properties = new Properties();
    93          final InputStream stream = this.getClass().getResourceAsStream("default.properties");
    94          try {
    95              properties.load(stream);
    96          } catch (IOException e) {
    97              throw new BuildException(e);
    98          }
    99          return properties;
    100     }
    101
    102     private String getDestDir(String targetStr) {
    103         if (targetStr == null)
    104             return getJavaSrcDir();
    105         if ("javasrc".equals(targetStr))
    106             return getJavaSrcDir();
    107         if ("testsrc".equals(targetStr))
    108             return getTestSrcDir();
    109         if ("proj".equals(targetStr))
    110             return getProjDir();
    111         if ("webapp".equals(targetStr))
    112             return getWebappDir();
    113         throw new BuildException("Illegal targetStr '" + targetStr + "'");
    114     }
    115
    116     protected List filesets = new ArrayList();
    117
    118     protected Context context;
    119
    120     public void addFileset(FileSet set) {
    121         filesets.add(set);
    122     }
    123
    124     private static S2Container container = null;
    125
    126     public Context initControlContext() {
    127         if (container == null) {
    128             //System.out.println("################################# init
    129             // container #################################");
    130             //ClassLoaderがちゃんとdiconファイルやDTDを読んでくれないので、仕方なく自分でコンポーネント定義をコーディング
    131             //BrownieS2ContainerFactory.create("joey-gen.dicon",
    132             // this.getProject().getBaseDir().getAbsolutePath());
    133             container = new S2ContainerImpl();
    134             container.register(XmlToRdbImpl.class);
    135             final ComponentDefImpl databasesLoaderDef = new ComponentDefImpl(DatabasesLoaderImpl.class);
    136             databasesLoaderDef.addAspectDef(new AspectDefImpl(new CacheInterceptor()));
    137             container.register(databasesLoaderDef);
    138             final ComponentDefImpl javaBuilderDef = new ComponentDefImpl(BasicBuilder.class);
    139             javaBuilderDef.addAspectDef(new AspectDefImpl(new CacheInterceptor()));
    140             container.register(javaBuilderDef);
    141         }
    142         try {
    143             return loadModels();
    144         } catch (Exception e) {
    145             throw new BuildException(e);
    146         }
    147     }
    148
    149     private Context loadModels() throws IOException, SAXException {
    150         if (filesets.isEmpty())
    151             throw new BuildException("You must specify an XML schema or " + "fileset of XML schemas!");
    152         final Databases databases = loadDatabaseModels();
    153         //Java関係のオブジェクトを生成
    154         final Rdb2JavaBuilder builder = (Rdb2JavaBuilder) container.getComponent(Rdb2JavaBuilder.class);
    155         builder.setProperties(Collections.unmodifiableMap(getProject().getProperties()));
    156         final AppDomain domain = builder.execute(databases);
    157         //
    158         context = new VelocityContext();
    159         context.put("databases", databases);
    160         context.put("domain", domain);
    161         context.put("builder", builder);
    162         context.put("helper", new VelocityHelper(context));
    163         context.put("stringUtils", new org.asyrinx.brownie.core.lang.StringUtils());
    164         context.put("hibernateUtils", new HibernateUtils());
    165         context.put("ognl", new VelocityOgnlHelper(context));
    166         OgnlRuntime.setPropertyAccessor(Map.class, new VelocityOgnlAccessor(context));
    167         return context;
    168     }
    169
    170     private Databases loadDatabaseModels() throws IOException, SAXException {
    171         final DatabasesLoader databasesLoader = (DatabasesLoader) container.getComponent(DatabasesLoader.class);
    172         return databasesLoader.load(this.filesets, this.project);
    173     }
    174
    175     private final Log log = LogFactory.getLog(this.getClass());
    176
    177     public void setContextProperties(String file) {
    178         super.setContextProperties(file);
    179         final Map env = super.getProject().getProperties();
    180         for (Iterator i = env.keySet().iterator(); i.hasNext();) {
    181             final String key = (String) i.next();
    182             if (key.startsWith("joey-gen.")) {
    183                 String newKey = toVelocityKey(key.substring("joey-gen.".length()));
    184                 contextProperties.setProperty(newKey, env.get(key));
    185                 log.debug("joey-gen property available: " + newKey + ":" + env.get(key));
    186             }
    187         }
    188         for (Iterator i = env.keySet().iterator(); i.hasNext();) {
    189             final String key = (String) i.next();
    190             if (key.startsWith("proj.")) {
    191                 String newKey = toVelocityKey(key);
    192                 contextProperties.setProperty(newKey, env.get(key));
    193                 log.debug("project property available: " + newKey + ":" + env.get(key));
    194             }
    195         }
    196     }
    197
    198     private String toVelocityKey(String newKey) {
    199         int j = newKey.indexOf(".");
    200         while (j != -1) {
    201             newKey = newKey.substring(0, j) + StringUtils.capitalize(newKey.substring(j + 1));
    202             j = newKey.indexOf(".");
    203         }
    204         return newKey;
    205     }
    206
    207     private String targetTextFile = null;
    208
    209     public String getTargetTextFile() {
    210         return targetTextFile;
    211     }
    212
    213     public void setTargetTextFile(File targetTextFile) {
    214         try {
    215             this.targetTextFile = targetTextFile.getCanonicalPath();
    216         } catch (IOException e) {
    217             throw new BuildException(e);
    218         }
    219     }
    220
    221     private String projDir = null;
    222
    223     private String javaSrcDir = null;
    224
    225     private String testSrcDir = null;
    226
    227     private String webappDir = null;
    228
    229     public String getJavaSrcDir() {
    230         return javaSrcDir;
    231     }
    232
    233     public String getProjDir() {
    234         return projDir;
    235     }
    236
    237     public String getTestSrcDir() {
    238         return testSrcDir;
    239     }
    240
    241     public String getWebappDir() {
    242         return webappDir;
    243     }
    244
    245     public void setJavaSrcDir(File javaSrcDir) {
    246         try {
    247             this.javaSrcDir = javaSrcDir.getCanonicalPath();
    248         } catch (IOException e) {
    249             throw new BuildException(e);
    250         }
    251     }
    252
    253     public void setProjDir(File projDir) {
    254         try {
    255             this.projDir = projDir.getCanonicalPath();
    256         } catch (IOException e) {
    257             throw new BuildException(e);
    258         }
    259     }
    260
    261     public void setTestSrcDir(File testSrcDir) {
    262         try {
    263             this.testSrcDir = testSrcDir.getCanonicalPath();
    264         } catch (IOException e) {
    265             throw new BuildException(e);
    266         }
    267     }
    268
    269     public void setWebappDir(File webappDir) {
    270         try {
    271             this.webappDir = webappDir.getCanonicalPath();
    272         } catch (IOException e) {
    273             throw new BuildException(e);
    274         }
    275     }
    276
    277 }