Author: julien(a)jboss.com
Date: 2007-03-07 10:28:28 -0500 (Wed, 07 Mar 2007)
New Revision: 6574
Added:
trunk/widget/src/main/org/jboss/portal/widget/google/GWidget.java
trunk/widget/src/main/org/jboss/portal/widget/google/GWidgetDataType.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
Removed:
trunk/widget/src/main/org/jboss/portal/widget/google/GoogleWidget.java
trunk/widget/src/main/org/jboss/portal/widget/google/GoogleWidgetFactory.java
trunk/widget/src/main/org/jboss/portal/widget/google/GoogleWidgetProvider.java
trunk/widget/src/main/org/jboss/portal/widget/google/GoogleWidgetRetrieval.java
Modified:
trunk/widget/src/main/org/jboss/portal/widget/WidgetPortlet.java
Log:
early support for widget preferences, for now configure default preferences
Modified: trunk/widget/src/main/org/jboss/portal/widget/WidgetPortlet.java
===================================================================
--- trunk/widget/src/main/org/jboss/portal/widget/WidgetPortlet.java 2007-03-07 14:18:05
UTC (rev 6573)
+++ trunk/widget/src/main/org/jboss/portal/widget/WidgetPortlet.java 2007-03-07 15:28:28
UTC (rev 6574)
@@ -22,7 +22,7 @@
******************************************************************************/
package org.jboss.portal.widget;
-import org.jboss.portal.widget.google.GoogleWidgetProvider;
+import org.jboss.portal.widget.google.GWidgetProvider;
import javax.portlet.GenericPortlet;
import javax.portlet.RenderRequest;
@@ -52,7 +52,7 @@
private static final PortletMode EDIT_CONTENT_MODE = new
PortletMode("edit_content");
/** . */
- private GoogleWidgetProvider provider = new GoogleWidgetProvider();
+ private GWidgetProvider provider = new GWidgetProvider();
public void init() throws PortletException
{
Copied: trunk/widget/src/main/org/jboss/portal/widget/google/GWidget.java (from rev 6573,
trunk/widget/src/main/org/jboss/portal/widget/google/GoogleWidget.java)
===================================================================
--- trunk/widget/src/main/org/jboss/portal/widget/google/GWidget.java
(rev 0)
+++ trunk/widget/src/main/org/jboss/portal/widget/google/GWidget.java 2007-03-07 15:28:28
UTC (rev 6574)
@@ -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 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";
+ }
+
+
+}
Added: trunk/widget/src/main/org/jboss/portal/widget/google/GWidgetDataType.java
===================================================================
--- trunk/widget/src/main/org/jboss/portal/widget/google/GWidgetDataType.java
(rev 0)
+++ trunk/widget/src/main/org/jboss/portal/widget/google/GWidgetDataType.java 2007-03-07
15:28:28 UTC (rev 6574)
@@ -0,0 +1,111 @@
+/******************************************************************************
+ * 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;
+
+/**
+ * @author <a href="mailto:julien@jboss.org">Julien Viet</a>
+ * @version $Revision: 1.1 $
+ */
+public class GWidgetDataType
+{
+
+ /** . */
+ public static final int STRING = 0;
+
+ /** . */
+ public static final int BOOL = 1;
+
+ /** . */
+ public static final int ENUM = 2;
+
+ /** . */
+ public static final int HIDDEN = 3;
+
+ /** . */
+ public static final int LIST = 4;
+
+ /** . */
+ public static final int LOCATION = 5;
+
+ /** . */
+ public static final GWidgetDataType STRING_TYPE = new GWidgetDataType(STRING);
+
+ /** . */
+ private final int value;
+
+ /** . */
+ private final Object object;
+
+ public GWidgetDataType(int ordinal)
+ {
+ this(ordinal, null);
+ }
+
+ public GWidgetDataType(int dataType, Object dataObject)
+ {
+ this.value = dataType;
+ this.object = dataObject;
+ }
+
+ public int getValue()
+ {
+ return value;
+ }
+
+ public Object getObject()
+ {
+ return object;
+ }
+
+ public static int parseDataTypeLiteral(String literal)
+ {
+ if ("string".equals(literal))
+ {
+ return STRING;
+ }
+ else if ("bool".equals(literal))
+ {
+ return BOOL;
+ }
+ else if ("enum".equals(literal))
+ {
+ return ENUM;
+ }
+ else if ("hidden".equals(literal))
+ {
+ return HIDDEN;
+ }
+ else if ("list".equals(literal))
+ {
+ return LIST;
+ }
+ else if ("location".equals(literal))
+ {
+ return LOCATION;
+ }
+ else
+ {
+ throw new IllegalArgumentException("Cannot parse " + literal);
+ }
+ }
+}
Copied: trunk/widget/src/main/org/jboss/portal/widget/google/GWidgetFactory.java (from rev
6573, trunk/widget/src/main/org/jboss/portal/widget/google/GoogleWidgetFactory.java)
===================================================================
--- trunk/widget/src/main/org/jboss/portal/widget/google/GWidgetFactory.java
(rev 0)
+++ trunk/widget/src/main/org/jboss/portal/widget/google/GWidgetFactory.java 2007-03-07
15:28:28 UTC (rev 6574)
@@ -0,0 +1,140 @@
+/******************************************************************************
+ * 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.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 userPrefs = (Element)userPrefsEltIterator.next();
+ String nameAttr = userPrefs.getAttribute("name");
+ String displayNameAttr = userPrefs.getAttribute("displayName");
+ String urlParamAttr = userPrefs.getAttribute("urlparam");
+ String dataTypeAttr = userPrefs.getAttribute("datatype");
+ String requiredAttr = userPrefs.getAttribute("required");
+ String defaultValueAttr = userPrefs.getAttribute("default_value");
+
+ // We don't support that for now
+ if (urlParamAttr.length() > 0)
+ {
+ throw new Exception();
+ }
+
+ // String is default type when not specified
+ GWidgetDataType dataType = GWidgetDataType.STRING_TYPE;
+ if (dataTypeAttr.length() > 0)
+ {
+ int dataTypeValue = GWidgetDataType.parseDataTypeLiteral(dataTypeAttr);
+ dataType = new GWidgetDataType(dataTypeValue);
+ }
+
+ //
+ 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
+ }
+ }
+}
Added: trunk/widget/src/main/org/jboss/portal/widget/google/GWidgetPreferenceInfo.java
===================================================================
--- trunk/widget/src/main/org/jboss/portal/widget/google/GWidgetPreferenceInfo.java
(rev 0)
+++
trunk/widget/src/main/org/jboss/portal/widget/google/GWidgetPreferenceInfo.java 2007-03-07
15:28:28 UTC (rev 6574)
@@ -0,0 +1,80 @@
+/******************************************************************************
+ * 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;
+
+/**
+ * @author <a href="mailto:julien@jboss.org">Julien Viet</a>
+ * @version $Revision: 1.1 $
+ */
+public class GWidgetPreferenceInfo
+{
+
+ /** . */
+ private final String name;
+
+ /** . */
+ private final GWidgetDataType type;
+
+ /** . */
+ private final String displayName;
+
+ /** . */
+ private final boolean required;
+
+ /** . */
+ private final String defaultValue;
+
+ protected GWidgetPreferenceInfo(String name, GWidgetDataType 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 GWidgetDataType getType()
+ {
+ return type;
+ }
+
+ public String getDefaultValue()
+ {
+ return defaultValue;
+ }
+
+ public String getDisplayName()
+ {
+ return displayName;
+ }
+
+ public boolean isRequired()
+ {
+ return required;
+ }
+}
Added: trunk/widget/src/main/org/jboss/portal/widget/google/GWidgetPreferencesInfo.java
===================================================================
--- trunk/widget/src/main/org/jboss/portal/widget/google/GWidgetPreferencesInfo.java
(rev 0)
+++
trunk/widget/src/main/org/jboss/portal/widget/google/GWidgetPreferencesInfo.java 2007-03-07
15:28:28 UTC (rev 6574)
@@ -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 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);
+ }
+}
Copied: trunk/widget/src/main/org/jboss/portal/widget/google/GWidgetProvider.java (from
rev 6573, trunk/widget/src/main/org/jboss/portal/widget/google/GoogleWidgetProvider.java)
===================================================================
--- trunk/widget/src/main/org/jboss/portal/widget/google/GWidgetProvider.java
(rev 0)
+++ trunk/widget/src/main/org/jboss/portal/widget/google/GWidgetProvider.java 2007-03-07
15:28:28 UTC (rev 6574)
@@ -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 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();
+ }
+ }
+}
Copied: trunk/widget/src/main/org/jboss/portal/widget/google/GWidgetRetrieval.java (from
rev 6573,
trunk/widget/src/main/org/jboss/portal/widget/google/GoogleWidgetRetrieval.java)
===================================================================
--- trunk/widget/src/main/org/jboss/portal/widget/google/GWidgetRetrieval.java
(rev 0)
+++ trunk/widget/src/main/org/jboss/portal/widget/google/GWidgetRetrieval.java 2007-03-07
15:28:28 UTC (rev 6574)
@@ -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 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);
+ }
+ }
+ }
+}
Deleted: trunk/widget/src/main/org/jboss/portal/widget/google/GoogleWidget.java
===================================================================
--- trunk/widget/src/main/org/jboss/portal/widget/google/GoogleWidget.java 2007-03-07
14:18:05 UTC (rev 6573)
+++ trunk/widget/src/main/org/jboss/portal/widget/google/GoogleWidget.java 2007-03-07
15:28:28 UTC (rev 6574)
@@ -1,157 +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;
-
-/**
- * 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 GoogleWidget implements Widget
-{
-
- /** . */
- private static final UUIDGenerator generator = new UUIDGenerator();
-
- /** . */
- private final String url;
-
- /** . */
- private final LocalizedString title;
-
- /** . */
- private final LocalizedString description;
-
- /** . */
- private final int width;
-
- /** . */
- private final int height;
-
- public GoogleWidget(String url,
- LocalizedString title,
- LocalizedString description,
- int width,
- int height)
- {
- this.url = url;
- this.title = title;
- this.description = description;
- this.width = width;
- this.height = height;
- }
-
- public String getId()
- {
- return url;
- }
-
- public LocalizedString getTitle()
- {
- return title;
- }
-
- public LocalizedString getDescription()
- {
- return description;
- }
-
- public String render()
- {
- String id = generator.generateKey();
- String tmp = "http://gmodules.com/ig/ifr?url=" + url +
- "&synd=open" +
- "&w=" + width +
- "&h=" + height +
- "&title=" + title.getDefaultString() +
-// "&border=%23ffffff%7C3px%2C1px+solid+%23999999" +
- "&border=%23ffffff%7C3px%2C1px+none+%23999999" +
- "&output=js";
-
- 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/GoogleWidgetFactory.java
===================================================================
---
trunk/widget/src/main/org/jboss/portal/widget/google/GoogleWidgetFactory.java 2007-03-07
14:18:05 UTC (rev 6573)
+++
trunk/widget/src/main/org/jboss/portal/widget/google/GoogleWidgetFactory.java 2007-03-07
15:28:28 UTC (rev 6574)
@@ -1,80 +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.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;
-
-/**
- * @author <a href="mailto:julien@jboss.org">Julien Viet</a>
- * @version $Revision: 1.1 $
- */
-public class GoogleWidgetFactory
-{
-
- /** . */
- private final URL url;
-
- public GoogleWidgetFactory(URL url)
- {
- if (url == null)
- {
- throw new IllegalArgumentException("No null URL");
- }
- this.url = url;
- }
-
- public URL getURL()
- {
- return url;
- }
-
- public GoogleWidget create() throws Exception
- {
- DocumentBuilderFactory factory = XML.getDocumentBuilderFactory();
- DocumentBuilder builder = factory.newDocumentBuilder();
- Document doc = builder.parse(url.toString());
- Element moduleElt = doc.getDocumentElement();
- Iterator modulePrefsEltIterator = XML.getChildrenIterator(moduleElt,
"ModulePrefs");
- 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 GoogleWidget(url.toString(), title, description, width, height);
- }
-}
Deleted: trunk/widget/src/main/org/jboss/portal/widget/google/GoogleWidgetProvider.java
===================================================================
---
trunk/widget/src/main/org/jboss/portal/widget/google/GoogleWidgetProvider.java 2007-03-07
14:18:05 UTC (rev 6573)
+++
trunk/widget/src/main/org/jboss/portal/widget/google/GoogleWidgetProvider.java 2007-03-07
15:28:28 UTC (rev 6574)
@@ -1,276 +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.IOException;
-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 GoogleWidgetProvider implements WidgetProvider
-{
-
- /** . */
- private static final Logger log = Logger.getLogger(GoogleWidgetProvider.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();
- GoogleWidgetRetrieval.List retrievals = (GoogleWidgetRetrieval.List)tmp.get(key);
- if (retrievals != null)
- {
- GoogleWidgetRetrieval retrieval = retrievals.getRetrieval(key);
- if (retrieval.getStatus() == GoogleWidgetRetrieval.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();
- GoogleWidgetFactory widgetFactory = new GoogleWidgetFactory(widgetURL);
-
- //
- if (tmp.size() < BATCH_SIZE)
- {
- tmp.add(widgetFactory);
- }
- else
- {
- GoogleWidgetRetrieval.List retrievals = new
GoogleWidgetRetrieval.List(executor, tmp);
- for (int j = 0;j < retrievals.getSize();j++)
- {
- GoogleWidgetFactory 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/GoogleWidgetRetrieval.java
===================================================================
---
trunk/widget/src/main/org/jboss/portal/widget/google/GoogleWidgetRetrieval.java 2007-03-07
14:18:05 UTC (rev 6573)
+++
trunk/widget/src/main/org/jboss/portal/widget/google/GoogleWidgetRetrieval.java 2007-03-07
15:28:28 UTC (rev 6574)
@@ -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 GoogleWidgetRetrieval
-{
-
- /** . */
- private static final Logger log = Logger.getLogger(GoogleWidgetRetrieval.class);
-
- /** . */
- public static int UNAVAILABLE = 0;
-
- /** . */
- public static int AVAILABLE = 1;
-
- /** . */
- private GoogleWidget widget;
-
- public GoogleWidgetRetrieval(GoogleWidget widget)
- {
- this.widget = widget;
- }
-
- public GoogleWidget 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 GoogleWidgetFactory[] 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 = (GoogleWidgetFactory[])c.toArray(new
GoogleWidgetFactory[c.size()]);
- }
-
- public int getSize()
- {
- return factories.length;
- }
-
- public GoogleWidgetFactory getFactory(int index)
- {
- return factories[index];
- }
-
- public GoogleWidgetRetrieval getRetrieval(String key)
- {
- for (int i = 0;i < factories.length;i++)
- {
- GoogleWidgetFactory factory = factories[i];
- if (key.equals(factory.getURL().toString()))
- {
- return getRetrieval(i);
- }
- }
- return null;
- }
-
- private class Fetcher implements Callable
- {
-
- /** . */
- private final int index;
-
- /** . */
- private GoogleWidgetRetrieval retrieval;
-
- public Fetcher(int index)
- {
- this.index = index;
- }
-
- public Object call() throws Exception
- {
- GoogleWidgetFactory factory = factories[index];
- try
- {
- log.info("Creating widget " + factory.getURL());
- GoogleWidget widget = factory.create();
- retrieval = new GoogleWidgetRetrieval(widget);
- }
- catch (Exception e)
- {
- log.error("Widget " + factory.getURL() + " was not
retrievable", e);
- retrieval = new GoogleWidgetRetrieval(null);
- }
- return this;
- }
- }
-
- public GoogleWidgetRetrieval 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));
- }
- GoogleWidgetRetrieval[] retrievals = new
GoogleWidgetRetrieval[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
- {
- //
- GoogleWidgetRetrieval[] retrievals = (GoogleWidgetRetrieval[])future.get();
-
- //
- return retrievals[index];
- }
- catch (Exception e)
- {
- return new GoogleWidgetRetrieval(null);
- }
- }
- }
-}