Skip to content

Commit 2f3efaf

Browse files
committed
Add CRLAutoUpdateTask
The code in CRLIssuingPoint for updating the CRL automatically has been moved into CRLAutoUpdateTask.
1 parent 9a4c3e1 commit 2f3efaf

2 files changed

Lines changed: 223 additions & 179 deletions

File tree

Lines changed: 198 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,198 @@
1+
// --- BEGIN COPYRIGHT BLOCK ---
2+
// This program is free software; you can redistribute it and/or modify
3+
// it under the terms of the GNU General Public License as published by
4+
// the Free Software Foundation; version 2 of the License.
5+
//
6+
// This program is distributed in the hope that it will be useful,
7+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
8+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9+
// GNU General Public License for more details.
10+
//
11+
// You should have received a copy of the GNU General Public License along
12+
// with this program; if not, write to the Free Software Foundation, Inc.,
13+
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
14+
//
15+
// (C) 2007 Red Hat, Inc.
16+
// All rights reserved.
17+
// --- END COPYRIGHT BLOCK ---
18+
package com.netscape.ca;
19+
20+
import com.netscape.ca.CRLIssuingPoint.CRLIssuingPointStatus;
21+
import com.netscape.certsrv.base.EBaseException;
22+
23+
/**
24+
* @author awnuk
25+
* @author lhsiao
26+
* @author galperin
27+
*/
28+
public class CRLAutoUpdateTask implements Runnable {
29+
30+
public static final org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(CRLAutoUpdateTask.class);
31+
32+
CRLIssuingPoint ip;
33+
34+
CRLAutoUpdateTask(CRLIssuingPoint ip) {
35+
this.ip = ip;
36+
}
37+
38+
public void handleUnexpectedFailure(int loopCounter, long timeOfUnexpectedFailure) {
39+
40+
logger.info("CRLAutoUpdateTask: Handling unexpected failure");
41+
logger.info("CRLAutoUpdateTask: - loop counter: " + loopCounter);
42+
43+
if (loopCounter <= ip.mUnexpectedExceptionLoopMax) {
44+
logger.info("CRLAutoUpdateTask: Max loop not reached, no wait time");
45+
return;
46+
}
47+
48+
logger.info("CRLAutoUpdateTask: Max loop reached, slowdown procedure ensues");
49+
50+
long now = System.currentTimeMillis();
51+
logger.info("CRLAutoUpdateTask: - now: " + now);
52+
logger.info("CRLAutoUpdateTask: - time of unexpected failure: " + timeOfUnexpectedFailure);
53+
54+
long timeLapse = now - timeOfUnexpectedFailure;
55+
logger.info("CRLAutoUpdateTask: - time lapse: " + timeLapse);
56+
57+
long waitTime = ip.mUnexpectedExceptionWaitTime - timeLapse;
58+
logger.info("CRLAutoUpdateTask: Wait time after last failure:" + waitTime);
59+
60+
if (waitTime <= 0) {
61+
logger.info("CRLAutoUpdateTask: No wait after failure");
62+
return;
63+
}
64+
65+
logger.info("CRLAutoUpdateTask: Waiting for " + waitTime + " ms");
66+
67+
try {
68+
ip.wait(waitTime);
69+
70+
} catch (InterruptedException e) {
71+
logger.error("CRLAutoUpdateTask: " + e.getMessage(), e);
72+
}
73+
74+
// timeOfUnexpectedFailure will be reset again if it still fails
75+
}
76+
77+
/**
78+
* Defines auto-update logic used by worker thread.
79+
*/
80+
@Override
81+
public void run() {
82+
// mechanism to slow down the infinite loop when depending
83+
// components are not available: e.g. Directory server, HSM
84+
boolean unexpectedFailure = false;
85+
long timeOfUnexpectedFailure = 0;
86+
int loopCounter = 0;
87+
88+
try {
89+
while (ip.mEnable && (
90+
(ip.mEnableCRLCache && ip.mCacheUpdateInterval > 0) ||
91+
(ip.mInitialized == CRLIssuingPointStatus.NotInitialized) ||
92+
ip.mDoLastAutoUpdate ||
93+
(ip.mEnableCRLUpdates && (
94+
(ip.mEnableDailyUpdates && ip.mDailyUpdates != null && ip.mTimeListSize > 0) ||
95+
(ip.mEnableUpdateFreq && ip.mAutoUpdateInterval > 0) ||
96+
ip.mDoManualUpdate
97+
))
98+
)) {
99+
100+
synchronized (ip) {
101+
long delay = 0;
102+
long delay2 = 0;
103+
boolean doCacheUpdate = false;
104+
boolean scheduledUpdates = ip.mEnableCRLUpdates && (
105+
(ip.mEnableDailyUpdates && ip.mDailyUpdates != null && ip.mTimeListSize > 0) ||
106+
(ip.mEnableUpdateFreq && ip.mAutoUpdateInterval > 0)
107+
);
108+
109+
if (ip.mInitialized == CRLIssuingPointStatus.NotInitialized) {
110+
ip.initCRL();
111+
}
112+
113+
if ((ip.mEnableCRLUpdates && ip.mDoManualUpdate) || ip.mDoLastAutoUpdate) {
114+
delay = 0;
115+
} else if (scheduledUpdates) {
116+
delay = ip.findNextUpdate(true, false);
117+
}
118+
119+
if (ip.mEnableCRLCache && ip.mCacheUpdateInterval > 0) {
120+
delay2 = ip.mLastCacheUpdate + ip.mCacheUpdateInterval - System.currentTimeMillis();
121+
if (delay2 < delay ||
122+
(!(scheduledUpdates || ip.mDoLastAutoUpdate ||
123+
(ip.mEnableCRLUpdates && ip.mDoManualUpdate)))) {
124+
delay = delay2;
125+
if (delay <= 0) {
126+
doCacheUpdate = true;
127+
ip.mLastCacheUpdate = System.currentTimeMillis();
128+
}
129+
}
130+
}
131+
132+
if (delay > 0) {
133+
try {
134+
ip.wait(delay);
135+
} catch (InterruptedException e) {
136+
logger.error("CRLAutoUpdateTask: " + e.getMessage(), e);
137+
}
138+
139+
} else {
140+
/*
141+
* handle last failure so we don't get into
142+
* non-delayed loop
143+
*/
144+
if (unexpectedFailure) {
145+
// it gets mUnexpectedExceptionLoopMax tries
146+
loopCounter++;
147+
handleUnexpectedFailure(loopCounter, timeOfUnexpectedFailure);
148+
}
149+
150+
logger.debug("CRLAutoUpdateTask: Before CRL generation");
151+
try {
152+
if (doCacheUpdate) {
153+
logger.info("CRLAutoUpdateTask: Updating CRL cache");
154+
ip.updateCRLCacheRepository();
155+
} else if (ip.mAutoUpdateInterval > 0 || ip.mDoLastAutoUpdate || ip.mDoManualUpdate) {
156+
logger.info("CRLAutoUpdateTask: Updating CRL");
157+
ip.updateCRL();
158+
}
159+
160+
// reset if no exception
161+
if (unexpectedFailure) {
162+
logger.debug("CRLAutoUpdateTask: reset unexpectedFailure values if no exception");
163+
unexpectedFailure = false;
164+
timeOfUnexpectedFailure = 0;
165+
loopCounter = 0;
166+
}
167+
168+
} catch (Exception e) {
169+
logger.warn("CRLAutoUpdateTask: Unable to update " + (doCacheUpdate ? "CRL cache" : "CRL") + ": " + e.getMessage(), e);
170+
unexpectedFailure = true;
171+
timeOfUnexpectedFailure = System.currentTimeMillis();
172+
}
173+
174+
// put this here to prevent continuous loop if internal
175+
// db is down.
176+
177+
if (ip.mDoLastAutoUpdate) {
178+
logger.debug("CRLAutoUpdateTask: mDoLastAutoUpdate set to false");
179+
ip.mDoLastAutoUpdate = false;
180+
}
181+
182+
if (ip.mDoManualUpdate) {
183+
logger.debug("CRLAutoUpdateTask: mDoManualUpdate set to false");
184+
ip.mDoManualUpdate = false;
185+
ip.mSignatureAlgorithmForManualUpdate = null;
186+
}
187+
}
188+
189+
}
190+
}
191+
} catch (EBaseException e) {
192+
logger.error("CRLAutoUpdateTask: " + e.getMessage(), e);
193+
}
194+
195+
logger.debug("CRLAutoUpdateTask: out of the while loop");
196+
ip.mUpdateThread = null;
197+
}
198+
}

0 commit comments

Comments
 (0)