[gatein-commits] gatein SVN: r5269 - in portal/trunk: component/application-registry/src/main/java/org/exoplatform/application/gadget/impl and 1 other directories.

do-not-reply at jboss.org do-not-reply at jboss.org
Thu Nov 25 06:13:32 EST 2010


Author: julien_viet
Date: 2010-11-25 06:13:32 -0500 (Thu, 25 Nov 2010)
New Revision: 5269

Added:
   portal/trunk/component/application-registry/src/main/java/org/exoplatform/application/gadget/GadgetImporter.java
   portal/trunk/component/application-registry/src/main/java/org/exoplatform/application/gadget/RemoteImporter.java
Removed:
   portal/trunk/component/application-registry/src/main/java/org/exoplatform/application/gadget/LocalImporter.java
Modified:
   portal/trunk/component/application-registry/src/main/java/org/exoplatform/application/gadget/GadgetDeployer.java
   portal/trunk/component/application-registry/src/main/java/org/exoplatform/application/gadget/GadgetRegistryService.java
   portal/trunk/component/application-registry/src/main/java/org/exoplatform/application/gadget/ServletLocalImporter.java
   portal/trunk/component/application-registry/src/main/java/org/exoplatform/application/gadget/impl/GadgetRegistryServiceImpl.java
   portal/trunk/portlet/dashboard/src/main/java/org/exoplatform/gadget/webui/component/UIGadgetPortlet.java
Log:
GTNPORTAL-1692 : Decouple gadget import process and gadget import metadata extraction


Modified: portal/trunk/component/application-registry/src/main/java/org/exoplatform/application/gadget/GadgetDeployer.java
===================================================================
--- portal/trunk/component/application-registry/src/main/java/org/exoplatform/application/gadget/GadgetDeployer.java	2010-11-25 10:45:22 UTC (rev 5268)
+++ portal/trunk/component/application-registry/src/main/java/org/exoplatform/application/gadget/GadgetDeployer.java	2010-11-25 11:13:32 UTC (rev 5269)
@@ -19,8 +19,6 @@
 package org.exoplatform.application.gadget;
 
 import org.exoplatform.application.gadget.impl.GadgetRegistryServiceImpl;
-import org.exoplatform.commons.chromattic.ChromatticLifeCycle;
-import org.exoplatform.commons.chromattic.SessionContext;
 import org.exoplatform.commons.xml.DocumentSource;
 import org.exoplatform.commons.xml.XMLValidator;
 import org.exoplatform.container.ExoContainerContext;
@@ -40,11 +38,10 @@
 import org.w3c.dom.NodeList;
 
 import javax.servlet.ServletContext;
-import javax.xml.parsers.DocumentBuilder;
-import javax.xml.parsers.DocumentBuilderFactory;
-import java.io.InputStream;
 import java.net.MalformedURLException;
 import java.net.URL;
