FELIX-745 Mark optional imports as such
FELIX-746 Display Bundle-DocURL as clickable links
FELIX-747 Implement OBR installation/update again


git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@700068 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/webconsole/src/main/java/org/apache/felix/webconsole/internal/core/BundlesServlet.java b/webconsole/src/main/java/org/apache/felix/webconsole/internal/core/BundlesServlet.java
index f08b8f7..fcd415b 100644
--- a/webconsole/src/main/java/org/apache/felix/webconsole/internal/core/BundlesServlet.java
+++ b/webconsole/src/main/java/org/apache/felix/webconsole/internal/core/BundlesServlet.java
@@ -26,8 +26,10 @@
 import javax.servlet.http.HttpServletResponse;
 
 import org.apache.felix.bundlerepository.*;
+import org.apache.felix.shell.impl.UpdateCommandImpl;
 import org.apache.felix.webconsole.internal.BaseWebConsolePlugin;
 import org.apache.felix.webconsole.internal.Util;
+import org.apache.felix.webconsole.internal.obr.DeployerThread;
 import org.apache.felix.webconsole.internal.servlet.OsgiManager;
 import org.json.JSONException;
 import org.json.JSONWriter;
@@ -193,6 +195,7 @@
             else if ( "update".equals( action ) )
             {
                 // update bundle
+                update( bundle );
                 success = true;
             }
             else if ( "uninstall".equals( action ) )
@@ -443,15 +446,15 @@
     {
         //        enabled = bundle.getState() != Bundle.UNINSTALLED && this.hasUpdates( bundle );
 
-        // no updates if there is no installer service
-        Object isObject = getService( REPOSITORY_ADMIN_NAME );
-        if ( isObject == null )
+        // don't care for bundles with no symbolic name
+        if ( bundle.getSymbolicName() == null )
         {
             return false;
         }
 
-        // don't care for bundles with no symbolic name
-        if ( bundle.getSymbolicName() == null )
+        // no updates if there is no installer service
+        Object isObject = getService( REPOSITORY_ADMIN_NAME );
+        if ( isObject == null )
         {
             return false;
         }
@@ -511,12 +514,21 @@
         keyVal( jw, "Location", bundle.getLocation() );
         keyVal( jw, "Last Modification", new Date( bundle.getLastModified() ) );
 
+        String docUrl = ( String ) headers.get( Constants.BUNDLE_DOCURL );
+        if ( docUrl != null )
+        {
+            docUrl = "<a href=\"" + docUrl + "\" target=\"_blank\">" + docUrl + "</a>";
+            keyVal( jw, "Bundle Documentation", docUrl );
+        }
+
         keyVal( jw, "Vendor", headers.get( Constants.BUNDLE_VENDOR ) );
         keyVal( jw, "Copyright", headers.get( Constants.BUNDLE_COPYRIGHT ) );
         keyVal( jw, "Description", headers.get( Constants.BUNDLE_DESCRIPTION ) );
 
         keyVal( jw, "Start Level", getStartLevel( bundle ) );
-
+        
+        keyVal( jw, "Bundle Classpath", headers.get( Constants.BUNDLE_CLASSPATH ) );
+        
         if ( bundle.getState() == Bundle.INSTALLED )
         {
             listImportExportsUnresolved( jw, bundle );
@@ -631,7 +643,7 @@
                 for ( int i = 0; i < packages.length; i++ )
                 {
                     ExportedPackage ep = packages[i];
-                    printImport( val, ep.getName(), ep.getVersion(), ep );
+                    printImport( val, ep.getName(), ep.getVersion(), false, ep );
                 }
             }
             else
@@ -751,7 +763,7 @@
                             }
                         }
 
-                        printImport( val, r4Import.getName(), r4Import.getVersion(), ep );
+                        printImport( val, r4Import.getName(), r4Import.getVersion(), r4Import.isOptional(), ep );
                     }
                 }
                 else
@@ -851,10 +863,12 @@
     }
 
 
-    private void printImport( StringBuffer val, String name, Version version, ExportedPackage export )
+    private void printImport( StringBuffer val, String name, Version version, boolean optional, ExportedPackage export )
     {
         boolean bootDel = isBootDelegated( name );
-        if ( bootDel || export == null )
+        boolean isSpan = bootDel || export == null;
+
+        if ( isSpan )
         {
             val.append( "<span style=\"color: red\">!! " );
         }
@@ -869,19 +883,29 @@
 
             if ( bootDel )
             {
-                val.append( " -- Overwritten by Boot Delegation</span>" );
+                val.append( " -- Overwritten by Boot Delegation" );
             }
         }
         else
         {
             val.append( " -- Cannot be resolved" );
+            
+            if ( optional )
+            {
+                val.append( " but is not required" );
+            }
+
             if ( bootDel )
             {
                 val.append( " and overwritten by Boot Delegation" );
             }
-            val.append( "</span>" );
         }
 
+        if ( isSpan )
+        {
+            val.append( "</span>" );
+        }
+        
         val.append( "<br />" );
     }
 
@@ -957,4 +981,43 @@
         return val.toString();
     }
 
