Author: ivol37 at gmail.com
Date: Thu Nov  4 17:00:49 2010
New Revision: 250

Log:
[AMDATU-154] Fixed stop/start issue 

Added:
   
trunk/platform-bundles/httpcontext/src/main/java/org/amdatu/platform/httpcontext/service/HttpContextUtil.java
Modified:
   
trunk/amdatu-web/jsp-support-bundle/src/main/java/org/amdatu/web/jsp/service/ResourceProviderListener.java
   
trunk/application-bundles/gadget-management/src/main/java/org/amdatu/application/gadgetmanagement/service/GadgetManagementServiceImpl.java
   
trunk/example-bundles/course-gadget/src/main/java/org/amdatu/example/course/gadget/osgi/service/CourseGadgetServiceImpl.java
   
trunk/platform-bundles/httpcontext/src/main/java/org/amdatu/platform/httpcontext/service/HttpContextFactoryServiceImpl.java
   
trunk/platform-bundles/httpcontext/src/main/java/org/amdatu/platform/httpcontext/service/HttpContextImpl.java

Modified: 
trunk/amdatu-web/jsp-support-bundle/src/main/java/org/amdatu/web/jsp/service/ResourceProviderListener.java
==============================================================================
--- 
trunk/amdatu-web/jsp-support-bundle/src/main/java/org/amdatu/web/jsp/service/ResourceProviderListener.java
  (original)
+++ 
trunk/amdatu-web/jsp-support-bundle/src/main/java/org/amdatu/web/jsp/service/ResourceProviderListener.java
  Thu Nov  4 17:00:49 2010
@@ -116,7 +116,7 @@
     }
 
     // This callback is invoked for each ResourceProvider that becomes 
