git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@933542 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 f8d3831..0de3161 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
@@ -179,7 +179,7 @@
         try
         {
             StringWriter w = new StringWriter();
-            writeJSON( w, null, null, null, true, Locale.ENGLISH, null );
+            writeJSON( w, null, null, null, true, Locale.ENGLISH, null, null );
             String jsonString = w.toString();
             JSONObject json = new JSONObject( jsonString );
 
@@ -261,7 +261,7 @@
             final String servicesRoot = getServicesRoot( request );
             try
             {
-                this.renderJSON(response, reqInfo.bundle, pluginRoot, servicesRoot, request.getLocale(), request.getParameter(FILTER_PARAM) );
+                this.renderJSON(response, reqInfo.bundle, pluginRoot, servicesRoot, request.getLocale(), request.getParameter(FILTER_PARAM), null );
             }
             catch (InvalidSyntaxException e)
             {
@@ -281,6 +281,7 @@
     protected void doPost( HttpServletRequest req, HttpServletResponse resp ) throws ServletException, IOException
     {
         boolean success = false;
+        BundleException bundleException = null;
         final String action = WebConsoleUtil.getParameter( req, "action" );
         if ( "refreshPackages".equals( action ) )
         {
@@ -327,6 +328,7 @@
                     }
                     catch ( BundleException be )
                     {
+                        bundleException = be;
                         log( "Cannot start", be );
                     }
                 }
@@ -340,6 +342,7 @@
                     }
                     catch ( BundleException be )
                     {
+                        bundleException = be;
                         log( "Cannot stop", be );
                     }
                 }
@@ -365,9 +368,23 @@
                     }
                     catch ( BundleException be )
                     {
+                        bundleException = be;
                         log( "Cannot uninstall", be );
                     }
                 }
+
+                // let's wait a little bit to give the framework time
+                // to process our request
+                try {
+                    Thread.sleep(800);
+                } catch (InterruptedException e) {
+                    // we ignore this
+                }
+
+                // write the state only
+                resp.getWriter().print("{fragment:" + isFragmentBundle(bundle) //
+                    + ",stateRaw:" + bundle.getState() + "}");
+                return;
             }
         }
 
