This is an automated email from the ASF dual-hosted git repository.

cziegeler pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/felix-dev.git


The following commit(s) were added to refs/heads/master by this push:
     new ba9c598607 FELIX-6624 : Clean up code
ba9c598607 is described below

commit ba9c598607720ffcce11463b0dcea05ed68cd007
Author: Carsten Ziegeler <[email protected]>
AuthorDate: Mon Aug 14 14:45:11 2023 +0200

    FELIX-6624 : Clean up code
---
 .../internal/core/BaseUpdateInstallHelper.java     | 78 ++++++++--------------
 .../webconsole/internal/core/InstallHelper.java    | 21 +++---
 2 files changed, 34 insertions(+), 65 deletions(-)

diff --git 
a/webconsole/src/main/java/org/apache/felix/webconsole/internal/core/BaseUpdateInstallHelper.java
 
b/webconsole/src/main/java/org/apache/felix/webconsole/internal/core/BaseUpdateInstallHelper.java
index 718034896c..1e442e7c61 100644
--- 
a/webconsole/src/main/java/org/apache/felix/webconsole/internal/core/BaseUpdateInstallHelper.java
+++ 
b/webconsole/src/main/java/org/apache/felix/webconsole/internal/core/BaseUpdateInstallHelper.java
@@ -35,9 +35,10 @@ import org.osgi.framework.BundleException;
 import org.osgi.framework.Constants;
 import org.osgi.framework.FrameworkEvent;
 import org.osgi.framework.FrameworkListener;
+import org.osgi.framework.startlevel.BundleStartLevel;
+import org.osgi.framework.startlevel.FrameworkStartLevel;
 import org.osgi.framework.wiring.FrameworkWiring;
 import org.osgi.service.log.LogService;
-import org.osgi.service.startlevel.StartLevel;
 
 
 abstract class BaseUpdateInstallHelper implements Runnable
