KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > pentaho > plugin > mdx > MDXBaseComponent


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

16
17 package org.pentaho.plugin.mdx;
18
19 import java.io.File JavaDoc;
20 import java.util.Iterator JavaDoc;
21 import java.util.List JavaDoc;
22 import java.util.Properties JavaDoc;
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 JavaDoc QUERY = "query"; //$NON-NLS-1$
40

41     private static final String JavaDoc CONNECTION = "connection"; //$NON-NLS-1$
42

43     private static final String JavaDoc CONNECT_STR = "mdx-connection-string"; //$NON-NLS-1$
44

45     private static final String JavaDoc LOCATION = "location"; //$NON-NLS-1$
46

47     private static final String JavaDoc CATALOG = "catalog"; //$NON-NLS-1$
48

49     private static final String JavaDoc USER = "user-id"; //$NON-NLS-1$
50

51     private static final String JavaDoc PASSWORD = "password"; //$NON-NLS-1$
52

53     private static final String JavaDoc ROLE = "role"; //$NON-NLS-1$
54

55     private static final String JavaDoc CONNECTION_PROPS = "connection-properties"; //$NON-NLS-1$
56

57     private static final String JavaDoc PROPERTY = "property"; //$NON-NLS-1$
58

59     private static final String JavaDoc KEY_NODE = "key"; //$NON-NLS-1$
60

61     private static final String JavaDoc VALUE_NODE = "value"; //$NON-NLS-1$
62

