KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > pentaho > data > PentahoConnectionFactory


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 Aug 29, 2005
14  * @author wseyler
15  */

16
17 package org.pentaho.data;
18
19 import java.lang.reflect.Constructor JavaDoc;
20 import java.util.Properties JavaDoc;
21
22 import org.pentaho.core.connection.IPentahoConnection;
23 import org.pentaho.core.system.PentahoSystem;
24 import org.pentaho.data.connection.mdx.MDXConnection;
25 import org.pentaho.data.connection.sql.SQLConnection;
26 import org.pentaho.data.connection.xquery.XQConnection;
27 import org.pentaho.messages.Messages;
28 import org.pentaho.util.logging.ILogger;
29 import org.pentaho.util.logging.Logger;
30
31 /**
32  * @author wseyler
33  *
34  * TODO To change the template for this generated type comment go to Window -
35  * Preferences - Java - Code Style - Code Templates
36  */

37 public class PentahoConnectionFactory {
38     public static final int NONE_DATASOURCE = -1;
39
40     public static final int SQL_DATASOURCE = 0;
41
42     public static final int MDX_DATASOURCE = 1;
43
44     public static final int XML_DATASOURCE = 2;
45
46     /**
47      * @param datasourceType
48      * valid type is defined as SQL_DATASOURCE or MDX_DATASOURCE
49      * @param connectStr -
50      * In the case of SQL_DATASOURCE, the name of the JNDI connection
51      * to use. Or in the case of MDX_DATASOURCE a properly formatted
52      * connection String.
53      * @return a connection object that can be queried against.
54      */

55     public static IPentahoConnection getConnection(int datasourceType, String JavaDoc connectStr, ILogger logger) {
56         /*
57          * TODO - This is where the "connection factory" action occurs. Based on
58          * if the datasourceType, location, username, or password have changed
59          * then we create a new one.
60          */

61         switch (datasourceType) {
62         case SQL_DATASOURCE:
63             return new SQLConnection(connectStr, logger);
64         case MDX_DATASOURCE:
65             return new MDXConnection(connectStr, logger);
66         default:
67             return null;
68         }
69     }
70
71     public static IPentahoConnection getConnection(String JavaDoc propName, ILogger logger) {
72         IPentahoConnection connection = null;
73
74         String JavaDoc baseNode = "objects/" + propName; //$NON-NLS-1$
75
Properties JavaDoc props = getProperties(baseNode);
76
77         String JavaDoc className = props.getProperty(IPentahoConnection.CLASSNAME_KEY, null);
78         if (className == null) {
79             if (logger != null) {
80                 logger.error( Messages.getErrorString( "CONNECTFACTORY.ERROR_0001_NOT_DEFINED", propName )); //$NON-NLS-1$
81
} else {
82                 Logger.error(PentahoConnectionFactory.class.getName(), Messages.getErrorString( "CONNECTFACTORY.ERROR_0001_NOT_DEFINED", propName) ); //$NON-NLS-1$
83
}
84             return null;
85         }
86         try {
87             Class JavaDoc connectionClass = Class.forName(className);
88             Class JavaDoc[] argTypes = new Class JavaDoc[] { Properties JavaDoc.class, ILogger.class };
89             Object JavaDoc[] args = new Object JavaDoc[] { props, logger };
90             Constructor JavaDoc constructor = connectionClass.getConstructor(argTypes);
91             connection = (IPentahoConnection) constructor.newInstance(args);
92         } catch (Exception JavaDoc e) {
93             if (logger != null) {
94                 logger.error(e.getLocalizedMessage(), e);
95             } else {
96                 Logger.error(PentahoConnectionFactory.class.getName(), e.getLocalizedMessage(), e);
97             }
98         }
99
100         return connection;
101     }
102
103     private static Properties JavaDoc getProperties(String JavaDoc baseNode) {
104         Properties JavaDoc props = new Properties JavaDoc();
105         for (int i = 0; i < IPentahoConnection.KEYS.length; i++) {
106             String JavaDoc key = baseNode + "/" + IPentahoConnection.KEYS[i]; //$NON-NLS-1$
107
Object JavaDoc value = PentahoSystem.getSystemSetting(key, null);
108             if (value != null) {
109                 props.put(IPentahoConnection.KEYS[i], value);
110             }
111         }
112         return props;
113     }
114
115     /**
116      * @param datasourceType
117      * valid types are defined as SQL_DATASOURCE, MDX_DATASOURCE and
118      * XML_DATASOURCE
119      * @param location -
120      * A string specfic to the location and type of datasource. For
121      * an SQL instance it would be the URL string required by the
122      * implementing driver.
123      * @param userName
124      * @param password
125      * @return a connection object that can be queried against.
126      */

127     public static IPentahoConnection getConnection(int datasourceType, String JavaDoc driver, String JavaDoc location, String JavaDoc userName, String JavaDoc password, ILogger logger) {
128         /*
129          * TODO - This is where the "connection factory" action occurs. Based on
130          * if the datasourceType, location, username, or password have changed
131          * then we create a new one.
132          */

133         switch (datasourceType) {
134         case SQL_DATASOURCE: {
135             SQLConnection connection = new SQLConnection(driver, location, userName, password, logger);
136             if (connection.initialized()) {
137                 return connection;
138             } else {
139                 return null;
140             }
141         }
142         case MDX_DATASOURCE:
143             return new MDXConnection(driver, location, userName, password);
144         case XML_DATASOURCE:
145             return new XQConnection(logger);
146         default:
147             return null;
148         }
149     }
150
151     public static IPentahoConnection getConnection(int datasourceType, ILogger logger) {
152         switch (datasourceType) {
153         case XML_DATASOURCE:
154             return new XQConnection(logger);
155         default:
156             return null;
157         }
158     }
159
160     public static IPentahoConnection getConnection(int datasourceType, Properties JavaDoc mdxConnectionProps, ILogger logger) {
161         switch (datasourceType) {
162             case MDX_DATASOURCE:
163                 return new MDXConnection(mdxConnectionProps, logger);
164             default:
165                 return null;
166         }
167     }
168
169 }
170
Popular Tags