001: /*
002:
003: Derby - Class org.apache.derby.impl.tools.dblook.DB_Jar
004:
005: Licensed to the Apache Software Foundation (ASF) under one or more
006: contributor license agreements. See the NOTICE file distributed with
007: this work for additional information regarding copyright ownership.
008: The ASF licenses this file to you under the Apache License, Version 2.0
009: (the "License"); you may not use this file except in compliance with
010: the License. You may obtain a copy of the License at
011:
012: http://www.apache.org/licenses/LICENSE-2.0
013:
014: Unless required by applicable law or agreed to in writing, software
015: distributed under the License is distributed on an "AS IS" BASIS,
016: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: See the License for the specific language governing permissions and
018: limitations under the License.
019:
020: */
021:
022: package org.apache.derby.impl.tools.dblook;
023:
024: import java.sql.Connection;
025: import java.sql.Statement;
026: import java.sql.PreparedStatement;
027: import java.sql.ResultSet;
028: import java.sql.SQLException;
029:
030: import java.util.HashMap;
031:
032: import java.io.File;
033: import java.io.FileOutputStream;
034: import java.io.FileInputStream;
035: import java.io.FileNotFoundException;
036: import java.io.IOException;
037:
038: import org.apache.derby.tools.dblook;
039:
040: public class DB_Jar {
041:
042: /* ************************************************
043: * Generate the DDL for all jars in a given
044: * database.
045: * @param dbName Name of the database (for locating the jar).
046: * @param conn Connection to the source database.
047: * @return The DDL for the jars has been written
048: * to output via Logs.java.
049: ****/
050:
051: public static void doJars(String dbName, Connection conn)
052: throws SQLException {
053:
054: String separator = System.getProperty("file.separator");
055: Statement stmt = conn.createStatement();
056: ResultSet rs = stmt.executeQuery("SELECT FILENAME, SCHEMAID, "
057: + "GENERATIONID FROM SYS.SYSFILES");
058:
059: boolean firstTime = true;
060: while (rs.next()) {
061:
062: String jarName = dblook.addQuotes(dblook
063: .expandDoubleQuotes(rs.getString(1)));
064: String schemaId = rs.getString(2);
065: String schemaName = dblook.lookupSchemaId(schemaId);
066: if (dblook.isIgnorableSchema(schemaName))
067: continue;
068:
069: if (firstTime) {
070: Logs
071: .reportString("----------------------------------------------");
072: Logs.reportMessage("DBLOOK_JarsHeader");
073: Logs.reportMessage("DBLOOK_Jar_Note");
074: Logs
075: .reportString("----------------------------------------------\n");
076: }
077:
078: String genID = rs.getString(3);
079:
080: String schemaWithoutQuotes = dblook.stripQuotes(schemaName);
081: StringBuffer jarFullName = new StringBuffer(separator);
082: jarFullName.append(dblook.stripQuotes(jarName));
083: jarFullName.append(".jar.G");
084: jarFullName.append(genID);
085:
086: StringBuffer oldJarPath = new StringBuffer();
087: oldJarPath.append(dbName);
088: oldJarPath.append(separator);
089: oldJarPath.append("jar");
090: oldJarPath.append(separator);
091: oldJarPath.append(schemaWithoutQuotes);
092: oldJarPath.append(jarFullName);
093:
094: // Copy jar file to DBJARS directory.
095: String absJarDir = null;
096: try {
097:
098: // Create the DBJARS directory.
099: File jarDir = new File(System.getProperty("user.dir")
100: + separator + "DBJARS" + separator
101: + schemaWithoutQuotes);
102: absJarDir = jarDir.getAbsolutePath();
103: jarDir.mkdirs();
104:
105: // Create streams.
106: FileInputStream oldJarFile = new FileInputStream(
107: oldJarPath.toString());
108: FileOutputStream newJarFile = new FileOutputStream(
109: absJarDir + jarFullName);
110:
111: // Copy.
112: int st = 0;
113: while (true) {
114: if (oldJarFile.available() == 0)
115: break;
116: byte[] bAr = new byte[oldJarFile.available()];
117: oldJarFile.read(bAr);
118: newJarFile.write(bAr);
119: }
120:
121: newJarFile.close();
122: oldJarFile.close();
123:
124: } catch (Exception e) {
125: Logs.debug("DBLOOK_FailedToLoadJar", absJarDir
126: + jarFullName.toString());
127: Logs.debug(e);
128: firstTime = false;
129: continue;
130: }
131:
132: // Now, add the DDL to read the jar from DBJARS.
133: StringBuffer loadJarString = new StringBuffer();
134: loadJarString.append("CALL SQLJ.INSTALL_JAR('file:");
135: loadJarString.append(absJarDir);
136: loadJarString.append(jarFullName);
137: loadJarString.append("', '");
138: loadJarString.append(schemaName);
139: loadJarString.append(".");
140: loadJarString.append(jarName);
141: loadJarString.append("', 0)");
142:
143: Logs.writeToNewDDL(loadJarString.toString());
144: Logs.writeStmtEndToNewDDL();
145: Logs.writeNewlineToNewDDL();
146: firstTime = false;
147:
148: }
149:
150: stmt.close();
151: rs.close();
152:
153: }
154:
155: }
|