1 package org.sapia.soto.state; 2 3 import org.sapia.soto.Debug; 4 import org.sapia.soto.Env; 5 import org.sapia.soto.EnvAware; 6 import org.sapia.soto.SotoContainer; 7 import org.sapia.soto.config.Include; 8 import org.sapia.soto.util.Resource; 9 10 import org.sapia.util.xml.confix.ConfigurationException; 11 import org.sapia.util.xml.confix.ObjectCreationCallback; 12 import org.sapia.util.xml.confix.ObjectHandlerIF; 13 14 15 23 public class Module implements ObjectHandlerIF, EnvAware, 24 ObjectCreationCallback { 25 private static final long NOT_MODIFIED = -1; 26 private String _name; 27 private String _uri; 28 private StateMachine _stm; 29 private boolean _inheritGlobals; 30 private boolean _inheritModules; 31 private Include _include; 32 private long _lastModified = NOT_MODIFIED; 33 private Resource _toMonitor; 34 private SotoContainer.EnvImpl _env; 35 36 public Module() { 37 } 38 39 public StateMachine getStateMachine(boolean reload) { 40 if (reload && (_lastModified > NOT_MODIFIED) && 41 (_lastModified != _toMonitor.lastModified())) { 42 synchronized (this) { 43 if (_lastModified != _toMonitor.lastModified()) { 44 if (Debug.DEBUG) { 45 Debug.debug("Module " + _name + " modified (" + _toMonitor + 46 "); reloading..."); 47 } 48 49 try { 50 StateMachine newStm = (StateMachine) _include.onCreate(); 51 newStm.getStmContext()._parent = _stm.getStmContext().getParent(); 52 53 if (_inheritGlobals && (newStm.getStmContext()._globals != null)) { 54 newStm.getStmContext()._globals.setParent(_stm.getStmContext() 55 .getParent() 56 .getStmContext()._globals); 57 } 58 59 if (_inheritModules) { 60 newStm.getStmContext()._inheritModules = true; 61 } 62 63 newStm.init(); 64 _stm = newStm; 65 } catch (ConfigurationException e) { 66 throw new ModuleReloadRuntimeException("Could not reload state machine", e); 67 } 68 69 if (Debug.DEBUG) { 70 Debug.debug("Module reloaded"); 71 } 72 73 _lastModified = _toMonitor.lastModified(); 74 } 75 } 76 } 77 78 return _stm; 79 } 80 81 public String getName() { 82 return _name; 83 } 84 85 public void setName(String name) { 86 _name = name; 87 } 88 89 public void setInheritGlobals(boolean inherit) { 90 _inheritGlobals = inherit; 91 } 92 93 public boolean isInheritGlobals() { 94 return _inheritGlobals; 95 } 96 97 public void setInheritModules(boolean inherit) { 98 _inheritModules = inherit; 99 } 100 101 public boolean isInheritModules() { 102 return _inheritModules; 103 } 104 105 public void setStateMachine(StateMachine stm) throws ConfigurationException { 106 if (_stm != null) { 107 throw new ConfigurationException("State machine already set for module: " + 108 getName()); 109 } 110 111 _stm = stm; 112 } 113 114 117 public void setEnv(Env env) { 118 if (env instanceof SotoContainer.EnvImpl) { 119 _env = (SotoContainer.EnvImpl) env; 120 } 121 } 122 123 public void setUri(String uri) { 124 _uri = uri; 125 } 126 127 130 public Object onCreate() throws ConfigurationException { 131 if ((_stm == null) && (_uri != null) && (_env != null)) { 132 _include = new Include(_env.getContainer(), 133 _env.getContainer().getApplicationFactory()); 134 _include.setUri(_uri); 135 _stm = (StateMachine) _include.onCreate(); 136 137 Resource res = _include.getResource(); 138 Debug.debug("======> checking: " + res); 139 _toMonitor = res; 140 _lastModified = _toMonitor.lastModified(); 141 } 142 143 return this; 144 } 145 146 149 public void handleObject(String name, Object obj) 150 throws ConfigurationException { 151 if (obj instanceof StateMachine) { 152 setStateMachine((StateMachine) obj); 153 } 154 } 155 } 156 | Popular Tags |