View Javadoc

1   package org.apache.torque.generator.source.jdbc;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.apache.torque.generator.configuration.ConfigurationException;
23  import org.apache.torque.generator.configuration.ConfigurationHandlers;
24  import org.apache.torque.generator.control.ControllerState;
25  import org.apache.torque.generator.source.Source;
26  import org.apache.torque.generator.source.SourceProvider;
27  
28  /**
29   * Creates a source from JDBC metadata.
30   *
31   * @version $Id: JdbcMetadataSourceProvider.java 1331190 2012-04-27 02:41:35Z tfischer $
32   */
33  public class JdbcMetadataSourceProvider extends SourceProvider
34  {
35      /** The name of the option to retrieve the JDBC URL from. */
36      private String urlOption;
37  
38      /** The name of the option to retrieve the JDBC driver from. */
39      private String driverOption;
40  
41      /** The name of the option to retrieve the JDBC user name from. */
42      private String usernameOption;
43  
44      /** The name of the option to retrieve the JDBC password from. */
45      private String passwordOption;
46  
47      /** The name of the option to retrieve the JDBC schema from. */
48      private String schemaOption;
49  
50      /** JDBC URL. */
51      private String url;
52  
53      /** JDBC driver. */
54      private String driver;
55  
56      /** JDBC user name. */
57      private String username;
58  
59      /** JDBC password. */
60      private String password;
61  
62      /** JDBC schema. */
63      private String schema;
64  
65      /** Whether next() was already called. */
66      private boolean nextCalled = false;
67  
68      public JdbcMetadataSourceProvider(
69                  String urlOption,
70                  String driverOption,
71                  String usernameOption,
72                  String passwordOption,
73                  String schemaOption)
74              throws ConfigurationException
75      {
76          if (urlOption == null)
77          {
78              throw new ConfigurationException(
79                      "JdbcMetadataSourceProvider: urlOption must not be null");
80          }
81          if (driverOption == null)
82          {
83              throw new ConfigurationException(
84                      "JdbcMetadataSourceProvider:"
85                      + " driverOption must not be null");
86          }
87          this.urlOption = urlOption;
88          this.driverOption = driverOption;
89          this.usernameOption = usernameOption;
90          this.passwordOption = passwordOption;
91          this.schemaOption = schemaOption;
92      }
93  
94  
95      /**
96       * Generates an XML database schema from JDBC metadata.
97       *
98       * @throws Exception a generic exception.
99       */
100     public void initInternal(
101                  ConfigurationHandlers configurationHandlers,
102                  ControllerState controllerState)
103             throws ConfigurationException
104     {
105         driver = controllerState.getStringOption(driverOption);
106         url = controllerState.getStringOption(urlOption);
107         if (usernameOption != null)
108         {
109             username = controllerState.getStringOption(usernameOption);
110         }
111         if (passwordOption != null)
112         {
113             password = controllerState.getStringOption(passwordOption);
114         }
115         if (schemaOption != null)
116         {
117             schema = controllerState.getStringOption(schemaOption);
118         }
119     }
120 
121     public boolean hasNext()
122     {
123         return !nextCalled;
124     }
125 
126     public Source next()
127     {
128         if (nextCalled)
129         {
130             throw new IndexOutOfBoundsException();
131         }
132         nextCalled = true;
133         return new JdbcMetadataSource(driver, url, username, password, schema);
134     }
135 
136 
137     protected void resetInternal(
138                 ConfigurationHandlers configurationHandlers,
139                 ControllerState controllerState)
140             throws ConfigurationException
141     {
142         driver = null;
143         url = null;
144         username = null;
145         password = null;
146         schema = null;
147         nextCalled = false;
148     }
149 
150     public void remove()
151     {
152         throw new UnsupportedOperationException();
153     }
154 
155 
156     public String getUrlOption()
157     {
158         return urlOption;
159     }
160 
161     public String getDriverOption()
162     {
163         return driverOption;
164     }
165 
166     public String getUsernameOption()
167     {
168         return usernameOption;
169     }
170 
171     public String getPasswordOption()
172     {
173         return passwordOption;
174     }
175 
176     public String getSchemaOption()
177     {
178         return schemaOption;
179     }
180 
181     public String getUrl()
182     {
183         return url;
184     }
185 
186     public String getDriver()
187     {
188         return driver;
189     }
190 
191     public String getUsername()
192     {
193         return username;
194     }
195 
196     public String getPassword()
197     {
198         return password;
199     }
200 
201     public String getSchema()
202     {
203         return schema;
204     }
205 
206     /**
207      * {@inheritDoc}
208      */
209     public SourceProvider copy() throws ConfigurationException
210     {
211         JdbcMetadataSourceProvider result = new JdbcMetadataSourceProvider(
212                 urlOption,
213                 driverOption,
214                 usernameOption,
215                 passwordOption,
216                 schemaOption);
217         return result;
218     }
219 
220     /**
221      * {@inheritDoc}
222      */
223     public void copyNotSetSettingsFrom(SourceProvider sourceProvider)
224     {
225         // do nothing.
226     }
227 
228 }