Debian Codegraph @

bundles/org.eclipse.equinox.p2.installer/src/org/eclipse/equinox/internal/p2/installer/InstallDescriptionParser.java

raw
1 /*******************************************************************************
2 * Copyright (c) 2007, 2017 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 * Code 9 - ongoing development
14 * Sonatype, Inc. - ongoing development
15 *******************************************************************************/
16 package org.eclipse.equinox.internal.p2.installer;
17
18 import java.io.*;
19 import java.net.URI;
20 import java.net.URISyntaxException;
21 import java.util.*;
22 import org.eclipse.core.runtime.*;
23 import org.eclipse.equinox.internal.p2.core.helpers.CollectionUtils;
24 import org.eclipse.equinox.internal.p2.core.helpers.LogHelper;
25 import org.eclipse.equinox.internal.p2.transport.ecf.RepositoryTransport;
26 import org.eclipse.equinox.internal.provisional.p2.installer.InstallDescription;
27 import org.eclipse.equinox.p2.metadata.IVersionedId;
28 import org.eclipse.equinox.p2.metadata.VersionedId;
29
30 /**
31 * This class is responsible for loading install descriptions from a stream.
32 */
33 public class InstallDescriptionParser {
34 private static final String PROP_AGENT_LOCATION = "eclipse.p2.agentLocation"; //$NON-NLS-1$
35 private static final String PROP_ARTIFACT_REPOSITORY = "eclipse.p2.artifacts";//$NON-NLS-1$
36 private static final String PROP_BUNDLE_LOCATION = "eclipse.p2.bundleLocation";//$NON-NLS-1$
37 private static final String PROP_INSTALL_LOCATION = "eclipse.p2.installLocation";//$NON-NLS-1$
38 private static final String PROP_IS_AUTO_START = "eclipse.p2.autoStart";//$NON-NLS-1$
39 private static final String PROP_LAUNCHER_NAME = "eclipse.p2.launcherName";//$NON-NLS-1$
40 private static final String PROP_METADATA_REPOSITORY = "eclipse.p2.metadata";//$NON-NLS-1$
41 private static final String PROP_PROFILE_NAME = "eclipse.p2.profileName";//$NON-NLS-1$
42 private static final String PROP_ROOT_ID = "eclipse.p2.rootId";//$NON-NLS-1$
43 private static final String PROP_ROOT_VERSION = "eclipse.p2.rootVersion";//$NON-NLS-1$
44 private static final String PROP_ROOTS = "eclipse.p2.roots";//$NON-NLS-1$
45
46 /**
47 * Loads and returns an install description that is stored in a properties file.
48 * @param site The URL of the install properties file.
49 */
50 public static InstallDescription createDescription(String site, SubMonitor monitor) throws Exception {
51 // if no description URL was given from the outside, look for an "install.properties" file
52 // in relative to where the installer is running. This allows the installer to be self-contained
53 if (site == null)
54 site = "installer.properties"; //$NON-NLS-1$
55
56 URI propsURI = URIUtil.fromString(site);
57 InputStream in = null;
58 if (!propsURI.isAbsolute()) {
59 String installerInstallArea = System.getProperty("osgi.install.area"); //$NON-NLS-1$
60 if (installerInstallArea == null)
61 throw new IllegalStateException("Install area is not specified."); //$NON-NLS-1$
62
63 propsURI = URIUtil.append(URIUtil.fromString(installerInstallArea), site);
64 File installerDescription = URIUtil.toFile(propsURI);
65 if (!installerDescription.exists()) {
66 throw new IllegalStateException("Can't find install description file: " + installerDescription); //$NON-NLS-1$
67 }
68 }
69 Map<String, String> properties;
70 try {
71 in = new RepositoryTransport().stream(propsURI, monitor);
72 properties = CollectionUtils.loadProperties(in);
73 } finally {
74 safeClose(in);
75 }
76
77 URI base = getBase(propsURI);
78 InstallDescription result = new InstallDescription();
79 result = initialize(result, properties, base);
80 initializeProfileProperties(result, properties);
81
82 // now override the properties from anything interesting in system properties
83 result = initialize(result, CollectionUtils.toMap(System.getProperties()), base);
84 return result;
85 }
86
87 private static URI getBase(URI uri) {
88 if (uri == null)
89 return null;
90
91 String uriString = uri.toString();
92 int slashIndex = uriString.lastIndexOf('/');
93 if (slashIndex == -1 || slashIndex == (uriString.length() - 1))
94 return uri;
95
96 return URI.create(uriString.substring(0, slashIndex + 1));
97 }
98
99 private static InstallDescription initialize(InstallDescription description, Map<String, String> properties, URI base) {
100 String property = properties.get(PROP_ARTIFACT_REPOSITORY);
101 if (property != null)
102 description.setArtifactRepositories(getURIs(property, base));
103
104 property = properties.get(PROP_METADATA_REPOSITORY);
105 if (property != null)
106 description.setMetadataRepositories(getURIs(property, base));
107
108 property = properties.get(PROP_IS_AUTO_START);
109 if (property != null)
110 description.setAutoStart(Boolean.TRUE.toString().equalsIgnoreCase(property));
111
112 property = properties.get(PROP_LAUNCHER_NAME);
113 if (property != null)
114 description.setLauncherName(property);
115
116 property = properties.get(PROP_INSTALL_LOCATION);
117 if (property != null)
118 description.setInstallLocation(IPath.fromOSString(property));
119
120 property = properties.get(PROP_AGENT_LOCATION);
121 if (property != null)
122 description.setAgentLocation(IPath.fromOSString(property));
123
124 property = properties.get(PROP_BUNDLE_LOCATION);
125 if (property != null)
126 description.setBundleLocation(IPath.fromOSString(property));
127
128 property = properties.get(PROP_PROFILE_NAME);
129 if (property != null)
130 description.setProductName(property);
131
132 // Process the retro root id and rootVersion properties
133 String id = properties.get(PROP_ROOT_ID);
134 if (id != null) {
135 String version = properties.get(PROP_ROOT_VERSION);
136 try {
137 description.setRoots(new IVersionedId[] {new VersionedId(id, version)});
138 } catch (IllegalArgumentException e) {
139 LogHelper.log(new Status(IStatus.ERROR, InstallerActivator.PI_INSTALLER, "Invalid version in install description: " + version, e)); //$NON-NLS-1$
140 }
141 }
142
143 String rootSpec = properties.get(PROP_ROOTS);
144 if (rootSpec != null) {
145 String[] rootList = getArrayFromString(rootSpec, ","); //$NON-NLS-1$
146 ArrayList<IVersionedId> roots = new ArrayList<>(rootList.length);
147 for (String rootList1 : rootList) {
148 try {
149 roots.add(VersionedId.parse(rootList1));
150 } catch (IllegalArgumentException e) {
151 LogHelper.log(new Status(IStatus.ERROR, InstallerActivator.PI_INSTALLER, "Invalid version in install description: " + rootList1, e)); //$NON-NLS-1$
152 }
153 }
154 if (!roots.isEmpty())
155 description.setRoots(roots.toArray(new IVersionedId[roots.size()]));
156 }
157 return description;
158 }
159
160 /**
161 * Add all of the given properties to profile properties of the given description
162 * after removing the keys known to be for the installer. This allows install descriptions
163 * to also set random profile properties.
164 * @param description
165 * @param properties
166 */
167 private static void initializeProfileProperties(InstallDescription description, Map<String, String> properties) {
168 //any remaining properties are profile properties
169 Map<String, String> profileProperties = new HashMap<>(properties);
170 profileProperties.remove(PROP_PROFILE_NAME);
171 profileProperties.remove(PROP_ARTIFACT_REPOSITORY);
172 profileProperties.remove(PROP_METADATA_REPOSITORY);
173 profileProperties.remove(PROP_IS_AUTO_START);
174 profileProperties.remove(PROP_LAUNCHER_NAME);
175 profileProperties.remove(PROP_AGENT_LOCATION);
176 profileProperties.remove(PROP_BUNDLE_LOCATION);
177 profileProperties.remove(PROP_ROOT_ID);
178 profileProperties.remove(PROP_ROOT_VERSION);
179 profileProperties.remove(PROP_ROOTS);
180 description.setProfileProperties(profileProperties);
181 }
182
183 /**
184 * Returns an array of URIs from the given comma-separated list
185 * of URLs. Returns null if the given spec does not contain any URLs.
186 * @param base
187 * @return An array of URIs in the given spec, or <code>null</code>
188 */
189 private static URI[] getURIs(String spec, URI base) {
190 String[] urlSpecs = getArrayFromString(spec, ","); //$NON-NLS-1$
191 ArrayList<URI> result = new ArrayList<>(urlSpecs.length);
192 for (String urlSpec : urlSpecs) {
193 try {
194 URI uri = URIUtil.fromString(urlSpec);
195 uri = URIUtil.makeAbsolute(uri, base);
196 result.add(uri);
197 } catch (URISyntaxException e) {
198 LogHelper.log(new Status(IStatus.ERROR, InstallerActivator.PI_INSTALLER, "Invalid URL in install description: " + urlSpec, e)); //$NON-NLS-1$
199 }
200 }
201 if (result.isEmpty())
202 return null;
203 return result.toArray(new URI[result.size()]);
204 }
205
206 private static void safeClose(InputStream in) {
207 try {
208 if (in != null)
209 in.close();
210 } catch (IOException e) {
211 //ignore secondary failure during close
212 }
213 }
214
215 /**
216 * Convert a list of tokens into an array. The list separator has to be
217 * specified.
218 */
219 public static String[] getArrayFromString(String list, String separator) {
220 if (list == null || list.trim().equals("")) //$NON-NLS-1$
221 return new String[0];
222 List<String> result = new ArrayList<>();
223 for (StringTokenizer tokens = new StringTokenizer(list, separator); tokens.hasMoreTokens();) {
224 String token = tokens.nextToken().trim();
225 if (!token.equals("")) //$NON-NLS-1$
226 result.add(token);
227 }
228 return result.toArray(new String[result.size()]);
229 }
230
231 }