Source Code Cross Referenced for JmsProcessManager.java in  » Workflow-Engines » bpmscript » org » bpmscript » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Workflow Engines » bpmscript » org.bpmscript 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Licensed to the Apache Software Foundation (ASF) under one or more
003:         * contributor license agreements.  See the NOTICE file distributed with
004:         * this work for additional information regarding copyright ownership.
005:         * The ASF licenses this file to You under the Apache License, Version 2.0
006:         * (the "License"); you may not use this file except in compliance with
007:         * the License.  You may obtain a copy of the License at
008:         *
009:         *      http://www.apache.org/licenses/LICENSE-2.0
010:         *
011:         * Unless required by applicable law or agreed to in writing, software
012:         * distributed under the License is distributed on an "AS IS" BASIS,
013:         * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014:         * See the License for the specific language governing permissions and
015:         * limitations under the License.
016:         */
017:        package org.bpmscript;
018:
019:        import java.sql.Timestamp;
020:        import java.util.ArrayList;
021:        import java.util.Collections;
022:        import java.util.Comparator;
023:        import java.util.HashMap;
024:        import java.util.Iterator;
025:        import java.util.List;
026:        import java.util.Map;
027:        import java.util.UUID;
028:
029:        import javax.jms.BytesMessage;
030:        import javax.jms.JMSException;
031:        import javax.jms.Message;
032:        import javax.jms.MessageConsumer;
033:        import javax.jms.MessageProducer;
034:        import javax.jms.Queue;
035:        import javax.jms.Session;
036:
037:        import org.bpmscript.jms.JmsTemplate;
038:        import org.bpmscript.jms.JmsTemplateException;
039:        import org.bpmscript.jms.SessionCallback;
040:        import org.springframework.beans.factory.InitializingBean;
041:
042:        /**
043:         * Jms version of the Process Manage for clustering
044:         */
045:        public class JmsProcessManager implements  IProcessManager,
046:                InitializingBean {
047:
048:            private final org.apache.commons.logging.Log log = org.apache.commons.logging.LogFactory
049:                    .getLog(JmsProcessManager.class);
050:
051:            private IMarshaler marshaler = new XStreamMarshaler();
052:
053:            private JmsTemplate template = null;
054:
055:            private String processQueueName = null;
056:            private String processInstanceQueueName = null;
057:            private String processSourceQueueName = null;
058:
059:            private Queue processQueue = null;
060:            private Queue processInstanceQueue = null;
061:            private Queue processSourceQueue = null;
062:
063:            private long receiveTimeout = 2000;
064:            private long instanceReceiveTimeout = 60 * 1000;
065:
066:            private long completedMessageExpiration = 24 * 60 * 60 * 1000;
067:
068:            //	private Map<String, Process> processCache = new ConcurrentHashMap<String, Process>();
069:
070:            public void copyMessageBytes(BytesMessage in, BytesMessage out)
071:                    throws JMSException {
072:                if (in.getBodyLength() > Integer.MAX_VALUE) {
073:                    throw new RuntimeException("more bytes than max int");
074:                }
075:                byte[] bytes = new byte[(int) in.getBodyLength()];
076:                in.readBytes(bytes);
077:                out.writeBytes(bytes);
078:            }
079:
080:            public String createProcessInstance(
081:                    final String parentProcessInstanceId,
082:                    final String processName, final String operation)
083:                    throws BpmScriptException {
084:
085:                final String id = UUID.randomUUID().toString();
086:
087:                try {
088:                    final Message processMessage = template.getBySelector(
089:                            processQueue, "name = '" + processName
090:                                    + "' and primaryProcess = true");
091:                    template.execute(new SessionCallback() {
092:
093:                        public Object doInJms(Session session) throws Exception {
094:                            BytesMessage instanceMessage = session
095:                                    .createBytesMessage();
096:                            instanceMessage.setStringProperty("processState",
097:                                    ProcessState.CREATED.name());
098:                            instanceMessage.setStringProperty("id", id);
099:                            instanceMessage.setStringProperty("processId",
100:                                    processMessage.getStringProperty("id"));
101:                            instanceMessage.setStringProperty("processName",
102:                                    processName);
103:                            instanceMessage.setLongProperty("lastModified",
104:                                    System.currentTimeMillis());
105:                            instanceMessage.setLongProperty("creationDate",
106:                                    System.currentTimeMillis());
107:                            if (parentProcessInstanceId != null) {
108:                                instanceMessage.setStringProperty(
109:                                        "parentProcessInstanceId",
110:                                        parentProcessInstanceId);
111:                            }
112:                            instanceMessage.setStringProperty("operation",
113:                                    operation);
114:                            MessageProducer producer = session
115:                                    .createProducer(processInstanceQueue);
116:                            producer.send(instanceMessage);
117:                            return null;
118:                        }
119:
120:                    });
121:                } catch (JmsTemplateException e) {
122:                    throw new BpmScriptException(e);
123:                }
124:
125:                log.debug("created process instance with id " + id
126:                        + " for process " + processName);
127:
128:                return id;
129:
130:            }
131:
132:            public ExecutorResult doWithProcessInstance(
133:                    final String processInstanceId,
134:                    final IProcessInstanceCallback callback) throws Throwable {
135:                try {
136:                    return (ExecutorResult) template.execute(true,
137:                            Session.AUTO_ACKNOWLEDGE, new SessionCallback() {
138:
139:                                public Object doInJms(Session session)
140:                                        throws Exception {
141:                                    try {
142:                                        //						BytesMessage instanceMessage = (BytesMessage) template.getBySelector(session, processInstanceQueue, "id = '" + processInstanceId + "'");
143:                                        //						if(instanceMessage == null) {
144:                                        //							throw new BpmScriptException("no process instance for " + processInstanceId + " could be found");
145:                                        //						}
146:                                        MessageConsumer consumer = session
147:                                                .createConsumer(
148:                                                        processInstanceQueue,
149:                                                        "id = '"
150:                                                                + processInstanceId
151:                                                                + "'");
152:                                        log
153:                                                .debug("requesting lock for processing instance id "
154:                                                        + processInstanceId);
155:                                        BytesMessage instanceMessage = (BytesMessage) consumer
156:                                                .receive(instanceReceiveTimeout);
157:                                        log
158:                                                .debug("got and locked processing instance id "
159:                                                        + processInstanceId);
160:                                        if (instanceMessage == null) {
161:                                            throw new BpmScriptException(
162:                                                    "could not get instance in time");
163:                                        }
164:                                        String processId = instanceMessage
165:                                                .getStringProperty("processId");
166:                                        Process process = messageToProcess(template
167:                                                .getBySelector(session,
168:                                                        processQueue, "id = '"
169:                                                                + processId
170:                                                                + "'"));
171:                                        //						Process process = processCache.get(processId);
172:                                        //						if(process == null) {
173:                                        //							process = messageToProcess(template.getBySelector(session, processQueue, "id = '" + processId + "'"));
174:                                        //							processCache.put(processId, process);
175:                                        //						}
176:                                        IProcessInstance instance = messageToInstance(
177:                                                instanceMessage, process);
178:
179:                                        MessageProducer producer = session
180:                                                .createProducer(processInstanceQueue);
181:
182:                                        ExecutorResult executorResult = null;
183:                                        try {
184:                                            executorResult = callback
185:                                                    .execute(instance);
186:                                            if (executorResult
187:                                                    .getProcessState() == ProcessState.PAUSED) {
188:                                                PausedResult pausedResult = ((PausedResult) executorResult);
189:                                                BytesMessage newInstanceMessage = session
190:                                                        .createBytesMessage();
191:                                                newInstanceMessage
192:                                                        .setStringProperty(
193:                                                                "id",
194:                                                                processInstanceId);
195:                                                newInstanceMessage
196:                                                        .setStringProperty(
197:                                                                "stackTraceElements",
198:                                                                marshaler
199:                                                                        .marshal(pausedResult
200:                                                                                .getStackTraceElements()));
201:                                                newInstanceMessage
202:                                                        .setStringProperty(
203:                                                                "throwable",
204:                                                                null);
205:                                                newInstanceMessage
206:                                                        .setStringProperty(
207:                                                                "processState",
208:                                                                ProcessState.PAUSED
209:                                                                        .name());
210:                                                newInstanceMessage
211:                                                        .setLongProperty(
212:                                                                "lastModified",
213:                                                                System
214:                                                                        .currentTimeMillis());
215:                                                newInstanceMessage
216:                                                        .setLongProperty(
217:                                                                "creationDate",
218:                                                                instanceMessage
219:                                                                        .getLongProperty("creationDate"));
220:                                                newInstanceMessage
221:                                                        .setStringProperty(
222:                                                                "processName",
223:                                                                instanceMessage
224:                                                                        .getStringProperty("processName"));
225:                                                newInstanceMessage
226:                                                        .setStringProperty(
227:                                                                "processId",
228:                                                                processId);
229:                                                newInstanceMessage
230:                                                        .setStringProperty(
231:                                                                "operation",
232:                                                                instanceMessage
233:                                                                        .getStringProperty("operation"));
234:                                                newInstanceMessage
235:                                                        .setStringProperty(
236:                                                                "parentProcessInstanceId",
237:                                                                instanceMessage
238:                                                                        .getStringProperty("parentProcessInstanceId"));
239:                                                newInstanceMessage
240:                                                        .writeBytes(pausedResult
241:                                                                .getContinuation());
242:                                                producer
243:                                                        .send(newInstanceMessage);
244:                                                session.commit();
245:                                            } else if (executorResult
246:                                                    .getProcessState() == ProcessState.COMPLETED) {
247:                                                BytesMessage newInstanceMessage = session
248:                                                        .createBytesMessage();
249:                                                newInstanceMessage
250:                                                        .setStringProperty(
251:                                                                "id",
252:                                                                processInstanceId);
253:                                                newInstanceMessage
254:                                                        .setStringProperty(
255:                                                                "processState",
256:                                                                ProcessState.COMPLETED
257:                                                                        .name());
258:                                                newInstanceMessage
259:                                                        .setLongProperty(
260:                                                                "lastModified",
261:                                                                System
262:                                                                        .currentTimeMillis());
263:                                                newInstanceMessage
264:                                                        .setLongProperty(
265:                                                                "creationDate",
266:                                                                instanceMessage
267:                                                                        .getLongProperty("creationDate"));
268:                                                newInstanceMessage
269:                                                        .setStringProperty(
270:                                                                "processName",
271:                                                                instanceMessage
272:                                                                        .getStringProperty("processName"));
273:                                                newInstanceMessage
274:                                                        .setStringProperty(
275:                                                                "processId",
276:                                                                processId);
277:                                                newInstanceMessage
278:                                                        .setStringProperty(
279:                                                                "operation",
280:                                                                instanceMessage
281:                                                                        .getStringProperty("operation"));
282:                                                newInstanceMessage
283:                                                        .setStringProperty(
284:                                                                "parentProcessInstanceId",
285:                                                                instanceMessage
286:                                                                        .getStringProperty("parentProcessInstanceId"));
287:                                                if (completedMessageExpiration > 0) {
288:                                                    newInstanceMessage
289:                                                            .setJMSExpiration(completedMessageExpiration);
290:                                                }
291:                                                producer
292:                                                        .send(newInstanceMessage);
293:                                                session.commit();
294:                                            } else if (executorResult
295:                                                    .getProcessState() == ProcessState.FAILED) {
296:                                                FailedResult failedResult = ((FailedResult) executorResult);
297:                                                Throwable throwable = failedResult
298:                                                        .getThrowable();
299:                                                BytesMessage newInstanceMessage = session
300:                                                        .createBytesMessage();
301:                                                newInstanceMessage
302:                                                        .setStringProperty(
303:                                                                "id",
304:                                                                processInstanceId);
305:                                                newInstanceMessage
306:                                                        .setStringProperty(
307:                                                                "stackTraceElements",
308:                                                                marshaler
309:                                                                        .marshal(failedResult
310:                                                                                .getStackTraceElements()));
311:                                                newInstanceMessage
312:                                                        .setStringProperty(
313:                                                                "throwable",
314:                                                                marshaler
315:                                                                        .marshal(throwable));
316:                                                newInstanceMessage
317:                                                        .setStringProperty(
318:                                                                "processState",
319:                                                                ProcessState.FAILED
320:                                                                        .name());
321:                                                newInstanceMessage
322:                                                        .setLongProperty(
323:                                                                "lastModified",
324:                                                                System
325:                                                                        .currentTimeMillis());
326:                                                newInstanceMessage
327:                                                        .setLongProperty(
328:                                                                "creationDate",
329:                                                                instanceMessage
330:                                                                        .getLongProperty("creationDate"));
331:                                                newInstanceMessage
332:                                                        .setStringProperty(
333:                                                                "processName",
334:                                                                instanceMessage
335:                                                                        .getStringProperty("processName"));
336:                                                newInstanceMessage
337:                                                        .setStringProperty(
338:                                                                "processId",
339:                                                                processId);
340:                                                newInstanceMessage
341:                                                        .setStringProperty(
342:                                                                "operation",
343:                                                                instanceMessage
344:                                                                        .getStringProperty("operation"));
345:                                                newInstanceMessage
346:                                                        .setStringProperty(
347:                                                                "parentProcessInstanceId",
348:                                                                instanceMessage
349:                                                                        .getStringProperty("parentProcessInstanceId"));
350:                                                copyMessageBytes(
351:                                                        instanceMessage,
352:                                                        newInstanceMessage);
353:                                                producer
354:                                                        .send(newInstanceMessage);
355:                                                session.commit();
356:                                                throw new ThrowableHolderException(
357:                                                        throwable);
358:                                            } else {
359:                                                session.commit();
360:                                            }
361:                                        } catch (ThrowableHolderException t) {
362:                                            throw t;
363:                                        } catch (Throwable t) {
364:                                            session.rollback();
365:                                            throw new ThrowableHolderException(
366:                                                    t);
367:                                        }
368:                                        return executorResult;
369:                                    } catch (ThrowableHolderException t) {
370:                                        throw t;
371:                                    } catch (Throwable t) {
372:                                        session.rollback();
373:                                        throw new ThrowableHolderException(t);
374:                                    } finally {
375:                                        log
376:                                                .debug("unlocked processing instance id "
377:                                                        + processInstanceId);
378:                                    }
379:                                }
380:
381:                            });
382:                } catch (JmsTemplateException e) {
383:                    if (e.getCause() != null
384:                            && e.getCause() instanceof  ThrowableHolderException) {
385:                        throw e.getCause();
386:                    } else {
387:                        throw e;
388:                    }
389:                }
390:            }
391:
392:            public IProcessSource getMainSource(String processId)
393:                    throws BpmScriptException {
394:                Message processSourceMessage;
395:                try {
396:                    processSourceMessage = template.getBySelector(
397:                            processSourceQueue, "processId = '" + processId
398:                                    + "' and main = true");
399:                    ProcessSource result = messageToProcessSource(processSourceMessage);
400:                    return result;
401:                } catch (JmsTemplateException e) {
402:                    throw new BpmScriptException(e);
403:                } catch (JMSException e) {
404:                    throw new BpmScriptException(e);
405:                }
406:            }
407:
408:            private ProcessSource messageToProcessSource(
409:                    Message processSourceMessage) throws JMSException {
410:                if (processSourceMessage == null) {
411:                    return null;
412:                }
413:                ProcessSource result = new ProcessSource();
414:                result.setCreationDate(new Timestamp(processSourceMessage
415:                        .getLongProperty("creationDate")));
416:                result.setId(processSourceMessage.getStringProperty("id"));
417:                result.setLastModified(new Timestamp(processSourceMessage
418:                        .getLongProperty("lastModified")));
419:                result.setMain(true);
420:                result.setName(processSourceMessage.getStringProperty("name"));
421:                result.setSource(processSourceMessage
422:                        .getStringProperty("source"));
423:                return result;
424:            }
425:
426:            public IPagedResult<IProcessInstance> getProcessInstances(
427:                    IQuery query) throws BpmScriptException {
428:
429:                List<Message> results;
430:                try {
431:                    results = template.findBySelector(processInstanceQueue,
432:                            null);
433:
434:                    String filter = query.getFilter();
435:                    if (filter != null) {
436:                        filter = filter.trim().toUpperCase();
437:                        if (filter.length() > 0) {
438:                            for (Iterator resultIterator = results.iterator(); resultIterator
439:                                    .hasNext();) {
440:                                Message result = (Message) resultIterator
441:                                        .next();
442:                                if (!result.getStringProperty("processName")
443:                                        .toUpperCase().contains(filter)) {
444:                                    resultIterator.remove();
445:                                }
446:                            }
447:                        }
448:                    }
449:
450:                    final List<IOrderBy> orderBys = query.getOrderBys();
451:                    if (orderBys != null && orderBys.size() > 0) {
452:
453:                        try {
454:                            Collections.sort(results,
455:                                    new Comparator<Message>() {
456:
457:                                        @SuppressWarnings("unchecked")
458:                                        public int compare(Message o1,
459:                                                Message o2) {
460:                                            for (IOrderBy orderBy : orderBys) {
461:                                                try {
462:                                                    Comparable c1 = (Comparable) o1
463:                                                            .getObjectProperty(orderBy
464:                                                                    .getField());
465:                                                    Comparable c2 = (Comparable) o2
466:                                                            .getObjectProperty(orderBy
467:                                                                    .getField());
468:                                                    int compareTo = c1
469:                                                            .compareTo(c2);
470:                                                    if (compareTo != 0) {
471:                                                        return orderBy.isAsc() ? compareTo
472:                                                                : -compareTo;
473:                                                    }
474:                                                } catch (JMSException e) {
475:                                                    throw new RuntimeException(
476:                                                            e);
477:                                                }
478:                                            }
479:                                            return 0;
480:                                        }
481:
482:                                    });
483:                        } catch (RuntimeException e) {
484:                            Throwable cause = e.getCause();
485:                            if (cause != null
486:                                    && cause instanceof  BpmScriptException) {
487:                                throw (BpmScriptException) cause;
488:                            } else {
489:                                throw e;
490:                            }
491:                        }
492:                    }
493:                    List<Message> trimmedResults = null;
494:                    int lastResult = query.getFirstResult()
495:                            + query.getMaxResults();
496:
497:                    if (query.getFirstResult() >= 0
498:                            && query.getMaxResults() >= 0) {
499:                        trimmedResults = new ArrayList<Message>(
500:                                results
501:                                        .subList(
502:                                                query.getFirstResult(),
503:                                                lastResult < results.size() ? lastResult
504:                                                        : results.size()));
505:                    } else {
506:                        trimmedResults = results;
507:                    }
508:
509:                    // TODO: need to speed this up, somehow...
510:                    List<Message> processes = template.findBySelector(
511:                            processQueue, null);
512:                    Map<String, Message> processMap = new HashMap<String, Message>();
513:                    for (Message message : processes) {
514:                        processMap
515:                                .put(message.getStringProperty("id"), message);
516:                    }
517:
518:                    List<IProcessInstance> instances = new ArrayList<IProcessInstance>();
519:                    for (Message message : trimmedResults) {
520:                        Message processMessage = processMap.get(message
521:                                .getStringProperty("processId"));
522:                        ProcessInstance processInstance = messageToInstance(
523:                                message, messageToProcess(processMessage));
524:                        instances.add(processInstance);
525:                    }
526:
527:                    return new PagedResult<IProcessInstance>(instances,
528:                            lastResult > 0 && lastResult < results.size(),
529:                            results.size());
530:
531:                } catch (JmsTemplateException e) {
532:                    throw new BpmScriptException(e);
533:                } catch (JMSException e) {
534:                    throw new BpmScriptException(e);
535:                }
536:            }
537:
538:            private ProcessInstance messageToInstance(Message message,
539:                    Process process) throws JMSException {
540:                if (message == null) {
541:                    return null;
542:                }
543:                BytesMessage bytesMessage = (BytesMessage) message;
544:                ProcessInstance processInstance = new ProcessInstance();
545:                long bodyLength = bytesMessage.getBodyLength();
546:                if (bodyLength > 0) {
547:                    byte[] body = new byte[(int) bodyLength];
548:                    bytesMessage.readBytes(body);
549:                    processInstance.setContinuation(body);
550:                }
551:                processInstance.setCreationDate(new Timestamp(message
552:                        .getLongProperty("creationDate")));
553:                processInstance.setLastModified(new Timestamp(message
554:                        .getLongProperty("lastModified")));
555:                processInstance.setId(message.getStringProperty("id"));
556:                processInstance.setOperation(message
557:                        .getStringProperty("operation"));
558:                processInstance.setParentProcessInstanceId(message
559:                        .getStringProperty("parentProcessInstanceId"));
560:                processInstance.setState(message
561:                        .getStringProperty("processState"));
562:                processInstance
563:                        .setStackTraceElements((StackTraceElement[]) marshaler
564:                                .unmarshal(message
565:                                        .getStringProperty("stackTraceElements")));
566:                processInstance.setThrowable((Throwable) marshaler
567:                        .unmarshal(message.getStringProperty("throwable")));
568:                processInstance.setProcess(process);
569:                return processInstance;
570:            }
571:
572:            private Process messageToProcess(Message processMessage)
573:                    throws JMSException {
574:                if (processMessage == null) {
575:                    return null;
576:                }
577:                Process process = new Process();
578:                process.setId(processMessage.getStringProperty("id"));
579:                process.setCreationDate(new Timestamp(processMessage
580:                        .getLongProperty("creationDate")));
581:                process.setLastModified(new Timestamp(processMessage
582:                        .getLongProperty("lastModified")));
583:                process.setName(processMessage.getStringProperty("namespace"));
584:                process.setName(processMessage.getStringProperty("name"));
585:                process.setPrimaryProcess(processMessage
586:                        .getBooleanProperty("primaryProcess"));
587:                process.setPinned(processMessage.getBooleanProperty("pinned"));
588:                return process;
589:            }
590:
591:            public List<IProcess> getPrimaryProcesses()
592:                    throws BpmScriptException {
593:                try {
594:                    List<Message> processes = template.findBySelector(
595:                            processQueue, "primaryProcess = true");
596:                    List<IProcess> result = new ArrayList<IProcess>();
597:                    for (Message message : processes) {
598:                        result.add(messageToProcess(message));
599:                    }
600:                    return result;
601:                } catch (JmsTemplateException e) {
602:                    throw new BpmScriptException(e);
603:                } catch (JMSException e) {
604:                    throw new BpmScriptException(e);
605:                }
606:            }
607:
608:            public IProcess getProcess(String processId)
609:                    throws BpmScriptException {
610:                try {
611:                    Message message = template.getBySelector(processQueue,
612:                            "id = '" + processId + "'");
613:                    return messageToProcess(message);
614:                } catch (JmsTemplateException e) {
615:                    throw new BpmScriptException(e);
616:                } catch (JMSException e) {
617:                    throw new BpmScriptException(e);
618:                }
619:            }
620:
621:            public List<IProcessSource> getProcessSources(String processId)
622:                    throws BpmScriptException {
623:                try {
624:                    List<Message> processSources = template.findBySelector(
625:                            processSourceQueue, "processId = '" + processId
626:                                    + "'");
627:                    List<IProcessSource> result = new ArrayList<IProcessSource>();
628:                    for (Message message : processSources) {
629:                        result.add(messageToProcessSource(message));
630:                    }
631:                    return result;
632:                } catch (JmsTemplateException e) {
633:                    throw new BpmScriptException(e);
634:                } catch (JMSException e) {
635:                    throw new BpmScriptException(e);
636:                }
637:            }
638:
639:            public void setProcessAsPrimary(final String processId)
640:                    throws BpmScriptException {
641:                try {
642:                    template.executeTransacted(new SessionCallback() {
643:
644:                        public Object doInJms(Session session) throws Exception {
645:                            Message newPrimary = template.getBySelector(
646:                                    session, processQueue, "id = '" + processId
647:                                            + "'");
648:                            if (newPrimary == null) {
649:                                throw new BpmScriptException(
650:                                        "no process found for " + processId);
651:                            }
652:
653:                            Message oldPrimary = template.getBySelector(
654:                                    session, processQueue, "name = '"
655:                                            + newPrimary
656:                                                    .getStringProperty("name")
657:                                            + "' and primaryProcess = true");
658:
659:                            if (oldPrimary != null) {
660:
661:                                MessageConsumer oldConsumer = session
662:                                        .createConsumer(
663:                                                processQueue,
664:                                                "id = '"
665:                                                        + oldPrimary
666:                                                                .getStringProperty("id")
667:                                                        + "'");
668:                                Message message = oldConsumer
669:                                        .receive(receiveTimeout);
670:                                if (message == null) {
671:                                    throw new BpmScriptException(
672:                                            "could not take old primary off the queue");
673:                                }
674:                                Message newOldPrimary = template.copyMessage(
675:                                        session, message);
676:                                newOldPrimary.setBooleanProperty(
677:                                        "primaryProcess", false);
678:                                newOldPrimary.setLongProperty("lastModified",
679:                                        System.currentTimeMillis());
680:
681:                                MessageProducer producer = session
682:                                        .createProducer(processQueue);
683:                                producer.send(processQueue, newOldPrimary);
684:                            }
685:
686:                            MessageConsumer newConsumer = session
687:                                    .createConsumer(processQueue, "id = '"
688:                                            + processId + "'");
689:                            Message message = newConsumer
690:                                    .receive(receiveTimeout);
691:                            if (message == null) {
692:                                throw new BpmScriptException(
693:                                        "could not take new primary off the queue");
694:                            }
695:                            Message newNewPrimary = template.copyMessage(
696:                                    session, message);
697:                            newNewPrimary.setBooleanProperty("primaryProcess",
698:                                    true);
699:                            newNewPrimary.setLongProperty("lastModified",
700:                                    System.currentTimeMillis());
701:                            MessageProducer producer = session
702:                                    .createProducer(processQueue);
703:                            producer.send(processQueue, newNewPrimary);
704:                            return null;
705:                        }
706:                    });
707:                } catch (JmsTemplateException e) {
708:                    throw new BpmScriptException(e);
709:                }
710:            }
711:
712:            public String createProcess(final String source, final String name,
713:                    final boolean pinned) throws BpmScriptException {
714:                final String id = UUID.randomUUID().toString();
715:                try {
716:                    template.execute(new SessionCallback() {
717:
718:                        public Object doInJms(Session session) throws Exception {
719:                            Message message = session.createMessage();
720:                            message.setStringProperty("id", id);
721:                            message.setLongProperty("lastModified", System
722:                                    .currentTimeMillis());
723:                            message.setLongProperty("creationDate", System
724:                                    .currentTimeMillis());
725:                            message.setStringProperty("namespace", source);
726:                            message.setStringProperty("name", name);
727:                            message.setBooleanProperty("primaryProcess", false);
728:                            message.setBooleanProperty("pinned", pinned);
729:                            MessageProducer producer = session
730:                                    .createProducer(processQueue);
731:                            producer.send(message);
732:                            return null;
733:                        }
734:
735:                    });
736:                } catch (JmsTemplateException e) {
737:                    throw new BpmScriptException(e);
738:                }
739:                return id;
740:            }
741:
742:            public void addSourceToProcess(final String processId,
743:                    final String name, final String source, final boolean main)
744:                    throws BpmScriptException {
745:                final String id = UUID.randomUUID().toString();
746:                try {
747:                    template.execute(new SessionCallback() {
748:
749:                        public Object doInJms(Session session) throws Exception {
750:                            Message message = session.createMessage();
751:                            message.setStringProperty("id", id);
752:                            message.setLongProperty("lastModified", System
753:                                    .currentTimeMillis());
754:                            message.setLongProperty("creationDate", System
755:                                    .currentTimeMillis());
756:                            message.setStringProperty("name", name);
757:                            message.setStringProperty("source", source);
758:                            message.setBooleanProperty("main", main);
759:                            message.setStringProperty("processId", processId);
760:                            MessageProducer producer = session
761:                                    .createProducer(processSourceQueue);
762:                            producer.send(message);
763:                            return null;
764:                        }
765:
766:                    });
767:                } catch (JmsTemplateException e) {
768:                    throw new BpmScriptException(e);
769:                }
770:            }
771:
772:            public IProcessInstance getProcessInstance(String processInstanceId)
773:                    throws BpmScriptException {
774:                try {
775:                    Message instance = template.getBySelector(
776:                            processInstanceQueue, "id = '" + processInstanceId
777:                                    + "'");
778:                    if (instance == null) {
779:                        return null;
780:                    } else {
781:                        return messageToInstance(
782:                                instance,
783:                                messageToProcess(template
784:                                        .getBySelector(
785:                                                processQueue,
786:                                                "id = '"
787:                                                        + instance
788:                                                                .getStringProperty("processId")
789:                                                        + "'")));
790:                    }
791:                } catch (JmsTemplateException e) {
792:                    throw new BpmScriptException(e);
793:                } catch (JMSException e) {
794:                    throw new BpmScriptException(e);
795:                }
796:            }
797:
798:            public IProcessSource getProcessSource(String processSourceId)
799:                    throws BpmScriptException {
800:                try {
801:                    Message processSource = template.getBySelector(
802:                            processSourceQueue, "id = '" + processSourceId
803:                                    + "'");
804:                    if (processSource == null) {
805:                        return null;
806:                    } else {
807:                        return messageToProcessSource(processSource);
808:                    }
809:                } catch (JmsTemplateException e) {
810:                    throw new BpmScriptException(e);
811:                } catch (JMSException e) {
812:                    throw new BpmScriptException(e);
813:                }
814:            }
815:
816:            public List<IProcessInstance> getChildProcessInstances(
817:                    String processInstanceId) throws BpmScriptException {
818:                try {
819:                    List<Message> processInstances = template.findBySelector(
820:                            processInstanceQueue, "parentProcessInstanceId = '"
821:                                    + processInstanceId + "'");
822:                    List<IProcessInstance> result = new ArrayList<IProcessInstance>();
823:                    for (Message message : processInstances) {
824:                        result
825:                                .add(messageToInstance(
826:                                        message,
827:                                        messageToProcess(template
828:                                                .getBySelector(
829:                                                        processQueue,
830:                                                        "id = '"
831:                                                                + message
832:                                                                        .getStringProperty("processId")
833:                                                                + "'"))));
834:                    }
835:                    return result;
836:                } catch (JmsTemplateException e) {
837:                    throw new BpmScriptException(e);
838:                } catch (JMSException e) {
839:                    throw new BpmScriptException(e);
840:                }
841:            }
842:
843:            public IProcess getPrimaryProcess(String name)
844:                    throws BpmScriptException {
845:                try {
846:                    return messageToProcess(template.getBySelector(
847:                            processQueue, "name = '" + name
848:                                    + "' and primaryProcess = true"));
849:                } catch (JmsTemplateException e) {
850:                    throw new BpmScriptException(e);
851:                } catch (JMSException e) {
852:                    throw new BpmScriptException(e);
853:                }
854:            }
855:
856:            public void setCompletedMessageExpiration(
857:                    long completedMessageExpiration) {
858:                this .completedMessageExpiration = completedMessageExpiration;
859:            }
860:
861:            public void setInstanceReceiveTimeout(long instanceReceiveTimeout) {
862:                this .instanceReceiveTimeout = instanceReceiveTimeout;
863:            }
864:
865:            public void setMarshaler(IMarshaler marshaler) {
866:                this .marshaler = marshaler;
867:            }
868:
869:            public void setProcessInstanceQueueName(
870:                    String processInstanceQueueName) {
871:                this .processInstanceQueueName = processInstanceQueueName;
872:            }
873:
874:            public void setProcessQueueName(String processQueueName) {
875:                this .processQueueName = processQueueName;
876:            }
877:
878:            public void setProcessSourceQueueName(String processSourceQueueName) {
879:                this .processSourceQueueName = processSourceQueueName;
880:            }
881:
882:            public void setReceiveTimeout(long receiveTimeout) {
883:                this .receiveTimeout = receiveTimeout;
884:            }
885:
886:            public void setTemplate(JmsTemplate template) {
887:                this .template = template;
888:            }
889:
890:            public void afterPropertiesSet() throws Exception {
891:                try {
892:                    processInstanceQueue = template
893:                            .getQueue(processInstanceQueueName);
894:                    processQueue = template.getQueue(processQueueName);
895:                    processSourceQueue = template
896:                            .getQueue(processSourceQueueName);
897:                } catch (JmsTemplateException e) {
898:                    throw new BpmScriptException(e);
899:                }
900:            }
901:
902:            public List<IProcess> getProcessVersions(String name)
903:                    throws BpmScriptException {
904:                // TODO Auto-generated method stub
905:                return null;
906:            }
907:
908:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.