01: /*
02: * JBoss, Home of Professional Open Source.
03: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
04: * as indicated by the @author tags. See the copyright.txt file in the
05: * distribution for a full listing of individual contributors.
06: *
07: * This is free software; you can redistribute it and/or modify it
08: * under the terms of the GNU Lesser General Public License as
09: * published by the Free Software Foundation; either version 2.1 of
10: * the License, or (at your option) any later version.
11: *
12: * This software is distributed in the hope that it will be useful,
13: * but WITHOUT ANY WARRANTY; without even the implied warranty of
14: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15: * Lesser General Public License for more details.
16: *
17: * You should have received a copy of the GNU Lesser General Public
18: * License along with this software; if not, write to the Free
19: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21: */
22: package org.jboss.ejb.plugins;
23:
24: import org.jboss.ejb.EntityEnterpriseContext;
25: import org.jboss.ejb.GlobalTxEntityMap;
26: import org.jboss.invocation.Invocation;
27: import org.jboss.tm.TxUtils;
28:
29: /**
30: * The instance interceptors role is to break entity creation into two
31: * calls, one for ejbCreate and one for ejbPostCreate. The ejbCreate
32: * method is passed over the invokeHome chain, and ejbPostCreate is
33: * passed over the invoke chain.
34: *
35: * @author <a href="mailto:dain@daingroup.com">Dain Sundstrom</a>
36: * @version $Revision: 62278 $
37: */
38: public class EntityCreationInterceptor extends AbstractInterceptor {
39: public Object invokeHome(Invocation mi) throws Exception {
40: // Invoke through interceptors
41: Object retVal = getNext().invokeHome(mi);
42:
43: // Is the context now with an identity?
44: // This means that a create method was called, so invoke ejbPostCreate.
45: EntityEnterpriseContext ctx = (EntityEnterpriseContext) mi
46: .getEnterpriseContext();
47: if (ctx != null && ctx.getId() != null) {
48: // copy from the context into the mi
49: // interceptors down the chain look in the mi for the id not the ctx.
50: mi.setId(ctx.getId());
51:
52: // invoke down the invoke chain
53: // the final interceptor in EntityContainer will redirect this
54: // call to postCreateEntity, which calls ejbPostCreate
55: getNext().invoke(mi);
56:
57: // now it's ready and can be scheduled for the synchronization
58: if (TxUtils.isActive(mi.getTransaction())) {
59: GlobalTxEntityMap.NONE.scheduleSync(
60: mi.getTransaction(), ctx);
61: }
62: }
63:
64: return retVal;
65: }
66:
67: public Object invoke(Invocation mi) throws Exception {
68: // nothing to see here... move along
69: return getNext().invoke(mi);
70: }
71: }
|