Package: CompensatingPipeline

CompensatingPipeline

nameinstructionbranchcomplexitylinemethod
CompensatingPipeline(PipelineStage)
M: 11 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 4 C: 0
0%
M: 1 C: 0
0%
compensate(GraphNode, InstallEnvironment, Exception)
M: 43 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 8 C: 0
0%
M: 1 C: 0
0%
doProcessGraph(GraphNode, InstallEnvironment)
M: 30 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 11 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /*******************************************************************************
2: * Copyright (c) 2008, 2011 VMware Inc. and others
3: * All rights reserved. This program and the accompanying materials
4: * are made available under the terms of the Eclipse Public License v1.0
5: * which accompanies this distribution, and is available at
6: * http://www.eclipse.org/legal/epl-v10.html
7: *
8: * Contributors:
9: * VMware Inc. - initial contribution
10: * EclipseSource - Bug 358442 Change InstallArtifact graph from a tree to a DAG
11: *******************************************************************************/
12:
13: package org.eclipse.virgo.kernel.install.pipeline.internal;
14:
15: import org.slf4j.Logger;
16: import org.slf4j.LoggerFactory;
17:
18: import org.eclipse.virgo.kernel.osgi.framework.UnableToSatisfyBundleDependenciesException;
19:
20: import org.eclipse.virgo.nano.deployer.api.core.DeployerLogEvents;
21: import org.eclipse.virgo.nano.deployer.api.core.DeploymentException;
22: import org.eclipse.virgo.kernel.install.artifact.InstallArtifact;
23: import org.eclipse.virgo.kernel.install.environment.InstallEnvironment;
24: import org.eclipse.virgo.kernel.install.pipeline.Pipeline;
25: import org.eclipse.virgo.kernel.install.pipeline.stage.PipelineStage;
26: import org.eclipse.virgo.util.common.GraphNode;
27:
28: /**
29: * {@link CompensatingPipeline} is a {@link Pipeline} which runs like any other pipeline but if one of its stages throws
30: * an exception, it runs a compensation <code>PipelineStage</code> and then re-throws the exception.
31: * <p />
32: * For example, consider the following pipeline:
33: *
34: * <pre>
35: * P = beginX -> X1 -> X2 -> X3 -> endX
36: * </pre>
37: *
38: * and suppose we need to drive a failure event using a pipeline stage failX when any of X1, X2, X3 throw an exception.
39: * Then we can use a <code>CompensatingPipeline</code> to produce the required behaviour:
40: *
41: * <pre>
42: * P' = beginX -> CompensatingPipeline(X1 -> X2 -> X3, failX) -> endX
43: * </pre>
44: * <p />
45: *
46: * <strong>Concurrent Semantics</strong><br />
47: *
48: * This class is thread safe.
49: *
50: */
51: final class CompensatingPipeline extends StandardPipeline {
52:
53: private final Logger logger = LoggerFactory.getLogger(this.getClass());
54:
55: private final PipelineStage compensation;
56:
57: /**
58: * Create a {@link CompensatingPipeline} with no pipeline stages and a given compensation stage.
59: * @param compensation stage for compensation
60: */
61: public CompensatingPipeline(PipelineStage compensation) {
62: super();
63: this.compensation = compensation;
64: }
65:
66: /**
67: * {@inheritDoc}
68: */
69: @Override
70: protected void doProcessGraph(GraphNode<InstallArtifact> installGraph, InstallEnvironment installEnvironment) throws DeploymentException,
71: UnableToSatisfyBundleDependenciesException {
72: try {
73: super.doProcessGraph(installGraph, installEnvironment);
74: } catch (DeploymentException de) {
75: compensate(installGraph, installEnvironment, de);
76: throw de;
77: } catch (UnableToSatisfyBundleDependenciesException utsbde) {
78: compensate(installGraph, installEnvironment, utsbde);
79: throw utsbde;
80: } catch (RuntimeException re) {
81: compensate(installGraph, installEnvironment, re);
82: throw re;
83: }
84: }
85:
86: private void compensate(GraphNode<InstallArtifact> installGraph, InstallEnvironment installEnvironment, Exception e) {
87: try {
88:• if (!(e instanceof DeploymentException) || !((DeploymentException)e).isDiagnosed()) {
89: installEnvironment.getInstallLog().logFailure(DeployerLogEvents.INSTALL_FAILURE, e);
90: } else {
91: installEnvironment.getInstallLog().logFailure(DeployerLogEvents.INSTALL_FAILURE, null);
92: }
93: this.compensation.process(installGraph, installEnvironment);
94: } catch (Exception ex) {
95: logger.warn(String.format("exception thrown while compensating for '%s'", e.getMessage()), ex);
96: }
97: }
98:
99: }