/tycho-core/src/main/java/org/eclipse/tycho/core/ee/ExecutionEnvironmentUtils.java

https://github.com/oberlies/tycho · Java · 129 lines · 88 code · 10 blank · 31 comment · 23 complexity · 2a0b3ddecd7e1860b79cb12209e8b5ad MD5 · raw file

  1. /*******************************************************************************
  2. * Copyright (c) 2008, 2012 Sonatype Inc. and others.
  3. * All rights reserved. This program and the accompanying materials
  4. * are made available under the terms of the Eclipse Public License v1.0
  5. * which accompanies this distribution, and is available at
  6. * http://www.eclipse.org/legal/epl-v10.html
  7. *
  8. * Contributors:
  9. * Sonatype Inc. - initial API and implementation
  10. *******************************************************************************/
  11. package org.eclipse.tycho.core.ee;
  12. import java.io.IOException;
  13. import java.io.InputStream;
  14. import java.net.URL;
  15. import java.util.ArrayList;
  16. import java.util.LinkedHashMap;
  17. import java.util.List;
  18. import java.util.Map;
  19. import java.util.Properties;
  20. import org.eclipse.osgi.internal.framework.EquinoxConfiguration;
  21. import org.eclipse.tycho.core.ee.shared.ExecutionEnvironment;
  22. import org.osgi.framework.BundleActivator;
  23. import org.osgi.framework.Constants;
  24. /**
  25. * Creative copy&paste from org.eclipse.osgi.framework.internal.core.Framework
  26. *
  27. * @author eclipse.org
  28. * @author igor
  29. */
  30. public class ExecutionEnvironmentUtils {
  31. private static Map<String, StandardExecutionEnvironment> executionEnvironmentsMap = fillEnvironmentsMap();
  32. private static Map<String, StandardExecutionEnvironment> fillEnvironmentsMap() {
  33. Properties listProps = readProperties(findInSystemBundle("profile.list"));
  34. String[] profileFiles = listProps.getProperty("java.profiles").split(",");
  35. Map<String, StandardExecutionEnvironment> envMap = new LinkedHashMap<String, StandardExecutionEnvironment>();
  36. for (String profileFile : profileFiles) {
  37. Properties props = readProperties(findInSystemBundle(profileFile.trim()));
  38. envMap.put(props.getProperty("osgi.java.profile.name").trim(), new StandardExecutionEnvironment(props));
  39. }
  40. return envMap;
  41. }
  42. private static Properties readProperties(final URL url) {
  43. Properties listProps = new Properties();
  44. InputStream stream = null;
  45. try {
  46. stream = url.openStream();
  47. listProps.load(stream);
  48. } catch (IOException e) {
  49. throw new RuntimeException(e);
  50. } finally {
  51. try {
  52. if (stream != null) {
  53. stream.close();
  54. }
  55. } catch (IOException e) {
  56. throw new RuntimeException(e);
  57. }
  58. }
  59. return listProps;
  60. }
  61. /**
  62. * Get the execution environment for the specified OSGi profile name.
  63. *
  64. * @param profileName
  65. * profile name value as specified for key "Bundle-RequiredExecutionEnvironment" in
  66. * MANIFEST.MF
  67. * @return the corresponding {@link ExecutionEnvironment}.
  68. * @throws UnknownEnvironmentException
  69. * if profileName is unknown.
  70. */
  71. public static StandardExecutionEnvironment getExecutionEnvironment(String profileName)
  72. throws UnknownEnvironmentException {
  73. StandardExecutionEnvironment executionEnvironment = executionEnvironmentsMap.get(profileName);
  74. if (executionEnvironment == null) {
  75. throw new UnknownEnvironmentException(profileName);
  76. }
  77. return executionEnvironment;
  78. }
  79. public static List<String> getProfileNames() {
  80. return new ArrayList<String>(executionEnvironmentsMap.keySet());
  81. }
  82. public static void applyProfileProperties(Properties properties, Properties profileProps) {
  83. String systemExports = properties.getProperty(Constants.FRAMEWORK_SYSTEMPACKAGES);
  84. // set the system exports property using the vm profile; only if the property is not already set
  85. if (systemExports == null) {
  86. systemExports = profileProps.getProperty(Constants.FRAMEWORK_SYSTEMPACKAGES);
  87. if (systemExports != null)
  88. properties.put(Constants.FRAMEWORK_SYSTEMPACKAGES, systemExports);
  89. }
  90. // set the org.osgi.framework.bootdelegation property according to the java profile
  91. String type = properties.getProperty(EquinoxConfiguration.PROP_OSGI_JAVA_PROFILE_BOOTDELEGATION); // a null value means ignore
  92. String profileBootDelegation = profileProps.getProperty(Constants.FRAMEWORK_BOOTDELEGATION);
  93. if (EquinoxConfiguration.PROP_OSGI_BOOTDELEGATION_OVERRIDE.equals(type)) {
  94. if (profileBootDelegation == null)
  95. properties.remove(Constants.FRAMEWORK_BOOTDELEGATION); // override with a null value
  96. else
  97. properties.put(Constants.FRAMEWORK_BOOTDELEGATION, profileBootDelegation); // override with the profile value
  98. } else if (EquinoxConfiguration.PROP_OSGI_BOOTDELEGATION_NONE.equals(type))
  99. properties.remove(Constants.FRAMEWORK_BOOTDELEGATION); // remove the bootdelegation property in case it was set
  100. // set the org.osgi.framework.executionenvironment property according to the java profile
  101. if (properties.getProperty(Constants.FRAMEWORK_EXECUTIONENVIRONMENT) == null) {
  102. // get the ee from the java profile; if no ee is defined then try the java profile name
  103. String ee = profileProps.getProperty(Constants.FRAMEWORK_EXECUTIONENVIRONMENT,
  104. profileProps.getProperty(EquinoxConfiguration.PROP_OSGI_JAVA_PROFILE_NAME));
  105. if (ee != null)
  106. properties.put(Constants.FRAMEWORK_EXECUTIONENVIRONMENT, ee);
  107. }
  108. if (properties.getProperty(Constants.FRAMEWORK_SYSTEMCAPABILITIES) == null) {
  109. String systemCapabilities = profileProps.getProperty(Constants.FRAMEWORK_SYSTEMCAPABILITIES);
  110. if (systemCapabilities != null)
  111. properties.put(Constants.FRAMEWORK_SYSTEMCAPABILITIES, systemCapabilities);
  112. }
  113. }
  114. private static URL findInSystemBundle(String entry) {
  115. // Check the ClassLoader in case we're launched off the Java boot classpath
  116. ClassLoader loader = BundleActivator.class.getClassLoader();
  117. return loader == null ? ClassLoader.getSystemResource(entry) : loader.getResource(entry);
  118. }
  119. }