Skip to main content

This CGIT instance is deprecated, and repositories have been moved to Gitlab or Github. See the repository descriptions for specific locations.

summaryrefslogtreecommitdiffstats
blob: ad24b6d95ac76195afff3b7a53976406d960ba3f (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
105
106
107
108
109
110
111
112
113
114
115
116
117
/**
 * Copyright (c) 2006 Cape Clear Software. All rights reserved.
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v1.0 which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 */
package org.eclipse.jst.server.generic.internal.core.util;

import java.util.Iterator;
import java.util.List;

import org.eclipse.jst.server.generic.servertype.definition.Classpath;
import org.eclipse.jst.server.generic.servertype.definition.Property;
import org.eclipse.jst.server.generic.servertype.definition.ServerRuntime;

/**
 * Combines a runtimedef files
 * 
 * - properties
 * - classpaths
 * 
 * entries with a serverdef, if the serverdef does not already have 
 * them - contains check is done by id.
 * 
 * @author <a href="mailto:david.black@capeclear.com">David Black</a>
 */
public class ServerRuntimeMergeUtil {

    /**
     * Combines a runtime definition and a server definition 
     * into a single logical <code>ServerRuntime</code>.
     * 
     * @param serverdef
     * @param runtimedef
     * @return serverdef
     */
    public static ServerRuntime combine(ServerRuntime serverdef, ServerRuntime runtimedef) {
        /** 
         * Add properties from runtimedef to serverdef if not already present,
         * this ensures that:
         * 
         * (1) while we are affecting the cached copy of serverdef, it is always
         *     required to be combined with its runtimedef (if there is one), and
         *     we check that the property has not already been added
         *     
         * (2) serverdef properties can override runtimedef properties    
         */
        List properties = runtimedef.getProperty();
        if (properties != null) {
            Iterator iter = properties.iterator();
            while (iter.hasNext()) {
                Property prop = (Property) iter.next();
                addPropertyIfNotPresent(serverdef.getProperty(), prop);
            }
        }
        
        /** 
         * Add classpaths from runtimedef to serverdef if not already present,
         * this ensures that:
         * 
         * (1) while we are affecting the cached copy of serverdef, it is always
         *     required to be combined with its runtimedef (if there is one), and
         *     we check that the classpath has not already been added (by id)
         *     
         * (2) serverdef classpath can override runtimedef classpath by id    
         */
        List classpaths = runtimedef.getClasspath();
        if (classpaths != null) {
            Iterator iter = classpaths.iterator();
            while (iter.hasNext()) {
                Classpath classpath = (Classpath)iter.next();
                addClasspathIfNotPresent(serverdef.getClasspath(), classpath);
            }
        }
        
        return serverdef;
    }

    private static void addClasspathIfNotPresent(List classpaths, Classpath classpath) {
        if (!containsClasspath(classpaths, classpath.getId())) {
            classpaths.add(classpath);
        }
    }

    private static boolean containsClasspath(List properties, String id) {
        boolean found = false;
        Iterator iter = properties.iterator();
        while (iter.hasNext()) {
            Classpath classpath = (Classpath) iter.next();
            if (classpath.getId().equals(id)) {
                found = true;
                break;
            }
        }
        return found;
    }
    
    private static void addPropertyIfNotPresent(List properties, Property prop) {
        if (!containsProperty(properties, prop.getId())) {
            properties.add(prop);
        }
    }

    private static boolean containsProperty(List properties, String id) {
        boolean found = false;
        Iterator iter = properties.iterator();
        while (iter.hasNext()) {
            Property prop = (Property) iter.next();
            if (prop.getId().equals(id)) {
                found = true;
                break;
            }
        }
        return found;
    }

}

Back to the top