View Javadoc

    1   package org.asyrinx.joey.gen.task;
    2
    3   import java.io.IOException;
    4   import java.util.ArrayList;
    5   import java.util.Collections;
    6   import java.util.Iterator;
    7   import java.util.List;
    8   import java.util.Map;
    9
    10  import ognl.OgnlRuntime;
    11
    12  import org.apache.commons.logging.Log;
    13  import org.apache.commons.logging.LogFactory;
    14  import org.apache.tools.ant.BuildException;
    15  import org.apache.tools.ant.types.FileSet;
    16  import org.apache.velocity.VelocityContext;
    17  import org.apache.velocity.context.Context;
    18  import org.apache.velocity.texen.ant.TexenTask;
    19  import org.asyrinx.brownie.core.lang.StringUtils;
    20  import org.asyrinx.brownie.seasar.aop.CacheInterceptor;
    21  import org.asyrinx.joey.gen.command.rdb2java.Rdb2JavaBuilder;
    22  import org.asyrinx.joey.gen.command.rdb2java.standard.BasicBuilder;
    23  import org.asyrinx.joey.gen.hibernate.HibernateUtils;
    24  import org.asyrinx.joey.gen.model.java.AppDomain;
    25  import org.asyrinx.joey.gen.model.rdb.Databases;
    26  import org.asyrinx.joey.gen.model.rdb.xml.DatabasesLoader;
    27  import org.asyrinx.joey.gen.model.rdb.xml.DatabasesLoaderImpl;
    28  import org.asyrinx.joey.gen.model.rdb.xml.XmlToRdbImpl;
    29  import org.seasar.framework.container.S2Container;
    30  import org.seasar.framework.container.impl.AspectDefImpl;
    31  import org.seasar.framework.container.impl.ComponentDefImpl;
    32  import org.seasar.framework.container.impl.S2ContainerImpl;
    33  import org.xml.sax.SAXException;
    34
    35  /***
    36   */
    37  public class DataModelTask extends TexenTask {
    38
    39      protected List filesets = new ArrayList();
    40
    41      protected Context context;
    42
    43      public void addFileset(FileSet set) {
    44          filesets.add(set);
    45      }
    46
    47      private static S2Container container = null;
    48
    49      public Context initControlContext() throws Exception {
    50          if (container == null) {
    51              //System.out.println("################################# init
    52              // container #################################");
    53              //ClassLoaderがちゃんとdiconファイルやDTDを読んでくれないので、仕方なく自分でコンポーネント定義をコーディング
    54              //BrownieS2ContainerFactory.create("joey-gen.dicon",
    55              // this.getProject().getBaseDir().getAbsolutePath());
    56              container = new S2ContainerImpl();
    57              container.register(XmlToRdbImpl.class);
    58              final ComponentDefImpl databasesLoaderDef = new ComponentDefImpl(DatabasesLoaderImpl.class);
    59              databasesLoaderDef.addAspectDef(new AspectDefImpl(new CacheInterceptor()));
    60              container.register(databasesLoaderDef);
    61              final ComponentDefImpl javaBuilderDef = new ComponentDefImpl(BasicBuilder.class);
    62              javaBuilderDef.addAspectDef(new AspectDefImpl(new CacheInterceptor()));
    63              container.register(javaBuilderDef);
    64          }
    65          return loadModels();
    66      }
    67
    68      private Context loadModels() throws IOException, SAXException {
    69          if (filesets.isEmpty())
    70              throw new BuildException("You must specify an XML schema or " + "fileset of XML schemas!");
    71          final Databases databases = loadDatabaseModels();
    72          //Java関係のオブジェクトを生成
    73          final Rdb2JavaBuilder builder = (Rdb2JavaBuilder) container.getComponent(Rdb2JavaBuilder.class);
    74          builder.setProperties(Collections.unmodifiableMap(getProject().getProperties()));
    75          final AppDomain domain = builder.execute(databases);
    76          //
    77          context = new VelocityContext();
    78          // Place our set of data models into the context along
    79          // with the names of the databases as a convenience for now.
    80          context.put("databases", databases);
    81          context.put("domain", domain);
    82          context.put("builder", builder);
    83          context.put("helper", new VelocityHelper(context));
    84          context.put("stringUtils", new org.asyrinx.brownie.core.lang.StringUtils());
    85          context.put("hibernateUtils", new HibernateUtils());
    86          context.put("ognl", new VelocityOgnlHelper(context));
    87          OgnlRuntime.setPropertyAccessor(Map.class, new VelocityOgnlAccessor(context));
    88          return context;
    89      }
    90
    91      private Databases loadDatabaseModels() throws IOException, SAXException {
    92          final DatabasesLoader databasesLoader = (DatabasesLoader) container.getComponent(DatabasesLoader.class);
    93          return databasesLoader.load(this.filesets, this.project);
    94      }
    95
    96      private final Log log = LogFactory.getLog(this.getClass());
    97
    98      public void setContextProperties(String file) {
    99          super.setContextProperties(file);
    100         // Map the torque.xxx elements from the env to the contextProperties
    101         Map env = super.getProject().getProperties();
    102         for (Iterator i = env.keySet().iterator(); i.hasNext();) {
    103             String key = (String) i.next();
    104             if (key.startsWith("joey-gen.")) {
    105                 String newKey = toVelocityKey(key.substring("joey-gen.".length()));
    106                 contextProperties.setProperty(newKey, env.get(key));
    107                 log.debug("joey-gen property available: " + newKey + ":" + env.get(key));
    108             }
    109         }
    110         for (Iterator i = env.keySet().iterator(); i.hasNext();) {
    111             String key = (String) i.next();
    112             if (key.startsWith("proj.")) {
    113                 String newKey = toVelocityKey(key);
    114                 contextProperties.setProperty(newKey, env.get(key));
    115                 log.debug("project property available: " + newKey + ":" + env.get(key));
    116             }
    117         }
    118     }
    119
    120     /***
    121      * @param newKey
    122      * @return
    123      */
    124     private String toVelocityKey(String newKey) {
    125         int j = newKey.indexOf(".");
    126         while (j != -1) {
    127             newKey = newKey.substring(0, j) + StringUtils.capitalize(newKey.substring(j + 1));
    128             j = newKey.indexOf(".");
    129         }
    130         return newKey;
    131     }
    132
    133 }