View Javadoc
1 package org.smartcomps.twister.engine.priv.core.dynamic; 2 3 import org.smartcomps.twister.engine.exception.InstantiationException; 4 import org.smartcomps.twister.common.persistence.FinderException; 5 import org.smartcomps.twister.engine.priv.Constants; 6 import org.smartcomps.twister.engine.priv.core.dynamic.impl.ExecutionContextImpl; 7 import org.smartcomps.twister.engine.priv.core.dynamic.impl.ProcessInstanceImpl; 8 import org.smartcomps.twister.engine.priv.core.dynamic.impl.dao.ExecutionContextDAO; 9 import org.smartcomps.twister.engine.priv.core.definition.Activity; 10 import org.smartcomps.twister.engine.priv.core.definition.StructuredActivity; 11 import org.smartcomps.twister.engine.priv.core.definition.impl.ActivityImpl; 12 import org.smartcomps.twister.common.persistence.DBSessionException; 13 import org.smartcomps.twister.common.configuration.EngineConfiguration; 14 15 import java.util.ArrayList; 16 17 /*** 18 * A factory for ExecutionContext creation, finding and modification. 19 */ 20 public class ExecutionContextFactory { 21 22 /*** 23 * Won't work if the activity instance implements two Activity interfaces. 24 * @param generatingActivity 25 * @param containerEC 26 * @return 27 * @throws DBSessionException 28 */ 29 public static ExecutionContext createExecutionContext(Activity generatingActivity, StructuredEC containerEC) throws DBSessionException { 30 ExecutionContextImpl instance = null; 31 try { 32 instance = getECInstance(getECInterface(generatingActivity)); 33 } catch (InstantiationException e) { 34 throw new DBSessionException(e); 35 } 36 37 containerEC.addExecutionContext(instance); 38 instance.setContainer(containerEC); 39 if (generatingActivity instanceof StructuredActivity) { 40 ((StructuredActivity)generatingActivity).addExecutionContext((StructuredEC)instance); 41 ((StructuredEC)instance).setActivity((StructuredActivity)generatingActivity); 42 } 43 instance.setStatus(ExecutionContext.OPEN); 44 45 ExecutionContextDAO.create(instance); 46 return instance; 47 } 48 49 public static ExecutionContext createExecutionContext(Activity generatingActivity, ProcessInstance processInstance) throws DBSessionException { 50 ExecutionContextImpl ec = null; 51 try { 52 ec = getECInstance(getECInterface(generatingActivity)); 53 } catch (InstantiationException e) { 54 throw new DBSessionException(e); 55 } 56 ec.setStatus(ExecutionContext.ACTIVE); 57 58 ec.setInstance(processInstance); 59 ((ProcessInstanceImpl)processInstance).setChildExecutionContext(ec); 60 if (generatingActivity instanceof StructuredActivity) { 61 ((StructuredActivity)generatingActivity).addExecutionContext((StructuredEC)ec); 62 ((StructuredEC)ec).setActivity((StructuredActivity)generatingActivity); 63 } 64 ec.setStatus(ExecutionContext.OPEN); 65 66 ExecutionContextDAO.create(ec); 67 return ec; 68 } 69 70 public static ExecutionContext findECById(Long ecId) throws DBSessionException, FinderException { 71 ExecutionContext context = (ExecutionContext) ExecutionContextDAO.findById(ecId, ExecutionContextImpl.class); 72 return context; 73 } 74 75 /*** 76 * Returns the execution context created from the provided activity and situated 77 * in the containment hierarchy of the provided process instance. 78 * @param instance 79 * @param activity 80 * @return 81 */ 82 public static ExecutionContext findECForActivityInInstance(ProcessInstance instance, Activity activity) throws FinderException { 83 if (!(activity instanceof ActivityImpl)) 84 throw new IllegalArgumentException("Provided activity have to be persistent (subclass of ActivityImpl)" + activity); 85 86 // Going up the activity containment tree to select a path... 87 ArrayList parentList = new ArrayList(); 88 Activity container = activity; 89 while (container != null) { 90 parentList.add(container); 91 container = container.getContainer(); 92 } 93 94 if (parentList.size() == 1) { 95 return instance.getChildExecutionContext(); 96 } 97 98 // ... and down the execution context tree the apply the path. 99 // one level under the root for activities and root for ecs 100 ExecutionContext child = instance.getChildExecutionContext(); 101 int depth = 2; 102 try { 103 while (depth <= parentList.size()) { 104 child = ((StructuredEC)child).getExecution((Activity) parentList.get(parentList.size() - depth)); 105 depth++; 106 } 107 } catch (NullPointerException e) { 108 // A NullPointerException means that no execution context exists yet. 109 throw new FinderException("This activity has not been executed (yet) in the provided instance " + instance.getId()); 110 } 111 return child; 112 } 113 114 /*** 115 * Does the bulk work of finding the right class from the interface and instantiating it. 116 * @param ecInterface 117 * @return 118 * @throws org.smartcomps.twister.engine.exception.InstantiationException 119 */ 120 private static ExecutionContextImpl getECInstance(Class ecInterface) throws InstantiationException { 121 String ecClassName = EngineConfiguration.getExecutionImplementation(ecInterface.getName()); 122 123 ExecutionContextImpl instance = null; 124 try { 125 instance = (ExecutionContextImpl) Class.forName(ecClassName).newInstance(); 126 } catch (java.lang.InstantiationException e) { 127 throw new InstantiationException("Could not instantiate an execution context implementation " + ecClassName + " for interface " + ecInterface, e); 128 } catch (IllegalAccessException e) { 129 throw new InstantiationException("Could not instantiate an execution context implementation " + ecClassName + " for interface " + ecInterface, e); 130 } catch (ClassNotFoundException e) { 131 throw new InstantiationException("The implementation class " + ecClassName + " for the interface " + ecInterface + " could not be found", e); 132 } catch (ClassCastException e) { 133 throw new InstantiationException("The instance returned from the interface " + ecInterface + " is not an ExecutionContext implementation : " + ecClassName, e); 134 } 135 return instance; 136 } 137 138 private static Class getECInterface(Activity generatingActivity) throws InstantiationException { 139 Class activityLeafIF = null; 140 Class[] interfaces = generatingActivity.getClass().getInterfaces(); 141 for (int m = 0; m < interfaces.length; m++) { 142 Class anInterface = interfaces[m]; 143 if (Activity.class.isAssignableFrom(anInterface)) { 144 activityLeafIF = anInterface; 145 } 146 } 147 148 String ecInterfaceName = EngineConfiguration.getActivityExecution(activityLeafIF.getName()); 149 150 Class ecInterface = null; 151 try { 152 ecInterface = Class.forName(ecInterfaceName); 153 } catch (ClassNotFoundException e) { 154 throw new InstantiationException("The execution interface " + ecInterfaceName + " for the activity " + generatingActivity.getClass().getName() + " could not be found", e); 155 } 156 return ecInterface; 157 } 158 }

This page was automatically generated by Maven