| 1 |
/******************************************************************************* |
| 2 |
* Copyright (c) 2008, 2021 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 |
* SAP AG - ongoing development |
| 16 |
******************************************************************************/ |
| 17 |
package org.eclipse.equinox.p2.publisher.actions; |
| 18 |
|
| 19 |
import java.io.BufferedInputStream; |
| 20 |
import java.io.File; |
| 21 |
import java.io.FileFilter; |
| 22 |
import java.io.FileInputStream; |
| 23 |
import java.io.FileNotFoundException; |
| 24 |
import java.io.FileWriter; |
| 25 |
import java.io.IOException; |
| 26 |
import java.io.InputStream; |
| 27 |
import java.lang.module.ModuleDescriptor; |
| 28 |
import java.lang.module.ModuleDescriptor.Exports; |
| 29 |
import java.net.URL; |
| 30 |
import java.nio.file.Files; |
| 31 |
import java.util.ArrayList; |
| 32 |
import java.util.Collections; |
| 33 |
import java.util.HashMap; |
| 34 |
import java.util.List; |
| 35 |
import java.util.Map; |
| 36 |
import java.util.Set; |
| 37 |
|
| 38 |
import org.eclipse.core.runtime.IPath; |
| 39 |
import org.eclipse.core.runtime.IProgressMonitor; |
| 40 |
import org.eclipse.core.runtime.IStatus; |
| 41 |
import org.eclipse.core.runtime.MultiStatus; |
| 42 |
import org.eclipse.core.runtime.Status; |
| 43 |
import org.eclipse.equinox.internal.p2.core.helpers.CollectionUtils; |
| 44 |
import org.eclipse.equinox.internal.p2.metadata.ArtifactKey; |
| 45 |
import org.eclipse.equinox.internal.p2.publisher.Activator; |
| 46 |
import org.eclipse.equinox.internal.p2.publisher.Messages; |
| 47 |
import org.eclipse.equinox.p2.metadata.IArtifactKey; |
| 48 |
import org.eclipse.equinox.p2.metadata.IInstallableUnit; |
| 49 |
import org.eclipse.equinox.p2.metadata.IProvidedCapability; |
| 50 |
import org.eclipse.equinox.p2.metadata.MetadataFactory; |
| 51 |
import org.eclipse.equinox.p2.metadata.MetadataFactory.InstallableUnitDescription; |
| 52 |
import org.eclipse.equinox.p2.metadata.MetadataFactory.InstallableUnitFragmentDescription; |
| 53 |
import org.eclipse.equinox.p2.metadata.Version; |
| 54 |
import org.eclipse.equinox.p2.metadata.VersionRange; |
| 55 |
import org.eclipse.equinox.p2.publisher.AbstractPublisherAction; |
| 56 |
import org.eclipse.equinox.p2.publisher.IPublisherInfo; |
| 57 |
import org.eclipse.equinox.p2.publisher.IPublisherResult; |
| 58 |
import org.eclipse.equinox.p2.repository.artifact.IArtifactDescriptor; |
| 59 |
import org.eclipse.equinox.spi.p2.publisher.PublisherHelper; |
| 60 |
import org.eclipse.osgi.util.ManifestElement; |
| 61 |
import org.eclipse.osgi.util.NLS; |
| 62 |
import org.osgi.framework.BundleException; |
| 63 |
import org.osgi.framework.Constants; |
| 64 |
import org.osgi.framework.FrameworkUtil; |
| 65 |
|
| 66 |
public class JREAction extends AbstractPublisherAction { |
| 67 |
private static final String DEFAULT_JRE_NAME = "a.jre"; //$NON-NLS-1$ |
| 68 |
private static final Version DEFAULT_JRE_VERSION = Version.parseVersion("17.0"); //$NON-NLS-1$ |
| 69 |
private static final String DEFAULT_PROFILE = "JavaSE-17"; //$NON-NLS-1$ |
| 70 |
private static final String PROFILE_LOCATION = "jre.action.profile.location"; //$NON-NLS-1$ |
| 71 |
private static final String PROFILE_NAME = "osgi.java.profile.name"; //$NON-NLS-1$ |
| 72 |
private static final String PROFILE_TARGET_VERSION = "org.eclipse.jdt.core.compiler.codegen.targetPlatform"; //$NON-NLS-1$ |
| 73 |
private static final String PROFILE_SYSTEM_PACKAGES = "org.osgi.framework.system.packages"; //$NON-NLS-1$ |
| 74 |
|
| 75 |
public static final String NAMESPACE_OSGI_EE = "osgi.ee"; //$NON-NLS-1$ |
| 76 |
public static final String VERSION_OSGI_EE = "version"; //$NON-NLS-1$ |
| 77 |
|
| 78 |
private File jreLocation; |
| 79 |
private String environment; |
| 80 |
private Map<String, String> profileProperties; |
| 81 |
private MultiStatus resultStatus; |
| 82 |
|
| 83 |
public JREAction(File location) { |
| 84 |
this.jreLocation = location; |
| 85 |
} |
| 86 |
|
| 87 |
public JREAction(String environment) { |
| 88 |
this.environment = environment; |
| 89 |
} |
| 90 |
|
| 91 |
@Override public IStatus perform(IPublisherInfo publisherInfo, IPublisherResult results, IProgressMonitor monitor) { |
| 92 |
String problemMessage = NLS.bind(Messages.message_problemsWhilePublishingEE, jreLocation != null ? jreLocation : environment); |
| 93 |
resultStatus = new MultiStatus(Activator.ID, 0, problemMessage, null); |
| 94 |
|
| 95 |
initialize(publisherInfo); |
| 96 |
IArtifactDescriptor artifact = createJREData(results); |
| 97 |
if (artifact != null) |
| 98 |
publishArtifact(artifact, new File[] {jreLocation}, null, publisherInfo, createRootPrefixComputer(jreLocation)); |
| 99 |
|
| 100 |
if (resultStatus.isOK()) |
| 101 |
return Status.OK_STATUS; |
| 102 |
return resultStatus; |
| 103 |
} |
| 104 |
|
| 105 |
private static Status newErrorStatus(String message, Exception exception) { |
| 106 |
return new Status(IStatus.ERROR, Activator.ID, message, exception); |
| 107 |
} |
| 108 |
|
| 109 |
private static Status newWarningStatus(String message) { |
| 110 |
return new Status(IStatus.WARNING, Activator.ID, message, null); |
| 111 |
} |
| 112 |
|
| 113 |
/** |
| 114 |
* Creates IUs and artifact descriptors for the JRE. The resulting IUs are added |
| 115 |
* to the given set, and the resulting artifact descriptor, if any, is returned. |
| 116 |
* If the jreLocation is <code>null</code>, default information is generated. |
| 117 |
*/ |
| 118 |
protected IArtifactDescriptor createJREData(IPublisherResult results) { |
| 119 |
InstallableUnitDescription iu = new MetadataFactory.InstallableUnitDescription(); |
| 120 |
iu.setSingleton(false); |
| 121 |
iu.setId(DEFAULT_JRE_NAME); |
| 122 |
iu.setVersion(DEFAULT_JRE_VERSION); |
| 123 |
iu.setTouchpointType(PublisherHelper.TOUCHPOINT_NATIVE); |
| 124 |
|
| 125 |
generateJREIUData(iu); |
| 126 |
|
| 127 |
InstallableUnitFragmentDescription cu = new InstallableUnitFragmentDescription(); |
| 128 |
String configId = "config." + iu.getId();//$NON-NLS-1$ |
| 129 |
cu.setId(configId); |
| 130 |
cu.setVersion(iu.getVersion()); |
| 131 |
VersionRange range = iu.getVersion() == Version.emptyVersion ? VersionRange.emptyRange : new VersionRange(iu.getVersion(), true, Version.MAX_VERSION, true); |
| 132 |
cu.setHost(MetadataFactory.createRequirement(IInstallableUnit.NAMESPACE_IU_ID, iu.getId(), range, null, false, |
| 133 |
false)); |
| 134 |
cu.setProperty(InstallableUnitDescription.PROP_TYPE_FRAGMENT, Boolean.TRUE.toString()); |
| 135 |
cu.setCapabilities(new IProvidedCapability[] {PublisherHelper.createSelfCapability(configId, iu.getVersion())}); |
| 136 |
cu.setTouchpointType(PublisherHelper.TOUCHPOINT_NATIVE); |
| 137 |
Map<String, String> touchpointData = new HashMap<>(); |
| 138 |
|
| 139 |
if (jreLocation == null || !jreLocation.isDirectory()) { |
| 140 |
touchpointData.put("install", ""); //$NON-NLS-1$ //$NON-NLS-2$ |
| 141 |
cu.addTouchpointData(MetadataFactory.createTouchpointData(touchpointData)); |
| 142 |
results.addIU(MetadataFactory.createInstallableUnit(iu), IPublisherResult.ROOT); |
| 143 |
results.addIU(MetadataFactory.createInstallableUnit(cu), IPublisherResult.ROOT); |
| 144 |
return null; |
| 145 |
} |
| 146 |
|
| 147 |
//Generate artifact for JRE |
| 148 |
IArtifactKey key = new ArtifactKey(PublisherHelper.BINARY_ARTIFACT_CLASSIFIER, iu.getId(), iu.getVersion()); |
| 149 |
iu.setArtifacts(new IArtifactKey[] {key}); |
| 150 |
results.addIU(MetadataFactory.createInstallableUnit(iu), IPublisherResult.ROOT); |
| 151 |
|
| 152 |
//Create config info for the CU |
| 153 |
String configurationData = "unzip(source:@artifact, target:${installFolder});"; //$NON-NLS-1$ |
| 154 |
touchpointData.put("install", configurationData); //$NON-NLS-1$ |
| 155 |
String unConfigurationData = "cleanupzip(source:@artifact, target:${installFolder});"; //$NON-NLS-1$ |
| 156 |
touchpointData.put("uninstall", unConfigurationData); //$NON-NLS-1$ |
| 157 |
cu.addTouchpointData(MetadataFactory.createTouchpointData(touchpointData)); |
| 158 |
results.addIU(MetadataFactory.createInstallableUnit(cu), IPublisherResult.ROOT); |
| 159 |
|
| 160 |
//Create the artifact descriptor |
| 161 |
return PublisherHelper.createArtifactDescriptor(info, key, jreLocation); |
| 162 |
} |
| 163 |
|
| 164 |
private List<IProvidedCapability> generateJRECapability(String id, Version version) { |
| 165 |
if (profileProperties == null) |
| 166 |
return Collections.emptyList(); |
| 167 |
|
| 168 |
List<IProvidedCapability> result = new ArrayList<>(); |
| 169 |
result.add(PublisherHelper.createSelfCapability(id, version)); |
| 170 |
generateProvidedPackages(result); |
| 171 |
generateOsgiEESystemCapabilities(result); |
| 172 |
return result; |
| 173 |
} |
| 174 |
|
| 175 |
private void generateProvidedPackages(List<IProvidedCapability> result) { |
| 176 |
String packages = profileProperties.get(PROFILE_SYSTEM_PACKAGES); |
| 177 |
if (packages != null && (packages.trim().length() > 0)) { |
| 178 |
|
| 179 |
try { |
| 180 |
ManifestElement[] jrePackages = ManifestElement.parseHeader(PROFILE_SYSTEM_PACKAGES, packages); |
| 181 |
for (ManifestElement jrePackage : jrePackages) { |
| 182 |
String packageName = jrePackage.getValue(); |
| 183 |
Version packageVersion = Version.create(jrePackage.getAttribute("version")); //$NON-NLS-1$ |
| 184 |
result.add(MetadataFactory.createProvidedCapability(PublisherHelper.CAPABILITY_NS_JAVA_PACKAGE, packageName, packageVersion)); |
| 185 |
} |
| 186 |
} catch (BundleException e) { |
| 187 |
// TODO Auto-generated catch block |
| 188 |
e.printStackTrace(); |
| 189 |
} |
| 190 |
} |
| 191 |
} |
| 192 |
|
| 193 |
void generateOsgiEESystemCapabilities(List<IProvidedCapability> result) { |
| 194 |
String message = NLS.bind(Messages.message_problemsWhileParsingProfileProperty, Constants.FRAMEWORK_SYSTEMCAPABILITIES); |
| 195 |
MultiStatus parsingStatus = new MultiStatus(Activator.ID, 0, message, null); |
| 196 |
|
| 197 |
String systemCapabilities = profileProperties.get(Constants.FRAMEWORK_SYSTEMCAPABILITIES); |
| 198 |
parseSystemCapabilities(systemCapabilities, parsingStatus, result); |
| 199 |
|
| 200 |
// result contains the valid entries, parsingStatus the invalid entries |
| 201 |
if (!parsingStatus.isOK()) |
| 202 |
resultStatus.add(parsingStatus); |
| 203 |
} |
| 204 |
|
| 205 |
static void parseSystemCapabilities(String systemCapabilities, MultiStatus parsingStatus, List<IProvidedCapability> parsingResult) { |
| 206 |
if (systemCapabilities == null || (systemCapabilities.trim().length() == 0)) { |
| 207 |
return; |
| 208 |
} |
| 209 |
|
| 210 |
try { |
| 211 |
ManifestElement[] eeEntries = ManifestElement.parseHeader(Constants.FRAMEWORK_SYSTEMCAPABILITIES, systemCapabilities); |
| 212 |
parseSystemCapabilities(eeEntries, parsingStatus, parsingResult); |
| 213 |
|
| 214 |
} catch (BundleException e) { |
| 215 |
parsingStatus.add(newErrorStatus(e.getLocalizedMessage(), e)); |
| 216 |
} |
| 217 |
} |
| 218 |
|
| 219 |
private static void parseSystemCapabilities(ManifestElement[] systemCapabilities, MultiStatus parsingStatus, List<IProvidedCapability> parsingResult) { |
| 220 |
for (ManifestElement systemCapability : systemCapabilities) { |
| 221 |
// this is general manifest syntax: a "manifest element" can have multiple "value components" -> all attributes apply to each value component (=namespace) |
| 222 |
String[] namespaces = systemCapability.getValueComponents(); |
| 223 |
for (String namespace : namespaces) { |
| 224 |
if (NAMESPACE_OSGI_EE.equals(namespace)) { // this is the OSGi capability namespace "osgi.ee" |
| 225 |
parseEECapability(systemCapability, parsingStatus, parsingResult); |
| 226 |
} else { |
| 227 |
parsingStatus.add(newWarningStatus(NLS.bind(Messages.message_eeIgnoringNamespace, namespace))); |
| 228 |
} |
| 229 |
} |
| 230 |
} |
| 231 |
} |
| 232 |
|
| 233 |
private static void parseEECapability(ManifestElement eeCapability, MultiStatus parsingStatus, List<IProvidedCapability> parsingResult) { |
| 234 |
String eeName = eeCapability.getAttribute(NAMESPACE_OSGI_EE); // this is an attribute required for capabilities in the "osgi.ee" namespace |
| 235 |
if (eeName == null) { |
| 236 |
parsingStatus.add(newErrorStatus(NLS.bind(Messages.message_eeMissingNameAttribute, eeCapability), null)); |
| 237 |
return; |
| 238 |
} |
| 239 |
|
| 240 |
String[] eeVersions = parseEECapabilityVersion(eeCapability, parsingStatus); |
| 241 |
if (eeVersions == null) { |
| 242 |
// status was already updated by parse method |
| 243 |
return; |
| 244 |
} |
| 245 |
|
| 246 |
for (String rawVersion : eeVersions) { |
| 247 |
try { |
| 248 |
Version parsedVersion = Version.parseVersion(rawVersion); |
| 249 |
|
| 250 |
// complete record -> store |
| 251 |
Map<String, Object> capAttrs = new HashMap<>(); |
| 252 |
capAttrs.put(NAMESPACE_OSGI_EE, eeName); |
| 253 |
capAttrs.put(VERSION_OSGI_EE, parsedVersion); |
| 254 |
|
| 255 |
parsingResult.add(MetadataFactory.createProvidedCapability(NAMESPACE_OSGI_EE, capAttrs)); |
| 256 |
|
| 257 |
} catch (IllegalArgumentException e) { |
| 258 |
parsingStatus.add(newErrorStatus(NLS.bind(Messages.message_eeInvalidVersionAttribute, rawVersion), e)); |
| 259 |
} |
| 260 |
} |
| 261 |
} |
| 262 |
|
| 263 |
private static String[] parseEECapabilityVersion(ManifestElement eeCapability, MultiStatus parsingStatus) { |
| 264 |
String singleVersion = eeCapability.getAttribute("version:Version"); //$NON-NLS-1$ |
| 265 |
String[] multipleVersions = ManifestElement.getArrayFromList(eeCapability.getAttribute("version:List<Version>")); //$NON-NLS-1$ |
| 266 |
|
| 267 |
if (singleVersion == null && multipleVersions == null) { |
| 268 |
parsingStatus.add(newErrorStatus(NLS.bind(Messages.message_eeMissingVersionAttribute, eeCapability), null)); |
| 269 |
return null; |
| 270 |
|
| 271 |
} else if (singleVersion == null) { |
| 272 |
return multipleVersions; |
| 273 |
|
| 274 |
} else if (multipleVersions == null) { |
| 275 |
return new String[] {singleVersion}; |
| 276 |
|
| 277 |
} else { |
| 278 |
parsingStatus.add(newErrorStatus(NLS.bind(Messages.message_eeDuplicateVersionAttribute, eeCapability), null)); |
| 279 |
return null; |
| 280 |
} |
| 281 |
} |
| 282 |
|
| 283 |
private void generateJREIUData(InstallableUnitDescription iu) { |
| 284 |
if (profileProperties == null || profileProperties.size() == 0) |
| 285 |
return; //got nothing |
| 286 |
|
| 287 |
String profileLocation = profileProperties.get(PROFILE_LOCATION); |
| 288 |
|
| 289 |
String profileName = profileLocation != null ? IPath.fromOSString(profileLocation).lastSegment() : profileProperties.get(PROFILE_NAME); |
| 290 |
if (profileName.endsWith(".profile")) //$NON-NLS-1$ |
| 291 |
profileName = profileName.substring(0, profileName.length() - 8); |
| 292 |
Version version = null; |
| 293 |
int idx = profileName.indexOf('-'); |
| 294 |
if (idx != -1) { |
| 295 |
try { |
| 296 |
version = Version.parseVersion(profileName.substring(idx + 1)); |
| 297 |
} catch (IllegalArgumentException e) { |
| 298 |
//ignore |
| 299 |
} |
| 300 |
profileName = profileName.substring(0, idx); |
| 301 |
} |
| 302 |
if (version == null) { |
| 303 |
try { |
| 304 |
String targetVersion = profileProperties.get(PROFILE_TARGET_VERSION); |
| 305 |
version = targetVersion != null ? Version.parseVersion(targetVersion) : null; |
| 306 |
} catch (IllegalArgumentException e) { |
| 307 |
//ignore |
| 308 |
} |
| 309 |
} |
| 310 |
|
| 311 |
if (version == null) |
| 312 |
version = DEFAULT_JRE_VERSION; |
| 313 |
|
| 314 |
iu.setVersion(version); |
| 315 |
|
| 316 |
profileName = profileName.replace('-', '.'); |
| 317 |
profileName = profileName.replace('/', '.'); |
| 318 |
profileName = profileName.replace('_', '.'); |
| 319 |
iu.setId("a.jre." + profileName.toLowerCase()); //$NON-NLS-1$ |
| 320 |
|
| 321 |
List<IProvidedCapability> capabilities = generateJRECapability(iu.getId(), iu.getVersion()); |
| 322 |
iu.addProvidedCapabilities(capabilities); |
| 323 |
} |
| 324 |
|
| 325 |
private void initialize(IPublisherInfo publisherInfo) { |
| 326 |
File runtimeProfile = null; |
| 327 |
this.info = publisherInfo; |
| 328 |
if (jreLocation == null && environment == null) { |
| 329 |
// create a runtime profile |
| 330 |
StringBuilder buffer = createDefaultProfileFromRunningJvm(); |
| 331 |
try { |
| 332 |
File tempDirectory = Files.createTempDirectory("JREAction").toFile(); //$NON-NLS-1$ |
| 333 |
runtimeProfile = new File(tempDirectory, DEFAULT_PROFILE + ".profile"); //$NON-NLS-1$ |
| 334 |
try (FileWriter writer = new FileWriter(runtimeProfile)) { |
| 335 |
writer.write(buffer.toString()); |
| 336 |
} |
| 337 |
jreLocation = runtimeProfile; |
| 338 |
} catch (IOException e) { |
| 339 |
// ignore |
| 340 |
} |
| 341 |
} |
| 342 |
if (jreLocation != null) { |
| 343 |
|
| 344 |
File javaProfile = null; |
| 345 |
|
| 346 |
if (jreLocation.isDirectory()) { |
| 347 |
//Look for a JRE profile file to set version and capabilities |
| 348 |
File[] profiles = jreLocation.listFiles((FileFilter) pathname -> pathname.getAbsolutePath().endsWith(".profile")); //$NON-NLS-1$ |
| 349 |
if (profiles != null && profiles.length > 0) { |
| 350 |
javaProfile = profiles[0]; |
| 351 |
} |
| 352 |
} else if (jreLocation.isFile()) |
| 353 |
javaProfile = jreLocation; |
| 354 |
else |
| 355 |
// jreLocation file does not exist |
| 356 |
throw new IllegalArgumentException(NLS.bind(Messages.exception_nonExistingJreLocationFile, jreLocation.getAbsolutePath())); |
| 357 |
|
| 358 |
profileProperties = loadProfile(javaProfile); |
| 359 |
} |
| 360 |
if (profileProperties == null) { |
| 361 |
String profileFile = (environment != null ? environment : DEFAULT_PROFILE).replace('/', '_') + ".profile"; //$NON-NLS-1$ |
| 362 |
URL profileURL = getResouceFromSystemBundle(profileFile); |
| 363 |
profileProperties = loadProfile(profileURL); |
| 364 |
} |
| 365 |
if (runtimeProfile != null) { |
| 366 |
runtimeProfile.delete(); |
| 367 |
runtimeProfile.getParentFile().delete(); |
| 368 |
} |
| 369 |
} |
| 370 |
|
| 371 |
/* |
| 372 |
* Copied from org.eclipse.osgi.storage.Storage.calculateVMPackages and adopted |
| 373 |
* to Java 11 |
| 374 |
*/ |
| 375 |
private String calculateVMPackages() { |
| 376 |
try { |
| 377 |
List<String> packages = new ArrayList<>(); |
| 378 |
ModuleLayer bootLayer = ModuleLayer.boot(); |
| 379 |
Set<Module> bootModules = bootLayer.modules(); |
| 380 |
for (Module m : bootModules) { |
| 381 |
ModuleDescriptor descriptor = m.getDescriptor(); |
| 382 |
if (descriptor.isAutomatic()) { |
| 383 |
/* |
| 384 |
* Automatic modules are supposed to export all their packages. However, |
| 385 |
* java.lang.module.ModuleDescriptor::exports returns an empty set for them. Add |
| 386 |
* all their packages (as returned by |
| 387 |
* java.lang.module.ModuleDescriptor::packages) to the list of VM supplied |
| 388 |
* packages. |
| 389 |
*/ |
| 390 |
packages.addAll(descriptor.packages()); |
| 391 |
} else { |
| 392 |
for (Exports export : descriptor.exports()) { |
| 393 |
String pkg = export.source(); |
| 394 |
if (!export.isQualified()) { |
| 395 |
packages.add(pkg); |
| 396 |
} |
| 397 |
} |
| 398 |
} |
| 399 |
} |
| 400 |
Collections.sort(packages); |
| 401 |
StringBuilder result = new StringBuilder(); |
| 402 |
for (String pkg : packages) { |
| 403 |
if (result.length() != 0) { |
| 404 |
result.append(',').append(' '); |
| 405 |
} |
| 406 |
result.append(pkg); |
| 407 |
} |
| 408 |
return result.toString(); |
| 409 |
} catch (Exception e) { |
| 410 |
|
| 411 |
return null; |
| 412 |
} |
| 413 |
} |
| 414 |
|
| 415 |
/** |
| 416 |
* Creates default profile with minumum version as stated by |
| 417 |
* {@link #DEFAULT_PROFILE} and adds packages observed on currently used JVM. |
| 418 |
* |
| 419 |
* @return generated profile content |
| 420 |
*/ |
| 421 |
@SuppressWarnings("nls") |
| 422 |
private StringBuilder createDefaultProfileFromRunningJvm() { |
| 423 |
StringBuilder buffer = new StringBuilder(); |
| 424 |
final String NEWLINE = System.lineSeparator(); |
| 425 |
// add systempackages |
| 426 |
buffer.append("org.osgi.framework.system.packages = \\"); |
| 427 |
buffer.append(NEWLINE); |
| 428 |
buffer.append(' '); |
| 429 |
String calculateVMPackages = calculateVMPackages(); |
| 430 |
if (calculateVMPackages != null) { |
| 431 |
String[] pack; |
| 432 |
pack = calculateVMPackages.split(","); |
| 433 |
for (int i = 0; i < pack.length; i++) { |
| 434 |
buffer.append(pack[i]); |
| 435 |
if (i != pack.length - 1) { |
| 436 |
buffer.append(','); |
| 437 |
buffer.append("\\"); |
| 438 |
} |
| 439 |
buffer.append(NEWLINE); |
| 440 |
} |
| 441 |
} |
| 442 |
// add EE |
| 443 |
buffer.append("org.osgi.framework.executionenvironment = \\\n" + " OSGi/Minimum-1.0,\\\n" |
| 444 |
+ " OSGi/Minimum-1.1,\\\n" + " OSGi/Minimum-1.2,\\\n" + " JavaSE/compact1-1.8,\\\n" |
| 445 |
+ " JavaSE/compact2-1.8,\\\n" + " JavaSE/compact3-1.8,\\\n" + " JRE-1.1,\\\n" + " J2SE-1.2,\\\n" |
| 446 |
+ " J2SE-1.3,\\\n" + " J2SE-1.4,\\\n" + " J2SE-1.5,\\\n" + " JavaSE-1.6,\\\n" + " JavaSE-1.7,\\\n" |
| 447 |
+ " JavaSE-1.8,\\\n"); |
| 448 |
String version = DEFAULT_PROFILE.substring(DEFAULT_PROFILE.indexOf('-') + 1); |
| 449 |
int ver = Integer.parseInt(version); |
| 450 |
for (int i = 9; i < ver; i++) { |
| 451 |
buffer.append(" JavaSE-" + String.valueOf(i) + ",\\\n"); |
| 452 |
} |
| 453 |
buffer.append(" JavaSE-" + String.valueOf(ver)); |
| 454 |
buffer.append(NEWLINE); |
| 455 |
|
| 456 |
// add capabilities |
| 457 |
StringBuilder versionList = new StringBuilder(); |
| 458 |
versionList.append("1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 1.7, 1.8"); |
| 459 |
for (int i = 9; i <= ver; i++) { |
| 460 |
versionList.append(", " + String.valueOf(i) + ".0"); |
| 461 |
} |
| 462 |
buffer.append("org.osgi.framework.system.capabilities = \\\n" |
| 463 |
+ " osgi.ee; osgi.ee=\"OSGi/Minimum\"; version:List<Version>=\"1.0, 1.1, 1.2\",\\\n" |
| 464 |
+ " osgi.ee; osgi.ee=\"JRE\"; version:List<Version>=\"1.0, 1.1\",\\\n" |
| 465 |
+ " osgi.ee; osgi.ee=\"JavaSE\"; version:List<Version>=\"" + versionList.toString() + "\",\\\n" |
| 466 |
+ " osgi.ee; osgi.ee=\"JavaSE/compact1\"; version:List<Version>=\"1.8," + String.valueOf(ver) |
| 467 |
+ ".0\",\\\n" + " osgi.ee; osgi.ee=\"JavaSE/compact2\"; version:List<Version>=\"1.8," |
| 468 |
+ String.valueOf(ver) + ".0\",\\\n" |
| 469 |
+ " osgi.ee; osgi.ee=\"JavaSE/compact3\"; version:List<Version>=\"1.8," + String.valueOf(ver) + ".0\""); |
| 470 |
buffer.append(NEWLINE); |
| 471 |
|
| 472 |
// add profile name and compiler options |
| 473 |
buffer.append("osgi.java.profile.name = " + DEFAULT_PROFILE + "\n" + "org.eclipse.jdt.core.compiler.compliance=" |
| 474 |
+ String.valueOf(ver) + "\n" + "org.eclipse.jdt.core.compiler.source=" + String.valueOf(ver) + "\n" |
| 475 |
+ "org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled\n" |
| 476 |
+ "org.eclipse.jdt.core.compiler.codegen.targetPlatform=" + String.valueOf(ver) + "\n" |
| 477 |
+ "org.eclipse.jdt.core.compiler.problem.assertIdentifier=error\n" |
| 478 |
+ "org.eclipse.jdt.core.compiler.problem.enumIdentifier=error"); |
| 479 |
buffer.append(NEWLINE); |
| 480 |
return buffer; |
| 481 |
} |
| 482 |
|
| 483 |
private static URL getResouceFromSystemBundle(String entry) { |
| 484 |
// get resource from system bundle which is typically on the Java boot classpath |
| 485 |
ClassLoader loader = FrameworkUtil.class.getClassLoader(); |
| 486 |
return loader == null ? ClassLoader.getSystemResource(entry) : loader.getResource(entry); |
| 487 |
} |
| 488 |
|
| 489 |
private Map<String, String> loadProfile(File profileFile) { |
| 490 |
if (profileFile == null || !profileFile.exists()) |
| 491 |
return null; |
| 492 |
|
| 493 |
try { |
| 494 |
InputStream stream = new BufferedInputStream(new FileInputStream(profileFile)); |
| 495 |
Map<String, String> properties = loadProfile(stream); |
| 496 |
if (properties != null) |
| 497 |
properties.put(PROFILE_LOCATION, profileFile.getAbsolutePath()); |
| 498 |
return properties; |
| 499 |
} catch (FileNotFoundException e) { |
| 500 |
//null |
| 501 |
} |
| 502 |
return null; |
| 503 |
} |
| 504 |
|
| 505 |
private Map<String, String> loadProfile(URL profileURL) { |
| 506 |
if (profileURL == null) |
| 507 |
return null; |
| 508 |
|
| 509 |
try { |
| 510 |
InputStream stream = profileURL.openStream(); |
| 511 |
return loadProfile(stream); |
| 512 |
} catch (IOException e) { |
| 513 |
//null |
| 514 |
} |
| 515 |
return null; |
| 516 |
} |
| 517 |
|
| 518 |
/** |
| 519 |
* Always closes the stream when done |
| 520 |
*/ |
| 521 |
private Map<String, String> loadProfile(InputStream stream) { |
| 522 |
if (stream != null) { |
| 523 |
try { |
| 524 |
return CollectionUtils.loadProperties(stream); |
| 525 |
} catch (IOException e) { |
| 526 |
return null; |
| 527 |
} finally { |
| 528 |
try { |
| 529 |
stream.close(); |
| 530 |
} catch (IOException e) { |
| 531 |
// error |
| 532 |
} |
| 533 |
} |
| 534 |
} |
| 535 |
return null; |
| 536 |
} |
| 537 |
} |