001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.ha.framework.test;
023:
024: import java.io.ByteArrayInputStream;
025: import java.io.ByteArrayOutputStream;
026: import java.io.ObjectInputStream;
027: import java.io.ObjectOutputStream;
028: import java.util.ArrayList;
029: import java.util.Iterator;
030:
031: import org.jboss.ha.framework.interfaces.ClusteringTargetsRepository;
032: import org.jboss.ha.framework.interfaces.FamilyClusterInfo;
033: import org.jboss.invocation.Invocation;
034: import org.jboss.invocation.InvocationContext;
035: import org.jboss.invocation.PayloadKey;
036: import org.jboss.invocation.ServiceUnavailableException;
037: import org.jboss.invocation.jrmp.interfaces.JRMPInvokerProxyHA;
038:
039: /**
040: * Used for testing clustering: mimics an exhausted set of targets.
041: * This interceptor should be placed between a RetryInterceptor and
042: * the InvokerInterceptor.
043: *
044: * @author brian.stansberry@jboss.com.
045: * @version $Id$
046: */
047:
048: public class ServiceUnavailableClientInterceptor extends
049: org.jboss.proxy.Interceptor {
050:
051: // Constants -----------------------------------------------------
052:
053: /** The serialVersionUID */
054: private static final long serialVersionUID = 8830272856328720750L;
055:
056: // Attributes ----------------------------------------------------
057:
058: private String proxyFamilyName;
059:
060: // Static --------------------------------------------------------
061:
062: // Constructors --------------------------------------------------
063:
064: public ServiceUnavailableClientInterceptor() {
065: }
066:
067: // Public --------------------------------------------------------
068:
069: // Z implementation ----------------------------------------------
070:
071: // Interceptor overrides ---------------------------------------------------
072:
073: public Object invoke(Invocation mi) throws Throwable {
074: Object data = mi.getValue("DO_FAIL_DURING_NEXT_CALL");
075:
076: if (data != null && data instanceof java.lang.Boolean
077: && data.equals(java.lang.Boolean.TRUE)) {
078:
079: // Clear the instruction
080: mi.setValue("DO_FAIL_DURING_NEXT_CALL", Boolean.FALSE,
081: PayloadKey.AS_IS);
082:
083: if (proxyFamilyName == null) {
084: proxyFamilyName = getProxyFamilyName(mi);
085: }
086:
087: // Clear the targets to simulate exhausting them all
088: FamilyClusterInfo info = ClusteringTargetsRepository
089: .getFamilyClusterInfo(proxyFamilyName);
090: ArrayList targets = info.getTargets();
091: for (Iterator it = targets.iterator(); it.hasNext();)
092: info.removeDeadTarget(it.next());
093:
094: throw new ServiceUnavailableException(
095: "Service unavailable", new Exception("Test"));
096: }
097:
098: return getNext().invoke(mi);
099: }
100:
101: // Package protected ---------------------------------------------
102:
103: // Protected -----------------------------------------------------
104:
105: // Private -------------------------------------------------------
106:
107: // Inner classes -------------------------------------------------
108:
109: static String getProxyFamilyName(Invocation invocation)
110: throws Exception {
111: InvocationContext ctx = invocation.invocationContext;
112: JRMPInvokerProxyHA invoker = (JRMPInvokerProxyHA) ctx
113: .getInvoker();
114:
115: ByteArrayOutputStream baos = new ByteArrayOutputStream();
116: ObjectOutputStream oos = new ObjectOutputStream(baos);
117: invoker.writeExternal(oos);
118: oos.close();
119: byte[] bytes = baos.toByteArray();
120:
121: ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
122: ObjectInputStream ois = new ObjectInputStream(bais);
123: Object targets = ois.readObject();
124: Object loadBalancePolicy = ois.readObject();
125: String proxyFamilyName = (String) ois.readObject();
126: ois.close();
127:
128: return proxyFamilyName;
129: }
130: }
|