+import java.util.ArrayList;
+import java.util.List;
 
 /**
  * @author <a href="mailto:julien.viet at exoplatform.com">Julien Viet</a>
@@ -115,28 +112,27 @@
 
    private void handle(ServletContext scontext, URL gadgetsURL)
    {
-      ChromatticLifeCycle lifeCycle = gadgetRegistryService.getChromatticLifeCycle();
-      lifeCycle.openContext();
       try
       {
+         List<GadgetImporter> importers = new ArrayList<GadgetImporter>();
          Document docXML = validator.validate(DocumentSource.create(gadgetsURL));
          NodeList nodeList = docXML.getElementsByTagName("gadget");
          for (int i = 0; i < nodeList.getLength(); i++)
          {
             Element gadgetElement = (Element)nodeList.item(i);
             String gadgetName = gadgetElement.getAttribute("name");
-            log.debug("About to import gadget " + gadgetName);
+
+            //
+            log.debug("About to parse gadget " + gadgetName);
             Element pathElt = XMLTools.getUniqueChild(gadgetElement, "path", false);
+            GadgetImporter importer = null;
             if (pathElt != null)
             {
                String path = XMLTools.asString(pathElt, true);
-               ServletLocalImporter importer = new ServletLocalImporter(
+               importer = new ServletLocalImporter(
                   gadgetName,
-                  gadgetRegistryService.getRegistry(),
                   path,
-                  scontext,
-                  true);
-               importer.doImport();
+                  scontext);
             }
             else
             {
@@ -144,24 +140,26 @@
                if (urlElt != null)
                {
                   String url = XMLTools.asString(urlElt, true);
-                  ServletLocalImporter importer = new ServletLocalImporter(
+                  importer = new RemoteImporter(
                      gadgetName,
-                     gadgetRegistryService.getRegistry(),
-                     url, 
-                     scontext,
-                     false);
-                  importer.doImport();
+                     url);
                }
             }
+
+            //
+            if (importer != null)
+            {
+               importers.add(importer);
+               log.debug("Add gadget " + gadgetName + " to gadget imports");
+            }
          }
+
+         // Import everything
+         gadgetRegistryService.deploy(importers);
       }
       catch (Exception e)
       {
          log.error("Could not process gadget file " + gadgetsURL, e);
       }
-      finally
-      {
-         lifeCycle.closeContext(true);
-      }
    }
 }

Copied: portal/trunk/component/application-registry/src/main/java/org/exoplatform/application/gadget/GadgetImporter.java (from rev 5265, portal/trunk/component/application-registry/src/main/java/org/exoplatform/application/gadget/LocalImporter.java)
===================================================================
--- portal/trunk/component/application-registry/src/main/java/org/exoplatform/application/gadget/GadgetImporter.java	                        (rev 0)
+++ portal/trunk/component/application-registry/src/main/java/org/exoplatform/application/gadget/GadgetImporter.java	2010-11-25 11:13:32 UTC (rev 5269)
@@ -0,0 +1,133 @@
+/*
+ * Copyright (C) 2009 eXo Platform SAS.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software 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
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+
+package org.exoplatform.application.gadget;
+
+import org.apache.shindig.common.uri.Uri;
+import org.apache.shindig.gadgets.spec.GadgetSpec;
+import org.apache.shindig.gadgets.spec.ModulePrefs;
+import org.exoplatform.application.gadget.impl.GadgetDefinition;
+import org.gatein.common.logging.Logger;
+import org.gatein.common.logging.LoggerFactory;
+
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+
+/**
+ * @author <a href="mailto:julien.viet at exoplatform.com">Julien Viet</a>
+ * @version $Revision$
+ */
+public abstract class GadgetImporter
+{
+
+   /** . */
+   private static final Logger log = LoggerFactory.getLogger(GadgetImporter.class);
+
+   /** The gadget name as seen by GateIn. */
+   private String gadgetName;
+
+   /** The gadget uri. */
+   private String gadgetURI;
+
+   protected GadgetImporter(
+      String gadgetName,
+      String gadgetURI)
+   {
+      this.gadgetName = gadgetName;
+      this.gadgetURI = gadgetURI;
+   }
+
+   public String getGadgetName()
+   {
+      return gadgetName;
+   }
+
+   public String getGadgetURI()
+   {
+      return gadgetURI;
+   }
+
+   protected abstract byte[] getGadgetBytes(String gadgetURI) throws IOException;
+
+   protected abstract String getGadgetURL(String gadgetURI) throws Exception;
+
+   protected abstract void process(String gadgetURI, GadgetDefinition def) throws Exception;
+
+   private String getGadgetTitle(ModulePrefs prefs, String defaultValue)
+   {
+      String title = prefs.getDirectoryTitle();
+      if (title == null || title.trim().length() < 1)
+      {
+         title = prefs.getTitle();
+      }
+      if (title == null || title.trim().length() < 1)
+      {
+         return defaultValue;
+      }
+      return title;
+   }
+
+   public void doImport(GadgetDefinition def) throws Exception
+   {
+      // Get bytes
+      byte[] bytes = getGadgetBytes(gadgetURI);
+      if (bytes == null)
+      {
+         log.error("Cannot import gadget " + gadgetURI + " because its data could not be found");
+         return;
+      }
+
+      // Get encoding
+      String encoding = EncodingDetector.detect(new ByteArrayInputStream(bytes));
+
+      //
+      String gadget = new String(bytes, encoding);
+
+      //
+      String gadgetURL = getGadgetURL(gadgetURI);
+      GadgetSpec spec = new GadgetSpec(Uri.parse(gadgetURL), gadget);
+      ModulePrefs prefs = spec.getModulePrefs();
+
+
+      //
+      String description = prefs.getDescription();
+      String thumbnail = prefs.getThumbnail().toString();
+      String title = getGadgetTitle(prefs, gadgetName);
+      String referenceURL = prefs.getTitleUrl().toString();
+
+      //
+      log.info("Importing gadget name=" + gadgetName + " description=" + description + " thumbnail=" + thumbnail + " title=" +
+               thumbnail + " title=" + title);
+
+      //
+      def.setDescription(description);
+      def.setThumbnail(thumbnail); // Do something better than that
+      def.setTitle(title);
+      def.setReferenceURL(referenceURL);
+
+      //
+      process(gadgetURI, def);
+   }
+
+   @Override
+   public String toString()
+   {
+      return getClass().getSimpleName() + "[name=" + getGadgetName() + ",path=" + getGadgetURI() + "]";
+   }
+}

