KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > pentaho > core > solution > SolutionEngine


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 Jul 18, 2005
14  * @author James Dixon
15  *
16  */

17
18 package org.pentaho.core.solution;
19
20 import java.math.BigDecimal JavaDoc;
21 import java.util.Date JavaDoc;
22 import java.util.HashMap JavaDoc;
23 import java.util.List JavaDoc;
24 import java.util.Map JavaDoc;
25 import org.apache.commons.logging.Log;
26 import org.apache.commons.logging.LogFactory;
27 import org.pentaho.core.audit.AuditHelper;
28 import org.pentaho.core.audit.MessageTypes;
29 import org.pentaho.core.repository.IRuntimeElement;
30 import org.pentaho.core.repository.IRuntimeRepository;
31 import org.pentaho.core.repository.ISolutionRepository;
32 import org.pentaho.core.runtime.IRuntimeContext;
33 import org.pentaho.core.runtime.RuntimeContext;
34 import org.pentaho.core.session.IPentahoSession;
35 import org.pentaho.core.system.JVMParameterProvider;
36 import org.pentaho.core.system.PentahoMessenger;
37 import org.pentaho.core.system.PentahoSystem;
38 import org.pentaho.core.ui.IPentahoUrlFactory;
39 import org.pentaho.messages.Messages;
40
41 public class SolutionEngine extends PentahoMessenger implements ISolutionEngine {
42
43     /**
44      *
45      */

46     private static final long serialVersionUID = -8957434833691831700L;
47
48     private boolean debug = PentahoSystem.debug;
49
50     private Map JavaDoc parameterProviders;
51
52     private boolean persisted;
53
54     private IActionCompleteListener listener;
55
56     private IPentahoSession session;
57
58     private IRuntimeContext runtime = null;
59
60     private int status;
61     
62     private boolean forcePrompt = false;
63
64     private static String JavaDoc LOG_NAME = "SOLUTION-ENGINE"; //$NON-NLS-1$
65
private static final String JavaDoc JVM_PARAMETER_PROVIDER = "jvm"; //$NON-NLS-1$
66

67     private static final Log logger = LogFactory.getLog(SolutionEngine.class);
68
69     private String JavaDoc parameterXsl = null;
70     
71     public Log getLogger() {
72         return logger;
73     }
74
75     public SolutionEngine() {
76         status = IRuntimeContext.RUNTIME_STATUS_NOT_STARTED;
77     }
78
79     public void init(IPentahoSession pSession) {
80         parameterProviders = new HashMap JavaDoc();
81         this.session = pSession;
82         this.setParameterProvider(JVM_PARAMETER_PROVIDER, new JVMParameterProvider());
83         // solutionRepository = SolutionRepository.getInstance( session );
84
// solutionRepository.setLoggingLevel( loggingLevel );
85
setForcePrompt( false );
86     }
87
88     public void setParameterProvider(String JavaDoc name, IParameterProvider parameterProvider) {
89         parameterProviders.put(name, parameterProvider);
90     }
91
92     protected Map JavaDoc getParameterProviders() {
93         return parameterProviders;
94     }
95
96     public void setlistener(IActionCompleteListener listener) {
97         this.listener = listener;
98     }
99
100     public void setSession(IPentahoSession session) {
101         this.session = session;
102     }
103
104     protected boolean checkParameters(String JavaDoc solutionName, String JavaDoc sequencePath, String JavaDoc sequenceName, String JavaDoc processId) {
105         if (processId == null) {
106             // cannot allow this
107
error(Messages.getErrorString("SolutionEngine.ERROR_0001_PROCESS_NOT_SPECIFIED")); //$NON-NLS-1$
108
return false;
109         }
110         if (solutionName == null) {
111             // cannot allow this
112
error(Messages.getErrorString("SolutionEngine.ERROR_0002_SOLUTION_NOT_SPECIFIED")); //$NON-NLS-1$
113
return false;
114         }
115         if (sequencePath == null) {
116             // cannot allow this
117
error(Messages.getErrorString("SolutionEngine.ERROR_0003_PATH_NOT_SPECIFIED")); //$NON-NLS-1$
118
return false;
119         }
120         if (sequenceName == null) {
121             // cannot allow this
122
error(Messages.getErrorString("SolutionEngine.ERROR_0004_ACTION_NOT_SPECIFIED")); //$NON-NLS-1$
123
return false;
124         }
125         return true;
126
127     }
128
129     public int getStatus() {
130         return status;
131     }
132
133     public IRuntimeContext getExecutionContext() {
134         return runtime;
135     }
136
137     protected IPentahoSession getSession() {
138         return session;
139     }
140
141     protected IRuntimeContext getRuntime() {
142         return runtime;
143     }
144
145     protected void setRuntime(IRuntimeContext runtime) {
146         this.runtime = runtime;
147     }
148
149     protected void auditStart(String JavaDoc solutionName, String JavaDoc sequencePath, String JavaDoc sequenceName, String JavaDoc instanceId) {
150         if (debug) {
151             debug(Messages.getString("SolutionEngine.DEBUG_STARTING_EXECUTION", solutionName, sequencePath, sequenceName)); //$NON-NLS-1$
152
}
153         genLogIdFromInfo( instanceId, LOG_NAME, sequenceName );
154     }
155
156     public IRuntimeContext execute(String JavaDoc solutionName, String JavaDoc sequencePath, String JavaDoc sequenceName, String JavaDoc processId, boolean async, boolean instanceEnds, String JavaDoc instanceId, boolean pPersisted, Map JavaDoc parameterProviderMap, IOutputHandler outputHandler,
157             IActionCompleteListener pListener, IPentahoUrlFactory urlFactory, List JavaDoc messages) {
158
159         this.persisted = pPersisted;
160         setlistener(pListener);
161         setSession(session);
162
163         setMessages(messages);
164
165         auditStart(solutionName, sequencePath, sequenceName, instanceId);
166
167         if (!checkParameters(solutionName, sequencePath, sequenceName, processId)) {
168             return null;
169         }
170
171         session.setProcessId(processId);
172         session.setActionName(sequenceName);
173
174         // create the runtime context object for this operation
175
if (debug)
176             debug(Messages.getString("SolutionEngine.DEBUG_GETTING_RUNTIME_CONTEXT")); //$NON-NLS-1$
177
IRuntimeElement runtimeData;
178         IRuntimeRepository runtimeRepository = PentahoSystem.getRuntimeRepository(session);
179         if (instanceId == null) {
180             // we need to create runtime data for this execution
181
try {
182                 runtimeRepository.setLoggingLevel(loggingLevel);
183                 runtimeData = runtimeRepository.newRuntimeElement(session.getId(), "session", !persisted); //$NON-NLS-1$
184
createRuntime(runtimeData, solutionName, outputHandler, processId, urlFactory);
185                 runtime.setLoggingLevel(loggingLevel);
186                 instanceId = runtime.getInstanceId();
187                 genLogIdFromInfo( instanceId, LOG_NAME, sequenceName );
188                 // audit the creation of this against the session
189
AuditHelper.audit(session.getId(), session.getName(), sequenceName, getObjectName(), processId, MessageTypes.INSTANCE_START, instanceId, "", null, this); //$NON-NLS-1$
190
} catch (Throwable JavaDoc t) {
191                 error(Messages.getErrorString("SolutionEngine.ERROR_0008_INVALID_INSTANCE", instanceId), t); //$NON-NLS-1$
192
status = IRuntimeContext.RUNTIME_STATUS_SETUP_FAIL;
193                 return null;
194             }
195         } else {
196             try {
197                 runtimeRepository.setLoggingLevel(loggingLevel);
198                 runtimeData = runtimeRepository.loadElementById(instanceId, null);
199                 createRuntime(runtimeData, solutionName, outputHandler, processId, urlFactory);
200                 runtime.setLoggingLevel(loggingLevel);
201                 instanceId = runtime.getInstanceId();
202                 genLogIdFromInfo( instanceId, LOG_NAME, sequenceName );
203             } catch (Throwable JavaDoc t) {
204                 error(Messages.getErrorString("SolutionEngine.ERROR_0008_INVALID_INSTANCE", instanceId), t); //$NON-NLS-1$
205
status = IRuntimeContext.RUNTIME_STATUS_SETUP_FAIL;
206                 return null;
207             }
208         }
209         return executeInternal(solutionName, sequencePath, sequenceName, processId, async, instanceEnds, parameterProviderMap);
210     }
211
212     public IRuntimeContext execute(IRuntimeContext pRuntime, String JavaDoc solutionName, String JavaDoc sequencePath, String JavaDoc sequenceName, String JavaDoc processId, boolean async, boolean instanceEnds, Map JavaDoc parameterProviderMap, IOutputHandler outputHandler) {
213
214         runtime = pRuntime;
215         runtime.setOutputHandler(outputHandler);
216         auditStart(solutionName, sequencePath, sequenceName, runtime.getInstanceId());
217
218         if (!checkParameters(solutionName, sequencePath, sequenceName, processId)) {
219             status = IRuntimeContext.RUNTIME_STATUS_FAILURE;
220             return null;
221         }
222         return executeInternal(solutionName, sequencePath, sequenceName, processId, async, instanceEnds, parameterProviderMap);
223
224     }
225
226     protected IRuntimeContext executeInternal(String JavaDoc solutionName, String JavaDoc sequencePath, String JavaDoc sequenceName, String JavaDoc processId, boolean async, boolean instanceEnds, Map JavaDoc parameterProviderMap) {
227
228         long start = new Date JavaDoc().getTime();
229
230         parameterProviders.putAll(parameterProviderMap);
231         parameterProviders.put("global", PentahoSystem.getGlobalParameters()); //$NON-NLS-1$
232

233         // load the solution action document
234
if (debug)
235             debug(Messages.getString("SolutionEngine.DEBUG_LOADING_ACTION_DEFINITION")); //$NON-NLS-1$
236
IActionSequence actionSequence = createActionSequence(sequenceName, sequencePath, solutionName);
237         if (actionSequence == null) {
238             error(Messages.getErrorString("SolutionEngine.ERROR_0005_ACTION_SEQUENCE_NOT_FOUND")); //$NON-NLS-1$
239
status = IRuntimeContext.RUNTIME_STATUS_FAILURE;
240             long end = new Date JavaDoc().getTime();
241             AuditHelper.audit(session.getId(), session.getName(), sequenceName, getObjectName(), processId, MessageTypes.INSTANCE_FAILED, runtime.getInstanceId(), Messages.getErrorString("SolutionEngine.ERROR_0005_ACTION_SEQUENCE_NOT_FOUND"), new BigDecimal JavaDoc(((double) (end-start) / 1000)), this); //$NON-NLS-1$
242
return runtime;
243         }
244
245         runtime.setActionSequence(actionSequence);
246         if( parameterXsl != null ) {
247                 runtime.setParameterXsl( parameterXsl );
248         }
249         
250         if( forcePrompt ) {
251             runtime.setPromptStatus( IRuntimeContext.PROMPT_WAITING );
252         } else {
253             runtime.setPromptStatus( IRuntimeContext.PROMPT_NO );
254         }
255         
256         if (runtime.validateSequence(sequenceName) != IRuntimeContext.RUNTIME_CONTEXT_VALIDATE_OK) {
257             error(Messages.getErrorString("SolutionEngine.ERROR_0006_ACTION_SEQUENCE_INVALID")); //$NON-NLS-1$
258
status = IRuntimeContext.RUNTIME_CONTEXT_VALIDATE_FAIL;
259             long end = new Date JavaDoc().getTime();
260             AuditHelper.audit(session.getId(), session.getName(), sequenceName, getObjectName(), processId, MessageTypes.INSTANCE_FAILED, runtime.getInstanceId(), Messages.getErrorString("SolutionEngine.ERROR_0006_ACTION_SEQUENCE_INVALID"), new BigDecimal JavaDoc(((double) (end-start) / 1000)), this); //$NON-NLS-1$
261
return runtime;
262         }
263
264         try {
265             if (runtime.executeSequence(listener, async) != IRuntimeContext.RUNTIME_STATUS_SUCCESS) {
266                 error(Messages.getErrorString("SolutionEngine.ERROR_0007_ACTION_EXECUTION_FAILED")); //$NON-NLS-1$
267
status = IRuntimeContext.RUNTIME_STATUS_FAILURE;
268                 long end = new Date JavaDoc().getTime();
269                 AuditHelper.audit(session.getId(), session.getName(), sequenceName, getObjectName(), processId, MessageTypes.INSTANCE_FAILED, runtime.getInstanceId(), Messages.getErrorString("SolutionEngine.ERROR_0007_ACTION_EXECUTION_FAILED"), new BigDecimal JavaDoc(((double) (end-start) / 1000)), this); //$NON-NLS-1$
270
return runtime;
271             }
272         } finally {
273             if (persisted) {
274                 // HibernateUtil.commitTransaction();
275
// HibernateUtil.closeSession();
276
}
277         }
278         // return the runtime context for the action
279
if (instanceEnds) {
280             long end = new Date JavaDoc().getTime();
281             AuditHelper.audit(session.getId(), session.getName(), sequenceName, getObjectName(), processId, MessageTypes.INSTANCE_END, runtime.getInstanceId(), "", new BigDecimal JavaDoc(((double) (end-start) / 1000)), this); //$NON-NLS-1$
282
}
283         status = runtime.getStatus();
284         return runtime;
285
286     }
287
288     protected void createRuntime(IRuntimeElement runtimeData, String JavaDoc solutionName, IOutputHandler outputHandler, String JavaDoc processId, IPentahoUrlFactory urlFactory) {
289         runtime = new RuntimeContext(runtimeData.getInstanceId(), this, solutionName, runtimeData, session, outputHandler, processId, urlFactory, parameterProviders, getMessages());
290     }
291
292     private IActionSequence createActionSequence(String JavaDoc actionName, String JavaDoc actionPath, String JavaDoc solutionName) {
293
294         ISolutionRepository solutionRepository = PentahoSystem.getSolutionRepository(session);
295         solutionRepository.setMessages( getMessages() );
296         return solutionRepository.getActionSequence(solutionName, actionPath, actionName, loggingLevel, ISolutionRepository.ACTION_EXECUTE);
297
298     }
299     
300     public void setForcePrompt( boolean forcePrompt ) {
301             this.forcePrompt = forcePrompt;
302     }
303     
304     public void setParameterXsl(String JavaDoc xsl) {
305         this.parameterXsl = xsl;
306     }
307
308 }
309
Popular Tags