@@ -115,16 +116,16 @@ abstract class BaseUpdateInstallHelper implements Runnable
     public final void run()
     {
         // now deploy the resolved bundles
-        try
-        {
+        try {
             // we need the framework wiring before we call the bundle
             // installation or update, since we might be updating
             // our selves in which case the bundle context will be
             // invalid by the time we want to call the update
-            final FrameworkWiring fw = refreshPackages ? 
plugin.getBundle().getBundleContext().getBundle(Constants.SYSTEM_BUNDLE_LOCATION).adapt(FrameworkWiring.class)
 : null;
+            final Bundle systemBundle = 
plugin.getBundle().getBundleContext().getBundle(Constants.SYSTEM_BUNDLE_LOCATION);
+            final FrameworkWiring fw = refreshPackages ? 
systemBundle.adapt(FrameworkWiring.class) : null;
 
             // same for the startlevel
-            StartLevel startLevel = null;
+            final FrameworkStartLevel startLevel = 
systemBundle.adapt(FrameworkStartLevel.class);
 
             Bundle bundle = getTargetBundle();
 
@@ -133,26 +134,23 @@ abstract class BaseUpdateInstallHelper implements Runnable
 
             // If the bundle has been started we want to stop it first, then 
update it, refresh it, and restart it
             // because otherwise, it will be stopped and started twice (once 
by the update and once by the refresh)
-            if ((state & (Bundle.ACTIVE | Bundle.STARTING)) != 0)
-            {
+            if ((state & (Bundle.ACTIVE | Bundle.STARTING)) != 0) {
                 // we need the StartLevel service  before we stop the bundle
                 // before the update, since we might be stopping
                 // our selves in which case the bundle context will be
                 // invalid by the time we want to call the startlevel
-                startLevel = (StartLevel) 
getService(StartLevel.class.getName());
 
                 // We want to start the bundle afterwards without affecting 
the persistent state of the bundle
                 // However, we can only use the transient options if the 
framework startlevel is not less than the
                 // bundle startlevel (in case that there is no starlevel 
service we assume we are good).
-                if (startLevel == null || startLevel.getStartLevel() >= 
startLevel.getBundleStartLevel(bundle))
-                {
+                final BundleStartLevel bsl = 
bundle.adapt(BundleStartLevel.class);
+                if (startLevel == null || bsl == null || 
startLevel.getStartLevel() >= bsl.getStartLevel()) {
                     startFlags |= Bundle.START_TRANSIENT;
                 }
 
                 // If the bundle is in the starting state it might be lazy and 
not started yet - hence, start it
                 // according to its policy.
-                if (state == Bundle.STARTING)
-                {
+                if (state == Bundle.STARTING) {
                     startFlags |= Bundle.START_ACTIVATION_POLICY;
                 }
 
@@ -163,46 +161,30 @@ abstract class BaseUpdateInstallHelper implements Runnable
 
             // We want to catch an exception during update to be able to 
restart the bundle if we stopped it previously
             Exception rethrow = null;
-            try
-            {
+            try {
                 // perform the action!
                 bundle = doRun();
 
 
-                if ( bundle != null )
-                {
+                if ( bundle != null ) {
                     // refresh packages and give it at most 5 seconds to finish
                     refreshPackages(fw, plugin.getBundle().getBundleContext(), 
5000L, bundle );
                 }
-            }
-            catch (Exception ex)
-            {
+            } catch (Exception ex) {
                 rethrow = ex;
                 throw ex;
-            }
-            finally
-            {
+            } finally {
                 // If we stopped the bundle lets try to restart it (we created 
the correct flags above already).
-                if ((state & (Bundle.ACTIVE | Bundle.STARTING)) != 0)
-                {
-                    try
-                    {
+                if ((state & (Bundle.ACTIVE | Bundle.STARTING)) != 0) {
+                    try {
                         bundle.start(startFlags);
-                    }
-                    catch (Exception ex)
-                    {
-                        if (rethrow == null)
-                        {
+                    } catch (Exception ex) {
+                        if (rethrow == null) {
                             throw ex;
-                        }
-                        else
-                        {
-                            try
-                            {
+                        } else {
+                            try{
                                 getLog().log( LogService.LOG_ERROR, "Cannot 
restart bundle: " + bundle + " after exception during update!", ex);
-                            }
-                            catch ( Exception secondary )
-                            {
+                            } catch ( Exception secondary ) {
                                 // at the time this exception happens the log 
used might have
                                 // been destroyed and is not available to use 
any longer. So
                                 // we only can write to stderr at this time to 
at least get
@@ -214,15 +196,10 @@ abstract class BaseUpdateInstallHelper implements Runnable
                     }
                 }
             }
-        }
-        catch ( Exception e )
-        {
-            try
-            {
+        } catch ( Exception e ) {
+            try {
                 getLog().log( LogService.LOG_ERROR, "Cannot install or update 
bundle from " + bundleFile, e );
-            }
-            catch ( Exception secondary )
-            {
+            } catch ( Exception secondary ) {
                 // at the time this exception happens the log used might have
                 // been destroyed and is not available to use any longer. So
                 // we only can write to stderr at this time to at least get
@@ -230,11 +207,8 @@ abstract class BaseUpdateInstallHelper implements Runnable
                 System.err.println( "Cannot install or update bundle from " + 
bundleFile );
                 e.printStackTrace( System.err );
             }
-        }
-        finally
-        {
-            if ( bundleFile != null )
-            {
+        } finally {
+            if ( bundleFile != null ) {
                 bundleFile.delete();
             }
 
diff --git 
a/webconsole/src/main/java/org/apache/felix/webconsole/internal/core/InstallHelper.java
 
b/webconsole/src/main/java/org/apache/felix/webconsole/internal/core/InstallHelper.java
index de057b5ef0..e184d8be54 100644
--- 
a/webconsole/src/main/java/org/apache/felix/webconsole/internal/core/InstallHelper.java
+++ 
b/webconsole/src/main/java/org/apache/felix/webconsole/internal/core/InstallHelper.java
@@ -27,11 +27,10 @@ import org.osgi.framework.Bundle;
 import org.osgi.framework.BundleContext;
 import org.osgi.framework.BundleException;
 import org.osgi.framework.Constants;
-import org.osgi.service.startlevel.StartLevel;
+import org.osgi.framework.startlevel.BundleStartLevel;
 
 
-class InstallHelper extends BaseUpdateInstallHelper
-{
+class InstallHelper extends BaseUpdateInstallHelper {
     private final BundleContext bundleContext;
     private final String location;
     private final int startlevel;
@@ -51,22 +50,18 @@ class InstallHelper extends BaseUpdateInstallHelper
 
 
     @Override
-    protected Bundle doRun( InputStream bundleStream ) throws BundleException
-    {
+    protected Bundle doRun( final InputStream bundleStream ) throws 
BundleException {
         Bundle bundle = bundleContext.installBundle( location, bundleStream );
 
-        if ( startlevel > 0 )
-        {
-            StartLevel sl = ( StartLevel ) getService( 
StartLevel.class.getName() );
-            if ( sl != null )
-            {
-                sl.setBundleStartLevel( bundle, startlevel );
+        if ( startlevel > 0 ) {
+            final BundleStartLevel bsl = bundle.adapt(BundleStartLevel.class);
+            if (bsl != null) {
+                bsl.setStartLevel(startlevel);
             }
         }
 
         // don't start fragments
-        if ( doStart && bundle.getHeaders().get( Constants.FRAGMENT_HOST ) == 
null )
-        {
+        if ( doStart && bundle.getHeaders().get( Constants.FRAGMENT_HOST ) == 
null ) {
             bundle.start();
         }
 

Reply via email to