001: /*
002: * The contents of this file are subject to the Mozilla Public License
003: * Version 1.1 (the "License"); you may not use this file except in
004: * compliance with the License. You may obtain a copy of the License at
005: * http://www.mozilla.org/MPL/
006: *
007: * Software distributed under the License is distributed on an "AS IS"
008: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
009: * License for the specific language governing rights and limitations
010: * under the License.
011: *
012: * The Original Code is iSQL-Viewer, A Mutli-Platform Database Tool.
013: *
014: * The Initial Developer of the Original Code is iSQL-Viewer, A Mutli-Platform Database Tool.
015: * Portions created by Mark A. Kobold are Copyright (C) 2000-2007. All Rights Reserved.
016: *
017: * Contributor(s):
018: * Mark A. Kobold [mkobold <at> isqlviewer <dot> com].
019: *
020: * If you didn't download this code from the following link, you should check
021: * if you aren't using an obsolete version: http://www.isqlviewer.com
022: */
023: package org.isqlviewer.sql.embedded;
024:
025: import java.io.BufferedReader;
026: import java.io.File;
027: import java.io.FileInputStream;
028: import java.io.FileNotFoundException;
029: import java.io.FileReader;
030: import java.io.IOException;
031: import java.io.InputStream;
032: import java.net.MalformedURLException;
033: import java.net.URL;
034: import java.util.ArrayList;
035: import java.util.Collection;
036: import java.util.Date;
037: import java.util.HashMap;
038: import java.util.Map;
039: import java.util.Set;
040: import java.util.StringTokenizer;
041:
042: import org.apache.log4j.Logger;
043: import org.isqlviewer.ServiceReference;
044: import org.isqlviewer.bookmarks.BookmarkFolder;
045: import org.isqlviewer.history.CommandType;
046: import org.isqlviewer.history.HistoricalCommand;
047: import org.isqlviewer.util.IsqlToolkit;
048: import org.isqlviewer.util.LocalMessages;
049: import org.isqlviewer.util.StringUtilities;
050: import org.isqlviewer.xml.BookmarkDigester;
051: import org.isqlviewer.xml.ServiceDigester;
052: import org.xml.sax.InputSource;
053:
054: /**
055: * Class for providing backward compatability to previous versions of ISQL-Viewer resources.
056: * <p>
057: *
058: * @author Mark A. Kobold <mkobold at isqlviewer dot com>
059: * @version 1.0
060: */
061: public class CompatabilityKit {
062:
063: private static final String CACHE_FILE = "services.cache";
064: private static final String KEY_SERVICE_LIST = "SERVICE_ORDER";
065: private static final String SERVICE_SEPERATOR = "\u0001";
066: private static final String HISTORY_FIELD_SEPERATOR = "\u0006";
067: private static final char SERVICE_FIELD_SEPERATOR = '=';
068: private static final String BUNDLE_NAME = "org.isqlviewer.sql.embedded.ResourceBundle";
069:
070: private static LocalMessages messages = new LocalMessages(
071: BUNDLE_NAME);
072: private static Logger logger = IsqlToolkit.getApplicationLogger();
073:
074: /**
075: * Gets all the bookmarks from the bookmarks.xml file from the iSQL-Viewer base directory.
076: * <p>
077: *
078: * @return root bookmark folder containing all bookmarks and sub-folders.
079: */
080: public static BookmarkFolder get2xxBookmarks() {
081:
082: File bookmarksFile = new File(IsqlToolkit.getBaseDirectory(),
083: "bookmarks.xml");
084: BookmarkFolder rootFolder = null;
085: if (bookmarksFile.exists()) {
086: FileInputStream fis = null;
087: try {
088: fis = new FileInputStream(bookmarksFile);
089: InputSource source = new InputSource(fis);
090: rootFolder = BookmarkDigester.parseBookmarks(source);
091: } catch (FileNotFoundException error) {
092: logger.warn(messages
093: .format("compatability_kit.no_bookmarks_xml"));
094: } catch (Exception error) {
095: logger
096: .error(
097: messages
098: .format("compatability_kit.generic_bookmark_error"),
099: error);
100: } finally {
101: if (fis != null) {
102: try {
103: fis.close();
104: } catch (Exception ignored) {
105: }
106: }
107: if (rootFolder == null) {
108: rootFolder = BookmarkFolder.createRootFolder();
109: }
110: }
111: }
112: return rootFolder;
113: }
114:
115: /**
116: * Loads all service definitions defined in the services.cache file in the iSQL-Viewer base directory.
117: * <p>
118: *
119: * @return all existing service definitions declared within the old caches file.
120: */
121: public static Collection<ServiceReference> get2xxServices() {
122:
123: ArrayList<ServiceReference> references = new ArrayList<ServiceReference>();
124: Map<String, File> serviceFileMapping = loadServiceCache();
125: int order = 0;
126: Set<Map.Entry<String, File>> serviceSet = serviceFileMapping
127: .entrySet();
128: for (Map.Entry<String, File> entry : serviceSet) {
129: ServiceReference reference = new ServiceReference();
130: reference.setName(entry.getKey());
131: reference.setOrder(order++);
132: try {
133: reference.setResourceURL(entry.getValue().toURL());
134: } catch (MalformedURLException ignored) {
135: // we already have it checked when we call loadServiceCache()//
136: }
137: references.add(reference);
138: }
139: return references;
140: }
141:
142: /**
143: * Aquires the autoloaded history elements from previous versions of iSQL-Viewer.
144: * <p>
145: *
146: * @return collection of all history elements logged in the 'autoload.history' file.
147: */
148: public static Collection<HistoricalCommand> get2xxHistory() {
149:
150: ArrayList<HistoricalCommand> history = new ArrayList<HistoricalCommand>();
151: FileReader fileReader = null;
152: StringBuilder buffer = new StringBuilder("");
153: File baseDirectory = new File(IsqlToolkit.getBaseDirectory(),
154: "history");
155: try {
156: File historyFile = new File(baseDirectory,
157: "autoload.history");
158: if (historyFile.exists() && historyFile.isFile()) {
159: fileReader = new FileReader(historyFile);
160: BufferedReader bufferedReader = new BufferedReader(
161: fileReader);
162: while (bufferedReader.ready()) {
163: String line = bufferedReader.readLine();
164: if (line.trim().length() == 0) {
165: String content = buffer.toString();
166: history.add(decodeHistoryItem(content));
167: buffer.setLength(0);
168: continue;
169: }
170: buffer.append(line);
171: buffer.append('\n');
172: }
173: }
174: } catch (IOException ignored) {
175: } finally {
176: try {
177: if (fileReader != null) {
178: fileReader.close();
179: }
180: } catch (Exception ignored) {
181: }
182: }
183: return history;
184: }
185:
186: // Old decode code for transforming the history item to the new object.
187: private static HistoricalCommand decodeHistoryItem(String s) {
188:
189: HistoricalCommand command = new HistoricalCommand();
190: command.setType(CommandType.QUERY);
191: String b64Decoded = StringUtilities.decodeBase64(s);
192: StringTokenizer st = new StringTokenizer(b64Decoded,
193: HISTORY_FIELD_SEPERATOR);
194: long executionTime = Long.parseLong(st.nextToken());
195: command.setQueryTime(new Date(executionTime));
196: command.setService(st.nextToken());
197: command.setCommandText(st.nextToken());
198: return command;
199: }
200:
201: // Old code from service manager to load the service cache file properly.
202: private static Map<String, File> loadServiceCache() {
203:
204: ArrayList<String> preferredOrder = new ArrayList<String>();
205: HashMap<String, File> serviceFileMapping = new HashMap<String, File>();
206: File file = new File(IsqlToolkit.getBaseDirectory(), CACHE_FILE);
207: if (!file.exists()) {
208: return serviceFileMapping;
209: }
210:
211: if (file.canRead()) {
212: FileReader fileReader = null;
213: try {
214: fileReader = new FileReader(file);
215: BufferedReader lineReader = new BufferedReader(
216: fileReader);
217: while (lineReader.ready()) {
218: String data = lineReader.readLine();
219: if (data.charAt(0) == '#') {
220: continue;
221: }
222: int idx0 = 0;
223: int idx1 = data.indexOf(SERVICE_FIELD_SEPERATOR);
224:
225: String name = StringUtilities.decodeASCII(data
226: .substring(idx0, idx1));
227: String uri = StringUtilities.decodeASCII(data
228: .substring(idx1 + 1));
229: if (name.equalsIgnoreCase(KEY_SERVICE_LIST)) {
230: StringTokenizer st = new StringTokenizer(uri,
231: SERVICE_SEPERATOR);
232: while (st.hasMoreTokens()) {
233: String serviceName = st.nextToken();
234: preferredOrder.add(serviceName
235: .toLowerCase().trim());
236: }
237: continue;
238: }
239:
240: try {
241: URL url = new URL(uri);
242: File serviceFile = new File(url.getFile());
243: if (serviceFile.isDirectory()) {
244: logger
245: .warn(messages
246: .format(
247: "compatability_kit.service_mapped_to_directory",
248: name, uri));
249: continue;
250: } else if (!serviceFile.canRead()) {
251: logger
252: .warn(messages
253: .format(
254: "compatability_kit.service_not_readable",
255: name, uri));
256: continue;
257: } else if (!serviceFile.exists()) {
258: logger
259: .warn(messages
260: .format(
261: "compatability_kit.service_does_not_exist",
262: name, uri));
263: continue;
264: }
265: String bindName = name.toLowerCase().trim();
266: InputStream inputStream = null;
267: try {
268: inputStream = url.openStream();
269: InputSource inputSource = new InputSource(
270: inputStream);
271: bindName = ServiceDigester.parseService(
272: inputSource,
273: IsqlToolkit
274: .getSharedEntityResolver())
275: .getName();
276: } catch (Exception error) {
277: continue;
278: }
279: if (serviceFileMapping.put(bindName,
280: serviceFile) != null) {
281: logger
282: .warn(messages
283: .format(
284: "compatability_kit.service_duplicate_name_error",
285: name, uri));
286: }
287: } catch (MalformedURLException e) {
288: logger.error(messages.format(
289: "compatability_kit.service_uri_error",
290: name, uri), e);
291: }
292: }
293: } catch (IOException ioe) {
294: logger.error("compatability_kit.service_generic_error",
295: ioe);
296: } finally {
297: if (fileReader != null) {
298: try {
299: fileReader.close();
300: } catch (Throwable ignored) {
301: }
302: }
303: }
304: }
305: return serviceFileMapping;
306: }
307: }
|