@@ -384,7 +401,7 @@
             final String servicesRoot = getServicesRoot( req );
             try
             {
-                this.renderJSON( resp, null, pluginRoot, servicesRoot, req.getLocale(), req.getParameter(FILTER_PARAM) );
+                this.renderJSON( resp, null, pluginRoot, servicesRoot, req.getLocale(), req.getParameter(FILTER_PARAM), bundleException );
             }
             catch (InvalidSyntaxException e)
             {
@@ -483,7 +500,7 @@
         StringWriter w = new StringWriter();
         try
         {
-            writeJSON(w, reqInfo.bundle, pluginRoot, servicesRoot, request.getLocale(), request.getParameter(FILTER_PARAM) );
+            writeJSON(w, reqInfo.bundle, pluginRoot, servicesRoot, request.getLocale(), request.getParameter(FILTER_PARAM), null );
         }
         catch (InvalidSyntaxException e)
         {
@@ -495,26 +512,26 @@
         response.getWriter().print(TEMPLATE_MAIN);
     }
 
-    private void renderJSON( final HttpServletResponse response, final Bundle bundle, final String pluginRoot, final String servicesRoot, final Locale locale, final String filter )
+    private void renderJSON( final HttpServletResponse response, final Bundle bundle, final String pluginRoot, final String servicesRoot, final Locale locale, final String filter, final BundleException be )
         throws IOException, InvalidSyntaxException
     {
         response.setContentType( "application/json" );
         response.setCharacterEncoding( "UTF-8" );
 
         final PrintWriter pw = response.getWriter();
-        writeJSON(pw, bundle, pluginRoot, servicesRoot, locale, filter);
+        writeJSON(pw, bundle, pluginRoot, servicesRoot, locale, filter, be);
     }
 
 
-    private void writeJSON( final Writer pw, final Bundle bundle, final String pluginRoot, final String servicesRoot, final Locale locale, final String filter )
+    private void writeJSON( final Writer pw, final Bundle bundle, final String pluginRoot, final String servicesRoot, final Locale locale, final String filter, final BundleException be )
         throws IOException, InvalidSyntaxException
     {
-        writeJSON( pw, bundle, pluginRoot, servicesRoot, false, locale, filter );
+        writeJSON( pw, bundle, pluginRoot, servicesRoot, false, locale, filter, be );
     }
 
 
     private void writeJSON( final Writer pw, final Bundle bundle, final String pluginRoot,
-        final String servicesRoot, final boolean fullDetails, final Locale locale, final String filter ) throws IOException, InvalidSyntaxException
+        final String servicesRoot, final boolean fullDetails, final Locale locale, final String filter, final BundleException be ) throws IOException, InvalidSyntaxException
     {
         final Bundle[] allBundles = this.getBundles();
         final Object[] status = getStatusLine(allBundles);
@@ -553,9 +570,18 @@
         {
             jw.object();
 
+            if (null != be)
+            {
+                final StringWriter s = new StringWriter();
+                final Throwable t = be.getNestedException() != null ? be.getNestedException() : be;
+                t.printStackTrace( new PrintWriter(s) );
+                jw.key( "error" );
+                jw.value( s.toString() );
+            }
+
             jw.key( "status" );
             jw.value( statusLine );
-
+            
             // add raw status
             jw.key( "s" );
             jw.array();
@@ -1059,7 +1085,7 @@
     {
         JSONArray val = new JSONArray();
 
-        Dictionary headers = bundle.getHeaders();
+        Dictionary headers = bundle.getHeaders(""); // don't localize at all - raw headers
         Enumeration he = headers.keys();
         while ( he.hasMoreElements() )
         {
diff --git a/webconsole/src/main/native2ascii/OSGI-INF/l10n/bundle_bg.properties b/webconsole/src/main/native2ascii/OSGI-INF/l10n/bundle_bg.properties
index 10cd41a..d43d617 100644
--- a/webconsole/src/main/native2ascii/OSGI-INF/l10n/bundle_bg.properties
+++ b/webconsole/src/main/native2ascii/OSGI-INF/l10n/bundle_bg.properties
@@ -158,6 +158,8 @@
 bundles.state.32=Активен
 bundles.state.unknown=Непознат статус: {0}
 bundles.state.fragment=Фрагмент
+# action error
+bundles.error.title=Грешка при изпълнение на операцията!
 
 
 # Components plugin
diff --git a/webconsole/src/main/resources/OSGI-INF/l10n/bundle.properties b/webconsole/src/main/resources/OSGI-INF/l10n/bundle.properties
index fea335f..d91f369 100644
--- a/webconsole/src/main/resources/OSGI-INF/l10n/bundle.properties
+++ b/webconsole/src/main/resources/OSGI-INF/l10n/bundle.properties
@@ -157,6 +157,8 @@
 bundles.state.32=Active
 bundles.state.unknown=Unknown State: {0}
 bundles.state.fragment=Fragment
+# action error
+bundles.error.title=Error while executing bundle operation!
 
 
 # Components plugin
diff --git a/webconsole/src/main/resources/res/ui/bundles.js b/webconsole/src/main/resources/res/ui/bundles.js
index b263597..a36c5c1 100644
--- a/webconsole/src/main/resources/res/ui/bundles.js
+++ b/webconsole/src/main/resources/res/ui/bundles.js
@@ -19,6 +19,7 @@
 var bundlesTable    = false;
 var bundlesBody     = false;
 var bundlesTemplate = false;
+var bundleOpError   = false;
 
 function renderData( eventData, filter )  {
 	lastBundleData = eventData;
@@ -39,6 +40,9 @@
         showDetails(id);
     }
     initStaticWidgets();
+	
+	// show dialog on error
+	if (eventData.error) bundleOpError.dialog('open').find('pre').text(eventData.error)
 }
 
 function entry( /* Object */ bundle, filter ) {
@@ -69,34 +73,46 @@
 	tr.find('td:eq(1) span:eq(1)').html( drawDetails ? name : '<a href="' + pluginRoot + '/' + id + '">' + name + '</a>' );
 	tr.find('td:eq(2)').text( bundle.version );
 	tr.find('td:eq(3)').text( bundle.category );
-	tr.find('td:eq(4)').text( stateString(bundle) );
 	if (id == 0) { // system bundle has no actions
+		tr.find('td:eq(4)').text( stateString(bundle) );
 		tr.find('td:eq(5) ul').addClass('ui-helper-hidden');
 	} else {
-		var start   = tr.find('td:eq(5) ul li:eq(0)');
-		var stop    = tr.find('td:eq(5) ul li:eq(1)');
-		var refresh = tr.find('td:eq(5) ul li:eq(2)').click(function() {changeDataEntryState(id, 'refresh')});
-		var update  = tr.find('td:eq(5) ul li:eq(3)').click(function() {changeDataEntryState(id, 'update')});
-		var remove  = tr.find('td:eq(5) ul li:eq(4)');
-		start = hasStart(bundle) ?
-			start.click(function() {changeDataEntryState(id, 'start')}) :
-			start.addClass('ui-helper-hidden');
-		stop = hasStop(bundle) ?
-			stop.click(function() {changeDataEntryState(id, 'stop')}) :
-			stop.addClass('ui-helper-hidden');
-		remove = hasUninstall(bundle) ?
-			remove.click(function() {changeDataEntryState(id, 'uninstall')}) :
-			remove.addClass('ui-helper-hidden');
+		entrySetupState( bundle, tr, id );
 	}
 	return tr;
 }
+function entrySetupState( /* Object */ bundle, tr, id) {
+	var start   = tr.find('td:eq(5) ul li:eq(0)').removeClass('ui-helper-hidden').unbind('click');
+	var stop    = tr.find('td:eq(5) ul li:eq(1)').removeClass('ui-helper-hidden').unbind('click');
+	var refresh = tr.find('td:eq(5) ul li:eq(2)').unbind('click').click(function() {return changeDataEntryState(id, 'refresh')});
+	var update  = tr.find('td:eq(5) ul li:eq(3)').unbind('click').click(function() {return changeDataEntryState(id, 'update')});
+	var remove  = tr.find('td:eq(5) ul li:eq(4)').removeClass('ui-helper-hidden').unbind('click');
+	start = hasStart(bundle) ?
+		start.click(function() {return changeDataEntryState(id, 'start')}) :
+		start.addClass('ui-helper-hidden');
+	stop = hasStop(bundle) ?
+		stop.click(function() {return changeDataEntryState(id, 'stop')}) :
+		stop.addClass('ui-helper-hidden');
+	remove = hasUninstall(bundle) ?
+		remove.click(function() {return changeDataEntryState(id, 'uninstall')}) :
+		remove.addClass('ui-helper-hidden');
+	tr.find('td:eq(4)').text( stateString(bundle) );
+}
 
 function loadData() {
     $.get(pluginRoot + "/.json", null, renderData, "json"); 
 }
 
 function changeDataEntryState(/* long */ id, /* String */ action) {
-    $.post(pluginRoot + "/" + id, {"action":action}, renderData, "json"); 
+    $.post(pluginRoot + '/' + id, {'action':action}, function(b) {
+		var _tr = bundlesBody.find('#entry' + id);
+		if (1 == b.stateRaw)  { // uninstalled
+			_tr.remove(); 
+		} else {
+			entrySetupState( b, _tr, id );
+		}
+	}, 'json');
+	return false;
 }
 
 function refreshPackages() {
@@ -184,6 +200,13 @@
 		return false;
 	});
 
+	bundleOpError = $('#bundleOpError').dialog({
+		autoOpen: false,
+		modal   : true,
+		width   : '80%'
+	});
+	bundleOpError.parent().addClass('ui-state-error');
+
 	// filter
 	$('.filterApply').click(function() {
 		if ($(this).hasClass('ui-state-disabled')) return;
diff --git a/webconsole/src/main/resources/templates/bundles.html b/webconsole/src/main/resources/templates/bundles.html
index 8f956db..3ec8bb0 100644
--- a/webconsole/src/main/resources/templates/bundles.html
+++ b/webconsole/src/main/resources/templates/bundles.html
@@ -140,3 +140,5 @@
 	</table>
 	</form>
 </div>
+
+<div id="bundleOpError" class="ui-helper-hidden" title="${bundles.error.title}"><pre>&nbsp;</pre></div>