KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ubermq > kernel > overflow > HandlerChain


1 package com.ubermq.kernel.overflow;
2
3 import com.ubermq.kernel.*;
4 import java.util.*;
5
6 /**
7  * Constructs a chain of overflow handlers in one object.
8  * The first overflow handler is entirely exhausted before
9  * moving on to the second handler, and so on, for all specified
10  * handlers.
11  */

12 public class HandlerChain
13     implements IConnectionOverflowHandler
14 {
15     private IOverflowHandler primary, secondary;
16     private boolean useSecondary;
17     
18     /**
19      * Convenience constructor that takes a primary and secondary
20      * overflow handler.
21      * @param primary the primary overflow handler
22      * @param secondary the secondary overflow handler
23      */

24     public HandlerChain(IOverflowHandler primary,
25                         IOverflowHandler secondary)
26     {
27         this.primary = primary;
28         this.secondary = secondary;
29
30         // get the retry handler for this setup
31
this.useSecondary = false;
32         
33     }
34     
35     /**
36      * Produces a new overflow handler to be used if the
37      * recommended retried output attempt fails again.
38      * This can be return null if the overflow()
39      * method never returns true. <P>
40      *
41      */

42     public IOverflowHandler getRetryHandler()
43     {
44         if (useSecondary) {
45             return secondary;
46         } else {
47             return new HandlerChain(primary.getRetryHandler(), secondary);
48         }
49     }
50
51     /**
52      * Advances the retry handler to the secondary, or backup,
53      * overflow handler. Call when the primary handler gives up
54      * by indicating that a retry should not be attempted.
55      */

56     private void useSecondaryHandler()
57     {
58         this.useSecondary = true;
59     }
60     
61     /**
62      * Handles an overflow situation that is not related to outputting
63      * a datagram.
64      */

65     public int overflow()
66     {
67         int a = primary.overflow();
68         if (a == IOverflowHandler.ACTION_FAIL) {
69             useSecondaryHandler();
70             a = IOverflowHandler.ACTION_RETRY;
71         }
72         
73         return a;
74     }
75     
76     /**
77      * Handles an overflow detected by a certain message processor
78      * on a specific connection.
79      * @param d the datagram that was being sent when the overflow
80      * occurred.
81      * @param connection the connection that was the target of the
82      * <code>output</code> call.
83      * @param messageProcessor the message processor that is calling
84      * this method.
85      */

86     public int overflow(IDatagram d, IConnectionInfo r, IMessageProcessor messageProcessor)
87     {
88         if (primary instanceof IConnectionOverflowHandler) {
89             int a = ((IConnectionOverflowHandler)primary).overflow(d, r, messageProcessor);
90             if (a == IOverflowHandler.ACTION_FAIL) {
91                 useSecondaryHandler();
92                 a = IOverflowHandler.ACTION_RETRY;
93             }
94             return a;
95         }
96         else
97             return overflow(d);
98     }
99     
100     /**
101      * Handles an overflow situation.
102      *
103      * @param d the IDatagram that is being sent.
104      * @return whether the caller should retry the output
105      * or not.
106      */

107     public int overflow(IDatagram d)
108     {
109         int a = primary.overflow(d);
110         if (a == IOverflowHandler.ACTION_FAIL) {
111             useSecondaryHandler();
112             a = IOverflowHandler.ACTION_RETRY;
113         }
114         
115         return a;
116     }
117     
118     
119 }
120
121
122
Popular Tags