Author: julien(a)jboss.com
Date: 2007-03-07 11:32:02 -0500 (Wed, 07 Mar 2007)
New Revision: 6579
Added:
trunk/widget/src/main/org/jboss/portal/widget/google/GGPreferenceInfo.java
trunk/widget/src/main/org/jboss/portal/widget/google/GGPreferencesInfo.java
trunk/widget/src/main/org/jboss/portal/widget/google/GGWidget.java
trunk/widget/src/main/org/jboss/portal/widget/google/GGWidgetFactory.java
trunk/widget/src/main/org/jboss/portal/widget/google/GGWidgetProvider.java
trunk/widget/src/main/org/jboss/portal/widget/google/GGWidgetRetrieval.java
Removed:
trunk/widget/src/main/org/jboss/portal/widget/google/GWidget.java
trunk/widget/src/main/org/jboss/portal/widget/google/GWidgetFactory.java
trunk/widget/src/main/org/jboss/portal/widget/google/GWidgetPreferenceInfo.java
trunk/widget/src/main/org/jboss/portal/widget/google/GWidgetPreferencesInfo.java
trunk/widget/src/main/org/jboss/portal/widget/google/GWidgetProvider.java
trunk/widget/src/main/org/jboss/portal/widget/google/GWidgetRetrieval.java
Modified:
trunk/widget/src/main/org/jboss/portal/widget/WidgetPortlet.java
Log:
renamed GW* to GG* standing for Google Gadget
Modified: trunk/widget/src/main/org/jboss/portal/widget/WidgetPortlet.java
===================================================================
--- trunk/widget/src/main/org/jboss/portal/widget/WidgetPortlet.java 2007-03-07 16:29:26
UTC (rev 6578)
+++ trunk/widget/src/main/org/jboss/portal/widget/WidgetPortlet.java 2007-03-07 16:32:02
UTC (rev 6579)
@@ -22,9 +22,9 @@
******************************************************************************/
package org.jboss.portal.widget;
-import org.jboss.portal.widget.google.GWidgetProvider;
-import org.jboss.portal.widget.google.GWidget;
-import org.jboss.portal.widget.google.GWidgetPreferenceInfo;
+import org.jboss.portal.widget.google.GGWidgetProvider;
+import org.jboss.portal.widget.google.GGWidget;
+import org.jboss.portal.widget.google.GGPreferenceInfo;
import org.jboss.portal.widget.google.type.DataType;
import javax.portlet.GenericPortlet;
@@ -55,7 +55,7 @@
private static final PortletMode EDIT_CONTENT_MODE = new
PortletMode("edit_content");
/** . */
- private GWidgetProvider provider = new GWidgetProvider();
+ private GGWidgetProvider provider = new GGWidgetProvider();
public void init() throws PortletException
{
@@ -95,7 +95,7 @@
if ("select".equals(op))
{
String uri = req.getParameter("uri");
- GWidget widget = (GWidget)provider.getWidget(uri);
+ GGWidget widget = (GGWidget)provider.getWidget(uri);
if (widget != null)
{
PortletURL selectURL = resp.createActionURL();
@@ -108,7 +108,7 @@
writer.println("<from>");
for (Iterator i =
widget.getPreferencesInfo().getPreferencesInfo().iterator();i.hasNext();)
{
- GWidgetPreferenceInfo prefInfo = (GWidgetPreferenceInfo)i.next();
+ GGPreferenceInfo prefInfo = (GGPreferenceInfo)i.next();
switch (prefInfo.getType().getOrdinal())
{
case DataType.ENUM:
Copied: trunk/widget/src/main/org/jboss/portal/widget/google/GGPreferenceInfo.java (from
rev 6575,
trunk/widget/src/main/org/jboss/portal/widget/google/GWidgetPreferenceInfo.java)
===================================================================
--- trunk/widget/src/main/org/jboss/portal/widget/google/GGPreferenceInfo.java
(rev 0)
+++ trunk/widget/src/main/org/jboss/portal/widget/google/GGPreferenceInfo.java 2007-03-07
16:32:02 UTC (rev 6579)
@@ -0,0 +1,82 @@
+/******************************************************************************
+ * JBoss, a division of Red Hat *
+ * Copyright 2006, Red Hat Middleware, LLC, and individual *
+ * contributors as indicated by the @authors tag. See the *
+ * copyright.txt in the distribution for a full listing of *
+ * individual contributors. *
+ * *
+ * 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.jboss.portal.widget.google;
+
+import org.jboss.portal.widget.google.type.DataType;
+
+/**
+ * @author <a href="mailto:julien@jboss.org">Julien Viet</a>
+ * @version $Revision: 1.1 $
+ */
+public class GGPreferenceInfo
+{
+
+ /** . */
+ private final String name;
+
+ /** . */
+ private final DataType type;
+
+ /** . */
+ private final String displayName;
+
+ /** . */
+ private final boolean required;
+
+ /** . */
+ private final String defaultValue;
+
+ protected GGPreferenceInfo(String name, DataType type, String displayName, boolean
required, String defaultValue)
+ {
+ this.name = name;
+ this.type = type;
+ this.displayName = displayName;
+ this.required = required;
+ this.defaultValue = defaultValue;
+ }
+
+ public String getName()
+ {
+ return name;
+ }
+
+ public DataType getType()
+ {
+ return type;
+ }
+
+ public String getDefaultValue()
+ {
+ return defaultValue;
+ }
+
+ public String getDisplayName()
+ {
+ return displayName;
+ }
+
+ public boolean isRequired()
+ {
+ return required;
+ }
+}
Copied: trunk/widget/src/main/org/jboss/portal/widget/google/GGPreferencesInfo.java (from
rev 6575,
trunk/widget/src/main/org/jboss/portal/widget/google/GWidgetPreferencesInfo.java)
===================================================================
--- trunk/widget/src/main/org/jboss/portal/widget/google/GGPreferencesInfo.java
(rev 0)
+++ trunk/widget/src/main/org/jboss/portal/widget/google/GGPreferencesInfo.java 2007-03-07
16:32:02 UTC (rev 6579)
@@ -0,0 +1,75 @@
+/******************************************************************************
+ * JBoss, a division of Red Hat *
+ * Copyright 2006, Red Hat Middleware, LLC, and individual *
+ * contributors as indicated by the @authors tag. See the *
+ * copyright.txt in the distribution for a full listing of *
+ * individual contributors. *
+ * *
+ * 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.jboss.portal.widget.google;
+
+import java.util.Map;
+import java.util.Collection;
+import java.util.Iterator;
+import java.util.HashMap;
+import java.util.Collections;
+
+/**
+ * @author <a href="mailto:julien@jboss.org">Julien Viet</a>
+ * @version $Revision: 1.1 $
+ */
+public class GGPreferencesInfo
+{
+
+ public static final GGPreferencesInfo EMPTY_PREFS = new GGPreferencesInfo();
+
+ /** . */
+ private final Map content;
+
+ public GGPreferencesInfo(Collection prefInfos)
+ {
+ if (prefInfos == null)
+ {
+ throw new IllegalArgumentException();
+ }
+ content = new HashMap(prefInfos.size());
+ for (Iterator i = prefInfos.iterator();i.hasNext();)
+ {
+ GGPreferenceInfo prefInfo = (GGPreferenceInfo)i.next();
+ content.put(prefInfo.getName(), prefInfo);
+ }
+ }
+
+ private GGPreferencesInfo()
+ {
+ content = null;
+ }
+
+ public Collection getPreferencesInfo()
+ {
+ return content == null ? Collections.EMPTY_SET : content.values();
+ }
+
+ public GGPreferenceInfo getPreferenceInfo(String name)
+ {
+ if (name == null)
+ {
+ throw new IllegalArgumentException();
+ }
+ return content == null ? null : (GGPreferenceInfo)content.get(name);
+ }
+}
Copied: trunk/widget/src/main/org/jboss/portal/widget/google/GGWidget.java (from rev 6575,
trunk/widget/src/main/org/jboss/portal/widget/google/GWidget.java)
===================================================================
--- trunk/widget/src/main/org/jboss/portal/widget/google/GGWidget.java
(rev 0)
+++ trunk/widget/src/main/org/jboss/portal/widget/google/GGWidget.java 2007-03-07 16:32:02
UTC (rev 6579)
@@ -0,0 +1,181 @@
+/******************************************************************************
+ * JBoss, a division of Red Hat *
+ * Copyright 2006, Red Hat Middleware, LLC, and individual *
+ * contributors as indicated by the @authors tag. See the *
+ * copyright.txt in the distribution for a full listing of *
+ * individual contributors. *
+ * *
+ * 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.jboss.portal.widget.google;
+
+import org.jboss.portal.common.util.LocalizedString;
+import org.jboss.portal.common.util.UUIDGenerator;
+import org.jboss.portal.widget.Widget;
+
+import java.util.Iterator;
+
+/**
+ * Getting list :
+ *
http://www.google.com/ig/directory?synd=open
+ *
+ * synd=open -> mandatory to get the embedable widgets
+ * &source=gghp ????
+ *
+ * &start= default=0 [->sa=N ??? ]
+ * &num= default=24
+ * &cat= default=all
(popular|news|tools|communication|funandgames|finance|sports|lifestyle|technology|new)
+ *
+ * <Module>
+ * <ModulePrefs
+ * title=""
+ * author=""
+ * author_email=""
+ * author_affiliation=""
+ * author_location=""
+ * screenshot=""
+ * thumbnail=""
+ * category=""
+ * description=""
+ * render_inline="optional"
+ * height=""
+ * scaling="true|false"
+ * scrolling="true|false">
+ * <UserPref
+ * name=""
+ * display_name=""
+ * default_value=""/>
+ *
+ * Getting javascript :
+ *
+ *
http://gmodules.com/ig/ifr?url=
+ * up_color=blue
+ * synd=open
+ * w=554
+ * h=205
+ * title=Date+%26+Time
+ * border=%23ffffff%7C3px%2C1px+solid+%23999999
+ * output=js
+ * +
+ * up_ + user pref name = value
+ *
+ * @author <a href="mailto:julien@jboss.org">Julien Viet</a>
+ * @version $Revision: 1.1 $
+ */
+public class GGWidget implements Widget
+{
+
+ /** . */
+ private static final UUIDGenerator generator = new UUIDGenerator();
+
+ /** . */
+ private final String url;
+
+ /** . */
+ private final GGPreferencesInfo prefsInfo;
+
+ /** . */
+ private final LocalizedString title;
+
+ /** . */
+ private final LocalizedString description;
+
+ /** . */
+ private final int width;
+
+ /** . */
+ private final int height;
+
+ public GGWidget(String url,
+ GGPreferencesInfo prefsInfo,
+ LocalizedString title,
+ LocalizedString description,
+ int width,
+ int height)
+ {
+ this.url = url;
+ this.prefsInfo = prefsInfo;
+ this.title = title;
+ this.description = description;
+ this.width = width;
+ this.height = height;
+ }
+
+ public String getId()
+ {
+ return url;
+ }
+
+ public GGPreferencesInfo getPreferencesInfo()
+ {
+ return prefsInfo;
+ }
+
+ public LocalizedString getTitle()
+ {
+ return title;
+ }
+
+ public LocalizedString getDescription()
+ {
+ return description;
+ }
+
+ public String render()
+ {
+ String id = generator.generateKey();
+ StringBuffer tmp = new
StringBuffer("http://gmodules.com/ig/ifr?url=").append(url);
+
+ // Generate base params
+ tmp.append("&synd=open");
+ tmp.append("&w=").append(width);
+ tmp.append("&h=").append(height);
+ tmp.append("&title=").append(title.getDefaultString());
+ tmp.append("&border=%23ffffff%7C3px%2C1px+none+%23999999"); //
"&border=%23ffffff%7C3px%2C1px+solid+%23999999"
+ tmp.append("&output=js");
+
+ // Append default values
+ for (Iterator i = prefsInfo.getPreferencesInfo().iterator();i.hasNext();)
+ {
+ GGPreferenceInfo prefInfo = (GGPreferenceInfo)i.next();
+ if (prefInfo.getDefaultValue() != null)
+ {
+
tmp.append("&_up").append(prefInfo.getName()).append("=").append(prefInfo.getDefaultValue());
+ }
+ }
+
+ //
+ String clipper = "" +
+ "(function(){\n" +
+ "var a = document.getElementById('" + id + "');\n"
+
+ "var b = a.childNodes.item(2);\n" + // table
+ "var c = b.childNodes.item(0);\n" + // tbody
+ "var d = c.childNodes.item(0);\n" + // tr
+ "var e = c.childNodes.item(1);\n" + // tr
+ "var f = c.childNodes.item(3);\n" + // tr
+ "c.removeChild(d);\n" +
+ "c.removeChild(e);\n" +
+ "c.removeChild(f);\n" +
+ "})();";
+
+ return "<div id=\"" + id + "\">\n" +
+ "<script src=\"" + tmp +
"\"></script>\n" +
+ "<script>" + clipper + "</script>\n" +
+ "</div>\n";
+ }
+
+
+}
Copied: trunk/widget/src/main/org/jboss/portal/widget/google/GGWidgetFactory.java (from
rev 6576, trunk/widget/src/main/org/jboss/portal/widget/google/GWidgetFactory.java)
===================================================================
--- trunk/widget/src/main/org/jboss/portal/widget/google/GGWidgetFactory.java
(rev 0)
+++ trunk/widget/src/main/org/jboss/portal/widget/google/GGWidgetFactory.java 2007-03-07
16:32:02 UTC (rev 6579)
@@ -0,0 +1,176 @@
+/******************************************************************************
+ * JBoss, a division of Red Hat *
+ * Copyright 2006, Red Hat Middleware, LLC, and individual *
+ * contributors as indicated by the @authors tag. See the *
+ * copyright.txt in the distribution for a full listing of *
+ * individual contributors. *
+ * *
+ * 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.jboss.portal.widget.google;
+
+import org.jboss.portal.common.util.XML;
+import org.jboss.portal.common.util.LocalizedString;
+import org.jboss.portal.widget.google.type.DataType;
+import org.jboss.portal.widget.google.type.StringType;
+import org.jboss.portal.widget.google.type.LocationType;
+import org.jboss.portal.widget.google.type.HiddenType;
+import org.jboss.portal.widget.google.type.BoolType;
+import org.jboss.portal.widget.google.type.ListType;
+import org.jboss.portal.widget.google.type.EnumType;
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+
+import javax.xml.parsers.DocumentBuilderFactory;
+import javax.xml.parsers.DocumentBuilder;
+import java.net.URL;
+import java.util.Iterator;
+import java.util.Locale;
+import java.util.Collection;
+import java.util.ArrayList;
+
+/**
+ * @author <a href="mailto:julien@jboss.org">Julien Viet</a>
+ * @version $Revision: 1.1 $
+ */
+public class GGWidgetFactory
+{
+
+ /** . */
+ private final URL url;
+
+ public GGWidgetFactory(URL url)
+ {
+ if (url == null)
+ {
+ throw new IllegalArgumentException("No null URL");
+ }
+ this.url = url;
+ }
+
+ public URL getURL()
+ {
+ return url;
+ }
+
+ public GGWidget create() throws Exception
+ {
+ DocumentBuilderFactory factory = XML.getDocumentBuilderFactory();
+ DocumentBuilder builder = factory.newDocumentBuilder();
+ Document doc = builder.parse(url.toString());
+ Element moduleElt = doc.getDocumentElement();
+
+ //
+ Collection tmp = null;
+ Iterator userPrefsEltIterator = XML.getChildrenIterator(moduleElt,
"UserPref");
+ while (userPrefsEltIterator.hasNext())
+ {
+ Element userPref = (Element)userPrefsEltIterator.next();
+ String nameAttr = userPref.getAttribute("name");
+ String displayNameAttr = userPref.getAttribute("displayName");
+ String urlParamAttr = userPref.getAttribute("urlparam");
+ String dataTypeAttr = userPref.getAttribute("datatype");
+ String requiredAttr = userPref.getAttribute("required");
+ String defaultValueAttr = userPref.getAttribute("default_value");
+
+ // We don't support that for now
+ if (urlParamAttr.length() > 0)
+ {
+ throw new Exception();
+ }
+
+ // String is default type when not specified
+ DataType dataType = StringType.getInstance();
+ if (dataTypeAttr.length() > 0)
+ {
+ int dataTypeOrdinal = DataType.parseDataTypeLiteral(dataTypeAttr);
+ switch(dataTypeOrdinal)
+ {
+ case DataType.HIDDEN:
+ dataType = HiddenType.getInstance();
+ break;
+ case DataType.BOOL:
+ dataType = BoolType.getInstance();
+ break;
+ case DataType.STRING:
+ dataType = StringType.getInstance();
+ break;
+ case DataType.LIST:
+ dataType = ListType.getInstance();
+ break;
+ case DataType.ENUM:
+ Collection values = new ArrayList();
+ for (Iterator i = XML.getChildrenIterator(userPref,
"EnumValue");i.hasNext();)
+ {
+ Element enumValueElt = (Element)i.next();
+ String valueAttr = enumValueElt.getAttribute("value");
+ String displayValueAttr =
enumValueElt.getAttribute("display_value");
+ EnumType.Value value = new EnumType.Value(valueAttr,
displayValueAttr.length() > 0 ? displayValueAttr : null);
+ values.add(value);
+ }
+ dataType = new EnumType(values);
+ break;
+ case DataType.LOCATION:
+ dataType = LocationType.getInstance();
+ break;
+ }
+ }
+
+ //
+ GGPreferenceInfo prefInfo = new GGPreferenceInfo(
+ nameAttr,
+ dataType,
+ displayNameAttr.length() > 0 ? displayNameAttr : null,
+ requiredAttr.length() > 0 ? Boolean.valueOf(requiredAttr).booleanValue() :
false,
+ defaultValueAttr.length() > 0 ? defaultValueAttr : null
+ );
+
+ //
+ if (tmp == null)
+ {
+ tmp = new ArrayList();
+ }
+ tmp.add(prefInfo);
+ }
+ GGPreferencesInfo prefsInfo = GGPreferencesInfo.EMPTY_PREFS;
+ if (tmp != null)
+ {
+ prefsInfo = new GGPreferencesInfo(tmp);
+ }
+
+ //
+ Iterator modulePrefsEltIterator = XML.getChildrenIterator(moduleElt,
"ModulePrefs");
+ if (modulePrefsEltIterator.hasNext())
+ {
+ Element modulePrefsElt = (Element)modulePrefsEltIterator.next();
+ String titleAttr = modulePrefsElt.getAttribute("title");
+ String descriptionAttr = modulePrefsElt.getAttribute("description");
+ String widthAttr = modulePrefsElt.getAttribute("width");
+ String heightAttr = modulePrefsElt.getAttribute("height");
+
+ //
+ LocalizedString title = titleAttr != null ? new LocalizedString(titleAttr,
Locale.ENGLISH) : null;
+ LocalizedString description = descriptionAttr != null ? new
LocalizedString(descriptionAttr, Locale.ENGLISH) : null;
+ int width = (widthAttr != null && widthAttr.length() > 0) ?
Integer.parseInt(widthAttr) : 320;
+ int height = (heightAttr != null && heightAttr.length() > 0) ?
Integer.parseInt(heightAttr) : 200;
+ return new GGWidget(url.toString(), prefsInfo, title, description, width,
height);
+ }
+ else
+ {
+ throw new Exception(); // Basic for now
+ }
+ }
+}
Copied: trunk/widget/src/main/org/jboss/portal/widget/google/GGWidgetProvider.java (from
rev 6575, trunk/widget/src/main/org/jboss/portal/widget/google/GWidgetProvider.java)
===================================================================
--- trunk/widget/src/main/org/jboss/portal/widget/google/GGWidgetProvider.java
(rev 0)
+++ trunk/widget/src/main/org/jboss/portal/widget/google/GGWidgetProvider.java 2007-03-07
16:32:02 UTC (rev 6579)
@@ -0,0 +1,275 @@
+/******************************************************************************
+ * JBoss, a division of Red Hat *
+ * Copyright 2006, Red Hat Middleware, LLC, and individual *
+ * contributors as indicated by the @authors tag. See the *
+ * copyright.txt in the distribution for a full listing of *
+ * individual contributors. *
+ * *
+ * 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.jboss.portal.widget.google;
+
+import java.net.URL;
+import java.net.MalformedURLException;
+import java.io.InputStreamReader;
+import java.io.InputStream;
+import java.io.ByteArrayInputStream;
+import java.util.StringTokenizer;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Iterator;
+import java.util.LinkedHashSet;
+import java.util.LinkedHashMap;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.ArrayList;
+
+import org.jboss.portal.widget.WidgetProvider;
+import org.jboss.portal.widget.Widget;
+import org.jboss.portal.common.util.Tools;
+import org.apache.log4j.Logger;
+
+import javax.swing.text.html.HTML;
+import javax.swing.text.html.HTMLEditorKit;
+import javax.swing.text.MutableAttributeSet;
+
+import edu.emory.mathcs.backport.java.util.concurrent.FutureTask;
+import edu.emory.mathcs.backport.java.util.concurrent.Callable;
+import edu.emory.mathcs.backport.java.util.concurrent.ExecutorService;
+import edu.emory.mathcs.backport.java.util.concurrent.ThreadPoolExecutor;
+import edu.emory.mathcs.backport.java.util.concurrent.TimeUnit;
+import edu.emory.mathcs.backport.java.util.concurrent.LinkedBlockingDeque;
+import edu.emory.mathcs.backport.java.util.concurrent.locks.Lock;
+import edu.emory.mathcs.backport.java.util.concurrent.locks.ReentrantLock;
+
+/**
+ * @author <a href="mailto:julien@jboss.org">Julien Viet</a>
+ * @version $Revision: 1.1 $
+ */
+public class GGWidgetProvider implements WidgetProvider
+{
+
+ /** . */
+ private static final Logger log = Logger.getLogger(GGWidgetProvider.class);
+
+ /** . */
+ private volatile FutureTask future;
+
+ /** . */
+ private final Lock lock = new ReentrantLock();
+
+ /** . */
+ private final ExecutorService executor = new ThreadPoolExecutor(3, 3, 0,
TimeUnit.SECONDS, new LinkedBlockingDeque());
+
+ public void start()
+ {
+ }
+
+ public void stop()
+ {
+ executor.shutdownNow();
+ }
+
+ private Map getMap()
+ {
+ try
+ {
+ lock.lock();
+ try
+ {
+ if (future == null)
+ {
+ future = new FutureTask(new Callable()
+ {
+ public Object call() throws Exception
+ {
+ WidgetBuilder map = new WidgetBuilder();
+ map.start();
+ return map;
+ }
+ });
+ executor.execute(future);
+ }
+ }
+ finally
+ {
+ lock.unlock();
+ }
+ WidgetBuilder wmap = (WidgetBuilder)future.get();
+ return wmap.map;
+ }
+ catch (Exception e)
+ {
+ e.printStackTrace();
+ return Collections.EMPTY_MAP;
+ }
+ }
+
+ public Collection getKeys()
+ {
+ return getMap().keySet();
+ }
+
+ public Widget getWidget(String key)
+ {
+ if (key == null)
+ {
+ throw new IllegalArgumentException();
+ }
+ Map tmp = getMap();
+ GGWidgetRetrieval.List retrievals = (GGWidgetRetrieval.List)tmp.get(key);
+ if (retrievals != null)
+ {
+ GGWidgetRetrieval retrieval = retrievals.getRetrieval(key);
+ if (retrieval.getStatus() == GGWidgetRetrieval.AVAILABLE)
+ {
+ return retrieval.getWidget();
+ }
+ }
+
+ //
+ return null;
+ }
+
+ private class WidgetBuilder
+ {
+
+ /** . */
+ private Map map;
+
+ public void start()
+ {
+ try
+ {
+ // Find widget URLs
+ URL url = new
URL("http://www.google.com/ig/directory?synd=open");
+
+ // Read fully the URL content first
+ byte[] bytes = null;
+ InputStream in = null;
+ try
+ {
+ log.info("Retrieving " + url);
+ in = url.openStream();
+ bytes = Tools.getBytes(in);
+ log.info("Document " + url + " has been retrieved");
+ }
+ finally
+ {
+ Tools.safeClose(in);
+ }
+
+ //
+ final LinkedHashSet urls = new LinkedHashSet();
+ HTMLEditorKit.ParserCallback callback = new HTMLEditorKit.ParserCallback()
+ {
+ public void handleStartTag(HTML.Tag t, MutableAttributeSet attrSet, int
pos)
+ {
+ if (t == HTML.Tag.A)
+ {
+ try
+ {
+ String href = (String)attrSet.getAttribute(HTML.Attribute.HREF);
+ URL url = new URL(href);
+ String query = url.getQuery();
+ if (query != null)
+ {
+ Map params = new HashMap();
+ StringTokenizer st = new StringTokenizer(query,
"&");
+ while (st.hasMoreTokens())
+ {
+ String pair = st.nextToken();
+ int separator = pair.indexOf('=');
+ if (separator != -1)
+ {
+ String left = pair.substring(0, separator);
+ String right = pair.substring(separator + 1);
+ params.put(left, right);
+ }
+ }
+
+ //
+ if (params.containsKey("url"))
+ {
+ String tmp = (String)params.get("url");
+ if (tmp.endsWith(".xml"))
+ {
+ url = new URL(tmp);
+ urls.add(url);
+ }
+ }
+ }
+ }
+ catch (MalformedURLException ignore)
+ {
+ }
+ }
+ }
+ };
+ HTMLEditorKit.Parser parser = new ParserAccessor().getParser();
+ parser.parse(new InputStreamReader(new ByteArrayInputStream(bytes),
"UTF-8"), callback, true);
+
+ //
+ Map map = new LinkedHashMap();
+
+ // Now parse each
+ Collection tmp = new ArrayList(BATCH_SIZE);
+ for (Iterator i = urls.iterator();i.hasNext();)
+ {
+ URL widgetURL = (URL)i.next();
+ GGWidgetFactory widgetFactory = new GGWidgetFactory(widgetURL);
+
+ //
+ if (tmp.size() < BATCH_SIZE)
+ {
+ tmp.add(widgetFactory);
+ }
+ else
+ {
+ GGWidgetRetrieval.List retrievals = new
GGWidgetRetrieval.List(executor, tmp);
+ for (int j = 0;j < retrievals.getSize();j++)
+ {
+ GGWidgetFactory t = retrievals.getFactory(j);
+ map.put(t.getURL().toString(), retrievals);
+ log.info("Added widget for later retrieval" +
t.getURL());
+ }
+
+ //
+ tmp.clear();
+ tmp.add(widgetFactory);
+ }
+ }
+
+ //
+ this.map = Collections.unmodifiableMap(map);
+ }
+ catch (Exception e)
+ {
+ e.printStackTrace();
+ }
+ }
+ }
+
+ private static final int BATCH_SIZE = 6;
+
+ private static class ParserAccessor extends HTMLEditorKit
+ {
+ public Parser getParser()
+ {
+ return super.getParser();
+ }
+ }
+}
Copied: trunk/widget/src/main/org/jboss/portal/widget/google/GGWidgetRetrieval.java (from
rev 6575, trunk/widget/src/main/org/jboss/portal/widget/google/GWidgetRetrieval.java)
===================================================================
--- trunk/widget/src/main/org/jboss/portal/widget/google/GGWidgetRetrieval.java
(rev 0)
+++ trunk/widget/src/main/org/jboss/portal/widget/google/GGWidgetRetrieval.java 2007-03-07
16:32:02 UTC (rev 6579)
@@ -0,0 +1,202 @@
+/******************************************************************************
+ * JBoss, a division of Red Hat *
+ * Copyright 2006, Red Hat Middleware, LLC, and individual *
+ * contributors as indicated by the @authors tag. See the *
+ * copyright.txt in the distribution for a full listing of *
+ * individual contributors. *
+ * *
+ * 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.jboss.portal.widget.google;
+
+import edu.emory.mathcs.backport.java.util.concurrent.FutureTask;
+import edu.emory.mathcs.backport.java.util.concurrent.Callable;
+import edu.emory.mathcs.backport.java.util.concurrent.CompletionService;
+import edu.emory.mathcs.backport.java.util.concurrent.ExecutorCompletionService;
+import edu.emory.mathcs.backport.java.util.concurrent.Future;
+import edu.emory.mathcs.backport.java.util.concurrent.Executor;
+import edu.emory.mathcs.backport.java.util.concurrent.locks.Lock;
+import edu.emory.mathcs.backport.java.util.concurrent.locks.ReentrantLock;
+
+import java.util.Collection;
+
+import org.apache.log4j.Logger;
+
+/**
+ * @author <a href="mailto:julien@jboss.org">Julien Viet</a>
+ * @version $Revision: 1.1 $
+ */
+public class GGWidgetRetrieval
+{
+
+ /** . */
+ private static final Logger log = Logger.getLogger(GGWidgetRetrieval.class);
+
+ /** . */
+ public static int UNAVAILABLE = 0;
+
+ /** . */
+ public static int AVAILABLE = 1;
+
+ /** . */
+ private GGWidget widget;
+
+ public GGWidgetRetrieval(GGWidget widget)
+ {
+ this.widget = widget;
+ }
+
+ public GGWidget getWidget() throws IllegalStateException
+ {
+ if (widget == null)
+ {
+ throw new IllegalStateException("Widget not available");
+ }
+ return widget;
+ }
+
+ public int getStatus()
+ {
+ return widget != null ? AVAILABLE : UNAVAILABLE;
+ }
+
+ public static class List
+ {
+
+ /** . */
+ private final GGWidgetFactory[] factories;
+
+ /** . */
+ private final Executor executor;
+
+ /** . */
+ private final Lock lock = new ReentrantLock();
+
+ /** . */
+ private FutureTask future;
+
+ public List(Executor executor, Collection c)
+ {
+ this.executor = executor;
+ this.factories = (GGWidgetFactory[])c.toArray(new GGWidgetFactory[c.size()]);
+ }
+
+ public int getSize()
+ {
+ return factories.length;
+ }
+
+ public GGWidgetFactory getFactory(int index)
+ {
+ return factories[index];
+ }
+
+ public GGWidgetRetrieval getRetrieval(String key)
+ {
+ for (int i = 0;i < factories.length;i++)
+ {
+ GGWidgetFactory factory = factories[i];
+ if (key.equals(factory.getURL().toString()))
+ {
+ return getRetrieval(i);
+ }
+ }
+ return null;
+ }
+
+ private class Fetcher implements Callable
+ {
+
+ /** . */
+ private final int index;
+
+ /** . */
+ private GGWidgetRetrieval retrieval;
+
+ public Fetcher(int index)
+ {
+ this.index = index;
+ }
+
+ public Object call() throws Exception
+ {
+ GGWidgetFactory factory = factories[index];
+ try
+ {
+ log.info("Creating widget " + factory.getURL());
+ GGWidget widget = factory.create();
+ retrieval = new GGWidgetRetrieval(widget);
+ }
+ catch (Exception e)
+ {
+ log.error("Widget " + factory.getURL() + " was not
retrievable", e);
+ retrieval = new GGWidgetRetrieval(null);
+ }
+ return this;
+ }
+ }
+
+ public GGWidgetRetrieval getRetrieval(int index)
+ {
+ //
+ lock.lock();
+ try
+ {
+ if (future == null)
+ {
+ future = new FutureTask(new Callable()
+ {
+ public Object call() throws Exception
+ {
+ log.info("Creating widget batch");
+ CompletionService completionService = new
ExecutorCompletionService(executor);
+ for (int i = 0; i < factories.length; i++)
+ {
+ completionService.submit(new Fetcher(i));
+ }
+ GGWidgetRetrieval[] retrievals = new
GGWidgetRetrieval[factories.length];
+ for (int i = 0; i < factories.length; i++)
+ {
+ Future future = completionService.take();
+ Fetcher fetcher = (Fetcher)future.get();
+ retrievals[fetcher.index] = fetcher.retrieval;
+ }
+ return retrievals;
+ }
+ });
+ executor.execute(future);
+ }
+ }
+ finally
+ {
+ lock.unlock();
+ }
+
+ try
+ {
+ //
+ GGWidgetRetrieval[] retrievals = (GGWidgetRetrieval[])future.get();
+
+ //
+ return retrievals[index];
+ }
+ catch (Exception e)
+ {
+ return new GGWidgetRetrieval(null);
+ }
+ }
+ }
+}
Deleted: trunk/widget/src/main/org/jboss/portal/widget/google/GWidget.java
===================================================================
--- trunk/widget/src/main/org/jboss/portal/widget/google/GWidget.java 2007-03-07 16:29:26
UTC (rev 6578)
+++ trunk/widget/src/main/org/jboss/portal/widget/google/GWidget.java 2007-03-07 16:32:02
UTC (rev 6579)
@@ -1,181 +0,0 @@
-/******************************************************************************
- * JBoss, a division of Red Hat *
- * Copyright 2006, Red Hat Middleware, LLC, and individual *
- * contributors as indicated by the @authors tag. See the *
- * copyright.txt in the distribution for a full listing of *
- * individual contributors. *
- * *
- * 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.jboss.portal.widget.google;
-
-import org.jboss.portal.common.util.LocalizedString;
-import org.jboss.portal.common.util.UUIDGenerator;
-import org.jboss.portal.widget.Widget;
-
-import java.util.Iterator;
-
-/**
- * Getting list :
- *
http://www.google.com/ig/directory?synd=open
- *
- * synd=open -> mandatory to get the embedable widgets
- * &source=gghp ????
- *
- * &start= default=0 [->sa=N ??? ]
- * &num= default=24
- * &cat= default=all
(popular|news|tools|communication|funandgames|finance|sports|lifestyle|technology|new)
- *
- * <Module>
- * <ModulePrefs
- * title=""
- * author=""
- * author_email=""
- * author_affiliation=""
- * author_location=""
- * screenshot=""
- * thumbnail=""
- * category=""
- * description=""
- * render_inline="optional"
- * height=""
- * scaling="true|false"
- * scrolling="true|false">
- * <UserPref
- * name=""
- * display_name=""
- * default_value=""/>
- *
- * Getting javascript :
- *
- *
http://gmodules.com/ig/ifr?url=
- * up_color=blue
- * synd=open
- * w=554
- * h=205
- * title=Date+%26+Time
- * border=%23ffffff%7C3px%2C1px+solid+%23999999
- * output=js
- * +
- * up_ + user pref name = value
- *
- * @author <a href="mailto:julien@jboss.org">Julien Viet</a>
- * @version $Revision: 1.1 $
- */
-public class GWidget implements Widget
-{
-
- /** . */
- private static final UUIDGenerator generator = new UUIDGenerator();
-
- /** . */
- private final String url;
-
- /** . */
- private final GWidgetPreferencesInfo prefsInfo;
-
- /** . */
- private final LocalizedString title;
-
- /** . */
- private final LocalizedString description;
-
- /** . */
- private final int width;
-
- /** . */
- private final int height;
-
- public GWidget(String url,
- GWidgetPreferencesInfo prefsInfo,
- LocalizedString title,
- LocalizedString description,
- int width,
- int height)
- {
- this.url = url;
- this.prefsInfo = prefsInfo;
- this.title = title;
- this.description = description;
- this.width = width;
- this.height = height;
- }
-
- public String getId()
- {
- return url;
- }
-
- public GWidgetPreferencesInfo getPreferencesInfo()
- {
- return prefsInfo;
- }
-
- public LocalizedString getTitle()
- {
- return title;
- }
-
- public LocalizedString getDescription()
- {
- return description;
- }
-
- public String render()
- {
- String id = generator.generateKey();
- StringBuffer tmp = new
StringBuffer("http://gmodules.com/ig/ifr?url=").append(url);
-
- // Generate base params
- tmp.append("&synd=open");
- tmp.append("&w=").append(width);
- tmp.append("&h=").append(height);
- tmp.append("&title=").append(title.getDefaultString());
- tmp.append("&border=%23ffffff%7C3px%2C1px+none+%23999999"); //
"&border=%23ffffff%7C3px%2C1px+solid+%23999999"
- tmp.append("&output=js");
-
- // Append default values
- for (Iterator i = prefsInfo.getPreferencesInfo().iterator();i.hasNext();)
- {
- GWidgetPreferenceInfo prefInfo = (GWidgetPreferenceInfo)i.next();
- if (prefInfo.getDefaultValue() != null)
- {
-
tmp.append("&_up").append(prefInfo.getName()).append("=").append(prefInfo.getDefaultValue());
- }
- }
-
- //
- String clipper = "" +
- "(function(){\n" +
- "var a = document.getElementById('" + id + "');\n"
+
- "var b = a.childNodes.item(2);\n" + // table
- "var c = b.childNodes.item(0);\n" + // tbody
- "var d = c.childNodes.item(0);\n" + // tr
- "var e = c.childNodes.item(1);\n" + // tr
- "var f = c.childNodes.item(3);\n" + // tr
- "c.removeChild(d);\n" +
- "c.removeChild(e);\n" +
- "c.removeChild(f);\n" +
- "})();";
-
- return "<div id=\"" + id + "\">\n" +
- "<script src=\"" + tmp +
"\"></script>\n" +
- "<script>" + clipper + "</script>\n" +
- "</div>\n";
- }
-
-
-}
Deleted: trunk/widget/src/main/org/jboss/portal/widget/google/GWidgetFactory.java
===================================================================
--- trunk/widget/src/main/org/jboss/portal/widget/google/GWidgetFactory.java 2007-03-07
16:29:26 UTC (rev 6578)
+++ trunk/widget/src/main/org/jboss/portal/widget/google/GWidgetFactory.java 2007-03-07
16:32:02 UTC (rev 6579)
@@ -1,176 +0,0 @@
-/******************************************************************************
- * JBoss, a division of Red Hat *
- * Copyright 2006, Red Hat Middleware, LLC, and individual *
- * contributors as indicated by the @authors tag. See the *
- * copyright.txt in the distribution for a full listing of *
- * individual contributors. *
- * *
- * 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.jboss.portal.widget.google;
-
-import org.jboss.portal.common.util.XML;
-import org.jboss.portal.common.util.LocalizedString;
-import org.jboss.portal.widget.google.type.DataType;
-import org.jboss.portal.widget.google.type.StringType;
-import org.jboss.portal.widget.google.type.LocationType;
-import org.jboss.portal.widget.google.type.HiddenType;
-import org.jboss.portal.widget.google.type.BoolType;
-import org.jboss.portal.widget.google.type.ListType;
-import org.jboss.portal.widget.google.type.EnumType;
-import org.w3c.dom.Document;
-import org.w3c.dom.Element;
-
-import javax.xml.parsers.DocumentBuilderFactory;
-import javax.xml.parsers.DocumentBuilder;
-import java.net.URL;
-import java.util.Iterator;
-import java.util.Locale;
-import java.util.Collection;
-import java.util.ArrayList;
-
-/**
- * @author <a href="mailto:julien@jboss.org">Julien Viet</a>
- * @version $Revision: 1.1 $
- */
-public class GWidgetFactory
-{
-
- /** . */
- private final URL url;
-
- public GWidgetFactory(URL url)
- {
- if (url == null)
- {
- throw new IllegalArgumentException("No null URL");
- }
- this.url = url;
- }
-
- public URL getURL()
- {
- return url;
- }
-
- public GWidget create() throws Exception
- {
- DocumentBuilderFactory factory = XML.getDocumentBuilderFactory();
- DocumentBuilder builder = factory.newDocumentBuilder();
- Document doc = builder.parse(url.toString());
- Element moduleElt = doc.getDocumentElement();
-
- //
- Collection tmp = null;
- Iterator userPrefsEltIterator = XML.getChildrenIterator(moduleElt,
"UserPref");
- while (userPrefsEltIterator.hasNext())
- {
- Element userPref = (Element)userPrefsEltIterator.next();
- String nameAttr = userPref.getAttribute("name");
- String displayNameAttr = userPref.getAttribute("displayName");
- String urlParamAttr = userPref.getAttribute("urlparam");
- String dataTypeAttr = userPref.getAttribute("datatype");
- String requiredAttr = userPref.getAttribute("required");
- String defaultValueAttr = userPref.getAttribute("default_value");
-
- // We don't support that for now
- if (urlParamAttr.length() > 0)
- {
- throw new Exception();
- }
-
- // String is default type when not specified
- DataType dataType = StringType.getInstance();
- if (dataTypeAttr.length() > 0)
- {
- int dataTypeOrdinal = DataType.parseDataTypeLiteral(dataTypeAttr);
- switch(dataTypeOrdinal)
- {
- case DataType.HIDDEN:
- dataType = HiddenType.getInstance();
- break;
- case DataType.BOOL:
- dataType = BoolType.getInstance();
- break;
- case DataType.STRING:
- dataType = StringType.getInstance();
- break;
- case DataType.LIST:
- dataType = ListType.getInstance();
- break;
- case DataType.ENUM:
- Collection values = new ArrayList();
- for (Iterator i = XML.getChildrenIterator(userPref,
"EnumValue");i.hasNext();)
- {
- Element enumValueElt = (Element)i.next();
- String valueAttr = enumValueElt.getAttribute("value");
- String displayValueAttr =
enumValueElt.getAttribute("display_value");
- EnumType.Value value = new EnumType.Value(valueAttr,
displayValueAttr.length() > 0 ? displayValueAttr : null);
- values.add(value);
- }
- dataType = new EnumType(values);
- break;
- case DataType.LOCATION:
- dataType = LocationType.getInstance();
- break;
- }
- }
-
- //
- GWidgetPreferenceInfo prefInfo = new GWidgetPreferenceInfo(
- nameAttr,
- dataType,
- displayNameAttr.length() > 0 ? displayNameAttr : null,
- requiredAttr.length() > 0 ? Boolean.valueOf(requiredAttr).booleanValue() :
false,
- defaultValueAttr.length() > 0 ? defaultValueAttr : null
- );
-
- //
- if (tmp == null)
- {
- tmp = new ArrayList();
- }
- tmp.add(prefInfo);
- }
- GWidgetPreferencesInfo prefsInfo = GWidgetPreferencesInfo.EMPTY_PREFS;
- if (tmp != null)
- {
- prefsInfo = new GWidgetPreferencesInfo(tmp);
- }
-
- //
- Iterator modulePrefsEltIterator = XML.getChildrenIterator(moduleElt,
"ModulePrefs");
- if (modulePrefsEltIterator.hasNext())
- {
- Element modulePrefsElt = (Element)modulePrefsEltIterator.next();
- String titleAttr = modulePrefsElt.getAttribute("title");
- String descriptionAttr = modulePrefsElt.getAttribute("description");
- String widthAttr = modulePrefsElt.getAttribute("width");
- String heightAttr = modulePrefsElt.getAttribute("height");
-
- //
- LocalizedString title = titleAttr != null ? new LocalizedString(titleAttr,
Locale.ENGLISH) : null;
- LocalizedString description = descriptionAttr != null ? new
LocalizedString(descriptionAttr, Locale.ENGLISH) : null;
- int width = (widthAttr != null && widthAttr.length() > 0) ?
Integer.parseInt(widthAttr) : 320;
- int height = (heightAttr != null && heightAttr.length() > 0) ?
Integer.parseInt(heightAttr) : 200;
- return new GWidget(url.toString(), prefsInfo, title, description, width,
height);
- }
- else
- {
- throw new Exception(); // Basic for now
- }
- }
-}
Deleted: trunk/widget/src/main/org/jboss/portal/widget/google/GWidgetPreferenceInfo.java
===================================================================
---
trunk/widget/src/main/org/jboss/portal/widget/google/GWidgetPreferenceInfo.java 2007-03-07
16:29:26 UTC (rev 6578)
+++
trunk/widget/src/main/org/jboss/portal/widget/google/GWidgetPreferenceInfo.java 2007-03-07
16:32:02 UTC (rev 6579)
@@ -1,82 +0,0 @@
-/******************************************************************************
- * JBoss, a division of Red Hat *
- * Copyright 2006, Red Hat Middleware, LLC, and individual *
- * contributors as indicated by the @authors tag. See the *
- * copyright.txt in the distribution for a full listing of *
- * individual contributors. *
- * *
- * 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.jboss.portal.widget.google;
-
-import org.jboss.portal.widget.google.type.DataType;
-
-/**
- * @author <a href="mailto:julien@jboss.org">Julien Viet</a>
- * @version $Revision: 1.1 $
- */
-public class GWidgetPreferenceInfo
-{
-
- /** . */
- private final String name;
-
- /** . */
- private final DataType type;
-
- /** . */
- private final String displayName;
-
- /** . */
- private final boolean required;
-
- /** . */
- private final String defaultValue;
-
- protected GWidgetPreferenceInfo(String name, DataType type, String displayName,
boolean required, String defaultValue)
- {
- this.name = name;
- this.type = type;
- this.displayName = displayName;
- this.required = required;
- this.defaultValue = defaultValue;
- }
-
- public String getName()
- {
- return name;
- }
-
- public DataType getType()
- {
- return type;
- }
-
- public String getDefaultValue()
- {
- return defaultValue;
- }
-
- public String getDisplayName()
- {
- return displayName;
- }
-
- public boolean isRequired()
- {
- return required;
- }
-}
Deleted: trunk/widget/src/main/org/jboss/portal/widget/google/GWidgetPreferencesInfo.java
===================================================================
---
trunk/widget/src/main/org/jboss/portal/widget/google/GWidgetPreferencesInfo.java 2007-03-07
16:29:26 UTC (rev 6578)
+++
trunk/widget/src/main/org/jboss/portal/widget/google/GWidgetPreferencesInfo.java 2007-03-07
16:32:02 UTC (rev 6579)
@@ -1,75 +0,0 @@
-/******************************************************************************
- * JBoss, a division of Red Hat *
- * Copyright 2006, Red Hat Middleware, LLC, and individual *
- * contributors as indicated by the @authors tag. See the *
- * copyright.txt in the distribution for a full listing of *
- * individual contributors. *
- * *
- * 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.jboss.portal.widget.google;
-
-import java.util.Map;
-import java.util.Collection;
-import java.util.Iterator;
-import java.util.HashMap;
-import java.util.Collections;
-
-/**
- * @author <a href="mailto:julien@jboss.org">Julien Viet</a>
- * @version $Revision: 1.1 $
- */
-public class GWidgetPreferencesInfo
-{
-
- public static final GWidgetPreferencesInfo EMPTY_PREFS = new
GWidgetPreferencesInfo();
-
- /** . */
- private final Map content;
-
- public GWidgetPreferencesInfo(Collection prefInfos)
- {
- if (prefInfos == null)
- {
- throw new IllegalArgumentException();
- }
- content = new HashMap(prefInfos.size());
- for (Iterator i = prefInfos.iterator();i.hasNext();)
- {
- GWidgetPreferenceInfo prefInfo = (GWidgetPreferenceInfo)i.next();
- content.put(prefInfo.getName(), prefInfo);
- }
- }
-
- private GWidgetPreferencesInfo()
- {
- content = null;
- }
-
- public Collection getPreferencesInfo()
- {
- return content == null ? Collections.EMPTY_SET : content.values();
- }
-
- public GWidgetPreferenceInfo getPreferenceInfo(String name)
- {
- if (name == null)
- {
- throw new IllegalArgumentException();
- }
- return content == null ? null : (GWidgetPreferenceInfo)content.get(name);
- }
-}
Deleted: trunk/widget/src/main/org/jboss/portal/widget/google/GWidgetProvider.java
===================================================================
--- trunk/widget/src/main/org/jboss/portal/widget/google/GWidgetProvider.java 2007-03-07
16:29:26 UTC (rev 6578)
+++ trunk/widget/src/main/org/jboss/portal/widget/google/GWidgetProvider.java 2007-03-07
16:32:02 UTC (rev 6579)
@@ -1,275 +0,0 @@
-/******************************************************************************
- * JBoss, a division of Red Hat *
- * Copyright 2006, Red Hat Middleware, LLC, and individual *
- * contributors as indicated by the @authors tag. See the *
- * copyright.txt in the distribution for a full listing of *
- * individual contributors. *
- * *
- * 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.jboss.portal.widget.google;
-
-import java.net.URL;
-import java.net.MalformedURLException;
-import java.io.InputStreamReader;
-import java.io.InputStream;
-import java.io.ByteArrayInputStream;
-import java.util.StringTokenizer;
-import java.util.HashMap;
-import java.util.Map;
-import java.util.Iterator;
-import java.util.LinkedHashSet;
-import java.util.LinkedHashMap;
-import java.util.Collection;
-import java.util.Collections;
-import java.util.ArrayList;
-
-import org.jboss.portal.widget.WidgetProvider;
-import org.jboss.portal.widget.Widget;
-import org.jboss.portal.common.util.Tools;
-import org.apache.log4j.Logger;
-
-import javax.swing.text.html.HTML;
-import javax.swing.text.html.HTMLEditorKit;
-import javax.swing.text.MutableAttributeSet;
-
-import edu.emory.mathcs.backport.java.util.concurrent.FutureTask;
-import edu.emory.mathcs.backport.java.util.concurrent.Callable;
-import edu.emory.mathcs.backport.java.util.concurrent.ExecutorService;
-import edu.emory.mathcs.backport.java.util.concurrent.ThreadPoolExecutor;
-import edu.emory.mathcs.backport.java.util.concurrent.TimeUnit;
-import edu.emory.mathcs.backport.java.util.concurrent.LinkedBlockingDeque;
-import edu.emory.mathcs.backport.java.util.concurrent.locks.Lock;
-import edu.emory.mathcs.backport.java.util.concurrent.locks.ReentrantLock;
-
-/**
- * @author <a href="mailto:julien@jboss.org">Julien Viet</a>
- * @version $Revision: 1.1 $
- */
-public class GWidgetProvider implements WidgetProvider
-{
-
- /** . */
- private static final Logger log = Logger.getLogger(GWidgetProvider.class);
-
- /** . */
- private volatile FutureTask future;
-
- /** . */
- private final Lock lock = new ReentrantLock();
-
- /** . */
- private final ExecutorService executor = new ThreadPoolExecutor(3, 3, 0,
TimeUnit.SECONDS, new LinkedBlockingDeque());
-
- public void start()
- {
- }
-
- public void stop()
- {
- executor.shutdownNow();
- }
-
- private Map getMap()
- {
- try
- {
- lock.lock();
- try
- {
- if (future == null)
- {
- future = new FutureTask(new Callable()
- {
- public Object call() throws Exception
- {
- WidgetBuilder map = new WidgetBuilder();
- map.start();
- return map;
- }
- });
- executor.execute(future);
- }
- }
- finally
- {
- lock.unlock();
- }
- WidgetBuilder wmap = (WidgetBuilder)future.get();
- return wmap.map;
- }
- catch (Exception e)
- {
- e.printStackTrace();
- return Collections.EMPTY_MAP;
- }
- }
-
- public Collection getKeys()
- {
- return getMap().keySet();
- }
-
- public Widget getWidget(String key)
- {
- if (key == null)
- {
- throw new IllegalArgumentException();
- }
- Map tmp = getMap();
- GWidgetRetrieval.List retrievals = (GWidgetRetrieval.List)tmp.get(key);
- if (retrievals != null)
- {
- GWidgetRetrieval retrieval = retrievals.getRetrieval(key);
- if (retrieval.getStatus() == GWidgetRetrieval.AVAILABLE)
- {
- return retrieval.getWidget();
- }
- }
-
- //
- return null;
- }
-
- private class WidgetBuilder
- {
-
- /** . */
- private Map map;
-
- public void start()
- {
- try
- {
- // Find widget URLs
- URL url = new
URL("http://www.google.com/ig/directory?synd=open");
-
- // Read fully the URL content first
- byte[] bytes = null;
- InputStream in = null;
- try
- {
- log.info("Retrieving " + url);
- in = url.openStream();
- bytes = Tools.getBytes(in);
- log.info("Document " + url + " has been retrieved");
- }
- finally
- {
- Tools.safeClose(in);
- }
-
- //
- final LinkedHashSet urls = new LinkedHashSet();
- HTMLEditorKit.ParserCallback callback = new HTMLEditorKit.ParserCallback()
- {
- public void handleStartTag(HTML.Tag t, MutableAttributeSet attrSet, int
pos)
- {
- if (t == HTML.Tag.A)
- {
- try
- {
- String href = (String)attrSet.getAttribute(HTML.Attribute.HREF);
- URL url = new URL(href);
- String query = url.getQuery();
- if (query != null)
- {
- Map params = new HashMap();
- StringTokenizer st = new StringTokenizer(query,
"&");
- while (st.hasMoreTokens())
- {
- String pair = st.nextToken();
- int separator = pair.indexOf('=');
- if (separator != -1)
- {
- String left = pair.substring(0, separator);
- String right = pair.substring(separator + 1);
- params.put(left, right);
- }
- }
-
- //
- if (params.containsKey("url"))
- {
- String tmp = (String)params.get("url");
- if (tmp.endsWith(".xml"))
- {
- url = new URL(tmp);
- urls.add(url);
- }
- }
- }
- }
- catch (MalformedURLException ignore)
- {
- }
- }
- }
- };
- HTMLEditorKit.Parser parser = new ParserAccessor().getParser();
- parser.parse(new InputStreamReader(new ByteArrayInputStream(bytes),
"UTF-8"), callback, true);
-
- //
- Map map = new LinkedHashMap();
-
- // Now parse each
- Collection tmp = new ArrayList(BATCH_SIZE);
- for (Iterator i = urls.iterator();i.hasNext();)
- {
- URL widgetURL = (URL)i.next();
- GWidgetFactory widgetFactory = new GWidgetFactory(widgetURL);
-
- //
- if (tmp.size() < BATCH_SIZE)
- {
- tmp.add(widgetFactory);
- }
- else
- {
- GWidgetRetrieval.List retrievals = new GWidgetRetrieval.List(executor,
tmp);
- for (int j = 0;j < retrievals.getSize();j++)
- {
- GWidgetFactory t = retrievals.getFactory(j);
- map.put(t.getURL().toString(), retrievals);
- log.info("Added widget for later retrieval" +
t.getURL());
- }
-
- //
- tmp.clear();
- tmp.add(widgetFactory);
- }
- }
-
- //
- this.map = Collections.unmodifiableMap(map);
- }
- catch (Exception e)
- {
- e.printStackTrace();
- }
- }
- }
-
- private static final int BATCH_SIZE = 6;
-
- private static class ParserAccessor extends HTMLEditorKit
- {
- public Parser getParser()
- {
- return super.getParser();
- }
- }
-}
Deleted: trunk/widget/src/main/org/jboss/portal/widget/google/GWidgetRetrieval.java
===================================================================
--- trunk/widget/src/main/org/jboss/portal/widget/google/GWidgetRetrieval.java 2007-03-07
16:29:26 UTC (rev 6578)
+++ trunk/widget/src/main/org/jboss/portal/widget/google/GWidgetRetrieval.java 2007-03-07
16:32:02 UTC (rev 6579)
@@ -1,202 +0,0 @@
-/******************************************************************************
- * JBoss, a division of Red Hat *
- * Copyright 2006, Red Hat Middleware, LLC, and individual *
- * contributors as indicated by the @authors tag. See the *
- * copyright.txt in the distribution for a full listing of *
- * individual contributors. *
- * *
- * 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.jboss.portal.widget.google;
-
-import edu.emory.mathcs.backport.java.util.concurrent.FutureTask;
-import edu.emory.mathcs.backport.java.util.concurrent.Callable;
-import edu.emory.mathcs.backport.java.util.concurrent.CompletionService;
-import edu.emory.mathcs.backport.java.util.concurrent.ExecutorCompletionService;
-import edu.emory.mathcs.backport.java.util.concurrent.Future;
-import edu.emory.mathcs.backport.java.util.concurrent.Executor;
-import edu.emory.mathcs.backport.java.util.concurrent.locks.Lock;
-import edu.emory.mathcs.backport.java.util.concurrent.locks.ReentrantLock;
-
-import java.util.Collection;
-
-import org.apache.log4j.Logger;
-
-/**
- * @author <a href="mailto:julien@jboss.org">Julien Viet</a>
- * @version $Revision: 1.1 $
- */
-public class GWidgetRetrieval
-{
-
- /** . */
- private static final Logger log = Logger.getLogger(GWidgetRetrieval.class);
-
- /** . */
- public static int UNAVAILABLE = 0;
-
- /** . */
- public static int AVAILABLE = 1;
-
- /** . */
- private GWidget widget;
-
- public GWidgetRetrieval(GWidget widget)
- {
- this.widget = widget;
- }
-
- public GWidget getWidget() throws IllegalStateException
- {
- if (widget == null)
- {
- throw new IllegalStateException("Widget not available");
- }
- return widget;
- }
-
- public int getStatus()
- {
- return widget != null ? AVAILABLE : UNAVAILABLE;
- }
-
- public static class List
- {
-
- /** . */
- private final GWidgetFactory[] factories;
-
- /** . */
- private final Executor executor;
-
- /** . */
- private final Lock lock = new ReentrantLock();
-
- /** . */
- private FutureTask future;
-
- public List(Executor executor, Collection c)
- {
- this.executor = executor;
- this.factories = (GWidgetFactory[])c.toArray(new GWidgetFactory[c.size()]);
- }
-
- public int getSize()
- {
- return factories.length;
- }
-
- public GWidgetFactory getFactory(int index)
- {
- return factories[index];
- }
-
- public GWidgetRetrieval getRetrieval(String key)
- {
- for (int i = 0;i < factories.length;i++)
- {
- GWidgetFactory factory = factories[i];
- if (key.equals(factory.getURL().toString()))
- {
- return getRetrieval(i);
- }
- }
- return null;
- }
-
- private class Fetcher implements Callable
- {
-
- /** . */
- private final int index;
-
- /** . */
- private GWidgetRetrieval retrieval;
-
- public Fetcher(int index)
- {
- this.index = index;
- }
-
- public Object call() throws Exception
- {
- GWidgetFactory factory = factories[index];
- try
- {
- log.info("Creating widget " + factory.getURL());
- GWidget widget = factory.create();
- retrieval = new GWidgetRetrieval(widget);
- }
- catch (Exception e)
- {
- log.error("Widget " + factory.getURL() + " was not
retrievable", e);
- retrieval = new GWidgetRetrieval(null);
- }
- return this;
- }
- }
-
- public GWidgetRetrieval getRetrieval(int index)
- {
- //
- lock.lock();
- try
- {
- if (future == null)
- {
- future = new FutureTask(new Callable()
- {
- public Object call() throws Exception
- {
- log.info("Creating widget batch");
- CompletionService completionService = new
ExecutorCompletionService(executor);
- for (int i = 0; i < factories.length; i++)
- {
- completionService.submit(new Fetcher(i));
- }
- GWidgetRetrieval[] retrievals = new
GWidgetRetrieval[factories.length];
- for (int i = 0; i < factories.length; i++)
- {
- Future future = completionService.take();
- Fetcher fetcher = (Fetcher)future.get();
- retrievals[fetcher.index] = fetcher.retrieval;
- }
- return retrievals;
- }
- });
- executor.execute(future);
- }
- }
- finally
- {
- lock.unlock();
- }
-
- try
- {
- //
- GWidgetRetrieval[] retrievals = (GWidgetRetrieval[])future.get();
-
- //
- return retrievals[index];
- }
- catch (Exception e)
- {
- return new GWidgetRetrieval(null);
- }
- }
- }
-}