KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > ha > framework > test > ServiceUnavailableClientInterceptor


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * 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.ha.framework.test;
23
24 import java.io.ByteArrayInputStream JavaDoc;
25 import java.io.ByteArrayOutputStream JavaDoc;
26 import java.io.ObjectInputStream JavaDoc;
27 import java.io.ObjectOutputStream JavaDoc;
28 import java.util.Iterator JavaDoc;
29 import java.util.List JavaDoc;
30
31 import org.jboss.ha.framework.interfaces.ClusteringTargetsRepository;
32 import org.jboss.ha.framework.interfaces.FamilyClusterInfo;
33 import org.jboss.invocation.Invocation;
34 import org.jboss.invocation.InvocationContext;
35 import org.jboss.invocation.PayloadKey;
36 import org.jboss.invocation.ServiceUnavailableException;
37 import org.jboss.invocation.jrmp.interfaces.JRMPInvokerProxyHA;
38
39 /**
40  * Used for testing clustering: mimics an exhausted set of targets.
41  * This interceptor should be placed between a RetryInterceptor and
42  * the InvokerInterceptor.
43  *
44  * @author brian.stansberry@jboss.com.
45  * @version $Id$
46  */

47
48 public class ServiceUnavailableClientInterceptor extends org.jboss.proxy.Interceptor
49 {
50
51    // Constants -----------------------------------------------------
52

53    /** The serialVersionUID */
54    private static final long serialVersionUID = 8830272856328720750L;
55    
56    // Attributes ----------------------------------------------------
57

58    private String JavaDoc proxyFamilyName;
59    
60    // Static --------------------------------------------------------
61

62    // Constructors --------------------------------------------------
63

64    public ServiceUnavailableClientInterceptor ()
65    {
66    }
67    
68    // Public --------------------------------------------------------
69

70    // Z implementation ----------------------------------------------
71

72    // Interceptor overrides ---------------------------------------------------
73

74    public Object JavaDoc invoke (Invocation mi) throws Throwable JavaDoc
75    {
76       Object JavaDoc data = mi.getValue ("DO_FAIL_DURING_NEXT_CALL");
77       
78       if (data != null &&
79             data instanceof java.lang.Boolean JavaDoc &&
80             data.equals (java.lang.Boolean.TRUE))
81       {
82          
83          // Clear the instruction
84
mi.setValue ("DO_FAIL_DURING_NEXT_CALL", Boolean.FALSE, PayloadKey.AS_IS);
85          
86          if (proxyFamilyName == null)
87          {
88             proxyFamilyName = getProxyFamilyName(mi);
89          }
90          
91          // Clear the targets to simulate exhausting them all
92
FamilyClusterInfo info = ClusteringTargetsRepository.getFamilyClusterInfo(proxyFamilyName);
93          List JavaDoc targets = info.getTargets();
94          for (Iterator JavaDoc it = targets.iterator(); it.hasNext(); )
95             info.removeDeadTarget(it.next());
96          
97          throw new ServiceUnavailableException("Service unavailable",
98                                                new Exception JavaDoc("Test"));
99       }
100
101       return getNext().invoke(mi);
102    }
103    
104    // Package protected ---------------------------------------------
105

106    // Protected -----------------------------------------------------
107

108    // Private -------------------------------------------------------
109

110    // Inner classes -------------------------------------------------
111

112    static String JavaDoc getProxyFamilyName(Invocation invocation) throws Exception JavaDoc
113    {
114       InvocationContext ctx = invocation.invocationContext;
115       JRMPInvokerProxyHA invoker = (JRMPInvokerProxyHA) ctx.getInvoker();
116       
117       ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
118       ObjectOutputStream JavaDoc oos = new ObjectOutputStream JavaDoc(baos);
119       invoker.writeExternal(oos);
120       oos.close();
121       byte[] bytes = baos.toByteArray();
122       
123       ByteArrayInputStream JavaDoc bais = new ByteArrayInputStream JavaDoc(bytes);
124       ObjectInputStream JavaDoc ois = new ObjectInputStream JavaDoc(bais);
125       Object JavaDoc targets = ois.readObject();
126       Object JavaDoc loadBalancePolicy = ois.readObject();
127       String JavaDoc proxyFamilyName = (String JavaDoc) ois.readObject();
128       ois.close();
129       
130       return proxyFamilyName;
131    }
132 }
133
Popular Tags