Return-Path: X-Original-To: apmail-db-torque-dev-archive@www.apache.org Delivered-To: apmail-db-torque-dev-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 43E0C7014 for ; Wed, 3 Aug 2011 13:35:36 +0000 (UTC) Received: (qmail 82448 invoked by uid 500); 3 Aug 2011 13:35:36 -0000 Delivered-To: apmail-db-torque-dev-archive@db.apache.org Received: (qmail 82365 invoked by uid 500); 3 Aug 2011 13:35:35 -0000 Mailing-List: contact torque-dev-help@db.apache.org; run by ezmlm Precedence: bulk List-Unsubscribe: List-Help: List-Post: List-Id: "Apache Torque Developers List" Reply-To: "Apache Torque Developers List" Delivered-To: mailing list torque-dev@db.apache.org Received: (qmail 82357 invoked by uid 500); 3 Aug 2011 13:35:35 -0000 Received: (qmail 82354 invoked by uid 99); 3 Aug 2011 13:35:35 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 03 Aug 2011 13:35:35 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=5.0 tests=ALL_TRUSTED,T_FRT_ADULT2 X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 03 Aug 2011 13:35:31 +0000 Received: from eris.apache.org (localhost [127.0.0.1]) by eris.apache.org (Postfix) with ESMTP id C4D9B238897D; Wed, 3 Aug 2011 13:35:09 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit 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 -0000 To: torque-commits@db.apache.org From: tfischer@apache.org X-Mailer: svnmailer-1.0.8 Message-Id: <20110803133509.C4D9B238897D@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org 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 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 unitConfigurations = configuration.getUnitConfigurations(); ControllerState controllerState = new ControllerState(); for (UnitConfiguration unitConfiguration : unitConfigurations) { - unitConfiguration.getLoglevel().apply(); - log.debug("run() : Loglevel applied."); - controllerState.setUnitConfiguration(unitConfiguration); - List 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 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 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 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 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 * output 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