|  1 |     | 
     | 
  |  2 |     | 
     | 
  |  3 |     | 
     | 
  |  4 |     | 
     | 
  |  5 |     | 
     | 
  |  6 |     | 
     | 
  |  7 |     | 
     | 
  |  8 |     | 
     | 
  |  9 |     | 
     | 
  |  10 |     | 
     | 
  |  11 |     | 
     | 
  |  12 |     | 
     | 
  |  13 |     | 
     | 
  |  14 |     | 
     | 
  |  15 |     | 
     | 
  |  16 |     | 
     | 
  |  17 |     | 
     | 
  |  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 |     | 
     | 
  |  31 |     | 
     | 
  |  32 |     | 
     | 
  |  33 |     | 
     | 
  |  34 |     | 
     | 
  |  35 |     | 
   public class ScriptGrammarManager { | 
  |  36 |     | 
     | 
  |  37 |     | 
             | 
  |  38 |     | 
     | 
  |  39 |     | 
     | 
  |  40 |     | 
     | 
  |  41 |     | 
     | 
  |  42 |     | 
       public ScriptGrammarManager() { | 
  |  43 |     | 
                 | 
  |  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 |     | 
                         | 
  |  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 |     | 
         | 
  |  114 |     | 
       Properties getGrammarRegistry() { | 
  |  115 |     | 
             | 
  |  116 |    12 |            return new Properties(registry);  | 
  |  117 |     | 
       }  | 
  |  118 |     | 
     | 
  |  119 |    12 |        private final Properties registry = new Properties();  | 
  |  120 |     | 
   }  |