Modified: portal/trunk/component/application-registry/src/main/java/org/exoplatform/application/gadget/GadgetRegistryService.java
===================================================================
--- portal/trunk/component/application-registry/src/main/java/org/exoplatform/application/gadget/GadgetRegistryService.java	2010-11-25 10:45:22 UTC (rev 5268)
+++ portal/trunk/component/application-registry/src/main/java/org/exoplatform/application/gadget/GadgetRegistryService.java	2010-11-25 11:13:32 UTC (rev 5269)
@@ -33,6 +33,13 @@
 {
 
    /**
+    * Deploy a set of gadgets.
+    *
+    * @param gadgets the gadgets to deploy
+    */
+   public void deploy(Iterable<GadgetImporter> gadgets);
+
+   /**
     * Return Gadget object retrieved from database by its name.
     * 
     * @param name the name of gadget

Deleted: portal/trunk/component/application-registry/src/main/java/org/exoplatform/application/gadget/LocalImporter.java
===================================================================
--- portal/trunk/component/application-registry/src/main/java/org/exoplatform/application/gadget/LocalImporter.java	2010-11-25 10:45:22 UTC (rev 5268)
+++ portal/trunk/component/application-registry/src/main/java/org/exoplatform/application/gadget/LocalImporter.java	2010-11-25 11:13:32 UTC (rev 5269)
@@ -1,247 +0,0 @@
-/*
- * Copyright (C) 2009 eXo Platform SAS.
- *
- * This is free software; you can redistribute it and/or modify it
- * under the terms of the GNU Lesser General Public License as
- * published by the Free Software Foundation; either version 2.1 of
- * the License, or (at your option) any later version.
- *
- * This software 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
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this software; if not, write to the Free
- * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
- * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
- */
-
-package org.exoplatform.application.gadget;
-
-import org.apache.shindig.common.uri.Uri;
-import org.apache.shindig.gadgets.spec.GadgetSpec;
-import org.apache.shindig.gadgets.spec.ModulePrefs;
-import org.chromattic.ext.ntdef.NTFolder;
-import org.chromattic.ext.ntdef.Resource;
-import org.exoplatform.application.gadget.impl.GadgetDefinition;
-import org.exoplatform.application.gadget.impl.GadgetRegistry;
-import org.exoplatform.application.gadget.impl.LocalGadgetData;
-import org.exoplatform.application.gadget.impl.RemoteGadgetData;
-import org.gatein.common.logging.Logger;
-import org.gatein.common.logging.LoggerFactory;
-import org.gatein.common.net.URLTools;
-
-import java.io.ByteArrayInputStream;
-import java.io.IOException;
-import java.net.URL;
-
-/**
- * @author <a href="mailto:julien.viet at exoplatform.com">Julien Viet</a>
- * @version $Revision$
- */
-public abstract class LocalImporter
-{
-
-   /** . */
-   private static final Logger log = LoggerFactory.getLogger(LocalImporter.class);
-
-   /** The gadget name as seen by GateIn. */
-   private String name;
-
-   /** The gadget registry. */
-   private GadgetRegistry registry;
-
-   /** The gadget path. */
-   private String gadgetPath;
-
-   /** . */
-   private boolean local;
-
-   /** Used temporarily when importing resources. */
-   private NTFolder folder;
-
-   protected LocalImporter(
-      String name,
-      GadgetRegistry registry,
-      String gadgetPath,
-      boolean local)
-   {
-      this.name = name;
-      this.registry = registry;
-      this.gadgetPath = gadgetPath;
-      this.local = local;
-   }
-
-   private byte[] getGadgetBytes() throws IOException
-   {
-      if (local)
-      {
-         return getContent(gadgetPath);
-      }
-      else
-      {
-         URL url = new URL(gadgetPath);
-         return URLTools.getContent(url, 5000, 5000);
-      }
-   }
-
-   private String getGadgetURL() throws Exception
-   {
-      if (local)
-      {
-         return "http://www.gatein.org";
-      }
-      else
-      {
-         return gadgetPath;
-      }
-   }
-
-   private String getGadgetTitle(ModulePrefs prefs, String defaultValue)
-   {
-      String title = prefs.getDirectoryTitle();
-      if (title == null || title.trim().length() < 1)
-      {
-         title = prefs.getTitle();
-      }
-      if (title == null || title.trim().length() < 1)
-      {
-         return defaultValue;
-      }
-      return title;
-   }
-
-   public void doImport() throws Exception
-   {
-      if (registry.getGadget(name) != null)
-      {
-         log.debug("Will not import existing gagdet " + name);
-         return;
-      }
-
-      // Get bytes
-      byte[] bytes = getGadgetBytes();
-      if (bytes == null)
-      {
-         log.error("Cannot import gadget " + gadgetPath + " because its data could not be found");
-         return;
-      }
-
-      // Get encoding
-      String encoding = EncodingDetector.detect(new ByteArrayInputStream(bytes));
-
-      //
-      String gadget = new String(bytes, encoding);
-
-      //
-      String gadgetURL = getGadgetURL();
-      GadgetSpec spec = new GadgetSpec(Uri.parse(gadgetURL), gadget);
-      ModulePrefs prefs = spec.getModulePrefs();
-
-      //
-      GadgetDefinition def = registry.addGadget(name);
-
-      //
-      String description = prefs.getDescription();
-      String thumbnail = prefs.getThumbnail().toString();
-      String title = getGadgetTitle(prefs, name);
-      String referenceURL = prefs.getTitleUrl().toString();
-
-      //
-      log.info("Importing gadget name=" + name + " description=" + description + " thumbnail=" + thumbnail + " title=" +
-               thumbnail + " title=" + title);
-
-      //
-      def.setDescription(description);
-      def.setThumbnail(thumbnail); // Do something better than that
-      def.setTitle(title);
-      def.setReferenceURL(referenceURL);
-      def.setLocal(local);
-
-      //
-      if (local)
-      {
-         LocalGadgetData data = (LocalGadgetData)def.getData();
-
-         //
-         String fileName = getName(gadgetPath);
-         data.setFileName(fileName);
-
-         // Import resource
-         folder = data.getResources();
-         String folderPath = getParent(gadgetPath);
-         visitChildren(folderPath);
-         folder = null;
-      }
-      else
-      {
-         RemoteGadgetData data = (RemoteGadgetData)def.getData();
-
-         // Set remote URL
-         data.setURL(gadgetPath);
-      }
-   }
-
-   private void visit(String resourcePath) throws Exception
-   {
-      String name = getName(resourcePath);
-      if (isFile(resourcePath))
-      {
-         byte[] content = getContent(resourcePath);
-
-         //
-         if (content != null)
-         {
-            String mimeType = getMimeType(name);
-
-            //
-            if (mimeType == null)
-            {
-               mimeType = "application/octet-stream";
-            }
-
-            // We can detect encoding for XML files
-            String encoding = null;
-            if ("application/xml".equals(mimeType))
-            {
-               encoding = EncodingDetector.detect(new ByteArrayInputStream(content));
-            }
-
-            // Correct mime type for gadgets
-            if (resourcePath.equals(gadgetPath)) {
-               mimeType = LocalGadgetData.GADGET_MIME_TYPE;
-            }
-
-            //
-            folder.createFile(name, new Resource(mimeType, encoding, content));
-         }
-      }
-      else
-      {
-         folder = folder.createFolder(name);
-         visitChildren(resourcePath);
-         folder = folder.getParent();
-      }
-   }
-
-   private void visitChildren(String folderPath) throws Exception
-   {
-      for (String childPath : getChildren(folderPath))
-      {
-         visit(childPath);
-      }
-   }
-
-   public abstract String getName(String resourcePath) throws IOException;
-
-   public abstract String getParent(String resourcePath) throws IOException;
-
-   public abstract byte[] getContent(String filePath) throws IOException;
-
-   public abstract Iterable<String> getChildren(String folderPath) throws IOException;
-
-   public abstract boolean isFile(String resourcePath) throws IOException;
-
-   public abstract String getMimeType(String fileName);
-}

Added: portal/trunk/component/application-registry/src/main/java/org/exoplatform/application/gadget/RemoteImporter.java
===================================================================
--- portal/trunk/component/application-registry/src/main/java/org/exoplatform/application/gadget/RemoteImporter.java	                        (rev 0)
+++ portal/trunk/component/application-registry/src/main/java/org/exoplatform/application/gadget/RemoteImporter.java	2010-11-25 11:13:32 UTC (rev 5269)
@@ -0,0 +1,65 @@
+/*
+ * Copyright (C) 2010 eXo Platform SAS.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software 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
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+
+package org.exoplatform.application.gadget;
+
+import org.exoplatform.application.gadget.impl.GadgetDefinition;
+import org.exoplatform.application.gadget.impl.RemoteGadgetData;
+import org.gatein.common.net.URLTools;
+
+import java.io.IOException;
+import java.net.URL;
+
+/**
+ * @author <a href="mailto:julien.viet at exoplatform.com">Julien Viet</a>
+ * @version $Revision$
+ */
+public class RemoteImporter extends GadgetImporter
+{
+
+   public RemoteImporter(String name, String gadgetPath)
+   {
+      super(name, gadgetPath);
+   }
+
+   @Override
+   protected byte[] getGadgetBytes(String gadgetURI) throws IOException
+   {
+      URL url = new URL(gadgetURI);
+      return URLTools.getContent(url, 5000, 5000);
+   }
+
+   @Override
+   protected String getGadgetURL(String gadgetURI) throws Exception
+   {
+      return "http://www.gatein.org";
+   }
+
+   @Override
+   protected void process(String gadgetURI, GadgetDefinition def) throws Exception
+   {
+      def.setLocal(false);
+
+      //
+      RemoteGadgetData data = (RemoteGadgetData)def.getData();
+
+      // Set remote URL
+      data.setURL(gadgetURI);
+   }
+}

Modified: portal/trunk/component/application-registry/src/main/java/org/exoplatform/application/gadget/ServletLocalImporter.java
===================================================================
--- portal/trunk/component/application-registry/src/main/java/org/exoplatform/application/gadget/ServletLocalImporter.java	2010-11-25 10:45:22 UTC (rev 5268)
+++ portal/trunk/component/application-registry/src/main/java/org/exoplatform/application/gadget/ServletLocalImporter.java	2010-11-25 11:13:32 UTC (rev 5269)
@@ -18,12 +18,16 @@
  */
 package org.exoplatform.application.gadget;
 
-import org.exoplatform.application.gadget.impl.GadgetRegistry;
+import org.chromattic.ext.ntdef.NTFolder;
+import org.chromattic.ext.ntdef.Resource;
+import org.exoplatform.application.gadget.impl.GadgetDefinition;
+import org.exoplatform.application.gadget.impl.LocalGadgetData;
 import org.gatein.common.io.IOTools;
 import org.gatein.common.logging.Logger;
 import org.gatein.common.logging.LoggerFactory;
 
 import javax.servlet.ServletContext;
+import java.io.ByteArrayInputStream;
 import java.io.IOException;
 import java.io.InputStream;
 import java.util.Set;
@@ -32,7 +36,7 @@
  * @author <a href="mailto:julien.viet at exoplatform.com">Julien Viet</a>
  * @version $Revision$
  */
-public class ServletLocalImporter extends LocalImporter
+public class ServletLocalImporter extends GadgetImporter
 {
 
    /** . */
@@ -41,22 +45,103 @@
    /** . */
    private final ServletContext servletContext;
 
+   /** Used temporarily when importing resources. */
+   private NTFolder folder;
+
    public ServletLocalImporter(
       String name,
-      GadgetRegistry registry,
       String gadgetPath,
-      ServletContext servletContext,
-      boolean local)
+      ServletContext servletContext)
    {
-      super(name, registry, gadgetPath, local);
+      super(name, gadgetPath);
 
       //
       this.servletContext = servletContext;
    }
 
    @Override
-   public String getName(String resourcePath) throws IOException
+   protected byte[] getGadgetBytes(String gadgetURI) throws IOException
    {
+      return getContent(gadgetURI);
+   }
+
+   @Override
+   protected String getGadgetURL(String gadgetURI) throws Exception
+   {
+      return gadgetURI;
+   }
+
+   @Override
+   protected void process(String gadgetURI, GadgetDefinition def) throws Exception
+   {
+      def.setLocal(true);
+
+      //
+      LocalGadgetData data = (LocalGadgetData)def.getData();
+
+      //
+      String fileName = getName(gadgetURI);
+      data.setFileName(fileName);
+
+      // Import resource
+      folder = data.getResources();
+      String folderPath = getParent(gadgetURI);
+      visitChildren(gadgetURI, folderPath);
+      folder = null;
+   }
+
+   private void visit(String uri, String resourcePath) throws Exception
+   {
+      String name = getName(resourcePath);
+      if (isFile(resourcePath))
+      {
+         byte[] content = getContent(resourcePath);
+
+         //
+         if (content != null)
+         {
+            String mimeType = getMimeType(name);
+
+            //
+            if (mimeType == null)
+            {
+               mimeType = "application/octet-stream";
+            }
+
+            // We can detect encoding for XML files
+            String encoding = null;
+            if ("application/xml".equals(mimeType))
+            {
+               encoding = EncodingDetector.detect(new ByteArrayInputStream(content));
+            }
+
+            // Correct mime type for gadgets
+            if (resourcePath.equals(uri)) {
+               mimeType = LocalGadgetData.GADGET_MIME_TYPE;
+            }
+
+            //
+            folder.createFile(name, new Resource(mimeType, encoding, content));
+         }
+      }
+      else
+      {
+         folder = folder.createFolder(name);
+         visitChildren(uri, resourcePath);
+         folder = folder.getParent();
+      }
+   }
+
+   private void visitChildren(String gadgetURI, String folderPath) throws Exception
+   {
+      for (String childPath : getChildren(folderPath))
+      {
+         visit(gadgetURI, childPath);
+      }
+   }
+
+   private String getName(String resourcePath) throws IOException
+   {
       // It's a directory, remove the trailing '/'
       if (resourcePath.endsWith("/"))
       {
@@ -70,8 +155,7 @@
       return resourcePath.substring(index + 1);
    }
 
-   @Override
-   public String getParent(String resourcePath) throws IOException
+   private String getParent(String resourcePath) throws IOException
    {
       // It's a directory, remove the trailing '/'
       if (resourcePath.endsWith("/"))
@@ -86,8 +170,7 @@
       return resourcePath.substring(0, index + 1);
    }
 
-   @Override
-   public byte[] getContent(String filePath) throws IOException
+   private byte[] getContent(String filePath) throws IOException
    {
       InputStream in = servletContext.getResourceAsStream(filePath);
       if (in == null)
@@ -101,21 +184,18 @@
       }
    }
 
-   @Override
-   public Iterable<String> getChildren(String folderPath) throws IOException
+   private Iterable<String> getChildren(String folderPath) throws IOException
    {
-      @SuppressWarnings("unchecked") Set resourcePaths = servletContext.getResourcePaths(folderPath);
+      @SuppressWarnings("unchecked") Set<String> resourcePaths = servletContext.getResourcePaths(folderPath);
       return resourcePaths;
    }
 
-   @Override
-   public boolean isFile(String resourcePath) throws IOException
+   private boolean isFile(String resourcePath) throws IOException
    {
       return !resourcePath.endsWith("/");
    }
 
-   @Override
-   public String getMimeType(String fileName)
+   private String getMimeType(String fileName)
    {
       return servletContext.getMimeType(fileName);
    }

Modified: portal/trunk/component/application-registry/src/main/java/org/exoplatform/application/gadget/impl/GadgetRegistryServiceImpl.java
===================================================================
--- portal/trunk/component/application-registry/src/main/java/org/exoplatform/application/gadget/impl/GadgetRegistryServiceImpl.java	2010-11-25 10:45:22 UTC (rev 5268)
+++ portal/trunk/component/application-registry/src/main/java/org/exoplatform/application/gadget/impl/GadgetRegistryServiceImpl.java	2010-11-25 11:13:32 UTC (rev 5269)
@@ -23,6 +23,7 @@
 import org.chromattic.ext.ntdef.Resource;
 import org.exoplatform.application.gadget.Gadget;
 import org.exoplatform.application.gadget.GadgetRegistryService;
+import org.exoplatform.application.gadget.GadgetImporter;
 import org.exoplatform.application.registry.impl.ApplicationRegistryChromatticLifeCycle;
 import org.exoplatform.commons.chromattic.ChromatticLifeCycle;
 import org.exoplatform.commons.chromattic.ChromatticManager;
@@ -30,11 +31,14 @@
 import org.exoplatform.container.xml.InitParams;
 import org.exoplatform.container.xml.PropertiesParam;
 import org.exoplatform.container.xml.ValueParam;
+import org.gatein.common.logging.Logger;
+import org.gatein.common.logging.LoggerFactory;
 
 import java.util.ArrayList;
 import java.util.Collections;
 import java.util.Comparator;
 import java.util.List;
+import java.util.concurrent.Callable;
 
 /**
  * @author <a href="mailto:julien.viet at exoplatform.com">Julien Viet</a>
@@ -44,6 +48,9 @@
 {
 
    /** . */
+   private final Logger log = LoggerFactory.getLogger(GadgetRegistryServiceImpl.class);
+
+   /** . */
    private static final String DEFAULT_DEVELOPER_GROUP = "/platform/administrators";
 
    /** . */
@@ -120,13 +127,23 @@
       return registry;
    }
 
