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.util.StringPool;
26  import com.liferay.portal.kernel.util.StringUtil;
27  import com.liferay.portal.kernel.util.Validator;
28  import com.liferay.portal.util.FileImpl;
29  
30  import java.io.BufferedReader;
31  import java.io.ByteArrayInputStream;
32  import java.io.ByteArrayOutputStream;
33  import java.io.StringReader;
34  
35  import java.sql.Connection;
36  import java.sql.DriverManager;
37  import java.sql.PreparedStatement;
38  import java.sql.Statement;
39  
40  import org.apache.derby.tools.ij;
41  
42  /**
43   * <a href="DBLoader.java.html"><b><i>View Source</i></b></a>
44   *
45   * @author Brian Wing Shun Chan
46   */
47  public class DBLoader {
48  
49      public static void main(String[] args) {
50          if (args.length == 2) {
51              new DBLoader(args[0], args[1], StringPool.BLANK);
52          }
53          else if (args.length == 3) {
54              new DBLoader(args[0], args[1], args[2]);
55          }
56          else {
57              throw new IllegalArgumentException();
58          }
59      }
60  
61      public DBLoader(String databaseType, String databaseName, String fileName) {
62          try {
63              _databaseType = databaseType;
64              _databaseName = databaseName;
65              _fileName = fileName;
66  
67              if (_databaseType.equals("derby")) {
68                  _loadDerby();
69              }
70              else if (_databaseType.equals("hypersonic")) {
71                  _loadHypersonic();
72              }
73          }
74          catch (Exception e) {
75              e.printStackTrace();
76          }
77      }
78  
79      private void _loadDerby() throws Exception {
80          Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
81  
82          Connection con = DriverManager.getConnection(
83              "jdbc:derby:" + _databaseName + ";create=true", "", "");
84  
85          if (Validator.isNull(_fileName)) {
86              _loadDerby(con, "../sql/portal/portal-derby.sql");
87              _loadDerby(con, "../sql/indexes.sql");
88          }
89          else {
90              _loadDerby(con, _fileName);
91          }
92      }
93  
94      private void _loadDerby(Connection con, String fileName)
95          throws Exception {
96  
97          StringBuilder sb = new StringBuilder();
98  
99          BufferedReader br = new BufferedReader(
100             new StringReader(_fileUtil.read(fileName)));
101 
102         String line = null;
103 
104         while ((line = br.readLine()) != null) {
105             if (!line.startsWith("--")) {
106                 sb.append(line);
107 
108                 if (line.endsWith(";")) {
109                     String sql = sb.toString();
110 
111                     sql =
112                         StringUtil.replace(
113                             sql,
114                             new String[] {
115                                 "\\'",
116                                 "\\\"",
117                                 "\\\\",
118                                 "\\n",
119                                 "\\r"
120                             },
121                             new String[] {
122                                 "''",
123                                 "\"",
124                                 "\\",
125                                 "\n",
126                                 "\r"
127                             });
128 
129                     sql = sql.substring(0, sql.length() - 1);
130 
131                     sb = new StringBuilder();
132 
133                     if (sql.startsWith("commit")) {
134                         continue;
135                     }
136 
137                     ij.runScript(
138                         con,
139                         new ByteArrayInputStream(sql.getBytes(StringPool.UTF8)),
140                         StringPool.UTF8, new ByteArrayOutputStream(),
141                         StringPool.UTF8);
142                 }
143             }
144         }
145 
146         br.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         BufferedReader br = new BufferedReader(
192             new StringReader(_fileUtil.read(fileName)));
193 
194         String line = null;
195 
196         while ((line = br.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         br.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 }