Coverage Report - net.sourceforge.addam.ddlrun.grammars.ScriptGrammarManager
 
Classes in this File Line Coverage Branch Coverage Complexity
ScriptGrammarManager
42%
21/50
50%
3/6
5.2
 
 1  
 /*
 2  
  * Copyright (c) 2004 International Decision Systems, Inc.  All Rights Reserved.
 3  
  *
 4  
  * By using this Software, You acknowledge that the Software is a valuable asset
 5  
  * and trade secret of either International Decision Systems, Inc. ("IDSI") or a
 6  
  * third party supplier of IDSI and constitutes confidential and proprietary
 7  
  * information.
 8  
  *
 9  
  * NEITHER IDSI NOR ANY AGENT OR PERSON ACTING FOR OR WITH IDSI HAS MADE OR DOES
 10  
  * MAKE ANY STATEMENTS, AFFIRMATIONS, REPRESENTATIONS OR WARRANTIES WHATSOEVER
 11  
  * TO YOU, WHETHER EXPRESS OR IMPLIED, AS TO THE SOFTWARE, THE QUALITY OR
 12  
  * CONDITION OF THE SOFTWARE, OR THE OPERATING CHARACTERISTICS OR RELIABILITY OF
 13  
  * THE SOFTWARE, OR ITS SUITABILITY FOR ANY GENERAL OR PARTICULAR PURPOSE, OR AS
 14  
  * TO ANY OTHER MATTER WHATSOEVER; ANY AND ALL OTHER WARRANTIES INCLUDING
 15  
  * WITHOUT LIMITATION ANY WARRANTIES IMPLIED BY LAW, SUCH AS THE IMPLIED
 16  
  * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND TITLE,
 17  
  * USE AND NON-INFRINGEMENT; ARE HEREBY EXPRESSLY DISCLAIMED AND EXCLUDED.
 18  
 */
 19  
 package net.sourceforge.addam.ddlrun.grammars;
 20  
 
 21  
 import java.io.IOException;
 22  
 import java.io.InputStream;
 23  
 import java.net.URL;
 24  
 import java.sql.Connection;
 25  
 import java.sql.SQLException;
 26  
 import java.util.Enumeration;
 27  
 import java.util.Properties;
 28  
 
 29  
 /**
 30  
  * Used to get the correct grammar for a JDBC connection
 31  
  *
 32  
  * @author TIM3
 33  
  * @since Mar 28, 2005
 34  
  */
 35  
 public class ScriptGrammarManager {
 36  
 
 37  
         /**
 38  
          * Constructor.
 39  
          *
 40  
          * @todo externalize the property filename in this method.
 41  
          */
 42  
     public ScriptGrammarManager() {
 43  
             //TODO externalize this property file name
 44  4
         this("net/sourceforge/addam/ddlrun/grammars/ScriptGrammars.properties");
 45  4
     }
 46  
 
 47  8
     public ScriptGrammarManager(String resourceName) {
 48  
         Enumeration resources;
 49  
         try {
 50  8
             resources = ScriptGrammarManager.class.getClassLoader().getResources(resourceName);
 51  0
         } catch (IOException e) {
 52  0
             throw new RuntimeException("unable to load resource named " + resourceName, e);
 53  4
         }
 54  
 
 55  8
         if (!resources.hasMoreElements()) {
 56  0
             throw new RuntimeException("no such resource " + resourceName);
 57  
         }
 58  
 
 59  24
         while (resources.hasMoreElements()) {
 60  16
             URL resource = (URL) resources.nextElement();
 61  16
             InputStream in = null;
 62  
             try {
 63  16
                 in = resource.openStream();
 64  16
                 registry.load(in);
 65  0
             } catch (IOException e) {
 66  0
                 throw new RuntimeException("unable to load resource " + resource, e);
 67  0
             } finally {
 68  0
                 try {
 69  16
                     if (in != null) {
 70  16
                         in.close();
 71  
                     }
 72  0
                 } catch (IOException e) {
 73  
                     // shouldn't happen
 74  0
                     throw new RuntimeException("unable to load resource " + resource, e);
 75  8
                 }
 76  0
             }
 77  8
         }
 78  8
     }
 79  
 
 80  4
     public ScriptGrammarManager(Properties registry) {
 81  4
         this.registry.putAll(registry);
 82  4
     }
 83  
 
 84  
     public ScriptGrammar getGrammar(Connection connection) {
 85  0
         String productName = null;
 86  0
         if (connection != null) {
 87  
             try {
 88  0
                 productName = connection.getMetaData().getDatabaseProductName();
 89  0
             } catch (SQLException e) {
 90  0
                 throw new RuntimeException("unable to determine database product name", e);
 91  0
             }
 92  
         }
 93  
 
 94  0
         ScriptGrammar grammar = null;
 95  0
         if (productName != null) {
 96  0
             String grammarClassName = registry.getProperty(productName);
 97  0
             if (grammarClassName != null) {
 98  
                 try {
 99  0
                     grammar = (ScriptGrammar) Class.forName(grammarClassName).newInstance();
 100  0
                 } catch (InstantiationException e) {
 101  0
                     throw new RuntimeException("unable to instantiate: " + grammarClassName, e);
 102  0
                 } catch (IllegalAccessException e) {
 103  0
                     throw new RuntimeException("illegal access when instantiating: " + grammarClassName, e);
 104  0
                 } catch (ClassNotFoundException e) {
 105  0
                     throw new RuntimeException("class not found: " + grammarClassName, e);
 106  0
                 }
 107  
             }
 108  
 
 109  
         }
 110  0
         return grammar;
 111  
     }
 112  
 
 113  
     // for testing
 114  
     Properties getGrammarRegistry() {
 115  
         // wrap the registry so the caller can't modify it
 116  12
         return new Properties(registry);
 117  
     }
 118  
 
 119  12
     private final Properties registry = new Properties();
 120  
 }