+
+    private void update( final Bundle bundle )
+    {
+        final RepositoryAdmin repoAdmin = ( RepositoryAdmin ) getService( REPOSITORY_ADMIN_NAME );
+        if ( repoAdmin != null && bundle.getSymbolicName() != null )
+        {
+            // current bundle version
+            Version bundleVersion = Version
+                .parseVersion( ( String ) bundle.getHeaders().get( Constants.BUNDLE_VERSION ) );
+
+            // discover candidates for the update
+            String filter = "(&(symbolicname=" + bundle.getSymbolicName() + ")(version>=" + bundleVersion + "))";
+            Resource[] cand = repoAdmin.discoverResources( filter );
+
+            // find the candidate with the highest version number
+            Version base = bundleVersion;
+            int idx = -1;
+            for ( int i = 0; cand != null && i < cand.length; i++ )
+            {
+                if ( cand[i].getVersion().compareTo( base ) > 0 )
+                {
+                    base = cand[i].getVersion();
+                    idx = i;
+                }
+            }
+
+            // try to resolve and deploy the best candidate
+            if ( idx >= 0 )
+            {
+                Resolver resolver = repoAdmin.resolver();
+                resolver.add( cand[idx] );
+
+                DeployerThread dt = new DeployerThread( resolver, getLog(), bundle.getState() == Bundle.ACTIVE,
+                    "Update " + bundle.getSymbolicName() );
+                dt.start();
+            }
+        }
+
+    }
 }
diff --git a/webconsole/src/main/java/org/apache/felix/webconsole/internal/core/InstallAction.java b/webconsole/src/main/java/org/apache/felix/webconsole/internal/core/InstallAction.java
index 13c3036..5aeff04 100644
--- a/webconsole/src/main/java/org/apache/felix/webconsole/internal/core/InstallAction.java
+++ b/webconsole/src/main/java/org/apache/felix/webconsole/internal/core/InstallAction.java
@@ -253,10 +253,13 @@
             {
                 Bundle bundle = getBundleContext().installBundle( location, bundleStream );
 
-                StartLevel sl = getStartLevel();
-                if ( sl != null )
+                if ( startlevel > 0 )
                 {
-                    sl.setBundleStartLevel( bundle, startlevel );
+                    StartLevel sl = getStartLevel();
+                    if ( sl != null )
+                    {
+                        sl.setBundleStartLevel( bundle, startlevel );
+                    }
                 }
 
                 if ( doStart )
diff --git a/webconsole/src/main/java/org/apache/felix/webconsole/internal/core/UpdateAction.java b/webconsole/src/main/java/org/apache/felix/webconsole/internal/core/UpdateAction.java
deleted file mode 100644
index 96ec646..0000000
--- a/webconsole/src/main/java/org/apache/felix/webconsole/internal/core/UpdateAction.java
+++ /dev/null
@@ -1,139 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements.  See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License.  You may obtain a copy of the License at
- *
- *      http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.apache.felix.webconsole.internal.core;
-
-
-import javax.servlet.http.HttpServletRequest;
-import javax.servlet.http.HttpServletResponse;
-
-import org.osgi.framework.Bundle;
-import org.osgi.framework.BundleContext;
-import org.osgi.service.log.LogService;
-import org.osgi.util.tracker.ServiceTracker;
-
-
-/**
- * The <code>UpdateAction</code> TODO
- */
-public class UpdateAction extends BundleAction
-{
-
-    public static final String NAME = "update";
-
-    public static final String LABEL = "Update";
-
-    private static final String INSTALLER_SERVICE_NAME = "org.apache.sling.osgi.assembly.installer.InstallerService";
-
-    // track the optional installer service manually
-    private ServiceTracker installerService;
-
-
-    public void activate( BundleContext bundleContext )
-    {
-        super.activate( bundleContext );
-
-        installerService = new ServiceTracker( bundleContext, INSTALLER_SERVICE_NAME, null );
-        installerService.open();
-    }
-
-
-    public String getName()
-    {
-        return NAME;
-    }
-
-
-    public String getLabel()
-    {
-        return LABEL;
-    }
-
-
-    public boolean performAction( HttpServletRequest request, HttpServletResponse response )
-    {
-
-        long bundleId = this.getBundleId( request );
-        if ( bundleId > 0 )
-        { // cannot stop system bundle !!
-            Bundle bundle = this.getBundleContext().getBundle( bundleId );
-            if ( bundle != null )
-            {
-                try
-                {
-                    this.updateFromRepo( bundle );
-                }
-                catch ( Throwable t )
-                {
-                    getLog().log( LogService.LOG_ERROR, "Uncaught Problem", t );
-                }
-
-            }
-        }
-
-        return true;
-    }
-
-
-    private void updateFromRepo( final Bundle bundle )
-    {
-        /*
-                final InstallerService is = (InstallerService) installerService.getService();
-                if (is == null) {
-                    return;
-                }
-
-                final String name = bundle.getSymbolicName();
-                final String version = (String) bundle.getHeaders().get(
-                    Constants.BUNDLE_VERSION);
-
-                // the name is required, otherwise we can do nothing
-                if (name == null) {
-                    return;
-                }
-
-                // TODO: Should be restrict to same major.micro ??
-
-                Thread t = new Thread("Background Update") {
-                    public void run() {
-                        // wait some time for the request to settle
-                        try {
-                            sleep(500L);
-                        } catch (InterruptedException ie) {
-                            // don't care
-                        }
-
-                        Installer installer = is.getInstaller();
-                        installer.addBundle(name, new VersionRange(version), -1);
-                        try {
-                            installer.install(false);
-                        } catch (InstallerException ie) {
-                            Throwable cause = (ie.getCause() != null)
-                                    ? ie.getCause()
-                                    : ie;
-                            getLog().log(LogService.LOG_ERROR, "Cannot update", cause);
-                        } finally {
-                            installer.dispose();
-                        }
-                    }
-                };
-
-                t.setDaemon(true); // make a daemon thread (detach from current thread)
-                t.start();
-                */
-    }
-
-}