001: /*
002: * Created on Dec 31, 2003
003: *
004: * To change the template for this generated file go to
005: * Window>Preferences>Java>Code Generation>Code and Comments
006: */
007: package org.xdev.base.xssl;
008:
009: import java.util.ArrayList;
010: import java.util.HashMap;
011:
012: import org.xdev.base.xssl.manage.ITransactionFinalizable;
013:
014: /**
015: * @author AYegorov
016: *
017: * To change the template for this generated type comment go to
018: * Window>Preferences>Java>Code Generation>Code and Comments
019: */
020: public abstract class XSSLActionQueue extends XSSLActionIterator
021: implements Runnable, ITransactionFinalizable {
022: protected ArrayList queue = null;
023:
024: protected Thread queueThread = null;
025:
026: private boolean interrupted;
027: private boolean running;
028:
029: private XSSLActionQueue queueProcess = null;
030:
031: /**
032: * @param id
033: */
034: public XSSLActionQueue(String id) {
035: super (id);
036: // XXX Auto-generated constructor stub
037: }
038:
039: /**
040: * @param id
041: * @param properties
042: */
043: public XSSLActionQueue(String id, HashMap properties) {
044: super (id, properties);
045: // XXX Auto-generated constructor stub
046: }
047:
048: /* (non-Javadoc)
049: * @see org.xdev.base.xssl.XSSLReturn#getValue()
050: */
051: public Object getObjectValue() {
052: // XXX Auto-generated method stub
053: return null;
054: }
055:
056: /* (non-Javadoc)
057: * @see org.xdev.base.xssl.XSSLAction#set()
058: */
059: protected void set() throws Exception {
060: this .queueProcess = (XSSLActionQueue) this .getReference();
061:
062: this .logDebug("Queue reference " + queueProcess
063: + " has been initialized: " + (queueProcess != null));
064:
065: if (this .queueProcess != null) {
066: if (!this .queueProcess.isRunning()) {
067: this .queueProcess.start();
068: }
069: } else {
070: this .start();
071: this .queueProcess = this ;
072:
073: this .define();
074: }
075: }
076:
077: protected void invoke(XSSLAction item) throws Exception {
078: this .queueProcess.queue(item);
079: }
080:
081: public void start() {
082:
083: this .logDebug("Queue is starting up.");
084:
085: if (this .queue == null) {
086: this .queue = new ArrayList();
087: this .setReference(this );
088: }
089:
090: if (this .queueThread == null) {
091: this .queueThread = new Thread(this );
092: }
093:
094: this .queueThread.start();
095: this .running = true;
096:
097: this .logDebug("Queue is up and running.");
098: }
099:
100: public void stop() {
101: if (this .queueThread != null) {
102: this .queueThread.interrupt();
103: }
104: this .running = false;
105: }
106:
107: public boolean isRunning() {
108: return this .running && this .queueThread != null
109: && this .queueThread.isAlive();
110: }
111:
112: public void queue(XSSLAction item) {
113:
114: synchronized (queue) {
115:
116: this .logDebug("Queuing item: " + item);
117:
118: this .queue.add(item);
119: this .queue.notifyAll();
120:
121: this .logDebug("Queue has been notified...");
122: }
123: }
124:
125: /* (non-Javadoc)
126: * @see java.lang.Runnable#run()
127: */
128: public void run() {
129:
130: XSSLAction queueItem = null;
131:
132: if (this .queue != null) {
133: try {
134: while (!this .interrupted) {
135: synchronized (queue) {
136: while (queue.isEmpty()) {
137:
138: this
139: .logDebug("Waiting for items being queued...");
140:
141: this .queue.wait();
142: }
143: try {
144:
145: queueItem = (XSSLAction) queue.remove(0);
146:
147: this .logDebug("Processing queue item: "
148: + queueItem);
149:
150: this .processQueueItem(queueItem);
151:
152: queueItem = null;
153: } catch (Exception ex) {
154: this .logDebug(ex);
155: }
156: }
157: }
158: } catch (Exception ex) {
159: this .logDebug(ex);
160: }
161: } else {
162: this .queueThread = null;
163: }
164:
165: }
166:
167: protected abstract void processQueueItem(XSSLAction item)
168: throws Exception;
169:
170: /**
171: * @return Returns the queue.
172: */
173: public ArrayList getQueue() {
174: return queue;
175: }
176:
177: /**
178: * @param queue The queue to set.
179: */
180: public void setQueue(ArrayList queue) {
181: this .queue = queue;
182: }
183:
184: /**
185: * @return Returns the queueThread.
186: */
187: public Thread getQueueThread() {
188: return queueThread;
189: }
190:
191: /**
192: * @param queueThread The queueThread to set.
193: */
194: public void setQueueThread(Thread queueThread) {
195: this .queueThread = queueThread;
196: }
197:
198: public void doFinalize() throws Exception {
199: this .interrupted = true;
200: }
201: }
|