-   public ChromatticLifeCycle getChromatticLifeCycle()
+   // ***************
+
+   public void deploy(Iterable<GadgetImporter> gadgets)
    {
-      return chromatticLifeCycle;
+      for (GadgetImporter importer : gadgets)
+      {
+         try
+         {
+            new DeployTask(importer).call();
+         }
+         catch (Exception e)
+         {
+            log.error("Could not process gadget file " + importer, e);
+         }
+      }
    }
 
-   // ***************
-
    public Gadget getGadget(String name) throws Exception
    {
       GadgetRegistry registry = getRegistry();
@@ -285,4 +302,40 @@
    {
       return hostName;
    }
+
+   private class DeployTask implements Callable<Boolean>
+   {
+
+      /** . */
+      private final GadgetImporter importer;
+
+      private DeployTask(GadgetImporter importer)
+      {
+         this.importer = importer;
+      }
+
+      public Boolean call() throws Exception
+      {
+         chromatticLifeCycle.openContext();
+         try
+         {
+            boolean done = false;
+            if (getRegistry().getGadget(importer.getGadgetName()) == null)
+            {
+               GadgetDefinition def = getRegistry().addGadget(importer.getGadgetName());
+               importer.doImport(def);
+               done = true;
+            }
+            else
+            {
+               log.debug("Will not import existing gagdet " + importer.getGadgetName());
+            }
+            return done;
+         }
+         finally
+         {
+            chromatticLifeCycle.closeContext(true);
+         }
+      }
+   }
 }

