Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: ae9fb333a41f531c152fb2e298f6c8567f9b0e0b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/*******************************************************************************
 * Copyright (c) 2009, 2010 VMware Inc.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * and Apache License v2.0 which accompanies this distribution. 
 * The Eclipse Public License is available at
 *   http://www.eclipse.org/legal/epl-v10.html
 * and the Apache License v2.0 is available at 
 *   http://www.opensource.org/licenses/apache2.0.php.
 * You may elect to redistribute this code under either of these licenses.  
 *
 * Contributors:
 *   VMware Inc. - initial contribution
 *******************************************************************************/

package org.eclipse.gemini.web.internal;

import java.util.Comparator;
import java.util.HashSet;
import java.util.Set;
import java.util.TreeSet;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

import org.eclipse.gemini.web.core.WebApplication;
import org.eclipse.gemini.web.core.WebApplicationStartFailedException;



final class WebApplicationStartFailureRetryController {

    private final Object monitor = new Object();

    private final ConcurrentMap<String, Set<StandardWebApplication>> failures = new ConcurrentHashMap<String, Set<StandardWebApplication>>();

    void recordFailure(StandardWebApplication failedWebApplication) {
        String contextPath = failedWebApplication.getContextPath();
        if (contextPath != null) {
            addFailureForWebContextPath(contextPath, failedWebApplication);
        }
    }

    private void addFailureForWebContextPath(String contextPath, StandardWebApplication failedWebApplication) {
        Set<StandardWebApplication> contextFailures = this.failures.get(contextPath);
        if (contextFailures == null) {
            contextFailures = new HashSet<StandardWebApplication>();
            Set<StandardWebApplication> previousContextFailures = this.failures.putIfAbsent(contextPath, contextFailures);
            if (previousContextFailures != null) {
                contextFailures = previousContextFailures;
            }
        }
        synchronized (this.monitor) {
            contextFailures.add(failedWebApplication);
        }
    }

    void retryFailures(StandardWebApplication stoppedWebApplication) {
        String contextPath = stoppedWebApplication.getContextPath();
        if (contextPath != null) {
            Set<StandardWebApplication> contextFailures = removeFailuresForWebContextPath(contextPath);
            contextFailures.remove(stoppedWebApplication);
            for (WebApplication failedWebApplication : contextFailures) {
                try {
                    failedWebApplication.start();
                } catch (WebApplicationStartFailedException _) {
                    // ignore as the web application will have been added to the new contextFailures set
                }
            }
        }
    }

    private Set<StandardWebApplication> removeFailuresForWebContextPath(String contextPath) {
        Set<StandardWebApplication> sortedContextFailures = createSetSortedByBundleId();
        Set<StandardWebApplication> contextFailures = this.failures.remove(contextPath);
        if (contextFailures != null) {
            sortedContextFailures.addAll(contextFailures);
        }
        return sortedContextFailures;

    }

    private Set<StandardWebApplication> createSetSortedByBundleId() {
        return new TreeSet<StandardWebApplication>(new Comparator<StandardWebApplication>() {

            public int compare(StandardWebApplication wa1, StandardWebApplication wa2) {
                long id1 = wa1.getBundle().getBundleId();
                long id2 = wa2.getBundle().getBundleId();
                if (id1 < id2) {
                    return -1;
                } else if (id1 > id2) {
                    return 1;
                } else {
                    return 0;
                }
            }
        });
    }

    void clear() {
        this.failures.clear();
    }
    
}

Back to the top