You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@labs.apache.org by th...@apache.org on 2007/02/20 04:42:30 UTC
svn commit: r509422 [3/3] - in /labs/droids: ./ conf/ lib/ src/ src/java/
src/java/org/ src/java/org/apache/ src/java/org/apache/droids/
src/java/org/apache/droids/Queue/ src/java/org/apache/droids/api/
src/java/org/apache/droids/conf/ src/java/org/apa...
Added: labs/droids/src/java/org/apache/droids/plugin/PluginManifestParser.java
URL: http://svn.apache.org/viewvc/labs/droids/src/java/org/apache/droids/plugin/PluginManifestParser.java?view=auto&rev=509422
==============================================================================
--- labs/droids/src/java/org/apache/droids/plugin/PluginManifestParser.java (added)
+++ labs/droids/src/java/org/apache/droids/plugin/PluginManifestParser.java Mon Feb 19 19:42:25 2007
@@ -0,0 +1,264 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.droids.plugin;
+
+import java.io.File;
+import java.io.IOException;
+import java.io.UnsupportedEncodingException;
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.net.URLDecoder;
+import java.util.HashMap;
+import java.util.Map;
+
+import javax.xml.stream.XMLInputFactory;
+import javax.xml.stream.XMLStreamConstants;
+import javax.xml.stream.XMLStreamException;
+import javax.xml.stream.XMLStreamReader;
+
+import org.apache.commons.logging.Log;
+import org.apache.droids.conf.Configuration;
+
+public class PluginManifestParser {
+ private static final String VALUE = "value";
+
+ private static final String SCHEMA = "schema";
+
+ private static final String PROVIDER_NAME = "provider-name";
+
+ private static final String VERSION = "version";
+
+ private static final String POINT = "point";
+
+ private static final String EXTENSION_POINT = "extension-point";
+
+ private static final String PARAMETER = "parameter";
+
+ private static final String IMPLEMENTATION = "implementation";
+
+ private static final String EXTENSION = "extension";
+
+ private static final String PLUGIN = "plugin";
+
+ private XMLInputFactory inputFactory = XMLInputFactory.newInstance();
+
+ private static final String ATTR_NAME = "name";
+
+ private static final String ATTR_CLASS = "class";
+
+ private static final String ATTR_ID = "id";
+
+ public static final Log LOG = PluginRepository.LOG;
+
+ private static final boolean WINDOWS = System.getProperty("os.name")
+ .startsWith("Windows");
+
+ private Configuration conf;
+
+ private PluginRepository pluginRepository;
+
+ private ClassLoader classLoader;
+ {
+ classLoader = Thread.currentThread().getContextClassLoader();
+ if (classLoader == null) {
+ classLoader = Configuration.class.getClassLoader();
+ }
+ }
+
+ public PluginManifestParser(Configuration conf,
+ PluginRepository pluginRepository) {
+ this.conf = conf;
+ this.pluginRepository = pluginRepository;
+ }
+
+ /**
+ * Returns a list of all found plugin descriptors.
+ *
+ * @param pluginFolders
+ * folders to search plugins from
+ * @return
+ */
+ public Map<String, PluginDescriptor> parsePluginFolder(String[] pluginFolders) {
+ Map<String, PluginDescriptor> map = new HashMap<String, PluginDescriptor>();
+
+ if (pluginFolders == null) {
+ throw new IllegalArgumentException("plugin.folders is not defined");
+ }
+
+ for (String name : pluginFolders) {
+ File directory = getPluginFolder(name);
+ if (directory == null) {
+ continue;
+ }
+ LOG.info("Plugins: looking in: " + directory.getAbsolutePath());
+ for (File oneSubFolder : directory.listFiles()) {
+ if (oneSubFolder.isDirectory()) {
+ String manifestPath = oneSubFolder.getAbsolutePath()
+ + File.separator + "plugin.xml";
+ LOG.debug("parsing: " + manifestPath);
+ PluginDescriptor p = parseManifestFile("file:///"+manifestPath);
+ map.put(p.getPluginId(), p);
+ }
+ }
+ }
+ return map;
+ }
+
+ private PluginDescriptor parseManifestFile(String manifestPath) {
+ PluginDescriptor manifest=null;
+ try {
+ XMLStreamReader parser = inputFactory.createXMLStreamReader(new URL(
+ manifestPath).openStream());
+ manifest=processManifest(parser,manifestPath);
+
+ } catch (MalformedURLException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ } catch (XMLStreamException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ } catch (IOException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+ // TODO Auto-generated method stub
+ return manifest;
+ }
+
+ private PluginDescriptor processManifest(XMLStreamReader parser, String manifestPath) throws XMLStreamException {
+ String id = "", name = "", version = "", providerName = "", pluginClazz = "";
+ ExtensionBean bean = null;
+ Extension extension=null;
+ PluginDescriptor pluginDescriptor=null;
+ while (true) {
+ int event = parser.next();
+ switch (event) {
+ case XMLStreamConstants.END_DOCUMENT:
+ parser.close();
+ return pluginDescriptor;
+ case XMLStreamConstants.END_ELEMENT:
+ if (parser.getLocalName().equals(IMPLEMENTATION)) {
+ pluginDescriptor.addExtension(extension);
+ }
+ break;
+ case XMLStreamConstants.START_ELEMENT:
+ if (parser.getLocalName().equals(PLUGIN)) {
+ for (int i = 0; i < parser.getAttributeCount(); i++) {
+ String localName = parser.getAttributeLocalName(i);
+ if (localName.equals(ATTR_ID)) {
+ id = parser.getAttributeValue(i);
+ } else if (localName.equals(ATTR_NAME)) {
+ name = parser.getAttributeValue(i);
+ } else if (localName.equals(ATTR_CLASS)) {
+ pluginClazz = parser.getAttributeValue(i);
+ } else if (localName.equals(VERSION)) {
+ version = parser.getAttributeValue(i);
+ } else if (localName.equals(PROVIDER_NAME)) {
+ providerName = parser.getAttributeValue(i);
+ }
+ }
+ pluginDescriptor = new PluginDescriptor(id, version, name,
+ providerName, pluginClazz, manifestPath, this.conf);
+ LOG.info("plugin: id=" + id + " name=" + name + " version=" + version
+ + " provider=" + providerName + "class=" + pluginClazz);
+ }else if (parser.getLocalName().equals(EXTENSION)) {
+ bean = new ExtensionBean();
+ for (int i = 0; i < parser.getAttributeCount(); i++) {
+ final String localName = parser.getAttributeLocalName(i);
+ final String value = parser.getAttributeValue(i);
+ if (localName.equals(ATTR_ID)) {
+ bean.setExtensionId(value) ;
+ } else if (localName.equals(ATTR_NAME)) {
+ bean.setName(value);
+ } else if (localName.equals(POINT)) {
+ bean.setPoint(value);
+ }
+ }
+ }else if (parser.getLocalName().equals(IMPLEMENTATION)) {
+ for (int i = 0; i < parser.getAttributeCount(); i++) {
+ final String localName = parser.getAttributeLocalName(i);
+ final String value = parser.getAttributeValue(i);
+ if (localName.equals(ATTR_ID)) {
+ bean.setImplementationId(value) ;
+ } else if (localName.equals(ATTR_CLASS)) {
+ bean.setImplementationClass(value);
+ }
+ }
+ extension = new Extension(pluginDescriptor, bean.getPoint(), id,
+ bean.getImplementationClass(), this.conf, this.pluginRepository);
+ }else if (parser.getLocalName().equals(PARAMETER)) {
+ String parameterValue ="",parameterName="";
+ for (int i = 0; i < parser.getAttributeCount(); i++) {
+ final String localName = parser.getAttributeLocalName(i);
+ final String value = parser.getAttributeValue(i);
+ if (localName.equals(VALUE)) {
+ parameterValue=value ;
+ } else if (localName.equals(ATTR_NAME)) {
+ parameterName=value;
+ }
+ }
+ extension.addAttribute(parameterName,parameterValue);
+ }else if (parser.getLocalName().equals(EXTENSION_POINT)) {
+ String pointId ="",pointName="",schema="";
+ for (int i = 0; i < parser.getAttributeCount(); i++) {
+ final String localName = parser.getAttributeLocalName(i);
+ final String value = parser.getAttributeValue(i);
+ if (localName.equals(ATTR_ID)) {
+ pointId=value ;
+ } else if (localName.equals(ATTR_NAME)) {
+ pointName=value;
+ } else if (localName.equals(SCHEMA)) {
+ schema=value;
+ }
+ }
+ ExtensionPoint extensionPoint = new ExtensionPoint(pointId, pointName, schema);
+ pluginDescriptor.addExtensionPoint(extensionPoint);
+ }
+ break;
+ }
+ }
+ }
+
+ private File getPluginFolder(String name) {
+ File directory = new File(name);
+ if (!directory.isAbsolute()) {
+ URL url = classLoader.getResource(name);
+ if (url == null && directory.exists() && directory.isDirectory()
+ && directory.listFiles().length > 0) {
+ return directory; // relative path that is not in the
+ // classpath
+ } else if (url == null) {
+ LOG.warn("Plugins: directory not found: " + name);
+ return null;
+ } else if (!"file".equals(url.getProtocol())) {
+ LOG.warn("Plugins: not a file: url. Can't load plugins from: "
+ + url);
+ return null;
+ }
+ String path = url.getPath();
+ if (WINDOWS && path.startsWith("/")) // patch a windows bug
+ path = path.substring(1);
+ try {
+ path = URLDecoder.decode(path, "UTF-8"); // decode the url
+ // path
+ } catch (UnsupportedEncodingException e) {
+ }
+ directory = new File(path);
+ }
+ return directory;
+ }
+}
Propchange: labs/droids/src/java/org/apache/droids/plugin/PluginManifestParser.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: labs/droids/src/java/org/apache/droids/plugin/PluginRepository.java
URL: http://svn.apache.org/viewvc/labs/droids/src/java/org/apache/droids/plugin/PluginRepository.java?view=auto&rev=509422
==============================================================================
--- labs/droids/src/java/org/apache/droids/plugin/PluginRepository.java (added)
+++ labs/droids/src/java/org/apache/droids/plugin/PluginRepository.java Mon Feb 19 19:42:25 2007
@@ -0,0 +1,238 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.droids.plugin;
+
+import java.lang.reflect.Constructor;
+import java.lang.reflect.InvocationTargetException;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.regex.Pattern;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.apache.droids.conf.Configuration;
+import org.apache.droids.exception.PluginRuntimeException;
+
+public class PluginRepository {
+
+ private static final HashMap<Configuration, PluginRepository> CACHE = new HashMap<Configuration, PluginRepository>();
+
+ static final Log LOG = LogFactory.getLog(PluginRepository.class);
+
+ private HashMap<String, Plugin> fActivatedPlugins;
+
+ private HashMap<String, ExtensionPoint> fExtensionPoints;
+
+ private Configuration conf;
+
+ private List<PluginDescriptor> fRegisteredPlugins;
+
+ public PluginRepository(Configuration conf) throws RuntimeException {
+ fActivatedPlugins = new HashMap<String, Plugin>();
+ fExtensionPoints = new HashMap<String, ExtensionPoint>();
+ this.conf = conf;
+ String[] pluginFolders = conf.getStrings("plugin.folders");
+ PluginManifestParser manifestParser = new PluginManifestParser(conf, this);
+ Map<String, PluginDescriptor> allPlugins = manifestParser
+ .parsePluginFolder(pluginFolders);
+ Pattern excludes = Pattern.compile(conf.get("plugin.excludes", ""));
+ Pattern includes = Pattern.compile(conf.get("plugin.includes", ""));
+ Map<String, PluginDescriptor> filteredPlugins = filter(excludes, includes,
+ allPlugins);
+ final ArrayList<PluginDescriptor> arrayList = new ArrayList<PluginDescriptor>(filteredPlugins.values());
+ installExtensionPoints(arrayList);
+ try {
+ installExtensions(arrayList);
+ } catch (PluginRuntimeException e) {
+ throw new RuntimeException(e.getMessage());
+ }
+ displayStatus();
+ }
+
+ private void displayStatus() {
+ LOG.info("Registered Plugins:");
+
+ if ((fRegisteredPlugins == null) || (fRegisteredPlugins.size() == 0)) {
+ LOG.info("\tNONE");
+ } else {
+ for (PluginDescriptor plugin : fRegisteredPlugins) {
+ LOG.info("\t" + plugin.getName() + " (" + plugin.getPluginId() + ")");
+ }
+ }
+
+ LOG.info("Registered Extension-Points:");
+ if ((fExtensionPoints == null) || (fExtensionPoints.size() == 0)) {
+ LOG.info("\tNONE");
+ } else {
+ for (ExtensionPoint ep : fExtensionPoints.values()) {
+ LOG.info("\t" + ep.getName() + " (" + ep.getId() + ")");
+ }
+ }
+ }
+
+ private void installExtensions(List<PluginDescriptor> pRegisteredPlugins) throws PluginRuntimeException {
+ for (PluginDescriptor descriptor : pRegisteredPlugins) {
+ for(Extension extension:descriptor.getExtensions()) {
+ String xpId = extension.getTargetPoint();
+ ExtensionPoint point = getExtensionPoint(xpId);
+ if (point == null) {
+ throw new PluginRuntimeException("Plugin ("
+ + descriptor.getPluginId() + "), " + "extension point: " + xpId
+ + " does not exist.");
+ }
+ point.addExtension(extension);
+ }
+ }
+ }
+
+ private void installExtensionPoints(List<PluginDescriptor> plugins) {
+ if (plugins == null) {
+ return;
+ }
+
+ for (PluginDescriptor plugin: plugins) {
+ ExtensionPoint[] points=plugin.getExtenstionPoints();
+ for (int i = 0; i < points.length; i++) {
+ ExtensionPoint point = points[i];
+ String xpId = point.getId();
+ LOG.debug("Adding extension point " + xpId);
+ fExtensionPoints.put(xpId, point);
+ }
+ /* for(ExtensionPoint point:plugin.getExtenstionPoints()) {
+ String xpId = point.getId();
+ LOG.debug("Adding extension point " + xpId);
+ fExtensionPoints.put(xpId, point);
+ }*/
+ }
+ }
+
+ /**
+ * Filters a list of plugins. The list of plugins is filtered regarding the
+ * configuration properties <code>plugin.excludes</code> and
+ * <code>plugin.includes</code>.
+ *
+ * @param excludes
+ * @param includes
+ * @param plugins
+ * Map of plugins
+ * @return map of plugins matching the configuration
+ */
+ private Map<String, PluginDescriptor> filter(Pattern excludes,
+ Pattern includes, Map<String, PluginDescriptor> plugins) {
+
+ Map<String, PluginDescriptor> map = new HashMap<String, PluginDescriptor>();
+
+ if (plugins == null) {
+ return map;
+ }
+
+ for (PluginDescriptor plugin : plugins.values()) {
+
+ if (plugin == null) {
+ continue;
+ }
+ String id = plugin.getPluginId();
+ if (id == null) {
+ continue;
+ }
+
+ if (!includes.matcher(id).matches()) {
+ LOG.debug("not including: " + id);
+ continue;
+ }
+ if (excludes.matcher(id).matches()) {
+ LOG.debug("excluding: " + id);
+ continue;
+ }
+ map.put(plugin.getPluginId(), plugin);
+ }
+ return map;
+ }
+
+ public static synchronized PluginRepository get(Configuration conf) {
+ PluginRepository result = CACHE.get(conf);
+ if (result == null) {
+ result = new PluginRepository(conf);
+ CACHE.put(conf, result);
+ }
+ return result;
+ }
+
+ public ExtensionPoint getExtensionPoint(String x_point_id) {
+ return this.fExtensionPoints.get(x_point_id);
+ }
+
+ /**
+ * Returns a instance of a plugin. Plugin instances are cached. So a plugin
+ * exist only as one instance. This allow a central management of plugin own
+ * resources.
+ *
+ * After creating the plugin instance the startUp() method is invoked. The
+ * plugin use a own classloader that is used as well by all instance of
+ * extensions of the same plugin. This class loader use all exported
+ * libraries from the dependend plugins and all plugin libraries.
+ *
+ * @param pDescriptor
+ * @return Plugin
+ * @throws PluginRuntimeException
+ */
+ public Plugin getPluginInstance(PluginDescriptor pDescriptor) {
+ if (fActivatedPlugins.containsKey(pDescriptor.getPluginId()))
+ return fActivatedPlugins.get(pDescriptor.getPluginId());
+ try {
+ // Must synchronize here to make sure creation and initialization
+ // of a plugin instance are done by one and only one thread.
+ // The same is in Extension.getExtensionInstance().
+ // Suggested by Stefan Groschupf <sg...@media-style.com>
+ synchronized (pDescriptor) {
+ PluginClassLoader loader = pDescriptor.getClassLoader();
+ Class pluginClass = loader.loadClass(pDescriptor.getPluginClass());
+ Constructor constructor = pluginClass.getConstructor(new Class[] {
+ PluginDescriptor.class, Configuration.class });
+ Plugin plugin = (Plugin) constructor.newInstance(new Object[] {
+ pDescriptor, this.conf });
+ fActivatedPlugins.put(pDescriptor.getPluginId(), plugin);
+ return plugin;
+ }
+ } catch (SecurityException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ } catch (NoSuchMethodException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ } catch (IllegalArgumentException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ } catch (InstantiationException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ } catch (IllegalAccessException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ } catch (InvocationTargetException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ } catch (ClassNotFoundException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+ return null;
+ }
+
+}
Propchange: labs/droids/src/java/org/apache/droids/plugin/PluginRepository.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: labs/droids/src/plugins/build-plugin.xml
URL: http://svn.apache.org/viewvc/labs/droids/src/plugins/build-plugin.xml?view=auto&rev=509422
==============================================================================
--- labs/droids/src/plugins/build-plugin.xml (added)
+++ labs/droids/src/plugins/build-plugin.xml Mon Feb 19 19:42:25 2007
@@ -0,0 +1,212 @@
+<?xml version="1.0"?>
+<!--
+ Licensed to the Apache Software Foundation (ASF) under one or more
+ contributor license agreements. See the NOTICE file distributed with
+ this work for additional information regarding copyright ownership.
+ The ASF licenses this file to You under the Apache License, Version 2.0
+ (the "License"); you may not use this file except in compliance with
+ the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+-->
+<!-- Imported by plugin build.xml files to define default targets. -->
+<project>
+
+ <property name="name" value="${ant.project.name}"/>
+ <property name="root" value="${basedir}"/>
+
+ <!-- load plugin-specific properties first -->
+ <property file="${user.home}/${name}.build.properties" />
+ <property file="${root}/build.properties" />
+
+ <property name="droids.root" location="${root}/../../../"/>
+
+ <property name="src.dir" location="${root}/src/java"/>
+ <property name="src.test" location="${root}/src/test"/>
+
+ <available file="${src.test}" type="dir" property="test.available"/>
+
+ <property name="conf.dir" location="${droids.root}/conf"/>
+
+ <property name="build.dir" location="${droids.root}/build/${name}"/>
+ <property name="build.classes" location="${build.dir}/classes"/>
+ <property name="build.test" location="${build.dir}/test"/>
+
+ <property name="deploy.dir" location="${droids.root}/build/plugins/${name}"/>
+
+ <!-- load droids defaults last so that they can be overridden above -->
+ <property file="${droids.root}/default.properties" />
+
+ <path id="plugin.deps"/>
+
+ <fileset id="lib.jars" dir="${root}" includes="lib/*.jar"/>
+
+ <!-- the normal classpath -->
+ <path id="classpath">
+ <pathelement location="${build.classes}"/>
+ <fileset refid="lib.jars"/>
+ <pathelement location="${droids.root}/build/classes"/>
+ <fileset dir="${droids.root}/lib">
+ <include name="*.jar" />
+ </fileset>
+ <path refid="plugin.deps"/>
+ </path>
+
+ <!-- the unit test classpath -->
+ <path id="test.classpath">
+ <pathelement location="${build.test}" />
+ <pathelement location="${droids.root}/build/test/classes"/>
+ <pathelement location="${droids.root}/src/test"/>
+ <pathelement location="${conf.dir}"/>
+ <pathelement location="${droids.root}/build"/>
+ <path refid="classpath"/>
+ </path>
+
+ <!-- ====================================================== -->
+ <!-- Stuff needed by all targets -->
+ <!-- ====================================================== -->
+ <target name="init">
+ <mkdir dir="${build.dir}"/>
+ <mkdir dir="${build.classes}"/>
+ <mkdir dir="${build.test}"/>
+
+ <antcall target="init-plugin"/>
+ </target>
+
+ <!-- to be overridden by sub-projects -->
+ <target name="init-plugin"/>
+
+ <!--
+ ! Used to build plugin compilation dependencies
+ ! (to be overridden by plugins)
+ !-->
+ <target name="deps-jar"/>
+
+ <!--
+ ! Used to deploy plugin runtime dependencies
+ ! (to be overridden by plugins)
+ !-->
+ <target name="deps-test"/>
+
+ <!-- ====================================================== -->
+ <!-- Compile the Java files -->
+ <!-- ====================================================== -->
+ <target name="compile" depends="init,deps-jar">
+ <echo message="Compiling plugin: ${name}"/>
+ <javac
+ encoding="${build.encoding}"
+ srcdir="${src.dir}"
+ includes="**/*.java"
+ destdir="${build.classes}"
+ debug="${javac.debug}"
+ optimize="${javac.optimize}"
+ target="${javac.version}"
+ source="${javac.version}"
+ deprecation="${javac.deprecation}">
+ <classpath refid="classpath"/>
+ </javac>
+ </target>
+
+ <target name="compile-core">
+ <ant target="compile-core" inheritall="false" dir="${droids.root}"/>
+ <ant target="compile"/>
+ </target>
+
+ <!-- ================================================================== -->
+ <!-- Make plugin .jar -->
+ <!-- ================================================================== -->
+ <!-- -->
+ <!-- ================================================================== -->
+ <target name="jar" depends="compile">
+ <jar
+ jarfile="${build.dir}/${name}.jar"
+ basedir="${build.classes}"
+ />
+ </target>
+
+ <target name="jar-core" depends="compile-core">
+ <jar
+ jarfile="${build.dir}/${name}.jar"
+ basedir="${build.classes}"
+ />
+ </target>
+
+ <!-- ================================================================== -->
+ <!-- Deploy plugin to ${deploy.dir} -->
+ <!-- ================================================================== -->
+ <!-- -->
+ <!-- ================================================================== -->
+ <target name="deploy" depends="jar, deps-test">
+ <mkdir dir="${deploy.dir}"/>
+ <copy file="plugin.xml" todir="${deploy.dir}"
+ preservelastmodified="true"/>
+ <available property="lib-available"
+ file="${build.dir}/${name}.jar"/>
+ <antcall target="copy-generated-lib"/>
+ <copy todir="${deploy.dir}" flatten="true">
+ <fileset refid="lib.jars"/>
+ </copy>
+ </target>
+
+ <target name="copy-generated-lib" if="lib-available">
+ <copy file="${build.dir}/${name}.jar" todir="${deploy.dir}" failonerror="false"/>
+ </target>
+
+ <!-- ================================================================== -->
+ <!-- Compile test code -->
+ <!-- ================================================================== -->
+ <target name="compile-test" depends="compile" if="test.available">
+ <javac
+ encoding="${build.encoding}"
+ srcdir="${src.test}"
+ includes="**/*.java"
+ destdir="${build.test}"
+ debug="${javac.debug}"
+ optimize="${javac.optimize}"
+ target="${javac.version}"
+ source="${javac.version}"
+ deprecation="${javac.deprecation}">
+ <classpath refid="test.classpath"/>
+ </javac>
+ </target>
+
+ <!-- ================================================================== -->
+ <!-- Run unit tests -->
+ <!-- ================================================================== -->
+ <target name="test" depends="compile-test, deploy" if="test.available">
+ <echo message="Testing plugin: ${name}"/>
+
+ <junit printsummary="yes" haltonfailure="no" fork="yes"
+ errorProperty="tests.failed" failureProperty="tests.failed">
+ <sysproperty key="test.data" value="${build.test}/data"/>
+ <sysproperty key="test.input" value="${root}/data"/>
+ <classpath refid="test.classpath"/>
+ <formatter type="plain" />
+ <batchtest todir="${build.test}" unless="testcase">
+ <fileset dir="${src.test}"
+ includes="**/Test*.java" excludes="**/${test.exclude}.java" />
+ </batchtest>
+ <batchtest todir="${build.test}" if="testcase">
+ <fileset dir="${src.test}" includes="**/${testcase}.java"/>
+ </batchtest>
+ </junit>
+
+ <fail if="tests.failed">Tests failed!</fail>
+
+ </target>
+
+ <!-- ================================================================== -->
+ <!-- Clean. Delete the build files, and their directories -->
+ <!-- ================================================================== -->
+ <target name="clean">
+ <delete dir="${build.dir}"/>
+ <delete dir="${deploy.dir}"/>
+ </target>
+
+</project>
Propchange: labs/droids/src/plugins/build-plugin.xml
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: labs/droids/src/plugins/build-plugin.xml
------------------------------------------------------------------------------
svn:executable = *
Added: labs/droids/src/plugins/build.xml
URL: http://svn.apache.org/viewvc/labs/droids/src/plugins/build.xml?view=auto&rev=509422
==============================================================================
--- labs/droids/src/plugins/build.xml (added)
+++ labs/droids/src/plugins/build.xml Mon Feb 19 19:42:25 2007
@@ -0,0 +1,62 @@
+<?xml version="1.0"?>
+<!--
+ Licensed to the Apache Software Foundation (ASF) under one or more
+ contributor license agreements. See the NOTICE file distributed with
+ this work for additional information regarding copyright ownership.
+ The ASF licenses this file to You under the Apache License, Version 2.0
+ (the "License"); you may not use this file except in compliance with
+ the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+-->
+<project name="Droids" default="deploy-core" basedir=".">
+
+ <target name="deploy-core">
+ <ant target="compile-core" inheritall="false" dir="../.."/>
+ <ant target="deploy"/>
+ </target>
+
+ <!-- ====================================================== -->
+ <!-- Build & deploy all the plugin jars. -->
+ <!-- ====================================================== -->
+ <!--FIXME: this should be done by traversing the plugin dir
+ which removes the need to patch this file when a new plugin will
+ be released-->
+ <target name="deploy">
+ <ant dir="droids-extensionpoints" target="deploy"/>
+ <ant dir="handle-save" target="deploy"/>
+ <ant dir="handle-log" target="deploy"/>
+ <ant dir="parse-html" target="deploy"/>
+ <ant dir="protocol-http" target="deploy"/>
+ <ant dir="queue-simple" target="deploy"/>
+ <ant dir="urlfilter-regex" target="deploy"/>
+ </target>
+
+ <!-- ====================================================== -->
+ <!-- Test all of the plugins. -->
+ <!-- ====================================================== -->
+ <!--<target name="test">
+ <parallel threadCount="2">
+ <ant dir="parse-html" target="test"/>
+ </parallel>
+ </target>-->
+
+ <!-- ====================================================== -->
+ <!-- Clean all of the plugins. -->
+ <!-- ====================================================== -->
+ <target name="clean">
+ <ant dir="droids-extensionpoints" target="clean"/>
+ <ant dir="handle-save" target="clean"/>
+ <ant dir="handle-log" target="clean"/>
+ <ant dir="parse-html" target="clean"/>
+ <ant dir="protocol-http" target="clean"/>
+ <ant dir="queue-simple" target="clean"/>
+ <ant dir="urlfilter-regex" target="clean"/>
+ </target>
+</project>
Propchange: labs/droids/src/plugins/build.xml
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: labs/droids/src/plugins/build.xml
------------------------------------------------------------------------------
svn:executable = *
Added: labs/droids/src/plugins/droids-extensionpoints/build.xml
URL: http://svn.apache.org/viewvc/labs/droids/src/plugins/droids-extensionpoints/build.xml?view=auto&rev=509422
==============================================================================
--- labs/droids/src/plugins/droids-extensionpoints/build.xml (added)
+++ labs/droids/src/plugins/droids-extensionpoints/build.xml Mon Feb 19 19:42:25 2007
@@ -0,0 +1,30 @@
+<?xml version="1.0"?>
+<!--
+ Licensed to the Apache Software Foundation (ASF) under one or more
+ contributor license agreements. See the NOTICE file distributed with
+ this work for additional information regarding copyright ownership.
+ The ASF licenses this file to You under the Apache License, Version 2.0
+ (the "License"); you may not use this file except in compliance with
+ the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+-->
+<project name="droids-extensionpoints" default="jar">
+
+ <import file="../build-plugin.xml"/>
+
+ <!--
+ ! Override the compile and jar targets,
+ ! since there is nothing to compile here.
+ ! -->
+ <target name="compile" depends="init"/>
+
+ <target name="jar" depends="compile"/>
+
+</project>
Propchange: labs/droids/src/plugins/droids-extensionpoints/build.xml
------------------------------------------------------------------------------
svn:eol-style = native
Added: labs/droids/src/plugins/droids-extensionpoints/plugin.xml
URL: http://svn.apache.org/viewvc/labs/droids/src/plugins/droids-extensionpoints/plugin.xml?view=auto&rev=509422
==============================================================================
--- labs/droids/src/plugins/droids-extensionpoints/plugin.xml (added)
+++ labs/droids/src/plugins/droids-extensionpoints/plugin.xml Mon Feb 19 19:42:25 2007
@@ -0,0 +1,51 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+ Licensed to the Apache Software Foundation (ASF) under one or more
+ contributor license agreements. See the NOTICE file distributed with
+ this work for additional information regarding copyright ownership.
+ The ASF licenses this file to You under the Apache License, Version 2.0
+ (the "License"); you may not use this file except in compliance with
+ the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+-->
+<plugin
+ id="droids-extensionpoints"
+ name="the droids core extension points"
+ version="0.8.0"
+ provider-name="droids.org">
+
+ <!-- this file hosts all extension points droids core code offers.
+ Please not that plugins can define extension points as well to be extendable.-->
+
+<extension-point
+ id="org.apache.droids.api.Queue"
+ name="Droids Queue"/>
+
+<extension-point
+ id="org.apache.droids.api.Parser"
+ name="Droids Content Parser"/>
+
+<extension-point
+ id="org.apache.droids.api.Protocol"
+ name="Droids Protocol"/>
+
+<extension-point
+ id="org.apache.droids.api.URLFilter"
+ name="Droids URL Filter"/>
+
+<extension-point
+ id="org.apache.droids.api.Handler"
+ name="Droids Handler"/>
+<!--
+<extension-point
+ id="org.apache.droids.net.URLNormalizer"
+ name="Droids URL Normalizer"/>-->
+
+</plugin>
Propchange: labs/droids/src/plugins/droids-extensionpoints/plugin.xml
------------------------------------------------------------------------------
svn:eol-style = native
Added: labs/droids/src/plugins/handle-log/build.xml
URL: http://svn.apache.org/viewvc/labs/droids/src/plugins/handle-log/build.xml?view=auto&rev=509422
==============================================================================
--- labs/droids/src/plugins/handle-log/build.xml (added)
+++ labs/droids/src/plugins/handle-log/build.xml Mon Feb 19 19:42:25 2007
@@ -0,0 +1,22 @@
+<?xml version="1.0"?>
+<!--
+ Licensed to the Apache Software Foundation (ASF) under one or more
+ contributor license agreements. See the NOTICE file distributed with
+ this work for additional information regarding copyright ownership.
+ The ASF licenses this file to You under the Apache License, Version 2.0
+ (the "License"); you may not use this file except in compliance with
+ the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+-->
+<project name="handle-log" default="jar-core">
+
+ <import file="../build-plugin.xml"/>
+
+</project>
Propchange: labs/droids/src/plugins/handle-log/build.xml
------------------------------------------------------------------------------
svn:eol-style = native
Added: labs/droids/src/plugins/handle-log/plugin.xml
URL: http://svn.apache.org/viewvc/labs/droids/src/plugins/handle-log/plugin.xml?view=auto&rev=509422
==============================================================================
--- labs/droids/src/plugins/handle-log/plugin.xml (added)
+++ labs/droids/src/plugins/handle-log/plugin.xml Mon Feb 19 19:42:25 2007
@@ -0,0 +1,34 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+ Licensed to the Apache Software Foundation (ASF) under one or more
+ contributor license agreements. See the NOTICE file distributed with
+ this work for additional information regarding copyright ownership.
+ The ASF licenses this file to You under the Apache License, Version 2.0
+ (the "License"); you may not use this file except in compliance with
+ the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+-->
+<plugin
+ id="handle-log"
+ name="Droids handle log"
+ version="1.0.0"
+ provider-name="droids.org">
+
+ <extension id="org.apache.droids.handle.log"
+ name="Droids handle log"
+ point="org.apache.droids.api.Handler">
+ <implementation id="Sysout"
+ class="org.apache.droids.handle.Sysout">
+ <parameter name="file" value="handle-log.txt"/>
+ </implementation>
+
+ </extension>
+
+</plugin>
Propchange: labs/droids/src/plugins/handle-log/plugin.xml
------------------------------------------------------------------------------
svn:eol-style = native
Added: labs/droids/src/plugins/handle-log/src/java/org/apache/droids/handle/Sysout.java
URL: http://svn.apache.org/viewvc/labs/droids/src/plugins/handle-log/src/java/org/apache/droids/handle/Sysout.java?view=auto&rev=509422
==============================================================================
--- labs/droids/src/plugins/handle-log/src/java/org/apache/droids/handle/Sysout.java (added)
+++ labs/droids/src/plugins/handle-log/src/java/org/apache/droids/handle/Sysout.java Mon Feb 19 19:42:25 2007
@@ -0,0 +1,32 @@
+package org.apache.droids.handle;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.OutputStreamWriter;
+import java.io.Reader;
+import java.io.Writer;
+import java.net.URL;
+
+import org.apache.droids.api.Handler;
+
+public class Sysout extends WriterHandler implements Handler {
+
+ public void handle(InputStream stream, URL url) {
+ try {
+ writeOutput(stream);
+ } catch (IOException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+ // TODO Auto-generated method stub
+
+ }
+
+ private void writeOutput(InputStream stream) throws IOException {
+ Reader reader = new InputStreamReader(stream);
+ Writer output = new OutputStreamWriter(System.out);
+ pipe(reader, output);
+ }
+
+}
Propchange: labs/droids/src/plugins/handle-log/src/java/org/apache/droids/handle/Sysout.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: labs/droids/src/plugins/handle-save/build.xml
URL: http://svn.apache.org/viewvc/labs/droids/src/plugins/handle-save/build.xml?view=auto&rev=509422
==============================================================================
--- labs/droids/src/plugins/handle-save/build.xml (added)
+++ labs/droids/src/plugins/handle-save/build.xml Mon Feb 19 19:42:25 2007
@@ -0,0 +1,22 @@
+<?xml version="1.0"?>
+<!--
+ Licensed to the Apache Software Foundation (ASF) under one or more
+ contributor license agreements. See the NOTICE file distributed with
+ this work for additional information regarding copyright ownership.
+ The ASF licenses this file to You under the Apache License, Version 2.0
+ (the "License"); you may not use this file except in compliance with
+ the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+-->
+<project name="handle-save" default="jar-core">
+
+ <import file="../build-plugin.xml"/>
+
+</project>
Propchange: labs/droids/src/plugins/handle-save/build.xml
------------------------------------------------------------------------------
svn:eol-style = native
Added: labs/droids/src/plugins/handle-save/plugin.xml
URL: http://svn.apache.org/viewvc/labs/droids/src/plugins/handle-save/plugin.xml?view=auto&rev=509422
==============================================================================
--- labs/droids/src/plugins/handle-save/plugin.xml (added)
+++ labs/droids/src/plugins/handle-save/plugin.xml Mon Feb 19 19:42:25 2007
@@ -0,0 +1,34 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+ Licensed to the Apache Software Foundation (ASF) under one or more
+ contributor license agreements. See the NOTICE file distributed with
+ this work for additional information regarding copyright ownership.
+ The ASF licenses this file to You under the Apache License, Version 2.0
+ (the "License"); you may not use this file except in compliance with
+ the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+-->
+<plugin
+ id="handle-save"
+ name="Droids handle log"
+ version="1.0.0"
+ provider-name="droids.org">
+
+ <extension id="org.apache.droids.handle.log"
+ name="Droids handle log"
+ point="org.apache.droids.api.Handler">
+ <implementation id="Save"
+ class="org.apache.droids.handle.Save">
+ <parameter name="file" value="handle-save.txt"/>
+ </implementation>
+
+ </extension>
+
+</plugin>
Propchange: labs/droids/src/plugins/handle-save/plugin.xml
------------------------------------------------------------------------------
svn:eol-style = native
Added: labs/droids/src/plugins/handle-save/src/java/org/apache/droids/handle/Save.java
URL: http://svn.apache.org/viewvc/labs/droids/src/plugins/handle-save/src/java/org/apache/droids/handle/Save.java?view=auto&rev=509422
==============================================================================
--- labs/droids/src/plugins/handle-save/src/java/org/apache/droids/handle/Save.java (added)
+++ labs/droids/src/plugins/handle-save/src/java/org/apache/droids/handle/Save.java Mon Feb 19 19:42:25 2007
@@ -0,0 +1,57 @@
+package org.apache.droids.handle;
+
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.OutputStreamWriter;
+import java.io.Reader;
+import java.io.Writer;
+import java.net.URL;
+
+import org.apache.droids.api.Handler;
+import org.apache.droids.conf.Configuration;
+
+public class Save extends WriterHandler implements Handler {
+ private String outputDir;
+ private URL url;
+
+ public void setConf(Configuration conf) {
+ this.conf=conf;
+ outputDir =conf.get("handle.save.output.dir","./build/handle/save");
+ }
+ public void handle(InputStream stream, URL url) {
+ this.url=url;
+ try {
+ writeOutput(stream);
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ }
+
+ private void writeOutput(InputStream stream) throws IOException {
+ if(!url.getFile().endsWith("/")){
+ Reader reader = new InputStreamReader(stream);
+ String file=outputDir+url.getFile();
+ File cache = new File(file);
+ createFile(cache);
+ Writer output = new OutputStreamWriter(new FileOutputStream(outputDir+url.getFile()));
+ pipe(reader, output);
+ }
+ }
+ private void createFile(File cache) throws IOException {
+ if(!cache.isDirectory()&!cache.getAbsolutePath().endsWith("/")){
+ try {
+ cache.createNewFile();
+ } catch (Exception e) {
+ // if we cannot create a file that means that the parent path
+ // does not exists
+ File path = new File(cache.getParent());
+ path.mkdirs();
+ cache.createNewFile();
+ }
+ }
+ }
+
+}
Propchange: labs/droids/src/plugins/handle-save/src/java/org/apache/droids/handle/Save.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: labs/droids/src/plugins/parse-html/build.xml
URL: http://svn.apache.org/viewvc/labs/droids/src/plugins/parse-html/build.xml?view=auto&rev=509422
==============================================================================
--- labs/droids/src/plugins/parse-html/build.xml (added)
+++ labs/droids/src/plugins/parse-html/build.xml Mon Feb 19 19:42:25 2007
@@ -0,0 +1,20 @@
+<?xml version="1.0"?>
+<!--
+ Licensed to the Apache Software Foundation (ASF) under one or more
+ contributor license agreements. See the NOTICE file distributed with
+ this work for additional information regarding copyright ownership.
+ The ASF licenses this file to You under the Apache License, Version 2.0
+ (the "License"); you may not use this file except in compliance with
+ the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+-->
+<project name="parse-html" default="jar-core">
+ <import file="../build-plugin.xml"/>
+</project>
Propchange: labs/droids/src/plugins/parse-html/build.xml
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: labs/droids/src/plugins/parse-html/build.xml
------------------------------------------------------------------------------
svn:executable = *
Added: labs/droids/src/plugins/parse-html/plugin.xml
URL: http://svn.apache.org/viewvc/labs/droids/src/plugins/parse-html/plugin.xml?view=auto&rev=509422
==============================================================================
--- labs/droids/src/plugins/parse-html/plugin.xml (added)
+++ labs/droids/src/plugins/parse-html/plugin.xml Mon Feb 19 19:42:25 2007
@@ -0,0 +1,35 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+ Licensed to the Apache Software Foundation (ASF) under one or more
+ contributor license agreements. See the NOTICE file distributed with
+ this work for additional information regarding copyright ownership.
+ The ASF licenses this file to You under the Apache License, Version 2.0
+ (the "License"); you may not use this file except in compliance with
+ the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+-->
+<plugin
+ id="parse-html"
+ name="Html Parse Plug-in"
+ version="1.0.0"
+ provider-name="droids.org">
+
+ <extension id="org.apache.droids.parse.html"
+ name="HtmlParser"
+ point="org.apache.droids.api.Parser">
+
+ <implementation id="org.apache.droids.parse.html.HtmlParser"
+ class="org.apache.droids.parse.html.HtmlParser">
+ <parameter name="contentType" value="text/html"/>
+ </implementation>
+
+ </extension>
+
+</plugin>
Propchange: labs/droids/src/plugins/parse-html/plugin.xml
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: labs/droids/src/plugins/parse-html/plugin.xml
------------------------------------------------------------------------------
svn:executable = *
Added: labs/droids/src/plugins/parse-html/src/java/org/apache/droids/parse/html/HtmlParser.java
URL: http://svn.apache.org/viewvc/labs/droids/src/plugins/parse-html/src/java/org/apache/droids/parse/html/HtmlParser.java?view=auto&rev=509422
==============================================================================
--- labs/droids/src/plugins/parse-html/src/java/org/apache/droids/parse/html/HtmlParser.java (added)
+++ labs/droids/src/plugins/parse-html/src/java/org/apache/droids/parse/html/HtmlParser.java Mon Feb 19 19:42:25 2007
@@ -0,0 +1,98 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.droids.parse.html;
+
+import java.io.InputStream;
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.util.ArrayList;
+import java.util.HashSet;
+
+import javax.xml.stream.XMLInputFactory;
+import javax.xml.stream.XMLStreamConstants;
+import javax.xml.stream.XMLStreamException;
+import javax.xml.stream.XMLStreamReader;
+
+import org.apache.droids.api.Parse;
+import org.apache.droids.api.Parser;
+import org.apache.droids.conf.Configuration;
+import org.apache.droids.parse.Outlink;
+import org.apache.droids.parse.ParseData;
+import org.apache.droids.parse.ParseImpl;
+
+public class HtmlParser implements Parser {
+
+ private XMLInputFactory inputFactory = XMLInputFactory.newInstance();
+ private URL base;
+ private Configuration conf;
+
+ public Parse getParse(InputStream stream, URL base) {
+ this.base=base;
+ ParseData parseData = null;
+ try {
+ XMLStreamReader parser = inputFactory.createXMLStreamReader(stream);
+ parseData = process(parser);
+ } catch (XMLStreamException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ return new ParseImpl(stream.toString(), parseData);
+ } catch (MalformedURLException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+ return new ParseImpl(stream.toString(), parseData);
+ }
+
+ private ParseData process(XMLStreamReader parser) throws XMLStreamException, MalformedURLException {
+ ArrayList<Outlink> l = new ArrayList<Outlink>();
+ HashSet<String> set = new HashSet<String>();
+ Outlink[] outlinks = new Outlink[0];
+ while (true) {
+ int event = parser.next();
+ switch (event) {
+ case XMLStreamConstants.END_DOCUMENT:
+ parser.close();
+ outlinks = (Outlink[])l.toArray(new Outlink[l.size()]);
+ return new ParseData(outlinks);
+ case XMLStreamConstants.START_ELEMENT:
+ if (parser.getLocalName().toLowerCase().equals("a")) {
+ for (int i = 0; i < parser.getAttributeCount(); i++) {
+ String localName = parser.getAttributeLocalName(i)
+ .toLowerCase();
+ String localValue = parser.getAttributeValue(i);
+ if (localName.equals("href")) {
+ final Outlink outlink = new Outlink(localValue.contains(":/")?localValue:new URL (base,localValue).toString());
+ if (!set.contains(outlink.getToUrl())) {
+ set.add(outlink.getToUrl());
+ l.add(outlink);
+ }
+ }
+ }
+ }
+ }
+ }
+ }
+
+ public Configuration getConf() {
+ return conf;
+ }
+
+ public void setConf(Configuration conf) {
+ this.conf=conf;
+ }
+
+}
Propchange: labs/droids/src/plugins/parse-html/src/java/org/apache/droids/parse/html/HtmlParser.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: labs/droids/src/plugins/protocol-http/build.xml
URL: http://svn.apache.org/viewvc/labs/droids/src/plugins/protocol-http/build.xml?view=auto&rev=509422
==============================================================================
--- labs/droids/src/plugins/protocol-http/build.xml (added)
+++ labs/droids/src/plugins/protocol-http/build.xml Mon Feb 19 19:42:25 2007
@@ -0,0 +1,20 @@
+<?xml version="1.0"?>
+<!--
+ Licensed to the Apache Software Foundation (ASF) under one or more
+ contributor license agreements. See the NOTICE file distributed with
+ this work for additional information regarding copyright ownership.
+ The ASF licenses this file to You under the Apache License, Version 2.0
+ (the "License"); you may not use this file except in compliance with
+ the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+-->
+<project name="protocol-http" default="jar-core">
+ <import file="../build-plugin.xml"/>
+</project>
Propchange: labs/droids/src/plugins/protocol-http/build.xml
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: labs/droids/src/plugins/protocol-http/build.xml
------------------------------------------------------------------------------
svn:executable = *
Added: labs/droids/src/plugins/protocol-http/plugin.xml
URL: http://svn.apache.org/viewvc/labs/droids/src/plugins/protocol-http/plugin.xml?view=auto&rev=509422
==============================================================================
--- labs/droids/src/plugins/protocol-http/plugin.xml (added)
+++ labs/droids/src/plugins/protocol-http/plugin.xml Mon Feb 19 19:42:25 2007
@@ -0,0 +1,35 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+ Licensed to the Apache Software Foundation (ASF) under one or more
+ contributor license agreements. See the NOTICE file distributed with
+ this work for additional information regarding copyright ownership.
+ The ASF licenses this file to You under the Apache License, Version 2.0
+ (the "License"); you may not use this file except in compliance with
+ the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+-->
+<plugin
+ id="protocol-http"
+ name="Http Protocol Plug-in"
+ version="1.0.0"
+ provider-name="droids.org">
+
+ <extension id="org.apache.droids.protocol.http"
+ name="HttpProtocol"
+ point="org.apache.droids.api.Protocol">
+
+ <implementation id="org.apache.droids.protocol.http.Http"
+ class="org.apache.droids.protocol.http.Http">
+ <parameter name="protocolName" value="http"/>
+ </implementation>
+
+ </extension>
+
+</plugin>
Propchange: labs/droids/src/plugins/protocol-http/plugin.xml
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: labs/droids/src/plugins/protocol-http/plugin.xml
------------------------------------------------------------------------------
svn:executable = *
Added: labs/droids/src/plugins/protocol-http/src/java/org/apache/droids/protocol/http/Http.java
URL: http://svn.apache.org/viewvc/labs/droids/src/plugins/protocol-http/src/java/org/apache/droids/protocol/http/Http.java?view=auto&rev=509422
==============================================================================
--- labs/droids/src/plugins/protocol-http/src/java/org/apache/droids/protocol/http/Http.java (added)
+++ labs/droids/src/plugins/protocol-http/src/java/org/apache/droids/protocol/http/Http.java Mon Feb 19 19:42:25 2007
@@ -0,0 +1,75 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.droids.protocol.http;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.HttpURLConnection;
+import java.net.MalformedURLException;
+import java.net.URL;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.apache.droids.api.Protocol;
+import org.apache.droids.conf.Configuration;
+
+public class Http implements Protocol {
+ public static final Log LOG = LogFactory.getLog(Http.class);
+ private Configuration conf;
+ public Http() {
+ }
+ public String getContentType(String url) {
+ URL source;
+ HttpURLConnection urlc = null;
+ try {
+ source = new URL(url);
+ urlc = (HttpURLConnection) source.openConnection();
+ return urlc.getContentType();
+ } catch (MalformedURLException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ } catch (IOException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }finally{
+ if(urlc!=null)urlc.disconnect();
+ }
+ return null;
+ }
+
+ public InputStream openStream(String url) throws IOException {
+ URL source;
+ try {
+ source = new URL(url);
+ return source.openStream();
+ } catch (MalformedURLException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ } catch (IOException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+ return null;
+ }
+ public Configuration getConf() {
+ return conf;
+ }
+
+ public void setConf(Configuration conf) {
+ this.conf=conf;
+ }
+}
Propchange: labs/droids/src/plugins/protocol-http/src/java/org/apache/droids/protocol/http/Http.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: labs/droids/src/plugins/queue-simple/build.xml
URL: http://svn.apache.org/viewvc/labs/droids/src/plugins/queue-simple/build.xml?view=auto&rev=509422
==============================================================================
--- labs/droids/src/plugins/queue-simple/build.xml (added)
+++ labs/droids/src/plugins/queue-simple/build.xml Mon Feb 19 19:42:25 2007
@@ -0,0 +1,20 @@
+<?xml version="1.0"?>
+<!--
+ Licensed to the Apache Software Foundation (ASF) under one or more
+ contributor license agreements. See the NOTICE file distributed with
+ this work for additional information regarding copyright ownership.
+ The ASF licenses this file to You under the Apache License, Version 2.0
+ (the "License"); you may not use this file except in compliance with
+ the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+-->
+<project name="queue-simple" default="jar-core">
+ <import file="../build-plugin.xml"/>
+</project>
Propchange: labs/droids/src/plugins/queue-simple/build.xml
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: labs/droids/src/plugins/queue-simple/build.xml
------------------------------------------------------------------------------
svn:executable = *
Added: labs/droids/src/plugins/queue-simple/plugin.xml
URL: http://svn.apache.org/viewvc/labs/droids/src/plugins/queue-simple/plugin.xml?view=auto&rev=509422
==============================================================================
--- labs/droids/src/plugins/queue-simple/plugin.xml (added)
+++ labs/droids/src/plugins/queue-simple/plugin.xml Mon Feb 19 19:42:25 2007
@@ -0,0 +1,35 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+ Licensed to the Apache Software Foundation (ASF) under one or more
+ contributor license agreements. See the NOTICE file distributed with
+ this work for additional information regarding copyright ownership.
+ The ASF licenses this file to You under the Apache License, Version 2.0
+ (the "License"); you may not use this file except in compliance with
+ the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+-->
+<plugin
+ id="queue-simple"
+ name="Simple Queue Plug-in"
+ version="1.0.0"
+ provider-name="droids.org">
+
+ <extension id="org.apache.droids.queue.simple"
+ name="SimpleQueue"
+ point="org.apache.droids.api.Queue">
+
+ <implementation id="org.apache.droids.queue.Simple"
+ class="org.apache.droids.queue.Simple">
+ <parameter name="queueName" value="simple"/>
+ </implementation>
+
+ </extension>
+
+</plugin>
Propchange: labs/droids/src/plugins/queue-simple/plugin.xml
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: labs/droids/src/plugins/queue-simple/plugin.xml
------------------------------------------------------------------------------
svn:executable = *
Added: labs/droids/src/plugins/queue-simple/src/java/org/apache/droids/queue/Simple.java
URL: http://svn.apache.org/viewvc/labs/droids/src/plugins/queue-simple/src/java/org/apache/droids/queue/Simple.java?view=auto&rev=509422
==============================================================================
--- labs/droids/src/plugins/queue-simple/src/java/org/apache/droids/queue/Simple.java (added)
+++ labs/droids/src/plugins/queue-simple/src/java/org/apache/droids/queue/Simple.java Mon Feb 19 19:42:25 2007
@@ -0,0 +1,98 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.droids.queue;
+
+import java.util.LinkedHashMap;
+import java.util.LinkedList;
+
+import org.apache.droids.Queue.QueueBean;
+import org.apache.droids.api.Link;
+import org.apache.droids.api.Queue;
+import org.apache.droids.api.Task;
+import org.apache.droids.conf.Configuration;
+
+public class Simple extends QueueBean implements Queue {
+ private LinkedHashMap<String, Task> allTasks;
+
+ public void setConf(Configuration conf) {
+ this.conf = conf;
+ maxSize = Integer.parseInt(conf.get("queue.max.queue.size", "0"));
+ maxDepth = Integer.parseInt(conf.get("queue.max.depth", "0"));
+ allTasks = new LinkedHashMap<String, Task>();
+ }
+
+ public Task getTask(String id) {
+ return allTasks.get(id);
+ }
+
+ public int totalSize() {
+ return allTasks.size();
+ }
+
+ public void init(Task[] initialTask) {
+ LinkedList<Task> list = new LinkedList<Task>();
+ for (int i = 0; i < initialTask.length; i++) {
+ Link task = (Link) initialTask[i];
+ if (null != task) {
+ allTasks.put(task.getId(), task);
+ list.add(task);
+ }
+ }
+ this.toDoLinks = list.toArray(new Link[list.size()]);
+ }
+
+ public boolean hasNext() {
+ if (null == toDoLinks)
+ return false;
+ return toDoLinks.length > 0;
+ }
+
+ public void merge(Task[] filterLinks) {
+ LinkedList<Task> list = new LinkedList<Task>();
+ if (null != toDoLinks) {
+ for (int i = 0; i < toDoLinks.length; i++) {
+ Task task = toDoLinks[i];
+ if (null != task) {
+ list.add(task);
+ }
+ }
+ }
+ for (int i = 0; i < filterLinks.length; i++) {
+ Task task = filterLinks[i];
+ if (null != task & !allTasks.containsKey(task.getId())) {
+ allTasks.put(task.getId(), task);
+ list.add(task);
+ }
+ }
+ toDoLinks = list.toArray(new Task[list.size()]);
+ }
+
+ public Task next() {
+ Task task = toDoLinks[0];
+ if (toDoLinks.length > 1) {
+ Task[] newToDoLinks = new Task[toDoLinks.length - 1];
+ for (int i = 1; i < toDoLinks.length; i++) {
+ Task taskOld = toDoLinks[i];
+ newToDoLinks[i - 1] = taskOld;
+ }
+ toDoLinks = newToDoLinks;
+ } else
+ toDoLinks = null;
+ return task;
+ }
+
+}
Propchange: labs/droids/src/plugins/queue-simple/src/java/org/apache/droids/queue/Simple.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: labs/droids/src/plugins/urlfilter-regex/build.xml
URL: http://svn.apache.org/viewvc/labs/droids/src/plugins/urlfilter-regex/build.xml?view=auto&rev=509422
==============================================================================
--- labs/droids/src/plugins/urlfilter-regex/build.xml (added)
+++ labs/droids/src/plugins/urlfilter-regex/build.xml Mon Feb 19 19:42:25 2007
@@ -0,0 +1,22 @@
+<?xml version="1.0"?>
+<!--
+ Licensed to the Apache Software Foundation (ASF) under one or more
+ contributor license agreements. See the NOTICE file distributed with
+ this work for additional information regarding copyright ownership.
+ The ASF licenses this file to You under the Apache License, Version 2.0
+ (the "License"); you may not use this file except in compliance with
+ the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+-->
+<project name="urlfilter-regex" default="jar-core">
+
+ <import file="../build-plugin.xml"/>
+
+</project>
Propchange: labs/droids/src/plugins/urlfilter-regex/build.xml
------------------------------------------------------------------------------
svn:eol-style = native
Added: labs/droids/src/plugins/urlfilter-regex/plugin.xml
URL: http://svn.apache.org/viewvc/labs/droids/src/plugins/urlfilter-regex/plugin.xml?view=auto&rev=509422
==============================================================================
--- labs/droids/src/plugins/urlfilter-regex/plugin.xml (added)
+++ labs/droids/src/plugins/urlfilter-regex/plugin.xml Mon Feb 19 19:42:25 2007
@@ -0,0 +1,34 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+ Licensed to the Apache Software Foundation (ASF) under one or more
+ contributor license agreements. See the NOTICE file distributed with
+ this work for additional information regarding copyright ownership.
+ The ASF licenses this file to You under the Apache License, Version 2.0
+ (the "License"); you may not use this file except in compliance with
+ the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+-->
+<plugin
+ id="urlfilter-regex"
+ name="Regex URL Filter"
+ version="1.0.0"
+ provider-name="droids.org">
+
+ <extension id="org.apache.droids.net.urlfilter.regex"
+ name="Droids Regex URL Filter"
+ point="org.apache.droids.api.URLFilter">
+ <implementation id="RegexURLFilter"
+ class="org.apache.droids.net.RegexURLFilter">
+ <parameter name="file" value="urlfilter-regex.txt"/>
+ </implementation>
+
+ </extension>
+
+</plugin>
Propchange: labs/droids/src/plugins/urlfilter-regex/plugin.xml
------------------------------------------------------------------------------
svn:eol-style = native
Added: labs/droids/src/plugins/urlfilter-regex/src/java/org/apache/droids/net/RegexRule.java
URL: http://svn.apache.org/viewvc/labs/droids/src/plugins/urlfilter-regex/src/java/org/apache/droids/net/RegexRule.java?view=auto&rev=509422
==============================================================================
--- labs/droids/src/plugins/urlfilter-regex/src/java/org/apache/droids/net/RegexRule.java (added)
+++ labs/droids/src/plugins/urlfilter-regex/src/java/org/apache/droids/net/RegexRule.java Mon Feb 19 19:42:25 2007
@@ -0,0 +1,57 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.droids.net;
+
+/**
+ * A generic regular expression rule.
+ *
+ */
+public abstract class RegexRule {
+
+ private boolean sign;
+ /**
+ * Constructs a new regular expression rule.
+ *
+ * @param sign specifies if this rule must filter-in or filter-out.
+ * A <code>true</code> value means that any url matching this rule
+ * must be accepted, a <code>false</code> value means that any url
+ * matching this rule must be rejected.
+ * @param regex is the regular expression used for matching (see
+ * {@link #match(String)} method).
+ */
+ protected RegexRule(boolean sign, String regex) {
+ this.sign = sign;
+ }
+
+ /**
+ * Return if this rule is used for filtering-in or out.
+ *
+ * @return <code>true</code> if any url matching this rule must be accepted,
+ * otherwise <code>false</code>.
+ */
+ protected boolean accept() { return sign; }
+
+ /**
+ * Checks if a url matches this rule.
+ * @param url is the url to check.
+ * @return <code>true</code> if the specified url matches this rule,
+ * otherwise <code>false</code>.
+ */
+ protected abstract boolean match(String url);
+
+}
+
Propchange: labs/droids/src/plugins/urlfilter-regex/src/java/org/apache/droids/net/RegexRule.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: labs/droids/src/plugins/urlfilter-regex/src/java/org/apache/droids/net/RegexURLFilter.java
URL: http://svn.apache.org/viewvc/labs/droids/src/plugins/urlfilter-regex/src/java/org/apache/droids/net/RegexURLFilter.java?view=auto&rev=509422
==============================================================================
--- labs/droids/src/plugins/urlfilter-regex/src/java/org/apache/droids/net/RegexURLFilter.java (added)
+++ labs/droids/src/plugins/urlfilter-regex/src/java/org/apache/droids/net/RegexURLFilter.java Mon Feb 19 19:42:25 2007
@@ -0,0 +1,129 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.droids.net;
+
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.Reader;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.regex.Pattern;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.apache.droids.api.URLFilter;
+import org.apache.droids.conf.Configuration;
+
+public class RegexURLFilter implements URLFilter {
+ private final static Log LOG = LogFactory.getLog(RegexURLFilter.class);
+
+ /** An array of applicable rules */
+ private RegexRule[] rules;
+
+ private Configuration conf;
+
+ public synchronized String filter(String url) {
+ for (int i = 0; i < rules.length; i++) {
+ if (rules[i].match(url)) {
+ return rules[i].accept() ? url : null;
+ }
+ }
+ ;
+ return null;
+ }
+
+ public void setConf(Configuration conf) {
+ this.conf = conf;
+ String file = getRulesFile(conf);
+ Reader reader = conf.getConfResourceAsReader(file);
+ if (reader == null) {
+ if (LOG.isFatalEnabled()) {
+ LOG.fatal("Can't find resource: " + file);
+ }
+ } else {
+ try {
+ rules = readRulesFile(reader);
+ } catch (IOException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+ }
+ }
+
+ private RegexRule[] readRulesFile(Reader reader) throws IOException {
+ BufferedReader in = new BufferedReader(reader);
+ List<RegexRule> rules = new ArrayList<RegexRule>();
+ String line;
+
+ while ((line = in.readLine()) != null) {
+ if (line.length() == 0) {
+ continue;
+ }
+ char first = line.charAt(0);
+ boolean sign = false;
+ switch (first) {
+ case '+':
+ sign = true;
+ break;
+ case '-':
+ sign = false;
+ break;
+ case ' ':
+ case '\n':
+ case '#': // skip blank & comment lines
+ continue;
+ default:
+ throw new IOException("Invalid first character: " + line);
+ }
+
+ String regex = line.substring(1);
+ if (LOG.isTraceEnabled()) {
+ LOG.trace("Adding rule [" + regex + "]");
+ }
+ RegexRule rule = createRule(sign, regex);
+ rules.add(rule);
+ }
+ return (RegexRule[]) rules.toArray(new RegexRule[rules.size()]);
+
+ }
+
+ private RegexRule createRule(boolean sign, String regex) {
+ return new Rule(sign, regex);
+ }
+
+ private String getRulesFile(Configuration conf) {
+ return conf.get("urlfilter.regex.file");
+ }
+
+ public Configuration getConf() {
+ return this.conf;
+ }
+
+ private class Rule extends RegexRule {
+
+ private Pattern pattern;
+
+ Rule(boolean sign, String regex) {
+ super(sign, regex);
+ pattern = Pattern.compile(regex);
+ }
+
+ protected boolean match(String url) {
+ return pattern.matcher(url).find();
+ }
+ }
+}
Propchange: labs/droids/src/plugins/urlfilter-regex/src/java/org/apache/droids/net/RegexURLFilter.java
------------------------------------------------------------------------------
svn:eol-style = native
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@labs.apache.org
For additional commands, e-mail: commits-help@labs.apache.org