db-torque-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From tfisc...@apache.org
Subject svn commit: r1153481 - /db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/control/Controller.java
Date Wed, 03 Aug 2011 13:35:09 GMT
Author: tfischer
Date: Wed Aug  3 13:35:09 2011
New Revision: 1153481

URL: http://svn.apache.org/viewvc?rev=1153481&view=rev
Log:
- split large code chunk into smaller methods
- simplified exception processing

Modified:
    db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/control/Controller.java

Modified: db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/control/Controller.java
URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/control/Controller.java?rev=1153481&r1=1153480&r2=1153481&view=diff
==============================================================================
--- db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/control/Controller.java
(original)
+++ db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/control/Controller.java
Wed Aug  3 13:35:09 2011
@@ -69,111 +69,23 @@ public class Controller
      *         processing.
      * @throws GeneratorException if a OutletException occurs during
      *         processing.
-     * @throws SourceException if a SourceException occurs during
-     *         processing.
-     * @throws SourceTransformerException if a SourceTransformerException
-     *         occurs during processing.
      * @throws IOException if a IOException occurs during processing.
      */
     public void run(List<UnitDescriptor> unitDescriptors)
-        throws ControllerException, GeneratorException,
-            SourceTransformerException, IOException
+        throws GeneratorException
     {
         initLogging();
-
-        log.info("run() : Starting to read configuration files");
-        Configuration configuration = new Configuration();
-        configuration.addUnits(unitDescriptors);
-        configuration.read();
-        log.info("run() : Configuration read.");
+        Configuration configuration = readConfiguration(unitDescriptors);
 
         List<UnitConfiguration> unitConfigurations
                 = configuration.getUnitConfigurations();
         ControllerState controllerState = new ControllerState();
         for (UnitConfiguration unitConfiguration : unitConfigurations)
         {
-            unitConfiguration.getLoglevel().apply();
-            log.debug("run() : Loglevel applied.");
-            controllerState.setUnitConfiguration(unitConfiguration);
-            List<Output> outputList = unitConfiguration.getOutputList();
-            for (Output output : outputList)
-            {
-                log.debug("Processing output " + output.getFilename());
-                controllerState.setOutput(output);
-                File targetDirectory;
-                if (output.isAlwaysNew())
-                {
-                    targetDirectory
-                        = unitConfiguration.getNewFileTargetDirectory();
-                    log.debug("Using newFileTargetDirectory "
-                            + targetDirectory);
-                }
-                else
-                {
-                    targetDirectory
-                        = unitConfiguration.getModifiedFileTargetDirectory();
-                    log.debug("Using modifiedFileTargetDirectory "
-                            + targetDirectory);
-                }
-
-                SourceProvider sourceProvider = output.getSourceProvider();
-                SourceProvider overrideSourceProvider
-                        = unitConfiguration.getOverrideSourceProvider();
-                if (overrideSourceProvider != null)
-                {
-                    if (overrideSourceProvider.isInit())
-                    {
-                        overrideSourceProvider.reset(
-                                configuration.getConfigurationHandlers(),
-                                controllerState);
-                    }
-                    sourceProvider = overrideSourceProvider;
-                }
-                sourceProvider.init(
-                        configuration.getConfigurationHandlers(),
-                        controllerState);
-                if (!sourceProvider.hasNext())
-                {
-                    log.info("No sources found, skipping output");
-                }
-
-                while (sourceProvider.hasNext())
-                {
-                    Source source = sourceProvider.next();
-                    log.info("Processing source " + source.getDescription());
-                    SourceElement rootElement = source.getRootElement();
-                    controllerState.setSourceFile(source.getSourceFile());
-                    SourceProcessConfiguration sourceProcessConfiguration
-                            = output.getSourceProcessConfiguration();
-                    rootElement = transformSource(
-                            rootElement,
-                            sourceProcessConfiguration.getTransformerDefinitions(),
-                            controllerState);
-                    controllerState.setRootElement(rootElement);
-
-                    String startElementsPath
-                            = sourceProcessConfiguration.getStartElementsPath();
-                    List<SourceElement> startElements
-                            = SourcePath.getElementsFromRoot(
-                                    rootElement,
-                                    startElementsPath);
-                    if (startElements.isEmpty())
-                    {
-                        log.info("No start Elements found for path "
-                                + startElementsPath);
-                    }
-                    for (SourceElement startElement : startElements)
-                    {
-                        processStartElement(
-                                startElement,
-                                output,
-                                source,
-                                targetDirectory,
-                                unitConfiguration,
-                                controllerState);
-                    }
-                }
-            }
+            processGenerationUnit(
+                    controllerState,
+                    unitConfiguration,
+                    configuration);
         }
         controllerState.getVariableStore().endGeneration();
     }
