Merge pull request #23911 from dmatej/felix-jdk17-fix

Felix jdk17 fix - able to start with the Security Manager enabled
diff --git a/appserver/featuresets/debug/pom.xml b/appserver/featuresets/debug/pom.xml
index a09addf..fe69dc7 100644
--- a/appserver/featuresets/debug/pom.xml
+++ b/appserver/featuresets/debug/pom.xml
@@ -40,16 +40,16 @@
 
     In the IDE, for instance, Eclipse, go to run - debug configurations - remote java application - project
     And add this project there.
-  </description>
+    </description>
 
     <name>Debug</name>
 
     <dependencies>
-    	<dependency>
+        <dependency>
             <groupId>jakarta.el</groupId>
             <artifactId>jakarta.el-api</artifactId>
         </dependency>
-    
+
         <dependency>
             <groupId>org.osgi</groupId>
             <artifactId>osgi.core</artifactId>
@@ -65,9 +65,9 @@
         </dependency>
         <dependency>
             <groupId>org.apache.felix</groupId>
-            <artifactId>org.apache.felix.framework</artifactId>
+            <artifactId>org.apache.felix.main</artifactId>
         </dependency>
-        
+
         <dependency>
             <groupId>org.glassfish.grizzly</groupId>
             <artifactId>grizzly-framework</artifactId>
