View Javadoc

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          this("net/sourceforge/addam/ddlrun/grammars/ScriptGrammars.properties");
45      }
46  
47      public ScriptGrammarManager(String resourceName) {
48          Enumeration resources;
49          try {
50              resources = ScriptGrammarManager.class.getClassLoader().getResources(resourceName);
51          } catch (IOException e) {
52              throw new RuntimeException("unable to load resource named " + resourceName, e);
53          }
54  
55          if (!resources.hasMoreElements()) {
56              throw new RuntimeException("no such resource " + resourceName);
57          }
58  
59          while (resources.hasMoreElements()) {
60              URL resource = (URL) resources.nextElement();
61              InputStream in = null;
62              try {
63                  in = resource.openStream();
64                  registry.load(in);
65              } catch (IOException e) {
66                  throw new RuntimeException("unable to load resource " + resource, e);
67              } finally {
68                  try {
69                      if (in != null) {
70                          in.close();
71                      }
72                  } catch (IOException e) {
73                      // shouldn't happen
74                      throw new RuntimeException("unable to load resource " + resource, e);
75                  }
76              }
77          }
78      }
79  
80      public ScriptGrammarManager(Properties registry) {
81          this.registry.putAll(registry);
82      }
83  
84      public ScriptGrammar getGrammar(Connection connection) {
85          String productName = null;
86          if (connection != null) {
87              try {
88                  productName = connection.getMetaData().getDatabaseProductName();
89              } catch (SQLException e) {
90                  throw new RuntimeException("unable to determine database product name", e);
91              }
92          }
93  
94          ScriptGrammar grammar = null;
95          if (productName != null) {
96              String grammarClassName = registry.getProperty(productName);
97              if (grammarClassName != null) {
98                  try {
99                      grammar = (ScriptGrammar) Class.forName(grammarClassName).newInstance();
100                 } catch (InstantiationException e) {
101                     throw new RuntimeException("unable to instantiate: " + grammarClassName, e);
102                 } catch (IllegalAccessException e) {
103                     throw new RuntimeException("illegal access when instantiating: " + grammarClassName, e);
104                 } catch (ClassNotFoundException e) {
105                     throw new RuntimeException("class not found: " + grammarClassName, e);
106                 }
107             }
108 
109         }
110         return grammar;
111     }
112 
113     // for testing
114     Properties getGrammarRegistry() {
115         // wrap the registry so the caller can't modify it
116         return new Properties(registry);
117     }
118 
119     private final Properties registry = new Properties();
120 }