@@ -199,6 +111,183 @@ public class Controller
     }
 
     /**
+     * Reads the configuration.
+     * 
+     * @param unitDescriptors the unit descriptors for which the configuration
+     *        should be read, not null, not empty.
+     *
+     * @return the configuration.
+     *
+     * @throws ConfigurationException if the configuration is faulty.
+     */
+    private Configuration readConfiguration(
+                List<UnitDescriptor> unitDescriptors)
+            throws ConfigurationException
+    {
+        log.info("readConfiguration() : Starting to read configuration files");
+        Configuration configuration = new Configuration();
+        configuration.addUnits(unitDescriptors);
+        configuration.read();
+        log.info("readConfiguration() : Configuration read.");
+        return configuration;
+    }
+
+    /**
+     * Processes a unit of generation.
+     * 
+     * @param controllerState the controller state, not null.
+     * @param unitConfiguration the configuration of the generation unit
+     *        to process, not null.
+     * @param configuration the generator configuration, not null.
+     *
+     * @throws GeneratorException if a generation error occurs.
+     */
+    protected void processGenerationUnit(
+                ControllerState controllerState,
+                UnitConfiguration unitConfiguration,
+                Configuration configuration)
+            throws GeneratorException
+    {
+        log.debug("processGenerationUnit() : start");
+        unitConfiguration.getLoglevel().apply();
+        log.debug("processGenerationUnit() : Loglevel applied.");
+        controllerState.setUnitConfiguration(unitConfiguration);
+        List<Output> outputList = unitConfiguration.getOutputList();
+        for (Output output : outputList)
+        {
+            processOutput(
+                    output,
+                    controllerState,
+                    unitConfiguration,
+                    configuration);
+        }
+    }
+
+    /**
+     * Processes an output definition.
+     * 
+     * @param output the output definition to process, not null.
+     * @param controllerState the controller state, not null.
+     * @param unitConfiguration the configuration of the generation unit
+     *        to process, not null.
+     * @param configuration the generator configuration, not null.
+     *
+     * @throws GeneratorException if a generation error occurs.
+     */
+    private void processOutput(
+                Output output,
+                ControllerState controllerState,
+                UnitConfiguration unitConfiguration,
+                Configuration configuration)
+            throws GeneratorException
+    {
+        log.debug("Processing output " + output.getFilename());
+        controllerState.setOutput(output);
+        File targetDirectory;
+        if (output.isAlwaysNew())
+        {
+            targetDirectory
+                = unitConfiguration.getNewFileTargetDirectory();
+            log.debug("Using newFileTargetDirectory "
+                    + targetDirectory);
+        }
+        else
+        {
+            targetDirectory
+                = unitConfiguration.getModifiedFileTargetDirectory();
+            log.debug("Using modifiedFileTargetDirectory "
+                    + targetDirectory);
+        }
+
+        SourceProvider sourceProvider = output.getSourceProvider();
+        SourceProvider overrideSourceProvider
+                = unitConfiguration.getOverrideSourceProvider();
+        if (overrideSourceProvider != null)
+        {
+            if (overrideSourceProvider.isInit())
+            {
+                overrideSourceProvider.reset(
+                        configuration.getConfigurationHandlers(),
+                        controllerState);
+            }
+            sourceProvider = overrideSourceProvider;
+        }
+        sourceProvider.init(
+                configuration.getConfigurationHandlers(),
+                controllerState);
+        if (!sourceProvider.hasNext())
+        {
+            log.info("No sources found, skipping output");
+        }
+
+        while (sourceProvider.hasNext())
+        {
+            Source source = sourceProvider.next();
+            processSourceInOutput(
+                    source,
+                    output,
+                    controllerState, 
+                    targetDirectory,
+                    unitConfiguration);
+        }
+    }
+
+    /**
+     * Processes a single source in an output definition.
+     * 
+     * @param source the source to process, not null.
+     * @param output the output to which the source belongs, not null.
+     * @param controllerState the controller state, not null.
+     * @param targetDirectory the directory to which the output
+     *        should be written, not null.
+     * @param unitConfiguration the configuration of the current generation
+     *        unit, not null.
+     * 
+     * @throws GeneratorException if a generation error occurs.
+     */
+    private void processSourceInOutput(
+                Source source,
+                Output output,
+                ControllerState controllerState,
+                File targetDirectory,
+                UnitConfiguration unitConfiguration)
+            throws GeneratorException
+    {
+        log.info("Processing source " + source.getDescription());
+        SourceElement rootElement = source.getRootElement();
+        controllerState.setSourceFile(source.getSourceFile());
+        SourceProcessConfiguration sourceProcessConfiguration
+                = output.getSourceProcessConfiguration();
+        rootElement = transformSource(
+                rootElement,
+                sourceProcessConfiguration.getTransformerDefinitions(),
+                controllerState);
+        controllerState.setRootElement(rootElement);
+
+        String startElementsPath
+                = sourceProcessConfiguration.getStartElementsPath();
+        List<SourceElement> startElements
+                = SourcePath.getElementsFromRoot(
+                        rootElement,
+                        startElementsPath);
+        if (startElements.isEmpty())
+        {
+            log.info("No start Elements found for path "
+                    + startElementsPath);
+        }
+        for (SourceElement startElement : startElements)
+        {
+            processStartElement(
+                    startElement,
+                    output,
+                    source,
+                    targetDirectory,
+                    unitConfiguration,
+                    controllerState);
+        }
+    }
+
+    /**
      * Creates the output file name and sets it in the output.
      * The filename is calculated either by the filenameConfigurator in
      * <code>output</code> or is given explicitly (in the latter case
@@ -271,11 +360,10 @@ public class Controller
      * @param controllerState the current controller state, not null.
      *
      * @throws ControllerException if startElement is null or the configured
-     *         outlet does not exist.
+     *         outlet does not exist or the output directory cannot be created
+     *         or the output file cannot be written..
      * @throws GeneratorException if the outlet throws an exception
      *         during execution.
-     * @throws IOException if the output directory cannot be created or the
-     *         output file cannot be written.
      */
     private void processStartElement(
                 SourceElement startElement,
@@ -284,7 +372,7 @@ public class Controller
                 File targetDirectory,
                 UnitConfiguration unitConfiguration,
                 ControllerState controllerState)
