1 16 17 package org.pentaho.plugin.mdx; 18 19 import java.io.File ; 20 import java.util.Iterator ; 21 import java.util.List ; 22 import java.util.Properties ; 23 24 import org.apache.commons.logging.Log; 25 import org.dom4j.Node; 26 import org.pentaho.core.component.IDataComponent; 27 import org.pentaho.core.connection.IPentahoConnection; 28 import org.pentaho.core.connection.IPentahoResultSet; 29 import org.pentaho.core.solution.IActionResource; 30 import org.pentaho.core.system.PentahoSystem; 31 import org.pentaho.core.util.DatasourceHelper; 32 import org.pentaho.data.PentahoConnectionFactory; 33 import org.pentaho.messages.Messages; 34 import org.pentaho.plugin.ComponentBase; 35 import org.pentaho.plugin.core.StandardSettings; 36 37 public abstract class MDXBaseComponent extends ComponentBase implements IDataComponent { 38 39 private static final String QUERY = "query"; 41 private static final String CONNECTION = "connection"; 43 private static final String CONNECT_STR = "mdx-connection-string"; 45 private static final String LOCATION = "location"; 47 private static final String CATALOG = "catalog"; 49 private static final String USER = "user-id"; 51 private static final String PASSWORD = "password"; 53 private static final String ROLE = "role"; 55 private static final String CONNECTION_PROPS = "connection-properties"; 57 private static final String PROPERTY = "property"; 59 private static final String KEY_NODE = "key"; 61 private static final String VALUE_NODE = "value"; 63 private IPentahoResultSet rSet; 64 private IPentahoConnection connection; 65 66 public abstract boolean validateSystemSettings(); 67 68 public abstract String getResultOutputName(); 69 70 public abstract Log getLogger(); 71 72 public IPentahoResultSet getResultSet() { 73 return rSet; 74 } 75 76 protected boolean validateAction() { 77 78 try { 79 if (!isDefinedInput(CONNECTION) && !isDefinedInput(CONNECT_STR) && !isDefinedInput(StandardSettings.JNDI) && !isDefinedInput(CONNECTION_PROPS)) { 80 error(Messages.getErrorString("MDXBaseComponent.ERROR_0002_CONNECTION_NOT_SPECIFIED", getActionName())); return false; 82 } 83 84 if (!isDefinedInput(QUERY)) { 85 error(Messages.getErrorString("MDXBaseComponent.ERROR_0001_QUERY_NOT_SPECIFIED", getActionName())); return false; 87 } 88 89 String outputName = getResultOutputName(); 90 if (outputName != null) { 91 if (!getOutputNames().contains(outputName)) { 92 error(Messages.getErrorString("MDXBaseComponent.ERROR_0003_OUTPUT_NOT_SPECIFIED", getActionName())); return false; 94 } 95 } 96 return true; 97 } catch (Exception e) { 98 error(Messages.getErrorString("MDXBaseComponent.ERROR_0004_VALIDATION_FAILED", getActionName()), e); } 100 101 return false; 102 } 103 104 public void done() { 105 } 106 107 protected boolean executeAction() { 108 109 try { 110 dispose(); 112 connection = getDatasourceConnection(); 113 if (connection == null) { 114 return false; 115 } 116 String query = getInputStringValue(QUERY); 117 return runQuery(connection, query); 118 } catch (Exception e) { 119 error(Messages.getErrorString("MDXBaseComponent.ERROR_0006_EXECUTE_FAILED", getActionName()), e); } 121 122 return false; 123 } 124 125 public void dispose() { 126 if (connection != null) { 127 connection.close(); 128 } 129 connection = null; 130 } 131 132 protected boolean runQuery(IPentahoConnection connection, String rawQuery) { 133 134 try { 135 if (connection == null) { 136 error( Messages.getErrorString( "MDXBaseComponent.ERROR_0008_NO_CONNECTION" ) ); return false; 138 } 139 if (!connection.initialized()) { 140 error( Messages.getErrorString( "MDXBaseComponent.ERROR_0008_NO_CONNECTION" ) ); return false; 142 } 143 144 if (debug) 145 debug(Messages.getString("MDXBaseComponent.DEBUG_RUNNING_QUERY", rawQuery)); 147 IPentahoResultSet resultSet = connection.executeQuery(rawQuery); 149 rSet = resultSet; 150 if (resultSet != null) { 151 if (getResultOutputName() != null) { 152 setOutputValue(getResultOutputName(), resultSet); 153 } 154 return true; 155 } else { 156 error(Messages.getErrorString("MDXBaseComponent.ERROR_0006_EXECUTE_FAILED", getActionName())); connection.close(); 159 return false; 160 } 161 162 } catch (Exception e) { 163 error(Messages.getErrorString("MDXBaseComponent.ERROR_0006_EXECUTE_FAILED", getActionName()), e); } 165 166 return false; 167 } 168 169 public IPentahoConnection getDatasourceConnection() { 170 IPentahoConnection con; 171 try { 172 con = getConnection(); 173 try { 174 con.clearWarnings(); 175 } catch (Exception ex) { 176 } 177 return con; 178 } catch (Exception ex) { 179 } 180 181 waitFor(200); 182 try { 183 con = getConnection(); 184 try { 185 con.clearWarnings(); 186 } catch (Exception ex) { 187 } 188 return con; 189 } catch (Exception ex) { 190 } 191 192 waitFor(500); 193 try { 194 con = getConnection(); 195 try { 196 con.clearWarnings(); 197 } catch (Exception ex) { 198 } 199 return con; 200 } catch (Exception ex) { 201 } 202 203 waitFor(2000); 204 con = getConnection(); 205 try { 206 con.clearWarnings(); 207 } catch (Exception ex) { 208 } 209 return con; 210 } 211 212 protected void waitFor(int millis) { 213 try { 214 if (debug) 215 debug(Messages.getString("MDXBaseComponent.DEBUG_WAITING_FOR_CONNECTION", Integer.toString(millis))); Thread.sleep(millis); 217 } catch (Exception ex) { 218 } 220 } 221 222 protected IPentahoConnection getConnection() { 223 IPentahoConnection connection = null; 224 try { 225 String mdxConnectionStr = getInputStringValue(CONNECT_STR); 226 Properties mdxConnectionProps = generateConnectionProps(); 227 String jdbcStr = getInputStringValue(CONNECTION); 228 String jndiStr = getInputStringValue(StandardSettings.JNDI); 229 String location = getInputStringValue(LOCATION); 230 String role = getInputStringValue(ROLE); 231 String catalog = null; 232 233 if (isDefinedInput(CATALOG)) { 234 catalog = getInputStringValue(CATALOG); 235 } else if (isDefinedResource(CATALOG)) { 236 IActionResource resource = getResource(CATALOG); 237 catalog = resource.getAddress(); 238 if (resource.getSourceType() == IActionResource.SOLUTION_FILE_RESOURCE) { 239 catalog = PentahoSystem.getApplicationContext().getSolutionPath(catalog); 240 } 241 if (resource.getSourceType() == IActionResource.URL_RESOURCE && catalog.indexOf("http") != 0) { catalog = PentahoSystem.getApplicationContext().getBaseUrl() + "GetMondrianModel?model=" + catalog; } else if (resource.getSourceType() == IActionResource.SOLUTION_FILE_RESOURCE || resource.getSourceType() == IActionResource.FILE_RESOURCE) { 244 File file = new File (catalog); 245 if (file.exists()) { 246 catalog = file.toURI().toString(); 247 } 248 } 249 } 250 if (catalog == null) { 251 warn(Messages.getString("MDXBaseComponent.ERROR_0007_CATALOG_NOT_DEFINED", getActionName())); } else { 253 if (mdxConnectionProps != null) { 254 mdxConnectionProps.put(CATALOG, catalog); 255 } 256 } 257 258 String userId = getInputStringValue(USER); 259 String password = getInputStringValue(PASSWORD); 260 if (mdxConnectionProps != null) { 261 connection = PentahoConnectionFactory.getConnection(PentahoConnectionFactory.MDX_DATASOURCE, mdxConnectionProps, null); 262 } else { 263 if (mdxConnectionStr != null) { 264 connection = PentahoConnectionFactory.getConnection(PentahoConnectionFactory.MDX_DATASOURCE, mdxConnectionStr, null); 265 } else { 266 String connectStr = null; 267 if (jdbcStr != null) { 268 connectStr = jdbcStr + "; Catalog=" + catalog; } else if (jndiStr != null) { 270 String dsName = DatasourceHelper.getDSBoundName(jndiStr); 271 if (dsName != null) { 272 connectStr = "dataSource=" + dsName + "; Catalog=" + catalog; } else { 274 error(Messages.getErrorString("MDXBaseComponent.ERROR_0005_INVALID_CONNECTION")); return null; 276 } 277 } 278 if (role != null) { 279 connectStr += "; Role=" + role; } 281 connection = PentahoConnectionFactory.getConnection(PentahoConnectionFactory.MDX_DATASOURCE, connectStr, location, userId, password, null); 282 } 283 if (connection == null) { 284 error(Messages.getErrorString("MDXBaseComponent.ERROR_0005_INVALID_CONNECTION")); return null; 286 } 287 } 288 return connection; 289 } catch (Exception e) { 290 error(Messages.getErrorString("MDXBaseComponent.ERROR_0006_EXECUTE_FAILED", getActionName()), e); } 292 return null; 293 } 294 295 private Properties generateConnectionProps() { 296 Node connectionProps = getComponentDefinition().selectSingleNode(CONNECTION_PROPS); 297 if (connectionProps == null) { 298 return null; 299 } 300 List props = connectionProps.selectNodes(PROPERTY); 301 Iterator iter = props.iterator(); 302 Properties properties = new Properties (); 303 while (iter.hasNext()) { 304 Node aProperty = (Node)iter.next(); 305 String key = aProperty.selectSingleNode(KEY_NODE).getText(); 306 String value = aProperty.selectSingleNode(VALUE_NODE).getText(); 307 properties.put(key, value); 308 } 309 310 return properties; 311 } 312 313 public boolean init() { 314 return true; 315 } 316 } 317 | Popular Tags |