001: /*--
002:
003: Copyright (C) 2002-2005 Adrian Price.
004: All rights reserved.
005:
006: Redistribution and use in source and binary forms, with or without
007: modification, are permitted provided that the following conditions
008: are met:
009:
010: 1. Redistributions of source code must retain the above copyright
011: notice, this list of conditions, and the following disclaimer.
012:
013: 2. Redistributions in binary form must reproduce the above copyright
014: notice, this list of conditions, and the disclaimer that follows
015: these conditions in the documentation and/or other materials
016: provided with the distribution.
017:
018: 3. The names "OBE" and "Open Business Engine" must not be used to
019: endorse or promote products derived from this software without prior
020: written permission. For written permission, please contact
021: adrianprice@sourceforge.net.
022:
023: 4. Products derived from this software may not be called "OBE" or
024: "Open Business Engine", nor may "OBE" or "Open Business Engine"
025: appear in their name, without prior written permission from
026: Adrian Price (adrianprice@users.sourceforge.net).
027:
028: THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
029: WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
030: OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
031: DISCLAIMED. IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT,
032: INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
033: (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
034: SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
035: HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
036: STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
037: IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
038: POSSIBILITY OF SUCH DAMAGE.
039:
040: For more information on OBE, please see
041: <http://obe.sourceforge.net/>.
042:
043: */
044:
045: package org.obe.runtime.tool;
046:
047: import org.apache.commons.logging.Log;
048: import org.apache.commons.logging.LogFactory;
049: import org.apache.velocity.app.Velocity;
050: import org.apache.velocity.context.Context;
051: import org.obe.OBEException;
052: import org.obe.client.api.repository.RepositoryException;
053: import org.obe.engine.EngineContext;
054: import org.obe.engine.WorkflowEngine;
055: import org.obe.spi.service.ServerConfig;
056: import org.w3c.dom.Document;
057:
058: import javax.mail.*;
059: import javax.mail.internet.InternetAddress;
060: import javax.mail.internet.MimeBodyPart;
061: import javax.mail.internet.MimeMessage;
062: import javax.mail.internet.MimeMultipart;
063: import java.io.*;
064: import java.net.MalformedURLException;
065: import java.net.URL;
066: import java.util.Arrays;
067: import java.util.Map;
068:
069: /**
070: * Provides run-time procedures that can be invoked by a workflow.
071: *
072: * @author Adrian Price
073: */
074: public class OBEProcedures {
075: private static final Log _logger = LogFactory
076: .getLog(OBEProcedures.class);
077: public static final String DEBUG = "debug";
078: public static final String TRACE = "trace";
079: public static final String ERROR = "error";
080: public static final String WARN = "warn";
081: public static final String INFO = "info";
082:
083: /**
084: * Assigns a process instance attribute.
085: *
086: * @param value The value to assign.
087: * @return The <code>value</code> parameter, unchanged.
088: */
089: public static Object assignProcessInstanceAttribute(Object value) {
090: // This looks weird, but the process definition invokes this
091: // procedure with an OUT parameter of the process instance attribute
092: // to assign. Thus, the assignment is actually done by the engine.
093: return value;
094: }
095:
096: /*
097: public static void assignActivityInstanceAttribute(EngineContext ctx,
098: String attrName, Object attrValue) throws RepositoryException {
099:
100: AttributeInstance attr = ctx.getActivityInstance().getAttributeInstance(
101: attrName);
102: attr.setValue(AttributeInstance.DEFAULT_TYPE, attrValue);
103: }
104:
105: public static void assignWorkItemAttribute(EngineContext ctx,
106: String attrName, Object attrValue) throws RepositoryException {
107:
108: AttributeInstance attr = ctx.getWorkItem().getAttributeInstance(
109: attrName);
110: attr.setValue(AttributeInstance.DEFAULT_TYPE, attrValue);
111: }
112: */
113:
114: public static void log(String level, Object message) {
115: if (level.equalsIgnoreCase(DEBUG)) {
116: _logger.debug(message);
117: } else if (level.equalsIgnoreCase(TRACE)) {
118: _logger.debug(message);
119: } else if (level.equalsIgnoreCase(ERROR)) {
120: _logger.error(message);
121: } else if (level.equalsIgnoreCase(WARN)) {
122: _logger.warn(message);
123: } else if (level.equalsIgnoreCase(INFO)) {
124: _logger.info(message);
125: } else {
126: // Prefix the message with a custom level.
127: message = level + ": " + message;
128: _logger.info(message);
129: }
130: }
131:
132: public static Document transform(Document src, String transformURI,
133: Map parms) throws RepositoryException {
134:
135: // Must use the data converter belonging to the calling engine.
136: return WorkflowEngine.getEngine().getServiceManager()
137: .getDataConverter().transform(src, transformURI, parms);
138: }
139:
140: // This overload is provided because currently XPDL cannot describe
141: // actual parameters of type array.
142: public static void sendEmail(String subject, String body,
143: String att, String to, String cc, String bcc)
144: throws MalformedURLException, OBEException {
145:
146: sendEmail(subject, body, att == null ? null
147: : new URL[] { new URL(att) }, to == null ? null
148: : new String[] { to }, cc == null ? null
149: : new String[] { cc }, bcc == null ? null
150: : new String[] { bcc });
151: }
152:
153: public static void sendEmail(String subject, String body,
154: URL[] att, String[] to, String[] cc, String[] bcc)
155: throws OBEException {
156:
157: if (_logger.isDebugEnabled()) {
158: _logger.debug("sendEmail(" + subject + ", " + body + ", "
159: + (att == null ? null : Arrays.asList(att)) + ", "
160: + (to == null ? null : Arrays.asList(to)) + ", "
161: + (cc == null ? null : Arrays.asList(cc)) + ", "
162: + (bcc == null ? null : Arrays.asList(bcc)) + ", "
163: + ')');
164: }
165:
166: try {
167: Session mailSession = ServerConfig.getMailSession();
168: BodyPart bodyPart = new MimeBodyPart();
169: bodyPart.setText(body);
170: Multipart content = new MimeMultipart();
171: content.addBodyPart(bodyPart);
172: Message msg = new MimeMessage(mailSession);
173: msg.setSubject(subject);
174: msg.setContent(content);
175: addRecipients(msg, to, Message.RecipientType.TO);
176: addRecipients(msg, cc, Message.RecipientType.CC);
177: addRecipients(msg, bcc, Message.RecipientType.BCC);
178: Transport.send(msg);
179: } catch (MessagingException e) {
180: _logger.error("Error sending mail", e);
181: }
182: }
183:
184: private static void addRecipients(Message msg, String[] recipients,
185: Message.RecipientType type) throws MessagingException {
186:
187: if (recipients != null) {
188: for (int i = 0; i < recipients.length; i++)
189: msg.addRecipient(type, new InternetAddress(
190: recipients[i]));
191: }
192: }
193:
194: public static String velocity(String vtl, boolean resolve)
195: throws Exception {
196:
197: if (_logger.isDebugEnabled())
198: _logger.debug("velocity(" + vtl + ", " + resolve + ')');
199:
200: // Make all process attributes available to the velocity template.
201: EngineContext engineContext = EngineContext.peekContext();
202: Context velocityContext = new VelocityProcessContext(
203: engineContext.getProcessInstance()
204: .getAttributeInstances());
205:
206: Writer out = new StringWriter();
207: Reader in;
208: if (resolve) {
209: in = new InputStreamReader(engineContext
210: .getServiceManager().getResourceRepository()
211: .findEntity(vtl));
212: } else {
213: in = new StringReader(vtl);
214: }
215: try {
216: Velocity.init();
217: if (!Velocity
218: .evaluate(velocityContext, out, "velocity", in))
219: _logger.warn("Velocity template processing failed");
220: } finally {
221: in.close();
222: out.close();
223: }
224: return out.toString();
225: }
226:
227: private OBEProcedures() {
228: }
229: }
|