001: /*
002: * Copyright 2006-2007 Pentaho Corporation. All rights reserved.
003: * This software was developed by Pentaho Corporation and is provided under the terms
004: * of the Mozilla Public License, Version 1.1, or any later version. You may not use
005: * this file except in compliance with the license. If you need a copy of the license,
006: * please go to http://www.mozilla.org/MPL/MPL-1.1.txt.
007: *
008: * Software distributed under the Mozilla Public License is distributed on an "AS IS"
009: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. Please refer to
010: * the license for the specific language governing your rights and limitations.
011: *
012: * Additional Contributor(s): Martin Schmid gridvision engineering GmbH
013: */
014: package org.pentaho.reportdesigner.crm.report.datasetplugin.jdbc;
015:
016: import org.jetbrains.annotations.NonNls;
017: import org.jetbrains.annotations.NotNull;
018: import org.pentaho.reportdesigner.lib.client.util.UncaughtExcpetionsModel;
019:
020: import java.io.File;
021: import java.io.IOException;
022: import java.net.MalformedURLException;
023: import java.net.URI;
024: import java.net.URISyntaxException;
025: import java.net.URL;
026: import java.net.URLClassLoader;
027: import java.sql.Driver;
028: import java.util.ArrayList;
029: import java.util.Enumeration;
030: import java.util.jar.JarEntry;
031: import java.util.jar.JarFile;
032: import java.util.logging.Level;
033: import java.util.logging.Logger;
034:
035: /**
036: * User: Martin
037: * Date: 23.09.2006
038: * Time: 09:16:05
039: */
040: public class ClasspathSearcher {
041: @NotNull
042: @NonNls
043: private static final Logger LOG = Logger
044: .getLogger(ClasspathSearcher.class.getName());
045:
046: @NotNull
047: private static final String[] EMPTY_STRING_ARRAY = new String[0];
048:
049: @NotNull
050: private static ClasspathSearcher instance = new ClasspathSearcher();
051:
052: @NotNull
053: public static ClasspathSearcher getInstance() {
054: return instance;
055: }
056:
057: @NotNull
058: private String[] filePaths;
059: @NotNull
060: private String[] drivers;
061:
062: private ClasspathSearcher() {
063: filePaths = EMPTY_STRING_ARRAY;
064:
065: try {
066: URL url = getClass().getResource(
067: "/res/icons/ReportFrameIcon.png");//NON-NLS
068: String urlStr = url.toString();
069: if (LOG.isLoggable(Level.FINE))
070: LOG.log(Level.FINE,
071: "FindTranslationsCommand.execute urlStr = "
072: + urlStr);
073: if (urlStr.startsWith("jar:"))//NON-NLS
074: {
075: //built version
076: int from = "jar:".length();//NON-NLS
077: int to = urlStr.indexOf("/lib/ReportDesigner.jar!/");//NON-NLS
078: String libFolderName = urlStr.substring(from, to)
079: + "/lib/";//NON-NLS
080: if (LOG.isLoggable(Level.FINE))
081: LOG.log(Level.FINE,
082: "FindTranslationsCommand.execute libFolderName = "
083: + libFolderName);
084:
085: URI libFolderURI = new URI(libFolderName);
086:
087: //scan lib folder
088: File libFolder = new File(new File(libFolderURI),
089: "jdbc");//NON-NLS
090: File[] files = libFolder.listFiles();
091: ArrayList<String> paths = new ArrayList<String>();
092: if (files != null) {
093: for (File file : files) {
094: paths.add(file.getPath());
095: }
096: }
097: filePaths = paths.toArray(new String[paths.size()]);
098: } else if (urlStr.startsWith("file")
099: && urlStr.contains("classes"))//NON-NLS
100: {
101: int to = urlStr
102: .indexOf("/res/icons/ReportFrameIcon.png");//NON-NLS
103: String resFolderName = urlStr.substring(0, to)
104: + "/../../../lib/jdbc/";//NON-NLS
105: File resFolder = new File(new URI(resFolderName));
106: File[] files = resFolder.listFiles();
107: ArrayList<String> paths = new ArrayList<String>();
108: if (files != null) {
109: for (File file : files) {
110: paths.add(file.getPath());
111: }
112: }
113: filePaths = paths.toArray(new String[paths.size()]);
114: } else {
115: //devel version
116: //
117: // MB - Fix problem of not being able to run from Eclipse because the
118: // output folder for classes in the Eclipse project is
119: // bin, not classes. This change should allow both situations
120: // to work.
121: int to = urlStr
122: .indexOf("/res/icons/ReportFrameIcon.png");//NON-NLS
123: String resFolderName = urlStr.substring(0, to)
124: + "/../lib/jdbc/";//NON-NLS
125: File resFolder = new File(new URI(resFolderName));
126: File[] files = resFolder.listFiles();
127: ArrayList<String> paths = new ArrayList<String>();
128: if (files != null) {
129: for (File file : files) {
130: paths.add(file.getPath());
131: }
132: }
133: filePaths = paths.toArray(new String[paths.size()]);
134: }
135: } catch (URISyntaxException e) {
136: if (LOG.isLoggable(Level.FINE))
137: LOG.log(Level.FINE, "TranslationUtil.findLocales ", e);
138: } catch (Throwable throwable) {
139: UncaughtExcpetionsModel.getInstance().addException(
140: throwable);
141: }
142:
143: drivers = findDrivers();
144: }
145:
146: @NotNull
147: public String[] getFilePaths() {
148: return filePaths;
149: }
150:
151: @NotNull
152: public String[] getDrivers() {
153: return drivers;
154: }
155:
156: @NotNull
157: private String[] findDrivers() {
158: ArrayList<URL> urls = new ArrayList<URL>();
159: for (String jar : filePaths) {
160: try {
161: urls.add(new File(jar).toURI().toURL());
162: } catch (MalformedURLException e) {
163: if (LOG.isLoggable(Level.FINE))
164: LOG.log(Level.FINE,
165: "ClasspathSearcher.findDrivers ", e);
166: }
167: }
168:
169: URLClassLoader urlClassLoader = new URLClassLoader(urls
170: .toArray(new URL[urls.size()]));
171:
172: ArrayList<String> driverClassNames = new ArrayList<String>();
173: for (String filePath1 : filePaths) {
174: try {
175: JarFile jarFile = new JarFile(new File(filePath1));
176: Enumeration<JarEntry> enumeration = jarFile.entries();
177: while (enumeration.hasMoreElements()) {
178: JarEntry jarEntry = enumeration.nextElement();
179: String name = jarEntry.getName();
180: if (!jarEntry.isDirectory()
181: && name.endsWith(".class"))//NON-NLS
182: {
183: String className = name.substring(0,
184: name.length() - 6).replace('/', '.');
185: try {
186: Class<?> aClass = Class.forName(className,
187: false, urlClassLoader);
188: if (Driver.class.isAssignableFrom(aClass)) {
189: driverClassNames.add(className);
190: if (LOG.isLoggable(Level.FINE))
191: LOG.log(Level.FINE,
192: "ClasspathSearcher.findDrivers aClass = "
193: + aClass);
194: }
195: } catch (Throwable e) {
196: if (LOG.isLoggable(Level.FINE))
197: LOG
198: .log(
199: Level.FINE,
200: "ClasspathSearcher.findDrivers ",
201: e);
202: }
203: }
204: }
205: } catch (IOException e) {
206: if (LOG.isLoggable(Level.FINE))
207: LOG.log(Level.FINE,
208: "ClasspathSearcher.findDrivers ", e);
209: }
210: }
211: return driverClassNames.toArray(new String[driverClassNames
212: .size()]);
213: }
214: }
|