1 17 18 package org.pentaho.core.solution; 19 20 import java.math.BigDecimal ; 21 import java.util.Date ; 22 import java.util.HashMap ; 23 import java.util.List ; 24 import java.util.Map ; 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 46 private static final long serialVersionUID = -8957434833691831700L; 47 48 private boolean debug = PentahoSystem.debug; 49 50 private Map 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 LOG_NAME = "SOLUTION-ENGINE"; private static final String JVM_PARAMETER_PROVIDER = "jvm"; 67 private static final Log logger = LogFactory.getLog(SolutionEngine.class); 68 69 private String 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 (); 81 this.session = pSession; 82 this.setParameterProvider(JVM_PARAMETER_PROVIDER, new JVMParameterProvider()); 83 setForcePrompt( false ); 86 } 87 88 public void setParameterProvider(String name, IParameterProvider parameterProvider) { 89 parameterProviders.put(name, parameterProvider); 90 } 91 92 protected Map 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 solutionName, String sequencePath, String sequenceName, String processId) { 105 if (processId == null) { 106 error(Messages.getErrorString("SolutionEngine.ERROR_0001_PROCESS_NOT_SPECIFIED")); return false; 109 } 110 if (solutionName == null) { 111 error(Messages.getErrorString("SolutionEngine.ERROR_0002_SOLUTION_NOT_SPECIFIED")); return false; 114 } 115 if (sequencePath == null) { 116 error(Messages.getErrorString("SolutionEngine.ERROR_0003_PATH_NOT_SPECIFIED")); return false; 119 } 120 if (sequenceName == null) { 121 error(Messages.getErrorString("SolutionEngine.ERROR_0004_ACTION_NOT_SPECIFIED")); 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 solutionName, String sequencePath, String sequenceName, String instanceId) { 150 if (debug) { 151 debug(Messages.getString("SolutionEngine.DEBUG_STARTING_EXECUTION", solutionName, sequencePath, sequenceName)); } 153 genLogIdFromInfo( instanceId, LOG_NAME, sequenceName ); 154 } 155 156 public IRuntimeContext execute(String solutionName, String sequencePath, String sequenceName, String processId, boolean async, boolean instanceEnds, String instanceId, boolean pPersisted, Map parameterProviderMap, IOutputHandler outputHandler, 157 IActionCompleteListener pListener, IPentahoUrlFactory urlFactory, List 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 if (debug) 176 debug(Messages.getString("SolutionEngine.DEBUG_GETTING_RUNTIME_CONTEXT")); IRuntimeElement runtimeData; 178 IRuntimeRepository runtimeRepository = PentahoSystem.getRuntimeRepository(session); 179 if (instanceId == null) { 180 try { 182 runtimeRepository.setLoggingLevel(loggingLevel); 183 runtimeData = runtimeRepository.newRuntimeElement(session.getId(), "session", !persisted); createRuntime(runtimeData, solutionName, outputHandler, processId, urlFactory); 185 runtime.setLoggingLevel(loggingLevel); 186 instanceId = runtime.getInstanceId(); 187 genLogIdFromInfo( instanceId, LOG_NAME, sequenceName ); 188 AuditHelper.audit(session.getId(), session.getName(), sequenceName, getObjectName(), processId, MessageTypes.INSTANCE_START, instanceId, "", null, this); } catch (Throwable t) { 191 error(Messages.getErrorString("SolutionEngine.ERROR_0008_INVALID_INSTANCE", instanceId), t); 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 t) { 204 error(Messages.getErrorString("SolutionEngine.ERROR_0008_INVALID_INSTANCE", instanceId), t); 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 solutionName, String sequencePath, String sequenceName, String processId, boolean async, boolean instanceEnds, Map 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 solutionName, String sequencePath, String sequenceName, String processId, boolean async, boolean instanceEnds, Map parameterProviderMap) { 227 228 long start = new Date ().getTime(); 229 230 parameterProviders.putAll(parameterProviderMap); 231 parameterProviders.put("global", PentahoSystem.getGlobalParameters()); 233 if (debug) 235 debug(Messages.getString("SolutionEngine.DEBUG_LOADING_ACTION_DEFINITION")); IActionSequence actionSequence = createActionSequence(sequenceName, sequencePath, solutionName); 237 if (actionSequence == null) { 238 error(Messages.getErrorString("SolutionEngine.ERROR_0005_ACTION_SEQUENCE_NOT_FOUND")); status = IRuntimeContext.RUNTIME_STATUS_FAILURE; 240 long end = new Date ().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 (((double) (end-start) / 1000)), this); 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")); status = IRuntimeContext.RUNTIME_CONTEXT_VALIDATE_FAIL; 259 long end = new Date ().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 (((double) (end-start) / 1000)), this); 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")); status = IRuntimeContext.RUNTIME_STATUS_FAILURE; 268 long end = new Date ().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 (((double) (end-start) / 1000)), this); return runtime; 271 } 272 } finally { 273 if (persisted) { 274 } 277 } 278 if (instanceEnds) { 280 long end = new Date ().getTime(); 281 AuditHelper.audit(session.getId(), session.getName(), sequenceName, getObjectName(), processId, MessageTypes.INSTANCE_END, runtime.getInstanceId(), "", new BigDecimal (((double) (end-start) / 1000)), this); } 283 status = runtime.getStatus(); 284 return runtime; 285 286 } 287 288 protected void createRuntime(IRuntimeElement runtimeData, String solutionName, IOutputHandler outputHandler, String 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 actionName, String actionPath, String 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 xsl) { 305 this.parameterXsl = xsl; 306 } 307 308 } 309 | Popular Tags |