KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > pentaho > core > admin > datasources > DatasourceAdminBase


1 /*
2  * Copyright 2006 Pentaho Corporation. All rights reserved.
3  * This software was developed by Pentaho Corporation and is provided under the terms
4  * of the Mozilla Public License, Version 1.1, or any later version. You may not use
5  * this file except in compliance with the license. If you need a copy of the license,
6  * please go to http://www.mozilla.org/MPL/MPL-1.1.txt. The Original Code is the Pentaho
7  * BI Platform. The Initial Developer is Pentaho Corporation.
8  *
9  * Software distributed under the Mozilla Public License is distributed on an "AS IS"
10  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. Please refer to
11  * the license for the specific language governing your rights and limitations.
12  *
13  * @created Dec 30, 2005
14  * @author James Dixon
15  */

16 package org.pentaho.core.admin.datasources;
17
18 import java.sql.Connection JavaDoc;
19 import java.sql.Driver JavaDoc;
20 import java.sql.DriverManager JavaDoc;
21 import java.util.Properties JavaDoc;
22 import org.pentaho.messages.Messages;
23
24 public abstract class DatasourceAdminBase implements IDatasourceAdmin {
25   public String JavaDoc testDataSource(String JavaDoc url, String JavaDoc driverName, String JavaDoc user, String JavaDoc password) {
26     try {
27       Driver JavaDoc driver = null;
28       try {
29         driver = DriverManager.getDriver(url);
30       } catch (Exception JavaDoc e) {
31         // if we don't find this connection, it isn't registered, so we'll try to find it on the classpath
32
}
33       if (driver == null) {
34         Class JavaDoc driverClass = Class.forName(driverName);
35         driver = (Driver JavaDoc) driverClass.newInstance();
36         DriverManager.registerDriver(driver);
37       }
38       Properties JavaDoc info = new Properties JavaDoc();
39       info.put("user", user); //$NON-NLS-1$
40
info.put("password", password); //$NON-NLS-1$
41
Connection JavaDoc connection = driver.connect(url, info);
42       connection.close();
43       return Messages.getString("DSAdmin.USER_CONNECTION_SUCCESS"); //$NON-NLS-1$
44
} catch (Throwable JavaDoc t) {
45       return Messages.getString("DSAdmin.USER_CONNECTION_FAILED", t.getMessage()); //$NON-NLS-1$
46
}
47   }
48
49   public String JavaDoc testDataSource(DataSourceInfo dsInfo) {
50     if (dsInfo == null) {
51       return Messages.getString("DSAdmin.USER_DATASOURCE_NOT_FOUND"); //$NON-NLS-1$
52
}
53     return testDataSource(dsInfo.getUrl(), dsInfo.getDriver(), dsInfo.getUserId(), dsInfo.getPassword());
54   }
55
56   public String JavaDoc testDataSource(String JavaDoc dsName) {
57     DataSourceInfo dsInfo = getDataSourceInfo(dsName);
58     return testDataSource(dsInfo);
59   }
60 }
61
Popular Tags