| 1 16 17 package org.pentaho.data; 18 19 import java.lang.reflect.Constructor ; 20 import java.util.Properties ; 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 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 55 public static IPentahoConnection getConnection(int datasourceType, String connectStr, ILogger logger) { 56 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 propName, ILogger logger) { 72 IPentahoConnection connection = null; 73 74 String baseNode = "objects/" + propName; Properties props = getProperties(baseNode); 76 77 String 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 )); } else { 82 Logger.error(PentahoConnectionFactory.class.getName(), Messages.getErrorString( "CONNECTFACTORY.ERROR_0001_NOT_DEFINED", propName) ); } 84 return null; 85 } 86 try { 87 Class connectionClass = Class.forName(className); 88 Class [] argTypes = new Class [] { Properties .class, ILogger.class }; 89 Object [] args = new Object [] { props, logger }; 90 Constructor constructor = connectionClass.getConstructor(argTypes); 91 connection = (IPentahoConnection) constructor.newInstance(args); 92 } catch (Exception 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 getProperties(String baseNode) { 104 Properties props = new Properties (); 105 for (int i = 0; i < IPentahoConnection.KEYS.length; i++) { 106 String key = baseNode + "/" + IPentahoConnection.KEYS[i]; Object value = PentahoSystem.getSystemSetting(key, null); 108 if (value != null) { 109 props.put(IPentahoConnection.KEYS[i], value); 110 } 111 } 112 return props; 113 } 114 115 127 public static IPentahoConnection getConnection(int datasourceType, String driver, String location, String userName, String password, ILogger logger) { 128 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 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 |