Author: ivol37 at gmail.com
Date: Mon Nov 15 11:21:29 2010
New Revision: 424

Log:
[AMDATU-82] Added read time out to speed up loading gadget specs
[AMDATU-169] Fixed mismatch in id's of gadgets causing the delete to fail when 
delete is invoked immediately after adding it (without refreshing the dashboard)

Modified:
   trunk/amdatu-opensocial/dashboard/src/main/resources/static/js/dashboard.js
   
trunk/amdatu-opensocial/dashboard/src/main/resources/static/js/gadgets_appdata.js
   
trunk/amdatu-opensocial/gadgetmanagement/src/main/java/org/amdatu/opensocial/gadgetmanagement/service/GadgetManagementServiceImpl.java

Modified: 
trunk/amdatu-opensocial/dashboard/src/main/resources/static/js/dashboard.js
==============================================================================
--- trunk/amdatu-opensocial/dashboard/src/main/resources/static/js/dashboard.js 
(original)
+++ trunk/amdatu-opensocial/dashboard/src/main/resources/static/js/dashboard.js 
Mon Nov 15 11:21:29 2010
@@ -75,6 +75,7 @@
   my.init();
 
   my.renderGadget = function(widget) {
+    startId = startId + 1;
     var metadata = widget.metadata;
     if (typeof metadata != 'undefined' && typeof metadata.gadgeturl != 
'undefined') {
       var gadget = shindig.container.createGadget(

Modified: 
trunk/amdatu-opensocial/dashboard/src/main/resources/static/js/gadgets_appdata.js
==============================================================================
--- 
trunk/amdatu-opensocial/dashboard/src/main/resources/static/js/gadgets_appdata.js
   (original)
+++ 
trunk/amdatu-opensocial/dashboard/src/main/resources/static/js/gadgets_appdata.js
   Mon Nov 15 11:21:29 2010
@@ -23,16 +23,17 @@
 // Adds a gadget to the AppData of the current user
 addWidgetToAppData = function(obj, startId, dashboard) {
   var currentGadgets = retrieveCurrentWidgetsInAppData(obj.securetoken);
+  var gadgetId = startId + '-' + obj.url;
 
   // use appData opensocial call to add this gadget to the users appData
   var postdata = '{"registeredgadgets":"';
   if (currentGadgets == "" || !currentGadgets) {
-    postdata += obj.id + '"}';
+    postdata += gadgetId + '"}';
   } else {
-    postdata += currentGadgets + " " + obj.id + '"}';
+    postdata += currentGadgets + " " + gadgetId + '"}';
   }
 
-  var url = "/social/rest/appdata/@me/@self/" + obj.id + 
"?fields=registeredgadgets&st=" + obj.securetoken;
+  var url = "/social/rest/appdata/@me/@self/" + gadgetId + 
"?fields=registeredgadgets&st=" + obj.securetoken;
   jQuery.ajax({
     url: url,
     type: "PUT",
@@ -42,7 +43,7 @@
     async:false,
     success: function(response) {
        dashboard.addWidget({
-            "id":startId,
+            "id":gadgetId,
             "title":obj.title,
             "url":obj.url,
             "securetoken":obj.securetoken,

Modified: 
trunk/amdatu-opensocial/gadgetmanagement/src/main/java/org/amdatu/opensocial/gadgetmanagement/service/GadgetManagementServiceImpl.java
==============================================================================
--- 
trunk/amdatu-opensocial/gadgetmanagement/src/main/java/org/amdatu/opensocial/gadgetmanagement/service/GadgetManagementServiceImpl.java
      (original)
+++ 
trunk/amdatu-opensocial/gadgetmanagement/src/main/java/org/amdatu/opensocial/gadgetmanagement/service/GadgetManagementServiceImpl.java
      Mon Nov 15 11:21:29 2010
@@ -77,6 +77,9 @@
  */
 @Path("gadgetstore")
 public class GadgetManagementServiceImpl implements GadgetManagement, 
ResourceProvider {
+    // Timeout for retrieving gadget specs
+    private final static int TIMEOUT = 5000;
+    
     private static final String[] GADGET_MODULEPREFS =
         new String[] { "title", "description", "author", "author_email", 
"author_affiliation", "screenshot",
         "title_url" };
@@ -89,6 +92,9 @@
     private volatile GadgetStore m_gadgetStore;
 
     private Component m_httpContextComponent;
+    
+    // Use the startId as prefix for the gadget id to ensure each gadget has a 
unique id
+    private static int m_startId = 1;
 
     /**
      * The init() method is invoked by the Felix dependency manager.
@@ -253,7 +259,7 @@
         String errorMsg = "An error occurred while retrieving gadgets for 
viewer '" + viewer + "'.";
         try {
             User user = null;
-            String[] gadgetUrls = null;
+            String[] gadgetIds = null;
             if (viewer != null && !"anonymous".equals(viewer)) {
                 user = 
m_userAdmin.getUser(LoginService.USER_NAME_CREDENTIAL_KEY, viewer);
 
@@ -265,7 +271,7 @@
                         // Replace double spaces with single space, occurs 
when removing gadget URLs
                         gadgetValue = gadgetValue.replace("  ", " ");
                     }
-                    gadgetUrls = gadgetValue.split(" ");
+                    gadgetIds = gadgetValue.split(" ");
                 }
                 else {
                     Map<String, String> values = new HashMap<String, String>();
@@ -286,16 +292,23 @@
                 }
             }
 
-            if (gadgetUrls == null) {
-                gadgetUrls = getDefaultGadgetUrls();
+            boolean generateStartId = false;
+            if (gadgetIds == null) {
+                generateStartId = true;
+                gadgetIds = getDefaultGadgetUrls();
             }
 
             // For now just return all gadgets, but with additional security 
token
             int column = 1;
-            for (String gadgetUrl : gadgetUrls) {
-                JSONObject gadget = retrieveGadget(gadgetUrl);
+            for (String gadgetId : gadgetIds) {
+                JSONObject gadget;           
+                if (generateStartId) {
+                    gadget = retrieveGadget(gadgetId);
+                } else {
+                    gadget = 
retrieveGadget(gadgetId.substring(gadgetId.indexOf("-") + 1));
+                }
                 if (gadget != null) {
-                    addSecurityToken(gadgetUrl, gadget, request);
+                    addSecurityToken(gadgetId, gadget, request);
                     // FIXME: for now handle layout here
                     if (column == 1) {
                         gadget.put("column", "first");
@@ -310,6 +323,9 @@
                         column = 1;
                     }
 
+                    // Assign id of the gadget, which is startId - [gadget url]
+                    setGadgetId(gadget, gadgetId, generateStartId);
+                    
                     gadgets.add(gadget);
                 }
             }
@@ -338,11 +354,25 @@
 
         return Response.ok(jsonObject.toString(), 
MediaType.APPLICATION_JSON_TYPE).build();
     }
+    
+    private void setGadgetId(JSONObject gadget, String gadgetId, boolean 
generateStartId) throws JSONException {
+        String id, url;
+        if (generateStartId) {
+            // For new default gagdets, use generated startId
+            id = new Integer(m_startId++).toString();
+            url = gadgetId;
+            gadget.put("id", id + "-" + url);
+        } else {
+            // For persistent gadgets, use persisted startId
+            gadget.put("id", gadgetId);
+        }
+    }
 
     private Map<String, String> getGadgetSpec(String gadgetUrl) {
         Map<String, String> gadgetSpec = new HashMap<String, String>();
 
         try {
+            m_logService.log(LogService.LOG_DEBUG, "Retrieving gadgetspec for 
'" + gadgetUrl + "'");
             String xml = loadXML(new URL(gadgetUrl));
             GadgetSpec spec = new GadgetSpec(Uri.parse(gadgetUrl), xml);
 
@@ -400,11 +430,13 @@
     }
 
     private String loadXML(URL url) {
+        long time = System.currentTimeMillis();
         BufferedReader reader = null;
         try {
             try {
                 String xml = "";
                 URLConnection inputConnection = url.openConnection();
+                inputConnection.setReadTimeout(TIMEOUT);
                 // TODO: assuming here it is UTF-8
                 reader =
                     new BufferedReader(
@@ -413,6 +445,7 @@
                 while ((inputLine = reader.readLine()) != null) {
                     xml += inputLine;
                 }
+                m_logService.log(LogService.LOG_DEBUG, "Retrieving gadgetspec 
'" + url + "' took " + (System.currentTimeMillis()-time) + " ms");
                 return xml;
             }
             finally {
@@ -425,6 +458,7 @@
             m_logService
                 .log(LogService.LOG_ERROR, "Could not retrieve gadget XML from 
url '" + url.toString() + "'", e);
         }
+        m_logService.log(LogService.LOG_DEBUG, "Retrieving gadgetspec '" + url 
+ "' failed");
         return null;
     }
 
@@ -506,6 +540,7 @@
             }
 
             gadget.put("id", gadgetUrl);
+            gadget.put("url", gadgetUrl);
             gadget.put("metadata", new JSONObject().put("gadgeturl", 
gadgetUrl));
 
             return gadget;

Reply via email to