001: /*
002: * ====================================================================
003: *
004: * XFLOW - Process Management System
005: * Copyright (C) 2003 Rob Tan
006: * All rights reserved.
007: *
008: * Redistribution and use in source and binary forms, with or without
009: * modification, are permitted provided that the following conditions
010: * are met:
011: *
012: * 1. Redistributions of source code must retain the above copyright
013: * notice, this list of conditions, and the following disclaimer.
014: *
015: * 2. Redistributions in binary form must reproduce the above copyright
016: * notice, this list of conditions, and the disclaimer that follows
017: * these conditions in the documentation and/or other materials
018: * provided with the distribution.
019: *
020: * 3. The name "XFlow" must not be used to endorse or promote products
021: * derived from this software without prior written permission. For
022: * written permission, please contact rcktan@yahoo.com
023: *
024: * 4. Products derived from this software may not be called "XFlow", nor
025: * may "XFlow" appear in their name, without prior written permission
026: * from the XFlow Project Management (rcktan@yahoo.com)
027: *
028: * In addition, we request (but do not require) that you include in the
029: * end-user documentation provided with the redistribution and/or in the
030: * software itself an acknowledgement equivalent to the following:
031: * "This product includes software developed by the
032: * XFlow Project (http://xflow.sourceforge.net/)."
033: * Alternatively, the acknowledgment may be graphical using the logos
034: * available at http://xflow.sourceforge.net/
035: *
036: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
037: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
038: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
039: * DISCLAIMED. IN NO EVENT SHALL THE XFLOW AUTHORS OR THE PROJECT
040: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
041: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
042: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
043: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
044: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
045: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
046: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
047: * SUCH DAMAGE.
048: *
049: * ====================================================================
050: * This software consists of voluntary contributions made by many
051: * individuals on behalf of the XFlow Project and was originally
052: * created by Rob Tan (rcktan@yahoo.com)
053: * For more information on the XFlow Project, please see:
054: * <http://xflow.sourceforge.net/>.
055: * ====================================================================
056: */
057:
058: package xflow.server.controller;
059:
060: import java.io.*;
061: import java.util.*;
062: import java.lang.*;
063: import java.sql.*;
064: import org.apache.log4j.Logger;
065: import xflow.common.*;
066: import xflow.protocol.*;
067: import xflow.util.*;
068:
069: public class Waiting {
070:
071: private static Logger log = Logger.getLogger(Waiting.class);
072:
073: public static void addProcess(WorkflowId wfId, int destNodeId,
074: int fromNodeId) throws XflowException {
075:
076: int workflowId = wfId.getValue();
077:
078: Connection con = null;
079: Statement s = null;
080: try {
081: con = Persistence.getConnection();
082: s = con.createStatement();
083:
084: String sql = "insert into waiting values (" + workflowId
085: + "," + destNodeId + "," + fromNodeId + ")";
086: s.execute(sql);
087: } catch (Exception e) {
088: e.printStackTrace();
089: throw new XflowException(
090: "Failed to save row in waiting table in database");
091: } finally {
092: if (s != null) {
093: try {
094: s.close();
095: } catch (Exception e) {
096: e.printStackTrace();
097: }
098: }
099: if (con != null) {
100: try {
101: con.close();
102: } catch (Exception e) {
103: }
104: }
105: }
106: }
107:
108: public static void removeProcesses(Vector fromNodes, WorkflowId wfId)
109: throws XflowException {
110: int workflowId = wfId.getValue();
111: boolean result = false;
112: int count = fromNodes.size();
113:
114: Connection con = null;
115: Statement s = null;
116: try {
117: con = Persistence.getConnection();
118: s = con.createStatement();
119:
120: String sql = "delete from waiting where workflowid = "
121: + workflowId + " and " + " fromNodeId in (";
122: for (int i = 0; i < count; i++) {
123: Node node = (Node) fromNodes.elementAt(i);
124: int nodeId = node.getNodeId();
125: if (i != 0) {
126: sql += ",";
127: }
128: sql += nodeId;
129: }
130: sql += ")";
131: log.info(sql);
132: s.execute(sql);
133: } catch (Exception e) {
134: e.printStackTrace();
135: throw new XflowException(
136: "Failed to delete rows from waiting table");
137: } finally {
138: if (s != null) {
139: try {
140: s.close();
141: } catch (Exception e) {
142: e.printStackTrace();
143: }
144: }
145: if (con != null) {
146: try {
147: con.close();
148: } catch (Exception e) {
149: }
150: }
151: }
152: }
153:
154: public static void removeProcesses(WorkflowId wfId)
155: throws XflowException {
156: int workflowId = wfId.getValue();
157: Connection con = null;
158: Statement s = null;
159: try {
160: con = Persistence.getConnection();
161: s = con.createStatement();
162:
163: String sql = "delete from waiting where workflowid = "
164: + workflowId;
165: log.info(sql);
166: s.execute(sql);
167: } catch (Exception e) {
168: e.printStackTrace();
169: throw new XflowException(
170: "Failed to delete rows from waiting table");
171: } finally {
172: if (s != null) {
173: try {
174: s.close();
175: } catch (Exception e) {
176: e.printStackTrace();
177: }
178: }
179: if (con != null) {
180: try {
181: con.close();
182: } catch (Exception e) {
183: }
184: }
185: }
186: }
187:
188: public static boolean allProcessesArrived(Vector fromNodes,
189: WorkflowId wfId, int destNodeId) throws XflowException {
190: int workflowId = wfId.getValue();
191: boolean result = false;
192: int count = fromNodes.size();
193:
194: Connection con = null;
195: Statement s = null;
196: try {
197: con = Persistence.getConnection();
198: s = con.createStatement();
199:
200: String sql = "select * from waiting where workflowid = "
201: + workflowId + " and " + "destNodeId = "
202: + destNodeId + " and fromNodeId in (";
203: for (int i = 0; i < count; i++) {
204: Node node = (Node) fromNodes.elementAt(i);
205: int nodeId = node.getNodeId();
206: if (i != 0) {
207: sql += ",";
208: }
209: sql += nodeId;
210: }
211: sql += ")";
212: log.info(sql);
213: ResultSet rs = s.executeQuery(sql);
214: int rcount = 0;
215: while (rs.next()) {
216: rcount++;
217: }
218: log.info("Nodes arrived count: " + rcount
219: + " fromNodes count = " + count);
220: if (rcount == count) {
221: result = true;
222: }
223: } catch (Exception e) {
224: e.printStackTrace();
225: throw new XflowException(
226: "Failed to select rows from waiting table in database");
227: } finally {
228: if (s != null) {
229: try {
230: s.close();
231: } catch (Exception e) {
232: e.printStackTrace();
233: }
234: }
235: if (con != null) {
236: try {
237: con.close();
238: } catch (Exception e) {
239: }
240: }
241: }
242:
243: return result;
244: }
245:
246: }
|