Modified: portal/trunk/portlet/dashboard/src/main/java/org/exoplatform/gadget/webui/component/UIGadgetPortlet.java
===================================================================
--- portal/trunk/portlet/dashboard/src/main/java/org/exoplatform/gadget/webui/component/UIGadgetPortlet.java	2010-11-25 10:45:22 UTC (rev 5268)
+++ portal/trunk/portlet/dashboard/src/main/java/org/exoplatform/gadget/webui/component/UIGadgetPortlet.java	2010-11-25 11:13:32 UTC (rev 5269)
@@ -20,8 +20,8 @@
 package org.exoplatform.gadget.webui.component;
 
 import org.exoplatform.application.gadget.Gadget;
+import org.exoplatform.application.gadget.GadgetImporter;
 import org.exoplatform.application.gadget.GadgetRegistryService;
-import org.exoplatform.application.gadget.LocalImporter;
 import org.exoplatform.container.ExoContainer;
 import org.exoplatform.container.ExoContainerContext;
 import org.exoplatform.portal.webui.application.GadgetUtil;
@@ -50,7 +50,7 @@
 {
    final static public String LOCAL_STRING = "local://";
 
-   private static final Logger log = LoggerFactory.getLogger(LocalImporter.class);
+   private static final Logger log = LoggerFactory.getLogger(GadgetImporter.class);
 
    public UIGadgetPortlet() throws Exception
    {



More information about the gatein-commits mailing list