diff --git a/nucleus/core/bootstrap/src/main/java/com/sun/enterprise/glassfish/bootstrap/GlassFishMain.java b/nucleus/core/bootstrap/src/main/java/com/sun/enterprise/glassfish/bootstrap/GlassFishMain.java
index ff97d81..ab6f6ac 100644
--- a/nucleus/core/bootstrap/src/main/java/com/sun/enterprise/glassfish/bootstrap/GlassFishMain.java
+++ b/nucleus/core/bootstrap/src/main/java/com/sun/enterprise/glassfish/bootstrap/GlassFishMain.java
@@ -101,9 +101,11 @@
                 try {
                     System.out.println("command = " + command);
                     if ("start".equalsIgnoreCase(command)) {
-                        if (gf.getStatus() != GlassFish.Status.STARTED || gf.getStatus() == GlassFish.Status.STOPPING || gf.getStatus() == GlassFish.Status.STARTING)
+                        if (gf.getStatus() != GlassFish.Status.STARTED || gf.getStatus() == GlassFish.Status.STOPPING || gf.getStatus() == GlassFish.Status.STARTING) {
                             gf.start();
-                        else System.out.println("Already started or stopping or starting");
+                        } else {
+                            System.out.println("Already started or stopping or starting");
+                        }
                     } else if ("stop".equalsIgnoreCase(command)) {
                         if (gf.getStatus() != GlassFish.Status.STARTED) {
                             System.out.println("GlassFish is not started yet. Please execute start first.");
@@ -174,9 +176,12 @@
 
         private void addShutdownHook() {
             Runtime.getRuntime().addShutdownHook(new Thread("GlassFish Shutdown Hook") {
+                @Override
                 public void run() {
                     try {
-                        gfr.shutdown();
+                        if (gfr != null) {
+                            gfr.shutdown();
+                        }
                     }
                     catch (Exception ex) {
                         System.err.println("Error stopping framework: " + ex);
diff --git a/nucleus/core/bootstrap/src/main/java/com/sun/enterprise/glassfish/bootstrap/osgi/OSGiGlassFishRuntimeBuilder.java b/nucleus/core/bootstrap/src/main/java/com/sun/enterprise/glassfish/bootstrap/osgi/OSGiGlassFishRuntimeBuilder.java
index c829ef2..32e940c 100644
--- a/nucleus/core/bootstrap/src/main/java/com/sun/enterprise/glassfish/bootstrap/osgi/OSGiGlassFishRuntimeBuilder.java
+++ b/nucleus/core/bootstrap/src/main/java/com/sun/enterprise/glassfish/bootstrap/osgi/OSGiGlassFishRuntimeBuilder.java
@@ -22,8 +22,7 @@
 import org.glassfish.embeddable.GlassFishException;
 import org.glassfish.embeddable.GlassFishRuntime;
 import org.glassfish.embeddable.spi.RuntimeBuilder;
-import org.osgi.framework.Bundle;
-import org.osgi.framework.BundleException;
+import org.osgi.framework.BundleContext;
 import org.osgi.framework.ServiceReference;
 import org.osgi.framework.launch.Framework;
 
@@ -79,7 +78,7 @@
 
     private Framework framework;
 
-    private Logger logger = LogFacade.BOOTSTRAP_LOGGER;
+    private final Logger logger = LogFacade.BOOTSTRAP_LOGGER;
 
     private Properties oldProvisioningOptions;
     private Properties newProvisioningOptions;
@@ -92,6 +91,7 @@
      */
     public OSGiGlassFishRuntimeBuilder() {}
 
+    @Override
     public GlassFishRuntime build(BootstrapProperties bsProps) throws GlassFishException {
         try {
             MainHelper.buildStartupContext(bsProps.getProperties());
@@ -147,6 +147,7 @@
         }
     }
 
+    @Override
     public boolean handles(BootstrapProperties bsProps) {
         // See GLASSFISH-16743 for the reason behind additional check
         final String builderName = bsProps.getProperty(Constants.BUILDER_NAME_PROPERTY);
@@ -214,7 +215,8 @@
     }
 
     private boolean newFramework() {
-        return framework.getBundleContext().getBundles().length == 1;
+        BundleContext context = framework.getBundleContext();
+        return context == null || context.getBundles().length == 1;
     }
 
     /**
@@ -238,16 +240,11 @@
     }
 
     private boolean hasBeenReconfigured(Properties properties) {
-        try {
-            logger.logp(Level.FINE, "OSGiGlassFishRuntimeBuilder", "hasBeenReconfigured", "oldProvisioningOptions = {0}",
-                    new Object[]{getOldProvisioningOptions()});
-            logger.logp(Level.FINE, "OSGiGlassFishRuntimeBuilder", "hasBeenReconfigured", "newProvisioningOptions = {0}",
-                    new Object[]{getNewProvisioningOptions(properties)});
-            return !getNewProvisioningOptions(properties).equals(getOldProvisioningOptions());
-        } catch (IOException e) {
-            e.printStackTrace();
-            return true;
-        }
+        logger.logp(Level.FINE, "OSGiGlassFishRuntimeBuilder", "hasBeenReconfigured", "oldProvisioningOptions = {0}",
+                new Object[]{getOldProvisioningOptions()});
+        logger.logp(Level.FINE, "OSGiGlassFishRuntimeBuilder", "hasBeenReconfigured", "newProvisioningOptions = {0}",
+                new Object[]{getNewProvisioningOptions(properties)});
+        return !getNewProvisioningOptions(properties).equals(getOldProvisioningOptions());
     }
 
     /**
@@ -294,45 +291,39 @@
     }
 
     private void storeProvisioningOptions(Properties properties) {
-        FileOutputStream os = null;
-        try {
-            File f = framework.getBundleContext().getDataFile(PROVISIONING_OPTIONS_FILENAME);
-            // GLASSFISH-19623: f can be null
-            if (f == null) {
-                logger.log(Level.WARNING, LogFacade.CANT_STORE_PROVISIONING_OPTIONS);
-                return;
-            }
-            os = new FileOutputStream(f);
+        BundleContext context = framework.getBundleContext();
+        if (context == null) {
+            throw new IllegalStateException("No bundle context available!");
+        }
+        File f = context.getDataFile(PROVISIONING_OPTIONS_FILENAME);
+        // GLASSFISH-19623: f can be null
+        if (f == null) {
+            logger.log(Level.WARNING, LogFacade.CANT_STORE_PROVISIONING_OPTIONS);
+            return;
+        }
+        try (FileOutputStream os = new FileOutputStream(f)) {
             getNewProvisioningOptions(properties).store(os, "");
             os.flush();
-            logger.logp(Level.FINE, "OSGiGlassFishRuntimeBuilder", "storeProvisioningOptions", "Stored provisioning options in {0}",
-                    new Object[]{f.getAbsolutePath()});
+            logger.logp(Level.CONFIG, "OSGiGlassFishRuntimeBuilder", "storeProvisioningOptions",
+                "Stored provisioning options in {0}", new Object[] {f.getAbsolutePath()});
         } catch (IOException e) {
-            e.printStackTrace();
-        } finally {
-            if (os != null) {
-                try {
-                    os.close();
-                } catch (IOException e) {
-                    // ignored
-                }
-            }
+            logger.log(Level.WARNING, "Storing provisioning options failed.", e);
         }
     }
 
-    private Properties getOldProvisioningOptions() throws IOException {
+    private Properties getOldProvisioningOptions() {
         if (oldProvisioningOptions == null) {
             Properties options = new Properties();
             try {
                 File f = framework.getBundleContext().getDataFile(PROVISIONING_OPTIONS_FILENAME);
-                if (f != null && f.exists()) { // GLASSFISH-19623: f can be null
+                if (f != null && f.exists()) {
                     options.load(new FileInputStream(f));
                     logger.logp(Level.FINE, "OSGiGlassFishRuntimeBuilder", "getOldProvisioningOptions",
                             "Read provisioning options from {0}", new Object[]{f.getAbsolutePath()});
                     oldProvisioningOptions = options;
                 }
             } catch (Exception e) {
-                e.printStackTrace();
+                logger.log(Level.WARNING, "Loading provisioning options failed.", e);
             }
         }
         return oldProvisioningOptions;
diff --git a/nucleus/parent/pom.xml b/nucleus/parent/pom.xml
index 819fbd5..c5c8eb1 100644
--- a/nucleus/parent/pom.xml
+++ b/nucleus/parent/pom.xml
@@ -462,10 +462,17 @@
                 <scope>provided</scope>
             </dependency>
 
+            <!-- framework is transitive dependency of others, but it is aggregated in main too -->
             <dependency>
                 <groupId>org.apache.felix</groupId>
                 <artifactId>org.apache.felix.main</artifactId>
-                <version>7.0.3</version>
+                <version>7.1.0-SNAPSHOT</version>
+            </dependency>
+            <dependency>
+                <groupId>org.apache.felix</groupId>
+                <artifactId>org.apache.felix.framework</artifactId>
+                <version>7.1.0-SNAPSHOT</version>
+                <scope>provided</scope>
             </dependency>
             <dependency>
                 <groupId>org.apache.felix</groupId>
@@ -524,12 +531,6 @@
                     </exclusion>
                 </exclusions>
             </dependency>
-            <dependency>
-                <groupId>org.apache.felix</groupId>
-                <artifactId>org.apache.felix.framework</artifactId>
-                <version>7.0.3</version>
-                <scope>provided</scope>
-            </dependency>
             <!-- This is currently used by osgi-shell cmd -->
             <dependency>
                 <groupId>org.jline</groupId>
diff --git a/snapshots/felix-framework/pom.xml b/snapshots/felix-framework/pom.xml
new file mode 100644
index 0000000..ef3c818
--- /dev/null
+++ b/snapshots/felix-framework/pom.xml
@@ -0,0 +1,49 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+
+    Copyright (c) 2022 Contributors to the Eclipse Foundation. All rights reserved.
+
+    This program and the accompanying materials are made available under the
+    terms of the Eclipse Public License v. 2.0, which is available at
+    http://www.eclipse.org/legal/epl-2.0.
+
+    This Source Code may also be made available under the following Secondary
+    Licenses when the conditions for such availability set forth in the
+    Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
+    version 2 with the GNU Classpath Exception, which is available at
+    https://www.gnu.org/software/classpath/license.html.
+
+    SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
+
+-->
+<project xmlns="http://maven.apache.org/POM/4.0.0"
+    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"
+>
+    <modelVersion>4.0.0</modelVersion>
+    <parent>
+        <groupId>org.glassfish.main.snapshots</groupId>
+        <artifactId>snapshotsmodule</artifactId>
+        <version>0.0.0-SNAPSHOT</version>
+    </parent>
+
+    <artifactId>felix-framework</artifactId>
+    <packaging>pom</packaging>
+
+    <properties>
+        <snapshots.sources.url>https://github.com/dmatej/felix-dev/archive/refs/heads/FELIX-6522-JDK17.zip</snapshots.sources.url>
+        <snapshots.sources.directory>${project.build.directory}/felix-dev-FELIX-6522-JDK17/framework</snapshots.sources.directory>
+    </properties>
+
+    <build>
+        <plugins>
+            <plugin>
+                <groupId>com.googlecode.maven-download-plugin</groupId>
+                <artifactId>download-maven-plugin</artifactId>
+            </plugin>
+            <plugin>
+                <artifactId>maven-invoker-plugin</artifactId>
+            </plugin>
+        </plugins>
+    </build>
+</project>
diff --git a/snapshots/felix-main/pom.xml b/snapshots/felix-main/pom.xml
new file mode 100644
index 0000000..c0dc007
--- /dev/null
+++ b/snapshots/felix-main/pom.xml
@@ -0,0 +1,58 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+
+    Copyright (c) 2022 Contributors to the Eclipse Foundation. All rights reserved.
+
+    This program and the accompanying materials are made available under the
+    terms of the Eclipse Public License v. 2.0, which is available at
+    http://www.eclipse.org/legal/epl-2.0.
+
+    This Source Code may also be made available under the following Secondary
+    Licenses when the conditions for such availability set forth in the
+    Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
+    version 2 with the GNU Classpath Exception, which is available at
+    https://www.gnu.org/software/classpath/license.html.
+
+    SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
+
+-->
+<project xmlns="http://maven.apache.org/POM/4.0.0"
+    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"
+>
+    <modelVersion>4.0.0</modelVersion>
+    <parent>
+        <groupId>org.glassfish.main.snapshots</groupId>
+        <artifactId>snapshotsmodule</artifactId>
+        <version>0.0.0-SNAPSHOT</version>
+    </parent>
+
+    <artifactId>felix-main</artifactId>
+    <packaging>pom</packaging>
+
+    <properties>
+        <snapshots.sources.url>https://github.com/dmatej/felix-dev/archive/refs/heads/FELIX-6522-JDK17.zip</snapshots.sources.url>
+        <snapshots.sources.directory>${project.build.directory}/felix-dev-FELIX-6522-JDK17/main</snapshots.sources.directory>
+    </properties>
+
+    <dependencies>
+        <dependency>
+            <groupId>org.glassfish.main.snapshots</groupId>
+            <artifactId>felix-framework</artifactId>
+            <type>pom</type>
+            <version>0.0.0-SNAPSHOT</version>
+        </dependency>
+    </dependencies>
+
+    <build>
+        <plugins>
+            <plugin>
+                <groupId>com.googlecode.maven-download-plugin</groupId>
+                <artifactId>download-maven-plugin</artifactId>
+            </plugin>
+            <plugin>
+                <artifactId>maven-invoker-plugin</artifactId>
+            </plugin>
+        </plugins>
+    </build>
+</project>
diff --git a/snapshots/pom.xml b/snapshots/pom.xml
index 3fff435..4746e20 100644
--- a/snapshots/pom.xml
+++ b/snapshots/pom.xml
@@ -39,6 +39,11 @@
     <name>GlassFish Snapshots Parent</name>
     <description>Temporary snapshot modules</description>
 
+    <modules>
+        <module>felix-framework</module>
+        <module>felix-main</module>
+    </modules>
+
     <build>
         <plugins>
             <plugin>
@@ -89,6 +94,8 @@
                                 </pomIncludes>
                                 <!-- source:jar - ie. metro uses sources of other modules -->
                                 <goals>clean source:jar install</goals>
+                                <!-- Felix uses ianal plugin made in 2008 which requires this -->
+                                <mavenOpts>--add-opens java.base/java.lang=ALL-UNNAMED --add-opens java.base/java.io=ALL-UNNAMED</mavenOpts>
                                 <settingsFile>${basedir}/../settings.xml</settingsFile>
                                 <profiles>staging,default</profiles>
                                 <parallelThreads>4</parallelThreads>