-            throws ControllerException, GeneratorException, IOException
+            throws ControllerException, GeneratorException
     {
         if (startElement == null)
         {
@@ -371,7 +459,7 @@ public class Controller
                 boolean success = parentOutputDir.mkdirs();
                 if (!success)
                 {
-                    throw new IOException(
+                    throw new ControllerException(
                             "Could not create directory \""
                                 + parentOutputDir.getAbsolutePath()
                                 + "\"");
@@ -390,11 +478,30 @@ public class Controller
                     controllerState.getOutputFile());
             fileWriter.append(result);
         }
+        catch (IOException e)
+        {
+            throw new ControllerException(
+                    "Could not write file \""
+                        + controllerState.getOutputFile().getAbsolutePath()
+                        + "\"",
+                    e);
+        }
         finally
         {
             if (fileWriter != null)
             {
-                fileWriter.close();
+                try
+                {
+                    fileWriter.close();
+                }
+                catch (IOException e)
+                {
+                    throw new ControllerException(
+                        "Could not close stream for file \""
+                            + controllerState.getOutputFile().getAbsolutePath()
+                            + "\"",
+                        e);
+                }
             }
         }
 



---------------------------------------------------------------------
To unsubscribe, e-mail: torque-dev-unsubscribe@db.apache.org
For additional commands, e-mail: torque-dev-help@db.apache.org


Mime
View raw message