Home » apache-openjpa-1.1.0-source » org.apache.openjpa.jdbc » ant » [javadoc | source]
    1   /*
    2    * Licensed to the Apache Software Foundation (ASF) under one
    3    * or more contributor license agreements.  See the NOTICE file
    4    * distributed with this work for additional information
    5    * regarding copyright ownership.  The ASF licenses this file
    6    * to you under the Apache License, Version 2.0 (the
    7    * "License"); you may not use this file except in compliance
    8    * with the License.  You may obtain a copy of the License at
    9    *
   10    * http://www.apache.org/licenses/LICENSE-2.0
   11    *
   12    * Unless required by applicable law or agreed to in writing,
   13    * software distributed under the License is distributed on an
   14    * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
   15    * KIND, either express or implied.  See the License for the
   16    * specific language governing permissions and limitations
   17    * under the License.    
   18    */
   19   package org.apache.openjpa.jdbc.ant;
   20   
   21   import java.security.AccessController;
   22   
   23   import org.apache.tools.ant.BuildException;
   24   import org.apache.tools.ant.types.EnumeratedAttribute;
   25   import org.apache.openjpa.jdbc.conf.JDBCConfiguration;
   26   import org.apache.openjpa.jdbc.conf.JDBCConfigurationImpl;
   27   import org.apache.openjpa.jdbc.meta.MappingTool;
   28   import org.apache.openjpa.jdbc.schema.SchemaTool;
   29   import org.apache.openjpa.lib.ant.AbstractTask;
   30   import org.apache.openjpa.lib.conf.ConfigurationImpl;
   31   import org.apache.openjpa.lib.util.Files;
   32   import org.apache.openjpa.lib.util.J2DoPrivHelper;
   33   import org.apache.openjpa.lib.util.Localizer;
   34   import org.apache.openjpa.util.MultiLoaderClassResolver;
   35   
   36   /**
   37    * Executes the {@link MappingTool} on the specified files.
   38    * This task can take the following arguments:
   39    * <ul>
   40    * <li><code>action</code></li>
   41    * <li><code>meta</code></li>
   42    * <li><code>schemaAction</code></li>
   43    * <li><code>dropTables</code></li>
   44    * <li><code>ignoreErrors</code></li>
   45    * <li><code>readSchema</code></li>
   46    * <li><code>primaryKeys</code></li>
   47    * <li><code>foreignKeys</code></li>
   48    * <li><code>indexes</code></li>
   49    * <li><code>file</code></li>
   50    * <li><code>schemaFile</code></li>
   51    * <li><code>sqlFile</code></li>
   52    * <li><code>tmpClassLoader</code></li>
   53    * </ul> Of these arguments, only <code>action</code> is required.
   54    */
   55   public class MappingToolTask
   56       extends AbstractTask {
   57   
   58       private static final Localizer _loc = Localizer.forPackage
   59           (MappingToolTask.class);
   60   
   61       protected MappingTool.Flags flags = new MappingTool.Flags();
   62       protected String file = null;
   63       protected String schemaFile = null;
   64       protected String sqlFile = null;
   65       protected boolean tmpClassLoader = true;
   66   
   67       /**
   68        * Set the enumerated MappingTool action type.
   69        */
   70       public void setAction(Action act) {
   71           flags.action = act.getValue();
   72       }
   73   
   74       /**
   75        * Set the enumerated SchemaTool action type.
   76        */
   77       public void setSchemaAction(SchemaAction act) {
   78           flags.schemaAction = act.getValue();
   79       }
   80   
   81       /**
   82        * Set whether the MappingTool should read the full schema.
   83        */
   84       public void setReadSchema(boolean readSchema) {
   85           flags.readSchema = readSchema;
   86       }
   87   
   88       /**
   89        * Set whether we want the MappingTool to ignore SQL errors.
   90        */
   91       public void setIgnoreErrors(boolean ignoreErrors) {
   92           flags.ignoreErrors = ignoreErrors;
   93       }
   94   
   95       /**
   96        * Set whether the MappingTool should drop tables.
   97        */
   98       public void setDropTables(boolean dropTables) {
   99           flags.dropTables = dropTables;
  100       }
  101   
  102       /**
  103        * Set whether to drop OpenJPA tables.
  104        */
  105       public void setOpenJPATables(boolean openjpaTables) {
  106           flags.openjpaTables = openjpaTables;
  107       }
  108   
  109       /**
  110        * Set whether the MappingTool should drop sequences.
  111        */
  112       public void setDropSequences(boolean dropSequences) {
  113           flags.dropSequences = dropSequences;
  114       }
  115   
  116       /**
  117        * Set whether the MappingTool should manipulate sequences.
  118        */
  119       public void setSequences(boolean sequences) {
  120           flags.sequences = sequences;
  121       }
  122   
  123       /**
  124        * Set whether to generate primary key information.
  125        */
  126       public void setPrimaryKeys(boolean pks) {
  127           flags.primaryKeys = pks;
  128       }
  129   
  130       /**
  131        * Set whether to generate foreign key information.
  132        */
  133       public void setForeignKeys(boolean fks) {
  134           flags.foreignKeys = fks;
  135       }
  136   
  137       /**
  138        * Set whether to generate index information.
  139        */
  140       public void setIndexes(boolean idxs) {
  141           flags.indexes = idxs;
  142       }
  143   
  144       /**
  145        * Set the output file we want the MappingTool to write to.
  146        */
  147       public void setFile(String file) {
  148           this.file = file;
  149       }
  150   
  151       /**
  152        * Set the output file for an XML representation of the planned schema.
  153        */
  154       public void setSchemaFile(String schemaFile) {
  155           this.schemaFile = schemaFile;
  156       }
  157   
  158       /**
  159        * Set the output file we want the MappingTool to write a SQL script to.
  160        */
  161       public void setSQLFile(String sqlFile) {
  162           this.sqlFile = sqlFile;
  163       }
  164   
  165       /**
  166        * Set whether this action applies to metadata as well as mappings.
  167        */
  168       public void setMeta(boolean meta) {
  169           flags.meta = meta;
  170       }
  171   
  172       protected ConfigurationImpl newConfiguration() {
  173           return new JDBCConfigurationImpl();
  174       }
  175   
  176       protected void executeOn(String[] files)
  177           throws Exception {
  178           if (MappingTool.ACTION_IMPORT.equals(flags.action))
  179               assertFiles(files);
  180   
  181           ClassLoader toolLoader = (ClassLoader) AccessController
  182                   .doPrivileged(J2DoPrivHelper
  183                           .getClassLoaderAction(MappingTool.class));
  184           ClassLoader loader = toolLoader;
  185           MultiLoaderClassResolver resolver = new MultiLoaderClassResolver();
  186   
  187           if (tmpClassLoader) {
  188               loader = (ClassLoader) AccessController.doPrivileged(J2DoPrivHelper
  189                       .newTemporaryClassLoaderAction(getClassLoader()));
  190               resolver.addClassLoader(loader);
  191           }
  192           resolver.addClassLoader(toolLoader);
  193               
  194           if (flags.meta && MappingTool.ACTION_ADD.equals(flags.action))
  195               flags.metaDataFile = Files.getFile(file, loader);
  196           else
  197               flags.mappingWriter = Files.getWriter(file, loader);
  198   
  199           flags.schemaWriter = Files.getWriter(schemaFile, loader);
  200           flags.sqlWriter = Files.getWriter(sqlFile, loader);
  201   
  202           JDBCConfiguration conf = (JDBCConfiguration) getConfiguration();
  203           conf.setClassResolver(resolver);
  204           
  205           if (!MappingTool.run(conf, files, flags, loader))
  206               throw new BuildException(_loc.get("bad-conf", "MappingToolTask")
  207                   .getMessage());
  208       }
  209   
  210       public static class Action
  211           extends EnumeratedAttribute {
  212   
  213           public String[] getValues() {
  214               return MappingTool.ACTIONS;
  215           }
  216       }
  217   
  218       public static class SchemaAction
  219           extends EnumeratedAttribute {
  220   
  221           public String[] getValues() {
  222               String[] actions = new String[SchemaTool.ACTIONS.length + 1];
  223               System.arraycopy(SchemaTool.ACTIONS, 0, actions, 0,
  224                   SchemaTool.ACTIONS.length);
  225               actions[actions.length - 1] = "none";
  226               return actions;
  227           }
  228       }
  229   
  230       /**
  231        * <P>
  232        * Set whether a temporary ClassLoader should be used by the MappingTool.
  233        * The default value is true
  234        * </P>
  235        * 
  236        * @param tmpClassLoader
  237        *            Whether the temporary ClassLoader should be used.
  238        */
  239       public void setTmpClassLoader(boolean tmpClassLoader) {
  240           this.tmpClassLoader = tmpClassLoader;
  241       }
  242   }
  243   

Save This Page
Home » apache-openjpa-1.1.0-source » org.apache.openjpa.jdbc » ant » [javadoc | source]