You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cvs@avalon.apache.org by cz...@apache.org on 2003/01/30 08:57:11 UTC
cvs commit: jakarta-avalon-excalibur/sourceresolve/src/java/org/apache/excalibur/source MoveableSource.java SourceResolver.java SourceUtil.java
cziegeler 2003/01/29 23:57:11
Modified: sourceresolve/src/java/org/apache/excalibur/source/impl
URLSourceFactory.java SourceResolverImpl.java
URLSource.java
sourceresolve/src/java/org/apache/excalibur/source
SourceResolver.java SourceUtil.java
Added: sourceresolve/src/java/org/apache/excalibur/source
MoveableSource.java
Log:
- Making a URLFactory
- Generalizing parameters for resolving
- Adding MoveableSource and copy/move from Cocoon by Stephan Michels
Revision Changes Path
1.2 +81 -6 jakarta-avalon-excalibur/sourceresolve/src/java/org/apache/excalibur/source/impl/URLSourceFactory.java
Index: URLSourceFactory.java
===================================================================
RCS file: /home/cvs/jakarta-avalon-excalibur/sourceresolve/src/java/org/apache/excalibur/source/impl/URLSourceFactory.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- URLSourceFactory.java 29 Jan 2003 16:46:18 -0000 1.1
+++ URLSourceFactory.java 30 Jan 2003 07:57:10 -0000 1.2
@@ -54,13 +54,19 @@
*/
package org.apache.excalibur.source.impl;
+import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
+import java.net.URL;
import java.util.Map;
import org.apache.avalon.framework.logger.AbstractLogEnabled;
+import org.apache.avalon.framework.parameters.ParameterException;
+import org.apache.avalon.framework.parameters.Parameterizable;
+import org.apache.avalon.framework.parameters.Parameters;
import org.apache.avalon.framework.thread.ThreadSafe;
import org.apache.excalibur.source.Source;
+import org.apache.excalibur.source.SourceException;
import org.apache.excalibur.source.SourceFactory;
/**
@@ -71,22 +77,91 @@
*/
public class URLSourceFactory
extends AbstractLogEnabled
- implements SourceFactory, ThreadSafe
+ implements SourceFactory, Parameterizable, ThreadSafe
{
+ /** The URLSource class used */
+ protected Class m_urlSourceClass;
+
+ public void parameterize( Parameters pars )
+ throws ParameterException
+ {
+ final String urlSourceClassName = pars.getParameter( "url-source",
+ "org.apache.excalibur.source.impl.URLSource" );
+ ClassLoader loader = Thread.currentThread().getContextClassLoader();
+ if( loader == null )
+ {
+ loader = getClass().getClassLoader();
+ }
+ try
+ {
+ m_urlSourceClass = loader.loadClass( urlSourceClassName );
+ }
+ catch( ClassNotFoundException cnfe )
+ {
+ this.getLogger().error( "Class not found: " + urlSourceClassName, cnfe );
+ throw new ParameterException( "Class not found: " + urlSourceClassName, cnfe );
+ }
+ }
+
/**
* @see org.apache.excalibur.source.SourceFactory#getSource(java.lang.String, java.util.Map)
*/
- public Source getSource(String location, Map parameters)
+ public Source getSource(String systemID, Map parameters)
throws MalformedURLException, IOException
{
if( getLogger().isDebugEnabled() )
{
- final String message = "Creating source object for " + location;
+ final String message = "Creating source object for " + systemID;
getLogger().debug( message );
}
- // FIXME: Some more work to do
- return null;
+
+ Source source;
+ try
+ {
+ if( getLogger().isDebugEnabled() == true )
+ {
+ this.getLogger().debug( "Making URL from " + systemID );
+ }
+ try
+ {
+ final URLSource urlSource =
+ (URLSource)this.m_urlSourceClass.newInstance();
+ urlSource.init( new URL( systemID ), parameters );
+ source = urlSource;
+ }
+ catch( MalformedURLException mue )
+ {
+ throw mue;
+ }
+ catch( Exception ie )
+ {
+ throw new SourceException( "Unable to create new instance of " +
+ this.m_urlSourceClass, ie );
+ }
+ }
+ catch( MalformedURLException mue )
+ {
+ if( getLogger().isDebugEnabled() )
+ {
+ this.getLogger().debug( "Making URL - MalformedURLException in getURL:", mue );
+ this.getLogger().debug( "Making URL a File (assuming that it is full path):" + systemID );
+ }
+ try
+ {
+ final URLSource urlSource =
+ (URLSource)this.m_urlSourceClass.newInstance();
+ urlSource.init( ( new File( systemID ) ).toURL(), parameters );
+ source = urlSource;
+ }
+ catch( Exception ie )
+ {
+ throw new SourceException( "Unable to create new instance of " +
+ this.m_urlSourceClass, ie );
+ }
+ }
+
+ return source;
}
/**
1.27 +56 -70 jakarta-avalon-excalibur/sourceresolve/src/java/org/apache/excalibur/source/impl/SourceResolverImpl.java
Index: SourceResolverImpl.java
===================================================================
RCS file: /home/cvs/jakarta-avalon-excalibur/sourceresolve/src/java/org/apache/excalibur/source/impl/SourceResolverImpl.java,v
retrieving revision 1.26
retrieving revision 1.27
diff -u -r1.26 -r1.27
--- SourceResolverImpl.java 30 Jan 2003 07:15:30 -0000 1.26
+++ SourceResolverImpl.java 30 Jan 2003 07:57:10 -0000 1.27
@@ -60,14 +60,12 @@
import java.net.URL;
import java.util.Map;
+import org.apache.avalon.framework.CascadingRuntimeException;
import org.apache.avalon.framework.activity.Disposable;
import org.apache.avalon.framework.context.Context;
import org.apache.avalon.framework.context.ContextException;
import org.apache.avalon.framework.context.Contextualizable;
import org.apache.avalon.framework.logger.AbstractLogEnabled;
-import org.apache.avalon.framework.parameters.ParameterException;
-import org.apache.avalon.framework.parameters.Parameterizable;
-import org.apache.avalon.framework.parameters.Parameters;
import org.apache.avalon.framework.service.ServiceException;
import org.apache.avalon.framework.service.ServiceManager;
import org.apache.avalon.framework.service.ServiceSelector;
@@ -100,7 +98,6 @@
implements Serviceable,
Contextualizable,
Disposable,
- Parameterizable,
SourceResolver,
ThreadSafe
{
@@ -115,9 +112,9 @@
*/
protected URL m_baseURL;
- /** The URLSource class used */
- protected Class m_urlSourceClass;
-
+ /** The default factory (if it is thread safe) */
+ protected SourceFactory m_defaultFactory;
+
/**
* Get the context
*/
@@ -170,38 +167,28 @@
{
m_manager = manager;
m_factorySelector = (ServiceSelector)m_manager.lookup( SourceFactory.ROLE + "Selector" );
+ m_defaultFactory = (SourceFactory) m_factorySelector.select( "*" );
+ if ( !(m_defaultFactory instanceof ThreadSafe) )
+ {
+ m_factorySelector.release(m_defaultFactory);
+ m_defaultFactory = null;
+ }
}
public void dispose()
{
- if( m_manager != null )
+ if( null != m_manager )
{
+ if ( null != m_defaultFactory )
+ {
+ m_factorySelector.release( m_defaultFactory );
+ m_defaultFactory = null;
+ }
m_manager.release( m_factorySelector );
m_factorySelector = null;
}
}
- public void parameterize( Parameters pars )
- throws ParameterException
- {
- final String urlSourceClassName = pars.getParameter( "url-source",
- "org.apache.excalibur.source.impl.URLSource" );
- ClassLoader loader = Thread.currentThread().getContextClassLoader();
- if( loader == null )
- {
- loader = getClass().getClassLoader();
- }
- try
- {
- m_urlSourceClass = loader.loadClass( urlSourceClassName );
- }
- catch( ClassNotFoundException cnfe )
- {
- this.getLogger().error( "Class not found: " + urlSourceClassName, cnfe );
- throw new ParameterException( "Class not found: " + urlSourceClassName, cnfe );
- }
- }
-
/**
* Get a <code>Source</code> object.
* @throws SourceNotFoundException if the source cannot be found
@@ -306,7 +293,6 @@
catch( final ServiceException ce )
{
// no selector available, use fallback
- //throw new SourceException( "Unable to select source factory for protocol " + protocol, ce );
}
finally
{
@@ -316,48 +302,27 @@
if( null == source )
{
- // no factory found, so usual url handling stuff...
- try
+ // no factory found, so use default factory
+ if ( null != m_defaultFactory)
{
- if( getLogger().isDebugEnabled() == true )
- {
- this.getLogger().debug( "Making URL from " + systemID );
- }
- try
- {
- final URLSource urlSource =
- (URLSource)this.m_urlSourceClass.newInstance();
- urlSource.init( new URL( systemID ), parameters );
- source = urlSource;
- }
- catch( MalformedURLException mue )
- {
- throw mue;
- }
- catch( Exception ie )
- {
- throw new SourceException( "Unable to create new instance of " +
- this.m_urlSourceClass, ie );
- }
- }
- catch( MalformedURLException mue )
+ // thread safe default factory
+ source = m_defaultFactory.getSource( systemID, parameters );
+ }
+ else
{
- if( getLogger().isDebugEnabled() )
+ try
{
- this.getLogger().debug( "Making URL - MalformedURLException in getURL:", mue );
- this.getLogger().debug( "Making URL a File (assuming that it is full path):" + systemID );
- }
- try
+ m_defaultFactory = (SourceFactory) m_factorySelector.select("*");
+ source = m_defaultFactory.getSource( systemID, parameters );
+ }
+ catch (ServiceException se )
{
- final URLSource urlSource =
- (URLSource)this.m_urlSourceClass.newInstance();
- urlSource.init( ( new File( systemID ) ).toURL(), parameters );
- source = urlSource;
- }
- catch( Exception ie )
+ throw new SourceException( "Unable to select source factory for " + systemID, se );
+ }
+ finally
{
- throw new SourceException( "Unable to create new instance of " +
- this.m_urlSourceClass, ie );
+ m_factorySelector.release( m_defaultFactory );
+ m_defaultFactory = null;
}
}
}
@@ -383,8 +348,29 @@
}
catch( ServiceException ce )
{
- //no factory available, so use fallback
- //throw new CascadingRuntimeException( "Unable to select source factory for protocol " + protocol, ce );
+ // no factory found, so use default factory
+ if ( null != m_defaultFactory)
+ {
+ // thread safe default factory
+ m_defaultFactory.release( source );
+ }
+ else
+ {
+ try
+ {
+ m_defaultFactory = (SourceFactory) m_factorySelector.select("*");
+ m_defaultFactory.release( source );
+ }
+ catch (ServiceException se )
+ {
+ throw new CascadingRuntimeException( "Unable to select source factory for " + source.getURI(), se );
+ }
+ finally
+ {
+ m_factorySelector.release( m_defaultFactory );
+ m_defaultFactory = null;
+ }
+ }
}
finally
{
1.19 +3 -13 jakarta-avalon-excalibur/sourceresolve/src/java/org/apache/excalibur/source/impl/URLSource.java
Index: URLSource.java
===================================================================
RCS file: /home/cvs/jakarta-avalon-excalibur/sourceresolve/src/java/org/apache/excalibur/source/impl/URLSource.java,v
retrieving revision 1.18
retrieving revision 1.19
diff -u -r1.18 -r1.19
--- URLSource.java 29 Jan 2003 06:56:01 -0000 1.18
+++ URLSource.java 30 Jan 2003 07:57:10 -0000 1.19
@@ -84,16 +84,6 @@
extends AbstractSource
implements Source
{
- /** With this parameter you can specify the method to use for a http request.
- * Default is GET.
- */
- static public final String HTTP_METHOD = "org.apache.avalon.excalibur.source.Source.http.method";
-
- /** With this parameter you can specify additional request parameters which are
- * appended to the URI.
- */
- static public final String REQUEST_PARAMETERS = "org.apache.avalon.excalibur.source.Source.request.parameters";
-
/** Identifier for file urls */
protected final String FILE = "file:";
@@ -152,8 +142,8 @@
this.isPost = false;
if( null != parameters )
{
- this.parameters = (SourceParameters)parameters.get( REQUEST_PARAMETERS );
- final String method = (String)parameters.get( HTTP_METHOD );
+ this.parameters = (SourceParameters)parameters.get( SourceResolver.URI_PARAMETERS );
+ final String method = (String)parameters.get( SourceResolver.METHOD );
if( "POST".equalsIgnoreCase( method ) )
this.isPost = true;
}
1.8 +16 -4 jakarta-avalon-excalibur/sourceresolve/src/java/org/apache/excalibur/source/SourceResolver.java
Index: SourceResolver.java
===================================================================
RCS file: /home/cvs/jakarta-avalon-excalibur/sourceresolve/src/java/org/apache/excalibur/source/SourceResolver.java,v
retrieving revision 1.7
retrieving revision 1.8
diff -u -r1.7 -r1.8
--- SourceResolver.java 29 Jan 2003 06:56:01 -0000 1.7
+++ SourceResolver.java 30 Jan 2003 07:57:10 -0000 1.8
@@ -85,9 +85,21 @@
{
String ROLE = SourceResolver.class.getName();
+ /** With this parameter you can specify the method to use for getting
+ * the content. It is up to the protocol implementation ({@link
+ * SourceFactory}) to support this or not
+ */
+ String METHOD = "org.apache.avalon.excalibur.source.Source.uri.method";
+
+ /** With this parameter you can specify additional request parameters which are
+ * appended to the URI. It is up to the protocol implementation ({@link
+ * SourceFactory}) to support this or not.
+ */
+ String URI_PARAMETERS = "org.apache.excalibur.source.Source.uri.parameters";
+
/**
- * Get a <code>Source</code> object.
- * This is a shortcut for <code>resolve(location, null, null)</code>
+ * Get a {@link Source} object. This is a shortcut for {@link #resolve
+ * (String, String, Map)}.
*
* @return the resolved source object.
* @throws MalformetURLException if <code>location</code> is malformed.
@@ -97,7 +109,7 @@
throws MalformedURLException, IOException;
/**
- * Get a <code>Source</code> object.
+ * Get a {@link Source} object.
* @param location - the URI to resolve. If this is relative it is either
* resolved relative to the base parameter (if not null)
* or relative to a base setting of the source resolver
1.5 +81 -1 jakarta-avalon-excalibur/sourceresolve/src/java/org/apache/excalibur/source/SourceUtil.java
Index: SourceUtil.java
===================================================================
RCS file: /home/cvs/jakarta-avalon-excalibur/sourceresolve/src/java/org/apache/excalibur/source/SourceUtil.java,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -r1.4 -r1.5
--- SourceUtil.java 29 Jan 2003 06:56:01 -0000 1.4
+++ SourceUtil.java 30 Jan 2003 07:57:10 -0000 1.5
@@ -57,6 +57,8 @@
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.util.BitSet;
import java.util.Iterator;
@@ -68,6 +70,7 @@
* Utility class for source resolving.
*
* @author <a href="mailto:cziegeler@apache.org">Carsten Ziegeler</a>
+ * @author <a href="mailto:stephan@apache.org">Stephan Michels</a>
* @version CVS $Revision$ $Date$
*/
public final class SourceUtil
@@ -324,4 +327,81 @@
}
return null;
}
+
+ /**
+ * Move the source to a specified destination.
+ *
+ * @param source Source of the source.
+ * @param destination Destination of the source.
+ *
+ * @throws SourceException If an exception occurs during
+ * the move.
+ */
+ static public void move(Source source,
+ Source destination)
+ throws SourceException
+ {
+ if (source instanceof MoveableSource
+ && source.getClass().equals(destination.getClass()))
+ {
+ ((MoveableSource)source).move(destination);
+ }
+ else if (source instanceof ModifiableSource)
+ {
+ copy(source, destination);
+ ((ModifiableSource) source).delete();
+ }
+ else
+ {
+ throw new SourceException("Source '"+source.getURI()+ "' is not writeable");
+ }
+ }
+
+ /**
+ * Copy the source to a specified destination.
+ *
+ * @param source Source of the source.
+ * @param destination Destination of the source.
+ *
+ * @throws SourceException If an exception occurs during
+ * the copy.
+ */
+ static public void copy(Source source,
+ Source destination)
+ throws SourceException {
+ if (source instanceof MoveableSource
+ && source.getClass().equals(destination.getClass()))
+ {
+ ((MoveableSource) source).copy(destination);
+ }
+ else
+ {
+ if ( !(destination instanceof ModifiableSource)) {
+ throw new SourceException("Source '"+
+ destination.getURI()+
+ "' is not writeable");
+ }
+
+ try {
+ OutputStream out = ((ModifiableSource) destination).getOutputStream();
+ InputStream in = source.getInputStream();
+
+ byte[] buffer = new byte[8192];
+ int length = -1;
+
+ while ((length = in.read(buffer))>-1) {
+ out.write(buffer, 0, length);
+ }
+ in.close();
+ out.flush();
+ out.close();
+ } catch (IOException ioe) {
+ throw new SourceException("Could not copy source '"+
+ source.getURI()+"' to '"+
+ destination.getURI()+"' :"+
+ ioe.getMessage(), ioe);
+ }
+ }
+ }
+
}
1.1 jakarta-avalon-excalibur/sourceresolve/src/java/org/apache/excalibur/source/MoveableSource.java
Index: MoveableSource.java
===================================================================
/* ====================================================================
* The Apache Software License, Version 1.1
*
* Copyright (c) 2002 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution,
* if any, must include the following acknowledgment:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowledgment may appear in the software
* itself, if and wherever such third-party acknowledgments
* normally appear.
*
* 4. The names "Jakarta", "Avalon", and "Apache Software Foundation"
* must not be used to endorse or promote products derived from this
* software without prior written permission. For written
* permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache",
* nor may "Apache" appear in their name, without prior written
* permission of the Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*/
package org.apache.excalibur.source;
import org.apache.excalibur.source.Source;
import org.apache.excalibur.source.SourceException;
/**
* This class marks a source to be able to moved and copied to
* serveral other locations. This class should only be used if
* the implementations details should be hidden, otherwise
* the class SourceUtils can be used.
*
* @author <a href="stephan@apache.org">Stephan Michels</a>
* @version CVS $Id: MoveableSource.java,v 1.1 2003/01/30 07:57:10 cziegeler Exp $
*/
public interface MoveableSource extends Source
{
/**
* Copy the current source to a specified destination.
*
* @param destination Destination of the source.
*
* @throws SourceException If an exception occurs during
* the copy.
*/
void copy(Source destination) throws SourceException;
/**
* Move the current source to a specified destination.
*
* @param destination Destination of the source.
*
* @throws SourceException If an exception occurs during
* the move.
*/
void move(Source source) throws SourceException;
}
---------------------------------------------------------------------
To unsubscribe, e-mail: avalon-cvs-unsubscribe@jakarta.apache.org
For additional commands, e-mail: avalon-cvs-help@jakarta.apache.org