Debian Codegraph @

bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/p2/publisher/AdviceFileAdvice.java

raw
1 /*******************************************************************************
2 * Copyright (c) 2008, 2018 IBM Corporation and others.
3 *
4 * This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License 2.0
6 * which accompanies this distribution, and is available at
7 * https://www.eclipse.org/legal/epl-2.0/
8 *
9 * SPDX-License-Identifier: EPL-2.0
10 *
11 * Contributors:
12 * IBM Corporation - initial API and implementation
13 *******************************************************************************/
14 package org.eclipse.equinox.p2.publisher;
15
16 import java.io.*;
17 import java.util.Collections;
18 import java.util.Map;
19 import java.util.zip.ZipEntry;
20 import java.util.zip.ZipFile;
21 import org.eclipse.core.runtime.*;
22 import org.eclipse.equinox.internal.p2.core.helpers.CollectionUtils;
23 import org.eclipse.equinox.internal.p2.core.helpers.LogHelper;
24 import org.eclipse.equinox.internal.p2.publisher.Activator;
25 import org.eclipse.equinox.p2.metadata.*;
26 import org.eclipse.equinox.p2.metadata.MetadataFactory.InstallableUnitDescription;
27 import org.eclipse.equinox.p2.publisher.actions.*;
28 import org.eclipse.equinox.p2.repository.artifact.IArtifactDescriptor;
29
30 /**
31 * Publishing advice from a p2 advice file. An advice file (p2.inf) can be embedded
32 * in the source of a bundle, feature, or product to specify additional advice to be
33 * added to the {@link IInstallableUnit} corresponding to the bundle, feature, or product.
34 */
35 public class AdviceFileAdvice extends AbstractAdvice implements ITouchpointAdvice, ICapabilityAdvice, IUpdateDescriptorAdvice, IPropertyAdvice, IAdditionalInstallableUnitAdvice {
36
37 /**
38 * The location of the bundle advice file, relative to the bundle root location.
39 */
40 public static final IPath BUNDLE_ADVICE_FILE = IPath.fromOSString("META-INF/p2.inf"); //$NON-NLS-1$
41
42 private final String id;
43 private final Version version;
44
45 private Map<String, ITouchpointInstruction> touchpointInstructions;
46 private IProvidedCapability[] providedCapabilities;
47 private IRequirement[] requiredCapabilities;
48 private IRequirement[] metaRequiredCapabilities;
49 private Map<String, String> iuProperties;
50 private InstallableUnitDescription[] additionalIUs;
51 private IUpdateDescriptor updateDescriptor;
52 private boolean containsAdvice = false;
53
54 /**
55 * Creates advice for an advice file at the given location. If <tt>basePath</tt>
56 * is a directory, then <tt>adviceFilePath</tt> is appended to this location to
57 * obtain the location of the advice file. If <tt>basePath</tt> is a file, then
58 * <tt>adviceFilePath</tt> is used to
59 * @param id The symbolic id of the installable unit this advice applies to
60 * @param version The version of the installable unit this advice applies to
61 * @param basePath The root location of the the advice file. This is either the location of
62 * the jar containing the advice, or a directory containing the advice file
63 * @param adviceFilePath The location of the advice file within the base path. This is
64 * either the path of a jar entry, or the path of the advice file within the directory
65 * specified by the base path.
66 */
67 public AdviceFileAdvice(String id, Version version, IPath basePath, IPath adviceFilePath) {
68 Assert.isNotNull(id);
69 Assert.isNotNull(version);
70 Assert.isNotNull(basePath);
71 Assert.isNotNull(adviceFilePath);
72 this.id = id;
73 this.version = version;
74
75 Map<String, String> advice = loadAdviceMap(basePath, adviceFilePath);
76 if (advice.isEmpty())
77 return;
78
79 AdviceFileParser parser = new AdviceFileParser(id, version, advice);
80 try {
81 parser.parse();
82 } catch (Exception e) {
83 String message = "An error occured while parsing advice file: basePath=" + basePath + ", adviceFilePath=" + adviceFilePath + "."; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
84 IStatus status = new Status(IStatus.ERROR, Activator.ID, message, e);
85 LogHelper.log(status);
86 return;
87 }
88 touchpointInstructions = parser.getTouchpointInstructions();
89 providedCapabilities = parser.getProvidedCapabilities();
90 requiredCapabilities = parser.getRequiredCapabilities();
91 metaRequiredCapabilities = parser.getMetaRequiredCapabilities();
92 iuProperties = parser.getProperties();
93 additionalIUs = parser.getAdditionalInstallableUnitDescriptions();
94 updateDescriptor = parser.getUpdateDescriptor();
95 containsAdvice = true;
96 }
97
98 public boolean containsAdvice() {
99 return containsAdvice;
100 }
101
102 /**
103 * Loads the advice file and returns it in map form.
104 */
105 private static Map<String, String> loadAdviceMap(IPath basePath, IPath adviceFilePath) {
106 File location = basePath.toFile();
107 if (location == null || !location.exists())
108 return Collections.emptyMap();
109
110 ZipFile jar = null;
111 InputStream stream = null;
112 try {
113 if (location.isDirectory()) {
114 File adviceFile = new File(location, adviceFilePath.toString());
115 if (!adviceFile.isFile())
116 return Collections.emptyMap();
117 stream = new BufferedInputStream(new FileInputStream(adviceFile));
118 } else if (location.isFile()) {
119 jar = new ZipFile(location);
120 ZipEntry entry = jar.getEntry(adviceFilePath.toString());
121 if (entry == null)
122 return Collections.emptyMap();
123
124 stream = new BufferedInputStream(jar.getInputStream(entry));
125 }
126 return CollectionUtils.loadProperties(stream);
127 } catch (IOException e) {
128 String message = "An error occured while reading advice file: basePath=" + basePath + ", adviceFilePath=" + adviceFilePath + "."; //$NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$
129 IStatus status = new Status(IStatus.ERROR, Activator.ID, message, e);
130 LogHelper.log(status);
131 return Collections.emptyMap();
132 } finally {
133 if (stream != null)
134 try {
135 stream.close();
136 } catch (IOException e) {
137 // ignore secondary failure
138 }
139 if (jar != null)
140 try {
141 jar.close();
142 } catch (IOException e) {
143 // ignore secondary failure
144 }
145 }
146 }
147
148 @Override
149 public boolean isApplicable(String configSpec, boolean includeDefault, String candidateId, Version candidateVersion) {
150 return id.equals(candidateId) && version.equals(candidateVersion);
151 }
152
153 @Override
154 public ITouchpointData getTouchpointData(ITouchpointData existing) {
155 return MetadataFactory.mergeTouchpointData(existing, touchpointInstructions);
156 }
157
158 @Override
159 public IProvidedCapability[] getProvidedCapabilities(InstallableUnitDescription iu) {
160 return providedCapabilities;
161 }
162
163 @Override
164 public IRequirement[] getRequiredCapabilities(InstallableUnitDescription iu) {
165 return requiredCapabilities;
166 }
167
168 @Override
169 public IRequirement[] getMetaRequiredCapabilities(InstallableUnitDescription iu) {
170 return metaRequiredCapabilities;
171 }
172
173 @Override
174 public InstallableUnitDescription[] getAdditionalInstallableUnitDescriptions(IInstallableUnit iu) {
175 return additionalIUs;
176 }
177
178 @Override
179 public IUpdateDescriptor getUpdateDescriptor(InstallableUnitDescription iu) {
180 return updateDescriptor;
181 }
182
183 @Override
184 public Map<String, String> getArtifactProperties(IInstallableUnit iu, IArtifactDescriptor descriptor) {
185 return null;
186 }
187
188 @Override
189 public Map<String, String> getInstallableUnitProperties(InstallableUnitDescription iu) {
190 return iuProperties;
191 }
192 }