001: package org.obe.server.j2ee.ejb;
002:
003: import java.util.ArrayList;
004: import java.util.Collection;
005: import java.util.Date;
006: import java.util.Iterator;
007: import javax.ejb.*;
008: import org.apache.commons.logging.Log;
009: import org.obe.OBERuntimeException;
010: import org.obe.engine.util.Comparators;
011: import org.obe.server.util.ObjectUtil;
012: import org.obe.spi.event.ApplicationEvent;
013: import org.obe.spi.service.ServerConfig;
014:
015: /**
016: * Stores an application event for subsequent consumption by an anticipated but
017: * as-yet unregistered subscription.
018: *
019: * @author Adrian Price
020: * @ejb:bean
021: * type="CMP"
022: * cmp-version="2.x"
023: * name="ApplicationEvent"
024: * display-name="OBE Application Event"
025: * local-jndi-name="org/obe/ejb/ApplicationEventLocal"
026: * primkey-field="eventId"
027: * reentrant="False"
028: * schema="APPLICATIONEVENT"
029: * transaction-type="Container"
030: * view-type="local"
031: * @ejb:home
032: * local-class="org.obe.server.j2ee.ejb.ApplicationEventLocalHome"
033: * local-extends="javax.ejb.EJBLocalHome"
034: * @ejb:interface
035: * local-class="org.obe.server.j2ee.ejb.ApplicationEventLocal"
036: * local-extends="javax.ejb.EJBLocalObject"
037: * @ejb:persistence
038: * table-name="OBEAPPLICATIONEVENT"
039: * @ejb:pk
040: * class="java.lang.Long"
041: * unchecked="true"
042: * @ejb:permission
043: * unchecked="true"
044: * @ejb:transaction
045: * type="Supports"
046: * @weblogic:transaction-isolation ${transaction.isolation}
047: * @ejb:finder
048: * signature="java.util.Collection findByEventHash(java.lang.String eventType, int eventKeyHashcode)"
049: * query="SELECT OBJECT(e) FROM APPLICATIONEVENT AS e WHERE e.eventType=?1 AND e.eventKeyHashcode=?2 ORDER BY e.eventId"
050: * result-type-mapping="Local"
051: * unchecked="true"
052: * @ejb:resource-ref
053: * res-name="jdbc/TxDataSource"
054: * res-type="javax.sql.DataSource"
055: * res-auth="Container"
056: * @jboss:resource-manager
057: * res-man-class="javax.sql.DataSource"
058: * res-man-name="jdbc/TxDataSource"
059: * res-man-jndi-name="java:/${xdoclet.DataSource}"
060: * @weblogic:resource-description
061: * res-ref-name="jdbc/TxDataSource"
062: * jndi-name="${xdoclet.DataSource}"
063: * @weblogic:data-source-name
064: * java:comp/env/jdbc/TxDataSource
065: * @jboss:create-table false
066: * @weblogic:pool
067: * max-beans-in-free-pool="225"
068: * initial-beans-in-free-pool="0"
069: * @weblogic:cache
070: * max-beans-in-cache="225"
071: * idle-timeout-seconds="0"
072: * concurrency-strategy="Database"
073: * @weblogic:persistence
074: * delay-updates-until-end-of-tx="True"
075: * @jboss:tuned-updates
076: * tune="true"
077: * @jboss:container-configuration
078: * name="${xdoclet.jboss.container-configuration}"
079: */
080: public abstract class ApplicationEventEJB extends AbstractEntityEJB
081: implements TimedObject {
082:
083: private static final Log _logger = getLogger(ApplicationEventEJB.class);
084:
085: /**
086: * @ejb:persistence column-name="EVENTID"
087: */
088: public abstract Long getEventId();
089:
090: /**
091: * @ejb:persistence column-name="EVENTID"
092: */
093: public abstract void setEventId(Long eventId);
094:
095: /**
096: * @ejb:persistence column-name="EVENTTYPE"
097: */
098: public abstract String getEventType();
099:
100: /**
101: * @ejb:persistence column-name="EVENTTYPE"
102: */
103: public abstract void setEventType(String eventType);
104:
105: /**
106: * @ejb:persistence column-name="EVENTKEYHASHCODE"
107: */
108: public abstract int getEventKeyHashcode();
109:
110: /**
111: * @ejb:persistence column-name="EVENTKEYHASHCODE"
112: */
113: public abstract void setEventKeyHashcode(int hashcode);
114:
115: /**
116: * @ejb:interface-method
117: * @ejb:persistence column-name="EVENT"
118: */
119: public abstract ApplicationEvent getEvent();
120:
121: /**
122: * @ejb:persistence column-name="EVENT"
123: */
124: public abstract void setEvent(ApplicationEvent event);
125:
126: /**
127: * Retrieves events matching the specified subscription.
128: *
129: * @param eventType The event type.
130: * @param subscriptionKeys The event keys supplied by the subscription.
131: * @return A collection of matching subscriptions.
132: * @ejb:home-method
133: */
134: public Collection ejbHomeXfindByEvent(String eventType,
135: Object[] subscriptionKeys) throws FinderException {
136:
137: // Look up potential matches by event type and event key hash.
138: ApplicationEventLocalHome home = (ApplicationEventLocalHome) _ctx
139: .getEJBLocalHome();
140: Collection candidates = home.findByEventHash(eventType,
141: ObjectUtil.hashcode(subscriptionKeys));
142:
143: // Select those events which actually match on each key.
144: Collection events = new ArrayList(candidates.size());
145: for (Iterator i = events.iterator(); i.hasNext();) {
146: ApplicationEventLocal wrapper = (ApplicationEventLocal) i
147: .next();
148: ApplicationEvent event = wrapper.getEvent();
149: if (Comparators.match(subscriptionKeys, event.getKeys(),
150: false))
151: events.add(event);
152: }
153: return events;
154: }
155:
156: /**
157: * @ejb:create-method
158: */
159: public Long ejbCreate(ApplicationEvent event, int inputKeysHashcode)
160: throws CreateException {
161:
162: if (event == null)
163: throw new IllegalArgumentException("event cannot be null");
164:
165: if (_logger.isDebugEnabled() && ServerConfig.isVerbose())
166: _logger.debug("ejbCreate(" + event + ", "
167: + inputKeysHashcode + ')');
168:
169: clear();
170:
171: setEventId(new Long(ApplicationEventDAO.getInstance()
172: .getNewId()));
173: setEventType(event.getEventType());
174: setEventKeyHashcode(inputKeysHashcode);
175: setEvent(event);
176:
177: return null;
178: }
179:
180: public void ejbPostCreate(ApplicationEvent event,
181: int eventKeyHashcode) throws CreateException {
182:
183: if (_logger.isDebugEnabled() && ServerConfig.isVerbose()) {
184: _logger.debug("ejbPostCreate(" + event + ", "
185: + eventKeyHashcode + ')');
186: }
187:
188: // If an expiration date is set, schedule a timer so we can delete it.
189: Date expiry = event.getExpiry();
190: if (expiry != null)
191: _ctx.getTimerService().createTimer(expiry, null);
192: }
193:
194: public void ejbTimeout(Timer timer) {
195: try {
196: _ctx.getEJBLocalObject().remove();
197: } catch (RemoveException e) {
198: throw new OBERuntimeException(e);
199: }
200: }
201:
202: protected Log getLogger() {
203: return _logger;
204: }
205: }
|