63     private IPentahoResultSet rSet;
64     private IPentahoConnection connection;
65
66     public abstract boolean validateSystemSettings();
67
68     public abstract String JavaDoc 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())); //$NON-NLS-1$
81
return false;
82             }
83
84             if (!isDefinedInput(QUERY)) {
85                 error(Messages.getErrorString("MDXBaseComponent.ERROR_0001_QUERY_NOT_SPECIFIED", getActionName())); //$NON-NLS-1$
86
return false;
87             }
88
89             String JavaDoc outputName = getResultOutputName();
90             if (outputName != null) {
91                 if (!getOutputNames().contains(outputName)) {
92                     error(Messages.getErrorString("MDXBaseComponent.ERROR_0003_OUTPUT_NOT_SPECIFIED", getActionName())); //$NON-NLS-1$
93
return false;
94                 }
95             }
96             return true;
97         } catch (Exception JavaDoc e) {
98             error(Messages.getErrorString("MDXBaseComponent.ERROR_0004_VALIDATION_FAILED", getActionName()), e); //$NON-NLS-1$
99
}
100
101         return false;
102     }
103
104     public void done() {
105     }
106
107     protected boolean executeAction() {
108
109         try {
110             // Cleanup between executions...
111
dispose();
112             connection = getDatasourceConnection();
113             if (connection == null) {
114                 return false;
115             }
116             String JavaDoc query = getInputStringValue(QUERY);
117             return runQuery(connection, query);
118         } catch (Exception JavaDoc e) {
119             error(Messages.getErrorString("MDXBaseComponent.ERROR_0006_EXECUTE_FAILED", getActionName()), e); //$NON-NLS-1$
120
}
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 JavaDoc rawQuery) {
133
134         try {
135             if (connection == null) {
136                 error( Messages.getErrorString( "MDXBaseComponent.ERROR_0008_NO_CONNECTION" ) ); //$NON-NLS-1$
137
return false;
138             }
139             if (!connection.initialized()) {
140                 error( Messages.getErrorString( "MDXBaseComponent.ERROR_0008_NO_CONNECTION" ) ); //$NON-NLS-1$
141
return false;
142             }
143
144             if (debug)
145                 debug(Messages.getString("MDXBaseComponent.DEBUG_RUNNING_QUERY", rawQuery)); //$NON-NLS-1$
146

147             // execute the query, read the results and cache them
148
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                 // close the connection
157
error(Messages.getErrorString("MDXBaseComponent.ERROR_0006_EXECUTE_FAILED", getActionName())); //$NON-NLS-1$
158
connection.close();
159                 return false;
160             }
161
162         } catch (Exception JavaDoc e) {
163             error(Messages.getErrorString("MDXBaseComponent.ERROR_0006_EXECUTE_FAILED", getActionName()), e); //$NON-NLS-1$
164
}
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 JavaDoc ex) {
176             }
177             return con;
178         } catch (Exception JavaDoc ex) {
179         }
180
181         waitFor(200);
182         try {
183             con = getConnection();
184             try {
185                 con.clearWarnings();
186             } catch (Exception JavaDoc ex) {
187             }
188             return con;
189         } catch (Exception JavaDoc ex) {
190         }
191
192         waitFor(500);
193         try {
194             con = getConnection();
195             try {
196                 con.clearWarnings();
197             } catch (Exception JavaDoc ex) {
198             }
199             return con;
200         } catch (Exception JavaDoc ex) {
201         }
202
203         waitFor(2000);
204         con = getConnection();
205         try {
206             con.clearWarnings();
207         } catch (Exception JavaDoc 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))); //$NON-NLS-1$
216
Thread.sleep(millis);
217         } catch (Exception JavaDoc ex) {
218             // ignore the interrupted exception, if it happens
219
}
220     }
221
222     protected IPentahoConnection getConnection() {
223         IPentahoConnection connection = null;
224         try {
225             String JavaDoc mdxConnectionStr = getInputStringValue(CONNECT_STR);
226             Properties JavaDoc mdxConnectionProps = generateConnectionProps();
227             String JavaDoc jdbcStr = getInputStringValue(CONNECTION);
228             String JavaDoc jndiStr = getInputStringValue(StandardSettings.JNDI);
229             String JavaDoc location = getInputStringValue(LOCATION);
230             String JavaDoc role = getInputStringValue(ROLE);
231             String JavaDoc 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) { //$NON-NLS-1$
242
catalog = PentahoSystem.getApplicationContext().getBaseUrl() + "GetMondrianModel?model=" + catalog; //$NON-NLS-1$
243
} else if (resource.getSourceType() == IActionResource.SOLUTION_FILE_RESOURCE || resource.getSourceType() == IActionResource.FILE_RESOURCE) {
244                     File JavaDoc file = new File JavaDoc(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())); //$NON-NLS-1$
252
} else {
253                 if (mdxConnectionProps != null) {
254                     mdxConnectionProps.put(CATALOG, catalog);
255                 }
256             }
257             
258             String JavaDoc userId = getInputStringValue(USER);
259             String JavaDoc 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 JavaDoc connectStr = null;
267                   if (jdbcStr != null) {
268                     connectStr = jdbcStr + "; Catalog=" + catalog; //$NON-NLS-1$
269
} else if (jndiStr != null) {
270                     String JavaDoc dsName = DatasourceHelper.getDSBoundName(jndiStr);
271                     if (dsName != null) {
272                       connectStr = "dataSource=" + dsName + "; Catalog=" + catalog; //$NON-NLS-1$ //$NON-NLS-2$
273
} else {
274                       error(Messages.getErrorString("MDXBaseComponent.ERROR_0005_INVALID_CONNECTION")); //$NON-NLS-1$
275
return null;
276                     }
277                   }
278                   if (role != null) {
279                     connectStr += "; Role=" + role; //$NON-NLS-1$
280
}
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")); //$NON-NLS-1$
285
return null;
286                 }
287             }
288             return connection;
289         } catch (Exception JavaDoc e) {
290             error(Messages.getErrorString("MDXBaseComponent.ERROR_0006_EXECUTE_FAILED", getActionName()), e); //$NON-NLS-1$
291
}
292         return null;
293     }
294
295     private Properties JavaDoc generateConnectionProps() {
296         Node connectionProps = getComponentDefinition().selectSingleNode(CONNECTION_PROPS);
297         if (connectionProps == null) {
298             return null;
299         }
300         List JavaDoc props = connectionProps.selectNodes(PROPERTY);
301         Iterator JavaDoc iter = props.iterator();
302         Properties JavaDoc properties = new Properties JavaDoc();
303         while (iter.hasNext()) {
304             Node aProperty = (Node)iter.next();
305             String JavaDoc key = aProperty.selectSingleNode(KEY_NODE).getText();
306             String JavaDoc 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