KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sapia > soto > state > Module


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 /**
16  * @author Yanick Duchesne
17  * <dl>
18  * <dt><b>Copyright:</b><dd>Copyright &#169; 2002-2003 <a HREF="http://www.sapia-oss.org">Sapia Open Source Software</a>. All Rights Reserved.</dd></dt>
19  * <dt><b>License:</b><dd>Read the license.txt file of the jar or visit the
20  * <a HREF="http://www.sapia-oss.org/license.html">license page</a> at the Sapia OSS web site</dd></dt>
21  * </dl>
22  */

23 public class Module implements ObjectHandlerIF, EnvAware,
24   ObjectCreationCallback {
25   private static final long NOT_MODIFIED = -1;
26   private String JavaDoc _name;
27   private String JavaDoc _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 JavaDoc getName() {
82     return _name;
83   }
84
85   public void setName(String JavaDoc 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   /**
115    * @see org.sapia.soto.EnvAware#setEnv(org.sapia.soto.Env)
116    */

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 JavaDoc uri) {
124     _uri = uri;
125   }
126
127   /**
128    * @see org.sapia.util.xml.confix.ObjectCreationCallback#onCreate()
129    */

130   public Object JavaDoc 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   /**
147    * @see org.sapia.util.xml.confix.ObjectHandlerIF#handleObject(java.lang.String, java.lang.Object)
148    */

149   public void handleObject(String JavaDoc name, Object JavaDoc obj)
150     throws ConfigurationException {
151     if (obj instanceof StateMachine) {
152       setStateMachine((StateMachine) obj);
153     }
154   }
155 }
156
Popular Tags