KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jacorb > notification > engine > AbstractRetryStrategy


1 package org.jacorb.notification.engine;
2
3 /*
4  * JacORB - a free Java ORB
5  *
6  * Copyright (C) 1997-2004 Gerald Brose.
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Library General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Library General Public License for more details.
17  *
18  * You should have received a copy of the GNU Library General Public
19  * License along with this library; if not, write to the Free
20  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21  */

22
23 import org.apache.avalon.framework.logger.Logger;
24 import org.jacorb.notification.interfaces.IProxyPushSupplier;
25 import org.jacorb.notification.util.LogUtil;
26 import org.omg.CORBA.OBJECT_NOT_EXIST JavaDoc;
27 import org.omg.CosEventComm.Disconnected;
28
29 /**
30  * @author Alphonse Bendt
31  * @version $Id: AbstractRetryStrategy.java,v 1.4 2005/04/27 10:48:40 alphonse.bendt Exp $
32  */

33 public abstract class AbstractRetryStrategy implements RetryStrategy
34 {
35     protected final Logger logger_ = LogUtil.getLogger(getClass().getName());
36     protected final PushOperation pushOperation_;
37
38     protected final IProxyPushSupplier pushSupplier_;
39
40     private boolean active_ = true;
41     
42     ////////////////////////////////////////
43

44     public AbstractRetryStrategy(IProxyPushSupplier pushSupplier, PushOperation operation)
45     {
46         pushSupplier_ = pushSupplier;
47         pushOperation_ = operation;
48     }
49
50     ////////////////////////////////////////
51

52     public void dispose()
53     {
54         pushOperation_.dispose();
55     }
56
57     protected boolean isRetryAllowed()
58     {
59         return active_ && pushSupplier_.isRetryAllowed();
60     }
61
62     protected void remoteExceptionOccured(Throwable JavaDoc error) throws RetryException
63     {
64         logger_.debug("Error during retry", error);
65         
66         if (isFatalException(error))
67         {
68             pushSupplier_.destroy();
69             active_ = false;
70
71             throw new RetryException("fatal exception while retrying push");
72         }
73
74         pushSupplier_.incErrorCounter();
75
76         if (!isRetryAllowed())
77         {
78             pushSupplier_.destroy();
79             active_ = false;
80
81             throw new RetryException("no more retries. giving up.");
82         }
83
84         waitUntilNextTry();
85     }
86
87     public static boolean isFatalException(Throwable JavaDoc error)
88     {
89         if (error instanceof OBJECT_NOT_EXIST JavaDoc)
90         {
91             return true;
92         }
93         else if (error instanceof Disconnected)
94         {
95             return true;
96         }
97         return false;
98     }
99
100     protected abstract long getTimeToWait();
101
102     public final void retry() throws RetryException
103     {
104         if (isRetryAllowed())
105         {
106             waitUntilNextTry();
107
108             retryInternal();
109         } else
110         {
111             dispose();
112         }
113     }
114
115     protected abstract void retryInternal() throws RetryException;
116
117     private void waitUntilNextTry()
118     {
119         long timeToWait = getTimeToWait();
120
121         try
122         {
123             if (timeToWait > 0)
124             {
125                 Thread.sleep(timeToWait);
126             }
127         } catch (InterruptedException JavaDoc ignored)
128         {
129             // ignored
130
}
131     }
132 }
Popular Tags