001: /*
002: * Copyright 2006 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. The Original Code is the Pentaho
007: * BI Platform. The Initial Developer is Pentaho Corporation.
008: *
009: * Software distributed under the Mozilla Public License is distributed on an "AS IS"
010: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. Please refer to
011: * the license for the specific language governing your rights and limitations.
012: *
013: * Created Aug 29, 2005
014: * @author wseyler
015: */
016:
017: package org.pentaho.data;
018:
019: import java.lang.reflect.Constructor;
020: import java.util.Properties;
021:
022: import org.pentaho.commons.connection.IPentahoConnection;
023: import org.pentaho.core.system.PentahoSystem;
024: import org.pentaho.data.connection.mdx.MDXConnection;
025: import org.pentaho.data.connection.sql.SQLConnection;
026: import org.pentaho.data.connection.xquery.XQConnection;
027: import org.pentaho.messages.Messages;
028: import org.pentaho.util.logging.ILogger;
029: import org.pentaho.util.logging.Logger;
030:
031: /**
032: * @author wseyler
033: *
034: * TODO To change the template for this generated type comment go to Window -
035: * Preferences - Java - Code Style - Code Templates
036: */
037: public class PentahoConnectionFactory {
038:
039: /**
040: * @param datasourceType
041: * valid type is defined as SQL_DATASOURCE or MDX_DATASOURCE
042: * @param connectStr -
043: * In the case of SQL_DATASOURCE, the name of the JNDI connection
044: * to use. Or in the case of MDX_DATASOURCE a properly formatted
045: * connection String.
046: * @return a connection object that can be queried against.
047: */
048: public static IPentahoConnection getConnection(int datasourceType,
049: String connectStr, ILogger logger) {
050: /*
051: * TODO - This is where the "connection factory" action occurs. Based on
052: * if the datasourceType, location, username, or password have changed
053: * then we create a new one.
054: */
055: switch (datasourceType) {
056: case IPentahoConnection.SQL_DATASOURCE:
057: return new SQLConnection(connectStr, logger);
058: case IPentahoConnection.MDX_DATASOURCE:
059: return new MDXConnection(connectStr, logger);
060: default:
061: return null;
062: }
063: }
064:
065: public static IPentahoConnection getConnection(String propName,
066: ILogger logger) {
067: IPentahoConnection connection = null;
068:
069: String baseNode = "objects/" + propName; //$NON-NLS-1$
070: Properties props = getProperties(baseNode);
071:
072: String className = props.getProperty(
073: IPentahoConnection.CLASSNAME_KEY, null);
074: if (className == null) {
075: if (logger != null) {
076: logger
077: .error(Messages
078: .getErrorString(
079: "CONNECTFACTORY.ERROR_0001_NOT_DEFINED", propName)); //$NON-NLS-1$
080: } else {
081: Logger
082: .error(
083: PentahoConnectionFactory.class
084: .getName(),
085: Messages
086: .getErrorString(
087: "CONNECTFACTORY.ERROR_0001_NOT_DEFINED", propName)); //$NON-NLS-1$
088: }
089: return null;
090: }
091: try {
092: Class connectionClass = Class.forName(className);
093: Class[] argTypes = new Class[] { Properties.class,
094: ILogger.class };
095: Object[] args = new Object[] { props, logger };
096: Constructor constructor = connectionClass
097: .getConstructor(argTypes);
098: connection = (IPentahoConnection) constructor
099: .newInstance(args);
100: } catch (Exception e) {
101: if (logger != null) {
102: logger.error(e.getLocalizedMessage(), e);
103: } else {
104: Logger.error(PentahoConnectionFactory.class.getName(),
105: e.getLocalizedMessage(), e);
106: }
107: }
108:
109: return connection;
110: }
111:
112: private static Properties getProperties(String baseNode) {
113: Properties props = new Properties();
114: for (int i = 0; i < IPentahoConnection.KEYS.length; i++) {
115: String key = baseNode + "/" + IPentahoConnection.KEYS[i]; //$NON-NLS-1$
116: Object value = PentahoSystem.getSystemSetting(key, null);
117: if (value != null) {
118: props.put(IPentahoConnection.KEYS[i], value);
119: }
120: }
121: return props;
122: }
123:
124: /**
125: * @param datasourceType
126: * valid types are defined as SQL_DATASOURCE, MDX_DATASOURCE and
127: * XML_DATASOURCE
128: * @param location -
129: * A string specfic to the location and type of datasource. For
130: * an SQL instance it would be the URL string required by the
131: * implementing driver.
132: * @param userName
133: * @param password
134: * @return a connection object that can be queried against.
135: */
136: public static IPentahoConnection getConnection(int datasourceType,
137: String driver, String location, String userName,
138: String password, ILogger logger) {
139: /*
140: * TODO - This is where the "connection factory" action occurs. Based on
141: * if the datasourceType, location, username, or password have changed
142: * then we create a new one.
143: */
144: switch (datasourceType) {
145: case IPentahoConnection.SQL_DATASOURCE: {
146: SQLConnection connection = new SQLConnection(driver,
147: location, userName, password, logger);
148: if (connection.initialized()) {
149: return connection;
150: } else {
151: return null;
152: }
153: }
154: case IPentahoConnection.MDX_DATASOURCE:
155: return new MDXConnection(driver, location, userName,
156: password);
157: case IPentahoConnection.XML_DATASOURCE:
158: return new XQConnection(logger);
159: default:
160: return null;
161: }
162: }
163:
164: public static IPentahoConnection getConnection(int datasourceType,
165: ILogger logger) {
166: switch (datasourceType) {
167: case IPentahoConnection.XML_DATASOURCE:
168: return new XQConnection(logger);
169: default:
170: return null;
171: }
172: }
173:
174: public static IPentahoConnection getConnection(int datasourceType,
175: Properties mdxConnectionProps, ILogger logger) {
176: switch (datasourceType) {
177: case IPentahoConnection.MDX_DATASOURCE:
178: return new MDXConnection(mdxConnectionProps, logger);
179: default:
180: return null;
181: }
182: }
183:
184: }
|