/* Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.activiti.crystalball.simulator.impl.persistence.entity; import org.activiti.crystalball.simulator.impl.context.SimulationContext; import org.activiti.crystalball.simulator.impl.db.DbSimulatorSqlSession; import org.activiti.crystalball.simulator.impl.interceptor.CommandContext; import org.activiti.crystalball.simulator.impl.simulationexecutor.JobHandler; import org.activiti.crystalball.simulator.runtime.Job; import org.activiti.engine.ActivitiException; import org.activiti.engine.impl.context.Context; import org.activiti.engine.impl.db.HasRevision; import org.activiti.engine.impl.db.PersistentObject; import org.activiti.engine.impl.persistence.entity.ByteArrayEntity; import org.activiti.engine.impl.persistence.entity.MessageEntity; import org.activiti.engine.impl.persistence.entity.TimerEntity; import java.io.Serializable; import java.io.UnsupportedEncodingException; import java.util.Date; import java.util.HashMap; import java.util.Map; /** * Stub of the common parts of a Job. You will normally work with a subclass of * JobEntity, such as {@link TimerEntity} or {@link MessageEntity}. * * @author Tom Baeyens * @author Nick Burch * @author Dave Syer * @author Frederik Heremans */ public class JobEntity implements Serializable, Job, PersistentObject, HasRevision { public static final boolean DEFAULT_EXCLUSIVE = true; public static final int DEFAULT_RETRIES = 3; private static final int MAX_EXCEPTION_MESSAGE_LENGTH = 255; private static final long serialVersionUID = 1L; protected String id; protected int revision; protected Date duedate; protected String lockOwner = null; protected Date lockExpirationTime = null; protected String simulationInstanceId = null; protected boolean isExclusive = DEFAULT_EXCLUSIVE; protected int retries = DEFAULT_RETRIES; protected String jobHandlerType = null; protected String jobHandlerConfiguration = null; protected ByteArrayEntity exceptionByteArray; protected String exceptionByteArrayId; protected String exceptionMessage; public void execute(CommandContext commandContext) { SimulationInstanceEntity simulationInstance = null; if (simulationInstanceId != null) { simulationInstance = commandContext.getSimulationInstanceManager().findSimulationInstanceById(simulationInstanceId); } Map<String, JobHandler> jobHandlers = SimulationContext.getSimulationEngineConfiguration().getJobHandlers(); JobHandler jobHandler = jobHandlers.get(jobHandlerType); jobHandler.execute(this, jobHandlerConfiguration, simulationInstance, commandContext); } public void insert() { DbSimulatorSqlSession dbSqlSession = SimulationContext .getCommandContext() .getDbSqlSession(); dbSqlSession.insert(this); // add link to execution // if(simulationInstanceId != null) { // SimulationInstanceEntity execution = SimulationContext.getCommandContext() // .getSimulationInstanceManager().findSimulationInstanceById(simulationInstanceId); // execution.addJob(this); // } } public void delete() { DbSimulatorSqlSession dbSqlSession = SimulationContext .getCommandContext() .getDbSqlSession(); dbSqlSession.delete(this); // Also delete the job's exception byte array if (exceptionByteArrayId != null) { Context.getCommandContext().getByteArrayManager().deleteByteArrayById(exceptionByteArrayId); } // remove link to execution // if(executionId != null) { // ExecutionEntity execution = Context.getCommandContext() // .getExecutionManager() // .findExecutionById(executionId); // execution.removeJob(this); // } } public Object getPersistentState() { Map<String, Object> persistentState = new HashMap<String, Object>(); persistentState.put("lockOwner", lockOwner); persistentState.put("lockExpirationTime", lockExpirationTime); persistentState.put("retries", retries); persistentState.put("duedate", duedate); persistentState.put("exceptionMessage", exceptionMessage); if(exceptionByteArrayId != null) { persistentState.put("exceptionByteArrayId", exceptionByteArrayId); } return persistentState; } public int getRevisionNext() { return revision+1; } // public void setExecution(ExecutionEntity execution) { // executionId = execution.getId(); // processInstanceId = execution.getProcessInstanceId(); // execution.addJob(this); // } // getters and setters ////////////////////////////////////////////////////// public String getSimulationInstanceId() { return simulationInstanceId; } public void setSimulationInstanceId(String simulationInstanceId) { this.simulationInstanceId = simulationInstanceId; } public int getRetries() { return retries; } public void setRetries(int retries) { this.retries = retries; } public String getExceptionStacktrace() { String exception = null; ByteArrayEntity byteArray = getExceptionByteArray(); if(byteArray != null) { try { exception = new String(byteArray.getBytes(), "UTF-8"); } catch (UnsupportedEncodingException e) { throw new ActivitiException("UTF-8 is not a supported encoding"); } } return exception; } public String getLockOwner() { return lockOwner; } public void setLockOwner(String claimedBy) { this.lockOwner = claimedBy; } public Date getLockExpirationTime() { return lockExpirationTime; } public void setLockExpirationTime(Date claimedUntil) { this.lockExpirationTime = claimedUntil; } public boolean isExclusive() { return isExclusive; } public void setExclusive(boolean isExclusive) { this.isExclusive = isExclusive; } public String getId() { return id; } public void setId(String id) { this.id = id; } public Date getDuedate() { return duedate; } public void setDuedate(Date duedate) { this.duedate = duedate; } public void setExceptionStacktrace(String exception) { byte[] exceptionBytes = null; if(exception == null) { exceptionBytes = null; } else { try { exceptionBytes = exception.getBytes("UTF-8"); } catch (UnsupportedEncodingException e) { throw new ActivitiException("UTF-8 is not a supported encoding"); } } ByteArrayEntity byteArray = getExceptionByteArray(); if(byteArray == null) { byteArray = new ByteArrayEntity("job.exceptionByteArray", exceptionBytes); Context .getCommandContext() .getDbSqlSession() .insert(byteArray); exceptionByteArrayId = byteArray.getId(); exceptionByteArray = byteArray; } else { byteArray.setBytes(exceptionBytes); } } public String getJobHandlerType() { return jobHandlerType; } public void setJobHandlerType(String jobHandlerType) { this.jobHandlerType = jobHandlerType; } public String getJobHandlerConfiguration() { return jobHandlerConfiguration; } public void setJobHandlerConfiguration(String jobHandlerConfiguration) { this.jobHandlerConfiguration = jobHandlerConfiguration; } public int getRevision() { return revision; } public void setRevision(int revision) { this.revision = revision; } public String getExceptionMessage() { return exceptionMessage; } public void setExceptionMessage(String exceptionMessage) { if(exceptionMessage != null && exceptionMessage.length() > MAX_EXCEPTION_MESSAGE_LENGTH) { this.exceptionMessage = exceptionMessage.substring(0, MAX_EXCEPTION_MESSAGE_LENGTH); } else { this.exceptionMessage = exceptionMessage; } } public String getExceptionByteArrayId() { return exceptionByteArrayId; } private ByteArrayEntity getExceptionByteArray() { if ((exceptionByteArray == null) && (exceptionByteArrayId != null)) { exceptionByteArray = SimulationContext .getCommandContext() .getDbSqlSession() .selectById(ByteArrayEntity.class, exceptionByteArrayId); } return exceptionByteArray; } }