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.server.j2ee.service;
046:
047: import org.apache.commons.logging.Log;
048: import org.apache.commons.logging.LogFactory;
049: import org.obe.OBERuntimeException;
050: import org.obe.server.j2ee.J2EEServerConfig;
051: import org.obe.server.j2ee.ejb.AbstractMessageEJB;
052:
053: import javax.jms.JMSException;
054: import javax.jms.Message;
055: import javax.jms.MessageListener;
056: import javax.jms.ObjectMessage;
057:
058: /**
059: * Provides a JMS-based asynchronous execution service. The caller simply
060: * places a <code>java.lang.Runnable</code> object onto the JMS queue; this MDB
061: * dequeues it and invokes the runnable object's run() method. Obviously, the
062: * security on this JMS queue must be properly configured to prevent
063: * unauthorized code from being injected.
064: *
065: * @author Adrian Price
066: * @ejb:bean name="AsyncExecution"
067: * display-name="OBE Async Request Dispatcher"
068: * transaction-type="Container"
069: * destination-type="javax.jms.Queue"
070: * acknowledge-mode="Auto-acknowledge"
071: * subscription-durability="Durable"
072: * @ejb:permission unchecked="true"
073: * @ejb:resource-ref res-name="mail/Session"
074: * res-type="javax.mail.Session"
075: * res-auth="Container"
076: * @jboss:resource-manager res-man-class="javax.mail.Session"
077: * res-man-name="mail/Session"
078: * res-man-jndi-name="java:/${xdoclet.MailSession}"
079: * @weblogic:resource-description res-ref-name="mail/Session"
080: * jndi-name="${xdoclet.MailSession}"
081: * @ejb:security-identity run-as="system"
082: * @ejb:transaction type="Required"
083: * @weblogic:transaction-isolation ${transaction.isolation}
084: * @jboss:destination-jndi-name name="queue/${xdoclet.AsyncRequestQueue}"
085: * @weblogic:message-driven destination-jndi-name="${xdoclet.AsyncRequestQueue}"
086: * jms-polling-interval-seconds="10"
087: * @weblogic:pool max-beans-in-free-pool="50"
088: * initial-beans-in-free-pool="0"
089: */
090: public class AsyncExecutionEJB extends AbstractMessageEJB implements
091: MessageListener {
092:
093: private static final long serialVersionUID = 643924724424304009L;
094: private static final Log _logger = LogFactory
095: .getLog(AsyncExecutionEJB.class);
096:
097: protected Log getLogger() {
098: return _logger;
099: }
100:
101: public void onMessage(Message message) {
102: if (_logger.isDebugEnabled())
103: _logger.debug("onMessage(" + message + ')');
104:
105: // If the server isn't initialized yet, we must wait until it is.
106: if (!J2EEServerConfig.isInitialized())
107: J2EEServerConfig.waitUntilInitialized(_logger);
108:
109: try {
110: if (message instanceof ObjectMessage) {
111: ObjectMessage objMsg = (ObjectMessage) message;
112: Object payload = objMsg.getObject();
113: if (payload instanceof Runnable) {
114: // TODO: consider whether/how to propagate security identity.
115: Runnable request = (Runnable) payload;
116: request.run();
117: return;
118: }
119: }
120: _logger.warn("Non-runnable message discarded: " + message);
121: } catch (JMSException e) {
122: throw new OBERuntimeException(e);
123: }
124: }
125: }
|