001: /**
002: * JOnAS: Java(TM) Open Application Server
003: * Copyright (C) 2006 Bull S.A.S.
004: * Contact: jonas-team@objectweb.org
005: * Copyright (C) 2006 Distributed Systems Lab.
006: * Universidad Polit??cnica de Madrid (Spain)
007: * Contact: http://lsd.ls.fi.upm.es/lsd
008: *
009: * This library is free software; you can redistribute it and/or
010: * modify it under the terms of the GNU Lesser General Public
011: * License as published by the Free Software Foundation; either
012: * version 2.1 of the License, or any later version.
013: *
014: * This library is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
017: * Lesser General Public License for more details.
018: *
019: * You should have received a copy of the GNU Lesser General Public
020: * License along with this library; if not, write to the Free Software
021: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
022: * USA
023: *
024: * --------------------------------------------------------------------------
025: *
026: * --------------------------------------------------------------------------
027: */package org.objectweb.jonas_ejb.container;
028:
029: import java.io.StringWriter;
030: import java.rmi.RemoteException;
031:
032: import javax.ejb.EJBException;
033:
034: import org.objectweb.carol.cmi.ClusterOIdFactory;
035: import org.objectweb.carol.cmi.ObjectId;
036: import org.objectweb.carol.cmi.ServerConfigException;
037: import org.objectweb.carol.cmi.configuration.TraceCmi;
038:
039: /**
040: * Generic part of the EJBObject implementation for replicated SFSBs
041: * @author Francisco Perez-Sorrosal (fpsorrosal@no-spam@fi.upm.es)
042: * @author Alberto Paz-Jimenez (apaz@no-spam@fi.upm.es)
043: */
044: public abstract class JRepStatefulLocal extends JSessionLocal {
045:
046: public ObjectId clusterOId = null;
047:
048: /**
049: * constructor
050: * @param bf The Session Factory
051: * @throws Exception
052: */
053: public JRepStatefulLocal(JSessionFactory bf) {
054: super (bf);
055: if (TraceCmi.isDebugCmiHA()) {
056: TraceCmi.debugCmiHA("JRepStatefulLocal : this=" + this );
057: }
058:
059: // COMPLETE: Handle errors
060: // Obtain a new cluster object id
061: try {
062: clusterOId = ClusterOIdFactory.getInstance().getId();
063: } catch (ServerConfigException e) {
064: java.io.StringWriter sw = new StringWriter();
065: java.io.PrintWriter pw = new java.io.PrintWriter(sw);
066: e.printStackTrace(pw);
067: TraceCmi.error("JRepStatefulLocal constructor: "
068: + sw.toString(), e);
069: }
070: }
071:
072: /**
073: * preInvoke is called before any request.
074: * @param txa Transaction Attribute (Supports, Required, ...)
075: * @return A RequestCtx object
076: * @throws RemoteException Thrown when the method failed due to a
077: * system-level failure.
078: */
079: public RequestCtx preInvoke(int txa) {
080: RequestCtx rctx = super .preInvoke(txa);
081: /* ******************REPLICATION CODE STARTS HERE*********************** */
082: preInvokeHook(rctx); // Replication algorithm link
083: /* **********************END OF REPLICATION CODE************************ */
084: return rctx;
085: }
086:
087: /**
088: * postInvoke is called after any request.
089: * @param rctx The RequestCtx that was returned at preInvoke()
090: * @param remove TODO
091: * @parem response The response that is going to be returned to the client
092: * (Needed by HA)
093: * @throws RemoteException Thrown when the method failed due to a
094: * system-level failure.
095: */
096: public void postInvoke(RequestCtx rctx, boolean remove) {
097: /* ******************REPLICATION CODE STARTS HERE*********************** */
098: try {
099: postInvokeHook(rctx, remove); // Replication algorithm
100: // link
101: /* **********************END OF REPLICATION CODE************************ */
102: } catch (Exception e) {
103: e.printStackTrace();
104: TraceCmi.error("Error calling postInvokeHook in sfsb.", e);
105: } finally {
106: super .postInvoke(rctx);
107: }
108: }
109:
110: /* ******************REPLICATION CODE STARTS HERE*********************** */
111:
112: // ------------------------------------------------------------------------
113: // Protected methods ------------------------------------------------------
114: // ------------------------------------------------------------------------
115: /**
116: * Injects the state of the bean if the cluster object id received in the
117: * request is in the current node. It extracts the object id passed through
118: * the interceptors from the request id included in the current HA context.
119: */
120: protected void injectState() {
121: JRepUtil
122: .injectState(this .clusterOId, (JStatefulSwitch) this .bs);
123: }
124:
125: // ------------------------------------------------------------------------
126: // Private methods --------------------------------------------------------
127: // ------------------------------------------------------------------------
128:
129: /**
130: * preInvokeHook implements the hook to perform the required replication
131: * tasks before the bean context has been established. In this case, it
132: * saves the current state of the bean before perform the invocation, in the
133: * request context.
134: * @param rctx The RequestCtx that was returned at preInvoke()
135: */
136: private void preInvokeHook(RequestCtx rctx) {
137: try {
138: JRepUtil.preInvokeHook(bs, clusterOId, rctx);
139: } catch (EJBException e) {
140: // COMPLETE: Handle errors
141: e.printStackTrace();
142: TraceCmi.error("Error in preInvokeHook in sfsb.", e);
143: }
144: }
145:
146: /**
147: * postInvokeHook implements the hook to perform the required replication
148: * tasks after the bean context has been established
149: * @param rctx The RequestCtx that was returned at preInvoke()
150: * @parem response The response that is going to be returned to the client
151: * @param remove true if it is an invocation to a remove method
152: */
153: private void postInvokeHook(RequestCtx rctx, boolean remove) {
154: JRepUtil.postInvokeHook(rctx, this .clusterOId, bs, remove);
155: }
156:
157: /**
158: * Gets the clusterOId
159: * @return the clusterOid
160: */
161: public ObjectId getClusterOId() {
162: return clusterOId;
163: }
164:
165: /**
166: * Sets the clusterOId
167: * @param oid
168: */
169: public void setClusterOId(ObjectId oid) {
170: clusterOId = oid;
171: }
172:
173: /* **********************END OF REPLICATION CODE************************ */
174: }
|