| 1 |
/******************************************************************************* |
| 2 |
* Copyright (c) 2008, 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 |
*******************************************************************************/ |
| 14 |
package org.eclipse.equinox.internal.p2.update; |
| 15 |
|
| 16 |
import java.io.*; |
| 17 |
import java.net.*; |
| 18 |
import java.util.*; |
| 19 |
import org.eclipse.core.runtime.*; |
| 20 |
import org.eclipse.equinox.internal.p2.core.helpers.LogHelper; |
| 21 |
import org.eclipse.equinox.internal.p2.core.helpers.URLUtil; |
| 22 |
import org.eclipse.equinox.internal.p2.touchpoint.eclipse.Activator; |
| 23 |
import org.eclipse.equinox.p2.core.ProvisionException; |
| 24 |
import org.eclipse.osgi.util.NLS; |
| 25 |
|
| 26 |
/** |
| 27 |
* @since 1.0 |
| 28 |
*/ |
| 29 |
public class ConfigurationWriter implements ConfigurationConstants { |
| 30 |
|
| 31 |
/* |
| 32 |
* Save the given configuration to the specified location. |
| 33 |
*/ |
| 34 |
static void save(Configuration configuration, File location, URL osgiInstallArea) throws ProvisionException { |
| 35 |
try (XMLWriter writer = new XMLWriter(new BufferedOutputStream(new FileOutputStream(location)))) { |
| 36 |
Map<String, String> args = new HashMap<>(); |
| 37 |
|
| 38 |
// always write out an up-to-date timestamp |
| 39 |
args.put(ATTRIBUTE_DATE, Long.toString(new Date().getTime())); |
| 40 |
|
| 41 |
String value = configuration.getSharedUR(); |
| 42 |
if (value != null) |
| 43 |
args.put(ATTRIBUTE_SHARED_UR, value); |
| 44 |
|
| 45 |
value = configuration.getVersion(); |
| 46 |
if (value != null) |
| 47 |
args.put(ATTRIBUTE_VERSION, value); |
| 48 |
|
| 49 |
args.put(ATTRIBUTE_TRANSIENT, Boolean.toString(configuration.isTransient())); |
| 50 |
|
| 51 |
writer.startTag(ELEMENT_CONFIG, args); |
| 52 |
|
| 53 |
for (Site site : configuration.internalGetSites(false)) { |
| 54 |
write(writer, site, osgiInstallArea); |
| 55 |
} |
| 56 |
|
| 57 |
writer.endTag(ELEMENT_CONFIG); |
| 58 |
} catch (FileNotFoundException e) { |
| 59 |
throw new ProvisionException(NLS.bind(Messages.error_saving_config, location), e); |
| 60 |
} |
| 61 |
// put the config in the cache in case someone in the same session wants to read it |
| 62 |
ConfigurationCache.put(location, configuration); |
| 63 |
} |
| 64 |
|
| 65 |
/* |
| 66 |
* Write out the given site. |
| 67 |
*/ |
| 68 |
private static void write(XMLWriter writer, Site site, URL osgiInstallArea) { |
| 69 |
Map<String, String> args = new HashMap<>(); |
| 70 |
|
| 71 |
String value = site.getLinkFile(); |
| 72 |
if (value != null) |
| 73 |
args.put(ATTRIBUTE_LINKFILE, value); |
| 74 |
|
| 75 |
value = site.getPolicy(); |
| 76 |
if (value != null) |
| 77 |
args.put(ATTRIBUTE_POLICY, value); |
| 78 |
|
| 79 |
value = site.getUrl(); |
| 80 |
if (value != null) { |
| 81 |
try { |
| 82 |
value = URIUtil.toUnencodedString(new URI(value)); |
| 83 |
} catch (URISyntaxException e) { |
| 84 |
// ignore this error and just use the value straight as-is |
| 85 |
} |
| 86 |
args.put(ATTRIBUTE_URL, getLocation(value, osgiInstallArea)); |
| 87 |
} |
| 88 |
|
| 89 |
value = toString(site.getList()); |
| 90 |
if (value != null) |
| 91 |
args.put(ATTRIBUTE_LIST, value); |
| 92 |
|
| 93 |
args.put(ATTRIBUTE_UPDATEABLE, Boolean.toString(site.isUpdateable())); |
| 94 |
args.put(ATTRIBUTE_ENABLED, Boolean.toString(site.isEnabled())); |
| 95 |
|
| 96 |
writer.startTag(ELEMENT_SITE, args); |
| 97 |
write(writer, site.getFeatures()); |
| 98 |
writer.endTag(ELEMENT_SITE); |
| 99 |
} |
| 100 |
|
| 101 |
/* |
| 102 |
* Return the location for the given location which is a url string. Take into account |
| 103 |
* the specified osgi install area. This method should make the path relative if |
| 104 |
* possible and could potentially return platform:/base/. |
| 105 |
*/ |
| 106 |
private static String getLocation(String value, URL osgiInstallArea) { |
| 107 |
if (osgiInstallArea == null || !value.startsWith("file:")) //$NON-NLS-1$ |
| 108 |
return value; |
| 109 |
try { |
| 110 |
// if our site represents the osgi install area, then write out platform:/base/ |
| 111 |
File installArea = URLUtil.toFile(osgiInstallArea); |
| 112 |
File path = URLUtil.toFile(new URL(value)); |
| 113 |
if (installArea.getAbsoluteFile().equals(path.getAbsoluteFile())) |
| 114 |
return ConfigurationParser.PLATFORM_BASE; |
| 115 |
} catch (MalformedURLException e) { |
| 116 |
LogHelper.log(new Status(IStatus.ERROR, Activator.ID, "Error occurred while writing configuration.", e)); //$NON-NLS-1$ |
| 117 |
} |
| 118 |
return PathUtil.makeRelative(value, osgiInstallArea); |
| 119 |
} |
| 120 |
|
| 121 |
/* |
| 122 |
* Convert the given list to a comma-separated string. |
| 123 |
*/ |
| 124 |
private static String toString(Object[] list) { |
| 125 |
if (list == null || list.length == 0) |
| 126 |
return null; |
| 127 |
StringBuilder buffer = new StringBuilder(); |
| 128 |
for (int i = 0; i < list.length; i++) { |
| 129 |
buffer.append(list[i].toString()); |
| 130 |
if (i + 1 < list.length) |
| 131 |
buffer.append(','); |
| 132 |
} |
| 133 |
return buffer.toString(); |
| 134 |
} |
| 135 |
|
| 136 |
/* |
| 137 |
* Write out the given list of features. |
| 138 |
*/ |
| 139 |
private static void write(XMLWriter writer, Feature[] features) { |
| 140 |
if (features == null || features.length == 0) |
| 141 |
return; |
| 142 |
for (Feature feature : features) { |
| 143 |
Map<String, String> args = new HashMap<>(); |
| 144 |
String value = feature.getId(); |
| 145 |
if (value != null) |
| 146 |
args.put(ATTRIBUTE_ID, value); |
| 147 |
value = feature.getUrl(); |
| 148 |
if (value != null) |
| 149 |
args.put(ATTRIBUTE_URL, value); |
| 150 |
value = feature.getVersion(); |
| 151 |
if (value != null) |
| 152 |
args.put(ATTRIBUTE_VERSION, value); |
| 153 |
value = feature.getPluginIdentifier(); |
| 154 |
// only write out the plug-in identifier if it is different from the feature id |
| 155 |
if (value != null && !value.equals(feature.getId())) |
| 156 |
args.put(ATTRIBUTE_PLUGIN_IDENTIFIER, value); |
| 157 |
value = feature.getPluginVersion(); |
| 158 |
// only write out the plug-in version if it is different from the feature version |
| 159 |
if (value != null && !value.equals(feature.getVersion())) |
| 160 |
args.put(ATTRIBUTE_PLUGIN_VERSION, value); |
| 161 |
if (feature.isPrimary()) |
| 162 |
args.put(ATTRIBUTE_PRIMARY, "true"); //$NON-NLS-1$ |
| 163 |
value = feature.getApplication(); |
| 164 |
if (value != null) |
| 165 |
args.put(ATTRIBUTE_APPLICATION, value); |
| 166 |
|
| 167 |
// collect the roots |
| 168 |
URL[] roots = feature.getRoots(); |
| 169 |
if (roots != null && roots.length > 0) |
| 170 |
args.put(ATTRIBUTE_ROOT, toString(roots)); |
| 171 |
|
| 172 |
writer.startTag(ELEMENT_FEATURE, args); |
| 173 |
writer.endTag(ELEMENT_FEATURE); |
| 174 |
} |
| 175 |
} |
| 176 |
} |