001: /**
002: * High Availability Service (HA) for JOnAS
003: *
004: * Copyright (C) 2006 Distributed Systems Lab.
005: * Universidad Polit??cnica de Madrid (Spain)
006: * Contact: http://lsd.ls.fi.upm.es/lsd
007: *
008: * This library is free software; you can redistribute it and/or
009: * modify it under the terms of the GNU Lesser General Public
010: * License as published by the Free Software Foundation; either
011: * version 2.1 of the License, or any later version.
012: *
013: * This library is distributed in the hope that it will be useful,
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
016: * Lesser General Public License for more details.
017: *
018: * You should have received a copy of the GNU Lesser General Public
019: * License along with this library; if not, write to the Free Software
020: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
021: * USA
022: *
023: * --------------------------------------------------------------------------
024: * $Id: HACurrentDelegateImpl.java 9280 2006-08-01 10:15:24Z japaz $
025: * --------------------------------------------------------------------------
026: */package org.objectweb.jonas.ha.interceptor;
027:
028: import java.util.Stack;
029:
030: import org.objectweb.carol.cmi.ha.RequestId;
031: import org.objectweb.carol.cmi.ha.ViewIdWrapper;
032: import org.objectweb.carol.cmi.ha.interceptor.HACurrentDelegate;
033:
034: /**
035: * This class handles the HAContext/Thread association
036: * @author Francisco Perez-Sorrosal (fpsorrosal@no-spam@fi.upm.es)
037: * @author Alberto Paz-Jimenez (apaz@no-spam@fi.upm.es)
038: */
039: public class HACurrentDelegateImpl implements HACurrentDelegate {
040:
041: /**
042: * Local thread
043: */
044: private static InheritableThreadLocal threadCtx;
045:
046: /**
047: * Init the thread
048: */
049: static {
050: threadCtx = new InheritableThreadLocal();
051: }
052:
053: /**
054: * Unique instance
055: */
056: private static HACurrentDelegateImpl currentDelegate = new HACurrentDelegateImpl();
057:
058: /**
059: * Returns the unique instance
060: * @return the current instance
061: */
062: public static HACurrentDelegateImpl getCurrent() {
063: return currentDelegate;
064: }
065:
066: /**
067: * Set the requests stack in the current context
068: * @param requests the next request
069: */
070: public void setRequests(Stack requests) {
071: HACurrentContext current = getCurrentContext();
072: current.setRequests(requests);
073: threadCtx.set(current);
074: }
075:
076: /**
077: * Get the request stack
078: * @return the request stack
079: */
080: public Stack getRequests() {
081: return getCurrentContext().getRequests();
082: }
083:
084: /**
085: * Put the request in the request stack
086: * @param nextReq the next request
087: */
088: public void putNextReq(RequestId nextReq) {
089: HACurrentContext current = getCurrentContext();
090: current.putNextReq(nextReq);
091: threadCtx.set(current);
092: }
093:
094: /**
095: * Sets the onFailover variable
096: * @param onFailover
097: */
098: public void setOnFailover(boolean onFailover) {
099: HACurrentContext current = getCurrentContext();
100: current.setOnFailover(onFailover);
101: threadCtx.set(current);
102: }
103:
104: /**
105: * Gets the onFailover variable
106: * @return true if on failover
107: */
108: public boolean isOnFailover() {
109: return getCurrentContext().isOnFailover();
110:
111: }
112:
113: private HACurrentContext getCurrentContext() {
114: HACurrentContext current = (HACurrentContext) threadCtx.get();
115: if (current == null) {
116: current = new HACurrentContext();
117: }
118: return current;
119: }
120:
121: public void pushViewIdWrapper(ViewIdWrapper info) {
122: HACurrentContext current = (HACurrentContext) threadCtx.get();
123: if (current == null) {
124: current = new HACurrentContext();
125: }
126: current.setViewIdWrapper(info);
127: threadCtx.set(current);
128: }
129:
130: public ViewIdWrapper getViewIdWrapper() {
131: HACurrentContext current = (HACurrentContext) threadCtx.get();
132: if (current == null) {
133: current = new HACurrentContext();
134: }
135: return current.getViewIdWrapper();
136: }
137: }
|