1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.tools;
24  
25  import com.liferay.portal.kernel.io.unsync.UnsyncBufferedReader;
26  import com.liferay.portal.kernel.io.unsync.UnsyncByteArrayInputStream;
27  import com.liferay.portal.kernel.io.unsync.UnsyncByteArrayOutputStream;
28  import com.liferay.portal.kernel.io.unsync.UnsyncStringReader;
29  import com.liferay.portal.kernel.util.StringPool;
30  import com.liferay.portal.kernel.util.StringUtil;
31  import com.liferay.portal.kernel.util.Validator;
32  import com.liferay.portal.util.FileImpl;
33  
34  import java.sql.Connection;
35  import java.sql.DriverManager;
36  import java.sql.PreparedStatement;
37  import java.sql.Statement;
38  
39  import org.apache.derby.tools.ij;
40  
41  /**
42   * <a href="DBLoader.java.html"><b><i>View Source</i></b></a>
43   *
44   * @author Brian Wing Shun Chan
45   */
46  public class DBLoader {
47  
48      public static void main(String[] args) {
49          if (args.length == 2) {
50              new DBLoader(args[0], args[1], StringPool.BLANK);
51          }
52          else if (args.length == 3) {
53              new DBLoader(args[0], args[1], args[2]);
54          }
55          else {
56              throw new IllegalArgumentException();
57          }
58      }
59  
60      public DBLoader(String databaseType, String databaseName, String fileName) {
61          try {
62              _databaseType = databaseType;
63              _databaseName = databaseName;
64              _fileName = fileName;
65  
66              if (_databaseType.equals("derby")) {
67                  _loadDerby();
68              }
69              else if (_databaseType.equals("hypersonic")) {
70                  _loadHypersonic();
71              }
72          }
73          catch (Exception e) {
74              e.printStackTrace();
75          }
76      }
77  
78      private void _loadDerby() throws Exception {
79          Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
80  
81          Connection con = DriverManager.getConnection(
82              "jdbc:derby:" + _databaseName + ";create=true", "", "");
83  
84          if (Validator.isNull(_fileName)) {
85              _loadDerby(con, "../sql/portal/portal-derby.sql");
86              _loadDerby(con, "../sql/indexes.sql");
87          }
88          else {
89              _loadDerby(con, _fileName);
90          }
91      }
92  
93      private void _loadDerby(Connection con, String fileName)
94          throws Exception {
95  
96          StringBuilder sb = new StringBuilder();
97  
98          UnsyncBufferedReader unsyncBufferedReader = new UnsyncBufferedReader(
99              new UnsyncStringReader(_fileUtil.read(fileName)));
100 
101         String line = null;
102 
103         while ((line = unsyncBufferedReader.readLine()) != null) {
104             if (!line.startsWith("--")) {
105                 sb.append(line);
106 
107                 if (line.endsWith(";")) {
108                     String sql = sb.toString();
109 
110                     sql =
111                         StringUtil.replace(
112                             sql,
113                             new String[] {
114                                 "\\'",
115                                 "\\\"",
116                                 "\\\\",
117                                 "\\n",
118                                 "\\r"
119                             },
120                             new String[] {
121                                 "''",
122                                 "\"",
123                                 "\\",
124                                 "\n",
125                                 "\r"
126                             });
127 
128                     sql = sql.substring(0, sql.length() - 1);
129 
130                     sb = new StringBuilder();
131 
132                     if (sql.startsWith("commit")) {
133                         continue;
134                     }
135 
136                     ij.runScript(
137                         con,
138                         new UnsyncByteArrayInputStream(
139                             sql.getBytes(StringPool.UTF8)),
140                         StringPool.UTF8, new UnsyncByteArrayOutputStream(),
141                         StringPool.UTF8);
142                 }
143             }
144         }
145 
146         unsyncBufferedReader.close();
147     }
148 
149     private void _loadHypersonic() throws Exception {
150         Class.forName("org.hsqldb.jdbcDriver");
151 
152         // See LEP-2927. Appending ;shutdown=true to the database connection URL
153         // guarantees that ${_databaseName}.log is purged.
154 
155         Connection con = DriverManager.getConnection(
156             "jdbc:hsqldb:" + _databaseName + ";shutdown=true", "sa", "");
157 
158         if (Validator.isNull(_fileName)) {
159             _loadHypersonic(con, "../sql/portal/portal-hypersonic.sql");
160             _loadHypersonic(con, "../sql/indexes.sql");
161         }
162         else {
163             _loadHypersonic(con, _fileName);
164         }
165 
166         // Shutdown Hypersonic
167 
168         Statement statement = con.createStatement();
169 
170         statement.execute("SHUTDOWN COMPACT");
171 
172         statement.close();
173 
174         con.close();
175 
176         // Hypersonic will encode unicode characters twice, this will undo
177         // it
178 
179         String content = _fileUtil.read(_databaseName + ".script");
180 
181         content = StringUtil.replace(content, "\\u005cu", "\\u");
182 
183         _fileUtil.write(_databaseName + ".script", content);
184     }
185 
186     private void _loadHypersonic(Connection con, String fileName)
187         throws Exception {
188 
189         StringBuilder sb = new StringBuilder();
190 
191         UnsyncBufferedReader unsyncBufferedReader = new UnsyncBufferedReader(
192             new UnsyncStringReader(_fileUtil.read(fileName)));
193 
194         String line = null;
195 
196         while ((line = unsyncBufferedReader.readLine()) != null) {
197             if (!line.startsWith("//")) {
198                 sb.append(line);
199 
200                 if (line.endsWith(";")) {
201                     String sql = sb.toString();
202 
203                     sql =
204                         StringUtil.replace(
205                             sql,
206                             new String[] {
207                                 "\\\"",
208                                 "\\\\",
209                                 "\\n",
210                                 "\\r"
211                             },
212                             new String[] {
213                                 "\"",
214                                 "\\",
215                                 "\\u000a",
216                                 "\\u000a"
217                             });
218 
219                     sb = new StringBuilder();
220 
221                     PreparedStatement ps = con.prepareStatement(sql);
222 
223                     ps.executeUpdate();
224 
225                     ps.close();
226                 }
227             }
228         }
229 
230         unsyncBufferedReader.close();
231     }
232 
233     private static FileImpl _fileUtil = FileImpl.getInstance();
234 
235     private String _databaseType;
236     private String _databaseName;
237     private String _fileName;
238 
239 }