Debian Codegraph @

bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/p2/publisher/actions/VersionAdvice.java

raw
1 /*******************************************************************************
2 * Copyright (c) 2008, 2017 Code 9 and others.
3 *
4 * This
5 * program and the accompanying materials are made available under the terms of
6 * the Eclipse Public License 2.0 which accompanies this distribution, and is
7 * available at
8 * https://www.eclipse.org/legal/epl-2.0/
9 *
10 * SPDX-License-Identifier: EPL-2.0
11 *
12 * Contributors:
13 * Code 9 - initial API and implementation
14 * IBM - ongoing development
15 ******************************************************************************/
16 package org.eclipse.equinox.p2.publisher.actions;
17
18 import java.io.*;
19 import java.util.HashMap;
20 import java.util.Map;
21 import java.util.Map.Entry;
22 import org.eclipse.equinox.internal.p2.core.helpers.CollectionUtils;
23 import org.eclipse.equinox.p2.metadata.Version;
24 import org.eclipse.equinox.p2.publisher.AbstractAdvice;
25 import org.eclipse.equinox.p2.publisher.IPublisherAdvice;
26
27 public class VersionAdvice extends AbstractAdvice implements IVersionAdvice {
28
29 Map<String, Map<String, Version>> versions = new HashMap<>(11);
30
31 /**
32 * Load the given namespace with version mappings from the properties file at
33 * the given location. The properties file is expected to be in the normal format
34 * produced and consumed by PDE Build.
35 * @param namespace the namespace to populate. If "null" is passed
36 * in, the version will be stored in the "null" namespace
37 * @param location the location of the mapping file
38 */
39 public void load(String namespace, String location) {
40 load(namespace, location, null);
41 }
42
43 public void load(String namespace, String location, String idSuffix) {
44 File file = new File(location);
45 if (namespace == null)
46 namespace = "null"; //$NON-NLS-1$
47
48 Map<String, String> properties;
49 try (InputStream stream = new BufferedInputStream(new FileInputStream(file))) {
50 properties = CollectionUtils.loadProperties(stream);
51 } catch (IOException e) {
52 return;
53 }
54 for (Entry<String, String> entry : properties.entrySet()) {
55 String key = entry.getKey();
56 if (idSuffix != null)
57 key += idSuffix;
58 setVersion(namespace, key, Version.parseVersion(entry.getValue()));
59 }
60 }
61
62 /**
63 * Returns the version advice for the given id in the given namespace.
64 * @param namespace the namespace in which to look for advice
65 * @param id the item for which advice is sought
66 * @return the version advice found or <code>null</code> if none
67 */
68 @Override
69 public Version getVersion(String namespace, String id) {
70 Map<String, Version> values = versions.get(namespace);
71 // if no one says anything then don't say anything. someone else might have an opinion
72 if (values != null) {
73 Version result = values.get(id);
74 if (result != null)
75 return result;
76 }
77
78 values = versions.get("null"); //$NON-NLS-1$
79 if (values == null)
80 return null;
81 return values.get(id);
82 }
83
84 /**
85 * Sets the version advice for the given id in the given namespace.
86 * @param namespace the namespace in which to look for advice
87 * @param id the item for which advice is sought
88 * @param version the version advice for the given id or <code>null</code> to remove advice
89 */
90 public void setVersion(String namespace, String id, Version version) {
91 Map<String, Version> values = versions.get(namespace);
92 if (values == null) {
93 // if we are clearing values then there is nothing to do
94 if (version == null)
95 return;
96 values = new HashMap<>();
97 versions.put(namespace, values);
98 }
99 if (version == null)
100 values.remove(id);
101 else
102 values.put(id, version);
103 }
104
105 public IPublisherAdvice merge(IPublisherAdvice advice) {
106 if (!(advice instanceof VersionAdvice))
107 return this;
108 VersionAdvice source = (VersionAdvice) advice;
109 for (String namespace : source.versions.keySet()) {
110 Map<String, Version> myValues = versions.get(namespace);
111 Map<String, Version> sourceValues = source.versions.get(namespace);
112 if (myValues == null)
113 versions.put(namespace, sourceValues);
114 else if (sourceValues != null)
115 versions.put(namespace, merge(myValues, sourceValues));
116 }
117 return this;
118 }
119
120 private Map<String, Version> merge(Map<String, Version> myValues, Map<String, Version> sourceValues) {
121 Map<String, Version> result = new HashMap<>(myValues);
122 for (String key : sourceValues.keySet()) {
123 if (result.get(key) == null)
124 result.put(key, sourceValues.get(key));
125 }
126 return result;
127 }
128 }