unavailable
-    public void omRemoved(ResourceProvider provider) {
+    public void onRemoved(ResourceProvider provider) {
         try {
             m_httpService.unregister(getServletAlias(provider));
             
m_dependencyManager.remove(m_components.get(provider.getResourceId()));

Modified: 
trunk/application-bundles/gadget-management/src/main/java/org/amdatu/application/gadgetmanagement/service/GadgetManagementServiceImpl.java
==============================================================================
--- 
trunk/application-bundles/gadget-management/src/main/java/org/amdatu/application/gadgetmanagement/service/GadgetManagementServiceImpl.java
  (original)
+++ 
trunk/application-bundles/gadget-management/src/main/java/org/amdatu/application/gadgetmanagement/service/GadgetManagementServiceImpl.java
  Thu Nov  4 17:00:49 2010
@@ -16,12 +16,6 @@
  */
 package org.amdatu.application.gadgetmanagement.service;
 
-import javax.servlet.http.HttpServletRequest;
-import javax.ws.rs.*;
-import javax.ws.rs.core.Context;
-import javax.ws.rs.core.HttpHeaders;
-import javax.ws.rs.core.MediaType;
-import javax.ws.rs.core.Response;
 import java.io.BufferedReader;
 import java.io.IOException;
 import java.io.InputStreamReader;
@@ -30,7 +24,22 @@
 import java.net.URLConnection;
 import java.nio.charset.Charset;
 import java.security.NoSuchAlgorithmException;
-import java.util.*;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Locale;
+import java.util.Map;
+
+import javax.servlet.http.HttpServletRequest;
+import javax.ws.rs.DefaultValue;
+import javax.ws.rs.GET;
+import javax.ws.rs.Path;
+import javax.ws.rs.Produces;
+import javax.ws.rs.QueryParam;
+import javax.ws.rs.core.Context;
+import javax.ws.rs.core.HttpHeaders;
+import javax.ws.rs.core.MediaType;
+import javax.ws.rs.core.Response;
 
 import org.amdatu.application.gadgetmanagement.DefaultGadgetRepository;
 import org.amdatu.application.gadgetmanagement.GadgetManagement;
@@ -58,7 +67,6 @@
 import org.json.JSONException;
 import org.json.JSONObject;
 import org.osgi.framework.BundleContext;
-import org.osgi.service.cm.ConfigurationAdmin;
 import org.osgi.service.log.LogService;
 import org.osgi.service.useradmin.Authorization;
 import org.osgi.service.useradmin.User;
@@ -70,14 +78,13 @@
 @Path("gadgetstore")
 public class GadgetManagementServiceImpl implements GadgetManagement, 
ResourceProvider {
     private static final String[] GADGET_MODULEPREFS =
-            new String[]{"title", "description", "author", "author_email", 
"author_affiliation", "screenshot",
-                    "title_url"};
+        new String[] { "title", "description", "author", "author_email", 
"author_affiliation", "screenshot",
+        "title_url" };
 
     // Services injected by the Felix dependency manager
     private volatile BundleContext m_bundleContext;
     private volatile LogService m_logService;
     private volatile UserAdmin m_userAdmin;
-    private volatile ConfigurationAdmin m_configurationAdmin;
     private volatile HttpContextServiceFactory m_httpContextFactoryService;
     private volatile GadgetStore m_gadgetStore;
 
@@ -93,7 +100,6 @@
         m_logService.log(LogService.LOG_INFO, getClass().getName() + " service 
initialized");
     }
 
-    @SuppressWarnings("unchecked")
     public void start() {
         // initialize the default gadget categories.
         addCategory(GadgetCategory.AMDATU_PLATFORM);
@@ -125,11 +131,12 @@
     /**
      * Returns gadgets available in the gadget store. URL to this resource:
      * http://localhost:3737/rest/services/gadgetstore/gadgetstore/categories
+     * 
      * @return All available gadgets.
      */
     @GET
     @Path("categories")
-    @Produces({MediaType.APPLICATION_JSON})
+    @Produces( { MediaType.APPLICATION_JSON })
     public Response getCategories(@Context HttpHeaders headers, @Context 
HttpServletRequest request) {
         JSONObject jsonObject = new JSONObject();
         List<JSONObject> categories = new ArrayList<JSONObject>();
@@ -145,11 +152,13 @@
             }
             jsonObject.put("category", categories);
 
-        } catch (JSONException e) {
+        }
+        catch (JSONException e) {
             m_logService.log(LogService.LOG_ERROR, "An error occurred while 
retrieving all categories", e);
-        } catch (CassandraException e) {
+        }
+        catch (CassandraException e) {
             m_logService.log(LogService.LOG_ERROR, "An error occurred while 
retrieving all categories", e);
-        } 
+        }
 
         return Response.ok(jsonObject.toString(), 
MediaType.APPLICATION_JSON_TYPE).build();
     }
@@ -157,13 +166,14 @@
     /**
      * Returns gadgets available in the gadget store. URL to this resource:
      * http://localhost:3737/rest/services/gadgetstore/gadgetstore/gadgets/all
+     * 
      * @return All available gadgets.
      */
     @GET
     @Path("gadgets/all")
-    @Produces({MediaType.APPLICATION_JSON})
+    @Produces( { MediaType.APPLICATION_JSON })
     public Response getAllGadgets(@Context HttpHeaders headers, @Context 
HttpServletRequest request,
-            @DefaultValue("") @QueryParam("category") final String categoryId) 
{
+        @DefaultValue("") @QueryParam("category") final String categoryId) {
         String errorMsg = "An error occurred while retrieving all gadgets for 
category '" + categoryId + "'.";
         JSONObject jsonObject = new JSONObject();
         List<JSONObject> gadgets = new ArrayList<JSONObject>();
@@ -192,29 +202,35 @@
             }
 
             jsonObject.put("data", gadgets);
-        } catch (IOException e) {
+        }
+        catch (IOException e) {
             m_logService.log(LogService.LOG_ERROR, errorMsg, e);
-        } catch (JSONException e) {
+        }
+        catch (JSONException e) {
             m_logService.log(LogService.LOG_ERROR, errorMsg, e);
-        } catch (NoSuchAlgorithmException e) {
+        }
+        catch (NoSuchAlgorithmException e) {
             m_logService.log(LogService.LOG_ERROR, errorMsg, e);
-        } catch (CassandraException e) {
+        }
+        catch (CassandraException e) {
             m_logService.log(LogService.LOG_ERROR, errorMsg, e);
-        } catch (BlobCrypterException e) {
+        }
+        catch (BlobCrypterException e) {
             m_logService.log(LogService.LOG_ERROR, errorMsg, e);
-        } 
+        }
         return Response.ok(jsonObject.toString(), 
MediaType.APPLICATION_JSON_TYPE).build();
     }
 
     /**
      * Returns gadgets registered for the current user. URL to this resource:
      * http://localhost:3737/rest/services/gadgetstore/gadgetstore/gadgets/mine
+     * 
      * @return Gadgets registered for the current user.
      */
     @SuppressWarnings("unchecked")
     @GET
     @Path("/gadgets/mine")
-    @Produces({MediaType.APPLICATION_JSON})
+    @Produces( { MediaType.APPLICATION_JSON })
     public Response getMyGadgets(@Context HttpHeaders headers, @Context 
HttpServletRequest request) {
         JSONObject jsonObject = new JSONObject();
         List<JSONObject> gadgets = new ArrayList<JSONObject>();
@@ -245,7 +261,8 @@
                 if (bytesValue != null) {
                     Map<String, String> values = (Map<String, String>) 
ConversionUtil.byteArrayToObject(bytesValue);
                     gadgetUrls = 
values.get(OpenSocialConstants.REGISTERED_GADGETS).trim().split(" ");
-                } else {
+                }
+                else {
                     Map<String, String> values = new HashMap<String, String>();
                     int i = 0;
                     String sDefaultGadgetUrls = "";
@@ -253,7 +270,8 @@
                     for (String defaultGadgetUrl : defaultGadgetUrls) {
                         if (i == 0) {
                             sDefaultGadgetUrls += defaultGadgetUrl;
-                        } else {
+                        }
+                        else {
                             sDefaultGadgetUrls += " " + defaultGadgetUrl;
                         }
                         i++;
@@ -277,10 +295,12 @@
                     if (column == 1) {
                         gadget.put("column", "first");
                         column++;
-                    } else if (column == 2) {
+                    }
+                    else if (column == 2) {
                         gadget.put("column", "second");
                         column++;
-                    } else if (column == 3) {
+                    }
+                    else if (column == 3) {
                         gadget.put("column", "third");
                         column = 1;
                     }
@@ -291,25 +311,29 @@
 
             jsonObject.put("layout", "layout5");
             jsonObject.put("data", gadgets);
-        } catch (JSONException e) {
+        }
+        catch (JSONException e) {
             m_logService.log(LogService.LOG_ERROR, errorMsg, e);
-        } catch (NoSuchAlgorithmException e) {
+        }
+        catch (NoSuchAlgorithmException e) {
             m_logService.log(LogService.LOG_ERROR, errorMsg, e);
-        } catch (UnsupportedEncodingException e) {
+        }
+        catch (UnsupportedEncodingException e) {
             m_logService.log(LogService.LOG_ERROR, errorMsg, e);
-        } catch (BlobCrypterException e) {
+        }
+        catch (BlobCrypterException e) {
             m_logService.log(LogService.LOG_ERROR, errorMsg, e);
-        } catch (ClassNotFoundException e) {
+        }
+        catch (ClassNotFoundException e) {
             m_logService.log(LogService.LOG_ERROR, errorMsg, e);
-        } catch (IOException e) {
+        }
+        catch (IOException e) {
             m_logService.log(LogService.LOG_ERROR, errorMsg, e);
-        } 
+        }
 
         return Response.ok(jsonObject.toString(), 
MediaType.APPLICATION_JSON_TYPE).build();
     }
 
-   
-
     private Map<String, String> getGadgetSpec(String gadgetUrl) {
         Map<String, String> gadgetSpec = new HashMap<String, String>();
 
@@ -348,12 +372,14 @@
 
             if (spec.getModulePrefs().getScreenshot() != null) {
                 gadgetSpec.put("screenshot", 
spec.getModulePrefs().getScreenshot().toString());
-            } else {
+            }
+            else {
                 gadgetSpec.put("screenshot", "/" + Activator.RESOURCE_ID + 
"/static/images/defaultgadget.png");
             }
 
             return gadgetSpec;
-        } catch (Exception e) {
+        }
+        catch (Exception e) {
             // Catch all exceptions since even an improper URL can cause a 
NullPointerException here
             m_logService.log(LogService.LOG_ERROR, "Could not retrieve gadget 
spec for url '" + gadgetUrl + "'", e);
         }
@@ -375,19 +401,24 @@
                 String xml = "";
                 URLConnection inputConnection = url.openConnection();
                 // TODO: assuming here it is UTF-8
-                reader = new BufferedReader(new 
InputStreamReader(inputConnection.getInputStream(), Charset.forName("UTF-8")));
+                reader =
+                    new BufferedReader(
+                        new 
InputStreamReader(inputConnection.getInputStream(), Charset.forName("UTF-8")));
                 String inputLine;
                 while ((inputLine = reader.readLine()) != null) {
                     xml += inputLine;
                 }
                 return xml;
-            } finally {
+            }
+            finally {
                 if (reader != null) {
                     reader.close();
                 }
             }
-        } catch (IOException e) {
-            m_logService.log(LogService.LOG_ERROR, "Could not retrieve gadget 
XML from url '" + url.toString() + "'", e);
+        }
+        catch (IOException e) {
+            m_logService
+                .log(LogService.LOG_ERROR, "Could not retrieve gadget XML from 
url '" + url.toString() + "'", e);
         }
         return null;
     }
@@ -403,14 +434,14 @@
     // 8 Locale that matches language "*" and country "*"
     private synchronized Locale getPreferredLocale(Locale preferredLocale, 
Map<Locale, LocaleSpec> locales) {
         Map<Integer, String[]> localeOrder = new HashMap<Integer, String[]>();
-        localeOrder.put(1, new String[]{preferredLocale.getLanguage(), 
preferredLocale.getCountry()});
-        localeOrder.put(2, new String[]{preferredLocale.getLanguage(), null});
-        localeOrder.put(3, new String[]{null, preferredLocale.getCountry()});
-        localeOrder.put(4, new String[]{"en", null});
-        localeOrder.put(5, new String[]{"en-us", null});
-        localeOrder.put(6, new String[]{"en-gb", null});
-        localeOrder.put(7, new String[]{"all", "all"});
-        localeOrder.put(8, new String[]{"*", "*"});
+        localeOrder.put(1, new String[] { preferredLocale.getLanguage(), 
preferredLocale.getCountry() });
+        localeOrder.put(2, new String[] { preferredLocale.getLanguage(), null 
});
+        localeOrder.put(3, new String[] { null, preferredLocale.getCountry() 
});
+        localeOrder.put(4, new String[] { "en", null });
+        localeOrder.put(5, new String[] { "en-us", null });
+        localeOrder.put(6, new String[] { "en-gb", null });
+        localeOrder.put(7, new String[] { "all", "all" });
+        localeOrder.put(8, new String[] { "*", "*" });
 
         int currentLocaleOrder = 0;
         Locale resultLocale = null;
@@ -437,7 +468,7 @@
     }
 
     private JSONObject retrieveGadget(String gadgetUrl) throws JSONException, 
NoSuchAlgorithmException,
-            UnsupportedEncodingException {
+        UnsupportedEncodingException {
         Map<String, String> gadgetSpec = getGadgetSpec(gadgetUrl);
         if (gadgetSpec != null) {
             JSONObject gadget = new JSONObject();
@@ -459,10 +490,12 @@
             if ("".equals(gadgetSpec.get("author")) || 
gadgetSpec.get("author") == null) {
                 if (!"".equals(gadgetSpec.get("author_email")) && 
gadgetSpec.get("author_email") != null) {
                     gadget.put("author", gadgetSpec.get("author_email"));
-                } else if (!"".equals(gadgetSpec.get("author_affiliation"))
-                        && gadgetSpec.get("author_affiliation") != null) {
+                }
+                else if (!"".equals(gadgetSpec.get("author_affiliation"))
+                    && gadgetSpec.get("author_affiliation") != null) {
                     gadget.put("author", gadgetSpec.get("author_affiliation"));
-                } else {
+                }
+                else {
                     gadget.put("author", "unknown author");
                 }
             }
@@ -471,14 +504,15 @@
             gadget.put("metadata", new JSONObject().put("gadgeturl", 
gadgetUrl));
 
             return gadget;
-        } else {
+        }
+        else {
             return null;
         }
     }
 
     // Adds a security token
     private void addSecurityToken(String gadgetUrl, JSONObject gadget, 
HttpServletRequest request)
-            throws BlobCrypterException, JSONException {
+        throws BlobCrypterException, JSONException {
         // TODO: This is the same key as in securitytokenkey.txt file of the 
shindig application bundle
         // But we are too lazy to load that key from the file
         String key = "Tr9W5GNmfCb7eexNi0m+/HVgAlvsHmCuMEofHs6+LoU=";
@@ -491,7 +525,8 @@
         if (auth != null) {
             st.setViewerId(auth.getName());
             st.setOwnerId(auth.getName());
-        } else {
+        }
+        else {
             st.setViewerId("anonymous");
             st.setOwnerId("anonymous");
         }
@@ -522,11 +557,11 @@
         }
         return false;
     }
-    
+
     public GadgetCategory getCategory(String categoryId) {
         return m_gadgetStore.getCategory(categoryId);
     }
-    
+
     public GadgetCategory[] getCategories() {
         return m_gadgetStore.getCategories();
     }

Modified: 
trunk/example-bundles/course-gadget/src/main/java/org/amdatu/example/course/gadget/osgi/service/CourseGadgetServiceImpl.java
==============================================================================
--- 
trunk/example-bundles/course-gadget/src/main/java/org/amdatu/example/course/gadget/osgi/service/CourseGadgetServiceImpl.java
        (original)
+++ 
trunk/example-bundles/course-gadget/src/main/java/org/amdatu/example/course/gadget/osgi/service/CourseGadgetServiceImpl.java
        Thu Nov  4 17:00:49 2010
@@ -62,12 +62,18 @@
         // Create our own http context and register resources
         m_httpContextComponent = 
m_httpContextFactoryService.create(m_bundleContext, this);
 
-        GadgetDefinition gadgetDef =
+        if (m_hostname == null || m_portnr == null) {
+            // FIXME: For some reason update() is only invoked the very first 
time this service is created. When
+            // you stop/start the bundle, updated() will not be invoked (at 
all!) and so hostname and portnr remain
+            // null.
+        } else {
+            GadgetDefinition gadgetDef =
                 new GadgetDefinition("http://"; + m_hostname + ":" + m_portnr + 
"/" + Activator.RESOURCE_ID
-                        + "/jsp/CourseGadget.jsp", 
GadgetCategory.AMDATU_EXAMPLES, false);
-        gadgetDef.setServiceName("course");
-        m_gadgetManagement.addGadget(gadgetDef);
-        m_logService.log(LogService.LOG_INFO, getClass().getName() + " service 
initialized");
+                    + "/jsp/CourseGadget.jsp", GadgetCategory.AMDATU_EXAMPLES, 
false);
+            gadgetDef.setServiceName("course");
+            m_gadgetManagement.addGadget(gadgetDef);
+            m_logService.log(LogService.LOG_INFO, getClass().getName() + " 
service initialized");
+        }
     }
 
     /**

Modified: 
trunk/platform-bundles/httpcontext/src/main/java/org/amdatu/platform/httpcontext/service/HttpContextFactoryServiceImpl.java
==============================================================================
--- 
trunk/platform-bundles/httpcontext/src/main/java/org/amdatu/platform/httpcontext/service/HttpContextFactoryServiceImpl.java
 (original)
+++ 
trunk/platform-bundles/httpcontext/src/main/java/org/amdatu/platform/httpcontext/service/HttpContextFactoryServiceImpl.java
 Thu Nov  4 17:00:49 2010
@@ -82,15 +82,7 @@
         String resourceId = resourceProvider.getResourceId();
         Component component = null;
         try {
-            String resId = "";
-            if (!resourceId.startsWith("/")) {
-                resId = "/" + resourceId;
-            } else {
-                resId = resourceId;
-            }
-            if (resourceId.endsWith("/")) {
-                resId  = resId.substring(0, resId.length()-1);
-            }
+            String resId = HttpContextUtil.getResourceAlias(resourceId);
 
             // Create the http context
             component = internalCreate(bundleContext, resourceProvider);

Modified: 
trunk/platform-bundles/httpcontext/src/main/java/org/amdatu/platform/httpcontext/service/HttpContextImpl.java
==============================================================================
--- 
trunk/platform-bundles/httpcontext/src/main/java/org/amdatu/platform/httpcontext/service/HttpContextImpl.java
       (original)
+++ 
trunk/platform-bundles/httpcontext/src/main/java/org/amdatu/platform/httpcontext/service/HttpContextImpl.java
       Thu Nov  4 17:00:49 2010
@@ -82,7 +82,7 @@
      */
     public void destroy() {
         if (m_resourceId != null) {
-            m_httpService.unregister("/" + m_resourceId + "/*");
+            
m_httpService.unregister(HttpContextUtil.getResourceAlias(m_resourceId));
         }
         m_logService.log(LogService.LOG_INFO, "HTTP Context destroyed for 
bundle " + m_bundleId + " ('" + m_resourceId
                 + "')");

Added: 
trunk/platform-bundles/httpcontext/src/main/java/org/amdatu/platform/httpcontext/service/HttpContextUtil.java
==============================================================================
--- (empty file)
+++ 
trunk/platform-bundles/httpcontext/src/main/java/org/amdatu/platform/httpcontext/service/HttpContextUtil.java
       Thu Nov  4 17:00:49 2010
@@ -0,0 +1,38 @@
+/*
+    Copyright (C) 2010 Amdatu.org
+
+    This program is free software: you can redistribute it and/or modify
+    it under the terms of the GNU General Public License as published by
+    the Free Software Foundation, either version 3 of the License, or
+    (at your option) any later version.
+
+    This program is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+    GNU General Public License for more details.
+
+    You should have received a copy of the GNU General Public License
+    along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+package org.amdatu.platform.httpcontext.service;
+
+/**
+ * This class contains some minor utility methods used by the services in this 
bundle.
+ * 
+ * @author ivol
+ */
+public class HttpContextUtil {
+    static String getResourceAlias(String resourceId) {
+        String result = "";
+        if (!resourceId.startsWith("/")) {
+            result = "/" + resourceId;
+        }
+        else {
+            result = resourceId;
+        }
+        if (resourceId.endsWith("/")) {
+            result = result.substring(0, result.length() - 1);
+        }
+        return result;
+    }
+}

Reply via email to