KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derby > impl > tools > dblook > DB_Jar


1 /*
2
3    Derby - Class org.apache.derby.impl.tools.dblook.DB_Jar
4
5    Licensed to the Apache Software Foundation (ASF) under one or more
6    contributor license agreements. See the NOTICE file distributed with
7    this work for additional information regarding copyright ownership.
8    The ASF licenses this file to you under the Apache License, Version 2.0
9    (the "License"); you may not use this file except in compliance with
10    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, software
15    distributed under the License is distributed on an "AS IS" BASIS,
16    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17    See the License for the specific language governing permissions and
18    limitations under the License.
19
20  */

21
22 package org.apache.derby.impl.tools.dblook;
23
24 import java.sql.Connection JavaDoc;
25 import java.sql.Statement JavaDoc;
26 import java.sql.PreparedStatement JavaDoc;
27 import java.sql.ResultSet JavaDoc;
28 import java.sql.SQLException JavaDoc;
29
30 import java.util.HashMap JavaDoc;
31
32 import java.io.File JavaDoc;
33 import java.io.FileOutputStream JavaDoc;
34 import java.io.FileInputStream JavaDoc;
35 import java.io.FileNotFoundException JavaDoc;
36 import java.io.IOException JavaDoc;
37
38 import org.apache.derby.tools.dblook;
39
40 public class DB_Jar {
41
42     /* ************************************************
43      * Generate the DDL for all jars in a given
44      * database.
45      * @param dbName Name of the database (for locating the jar).
46      * @param conn Connection to the source database.
47      * @return The DDL for the jars has been written
48      * to output via Logs.java.
49      ****/

50
51     public static void doJars(String JavaDoc dbName, Connection JavaDoc conn)
52         throws SQLException JavaDoc
53     {
54
55         String JavaDoc separator = System.getProperty("file.separator");
56         Statement JavaDoc stmt = conn.createStatement();
57         ResultSet JavaDoc rs = stmt.executeQuery("SELECT FILENAME, SCHEMAID, " +
58             "GENERATIONID FROM SYS.SYSFILES");
59
60         boolean firstTime = true;
61         while (rs.next()) {
62
63             String JavaDoc jarName = dblook.addQuotes(
64                 dblook.expandDoubleQuotes(rs.getString(1)));
65             String JavaDoc schemaId = rs.getString(2);
66             String JavaDoc schemaName = dblook.lookupSchemaId(schemaId);
67             if (dblook.isIgnorableSchema(schemaName))
68                 continue;
69
70             if (firstTime) {
71                 Logs.reportString("----------------------------------------------");
72                 Logs.reportMessage("DBLOOK_JarsHeader");
73                 Logs.reportMessage("DBLOOK_Jar_Note");
74                 Logs.reportString("----------------------------------------------\n");
75             }
76
77             String JavaDoc genID = rs.getString(3);
78
79             String JavaDoc schemaWithoutQuotes = dblook.stripQuotes(schemaName);
80             StringBuffer JavaDoc jarFullName = new StringBuffer JavaDoc(separator);
81             jarFullName.append(dblook.stripQuotes(jarName));
82             jarFullName.append(".jar.G");
83             jarFullName.append(genID);
84
85             StringBuffer JavaDoc oldJarPath = new StringBuffer JavaDoc();
86             oldJarPath.append(dbName);
87             oldJarPath.append(separator);
88             oldJarPath.append("jar");
89             oldJarPath.append(separator);
90             oldJarPath.append(schemaWithoutQuotes);
91             oldJarPath.append(jarFullName);
92
93             // Copy jar file to DBJARS directory.
94
String JavaDoc absJarDir = null;
95             try {
96
97                 // Create the DBJARS directory.
98
File JavaDoc jarDir = new File JavaDoc(System.getProperty("user.dir") +
99                     separator + "DBJARS" + separator + schemaWithoutQuotes);
100                 absJarDir = jarDir.getAbsolutePath();
101                 jarDir.mkdirs();
102
103                 // Create streams.
104
FileInputStream JavaDoc oldJarFile =
105                     new FileInputStream JavaDoc(oldJarPath.toString());
106                 FileOutputStream JavaDoc newJarFile =
107                     new FileOutputStream JavaDoc(absJarDir + jarFullName);
108
109                 // Copy.
110
int st = 0;
111                 while (true) {
112                     if (oldJarFile.available() == 0)
113                         break;
114                     byte[] bAr = new byte[oldJarFile.available()];
115                     oldJarFile.read(bAr);
116                     newJarFile.write(bAr);
117                 }
118
119                 newJarFile.close();
120                 oldJarFile.close();
121
122             } catch (Exception JavaDoc e) {
123                 Logs.debug("DBLOOK_FailedToLoadJar",
124                     absJarDir + jarFullName.toString());
125                 Logs.debug(e);
126                 firstTime = false;
127                 continue;
128             }
129
130             // Now, add the DDL to read the jar from DBJARS.
131
StringBuffer JavaDoc loadJarString = new StringBuffer JavaDoc();
132             loadJarString.append("CALL SQLJ.INSTALL_JAR('file:");
133             loadJarString.append(absJarDir);
134             loadJarString.append(jarFullName);
135             loadJarString.append("', '");
136             loadJarString.append(schemaName);
137             loadJarString.append(".");
138             loadJarString.append(jarName);
139             loadJarString.append("', 0)");
140
141             Logs.writeToNewDDL(loadJarString.toString());
142             Logs.writeStmtEndToNewDDL();
143             Logs.writeNewlineToNewDDL();
144             firstTime = false;
145
146         }
147
148         stmt.close();
149         rs.close();
150
151     }
152
153 }
154
Popular Tags