commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Rami Ojares" <rami.oja...@elisa.fi>
Subject Re: [vfs] FileSystemManager construction rethought
Date Tue, 22 Jun 2004 23:30:22 GMT
Here are some small changes that I came across when making the changes compilable.

I changed configure method to init because there was already
private configure(String)
method in DefaultFileSystemManager but the existing init method
was without arguments.

I saw also that there is quite a lot of configuration code in place
(GlobalConfiguration class and the configure method mentioned above
that parses configuration from XML). But I did not see the overall idea
so you'll have to explain it to me Mario. But it all seems somehow
still coupled with the DefaultFileSystemManager.

One question that arose is that why would someone want to write
a different FileSystemManager implementation. Could the DefaultFileSystemManager
be made so configurable that this would not be needed?

Anyways, here is a compilable listing of the code I sent in the previous email.

- rami

***** VfsTask.java *****

package org.apache.commons.vfs.ant;

import org.apache.commons.vfs.*;
import org.apache.tools.ant.*;

/**
 * Super class of Vfs Ant tasks that takes care of FileSystemManager handling.
 */
public class VfsTask extends Task {
    
    private FileSystemManager manager;
    private AntLogger logger;
    
    protected FileSystemManager getManager() {
        if (manager == null)
            setManager(null);
        return manager;
    }
    
    protected void setManager(FileSystemConfiguration conf) {
        
        if (conf == null) {
            conf = new FileSystemConfiguration(
                "org.apache.commons.vfs.impl.StandardFileSystemManager"
            );
        }
        // put AntLogger always as logger
        conf.setParameter("logger", getLogger());
        
        this.manager = AntHelper.getManager(this, conf);
    }
    
    protected AntLogger getLogger() {
        if (logger == null)
            logger = new AntLogger(this);
        return logger;
    }
    
    /**
     * Resolves a URI to a file, relative to the project's base directory.
     *
     * @param uri The URI to resolve.
     */
    protected FileObject resolveFile(final String uri) {
        try {
            return getManager().resolveFile(getProject().getBaseDir(), uri);
        }
        catch (FileSystemException fse) {
            throw new BuildException(fse);
        }
    }
    
}


***** VfsDataType.java *****

package org.apache.commons.vfs.ant;

import org.apache.commons.vfs.*;
import org.apache.tools.ant.types.*;
import org.apache.tools.ant.BuildException;

/**
 * Super class of Vfs Ant data types that takes care of FileSystemManager
 * handling.
 */
public class VfsDataType extends DataType {
    
    private FileSystemManager manager;
    private AntLogger logger;
    
    protected FileSystemManager getManager() {
        if (manager == null)
            setManager(null);
        return manager;
    }
    
    protected void setManager(FileSystemConfiguration conf) {
        
        if (conf == null) {
            conf = new FileSystemConfiguration(
                "org.apache.commons.vfs.impl.StandardFileSystemManager"
            );
        }
        // put AntLogger always as logger
        conf.setParameter("logger", getLogger());
        
        this.manager = AntHelper.getManager(this, conf);
    }
    
    protected AntLogger getLogger() {
        if (logger == null)
            logger = new AntLogger(this);
        return logger;
    }
    
    /**
     * Resolves a URI to a file, relative to the project's base directory.
     *
     * @param uri The URI to resolve.
     */
    protected FileObject resolveFile(final String uri) {
        try {
            return getManager().resolveFile(getProject().getBaseDir(), uri);
        }
        catch (FileSystemException fse) {
            throw new BuildException(fse);
        }
    }
    
}


***** AntHelper.java *****

package org.apache.commons.vfs.ant;

import org.apache.commons.vfs.*;
import org.apache.tools.ant.*;
import java.util.*;

/**
 * Holds a map of FileSystemKey -> FileSystemManger
 * When a Task or DataType asks for FileSystemManger, AntHelper looks it up
 * in the map. If it does not exist, it is created, added to the map and a
 * BuildListener is added to the project so that when the project finishes it
 * closes the FileSystemManger and removes it from the map.
 */
public class AntHelper {
    
    private static Map managers = new HashMap();
    
    public static FileSystemManager getManager(ProjectComponent projectComponent, FileSystemConfiguration
conf) {
        
        FileSystemKey key = new FileSystemKey(projectComponent.getProject(), conf);
        
        FileSystemManager manager = (FileSystemManager) managers.get(key);
        if (manager == null) {
            try {
                manager = VFS.createManager(conf);
            }
            catch (FileSystemException fse) {
                throw new BuildException(fse);
            }
            projectComponent.getProject().addBuildListener(new CloseListener(key));
            managers.put(key, manager);
        }
        
        return manager;
    }
    
    static void close(FileSystemKey key) {
        FileSystemManager manager = (FileSystemManager) managers.get(key);
        if (manager == null) {
            manager.close();
            managers.remove(manager);
        }
    }
    
}

/**
 * Combines Project and FileSystemConfiguration into a single key that is mapped
 * to a FileSystemManager.
 */
class FileSystemKey {
    
    private Project project;
    private FileSystemConfiguration fsConf;
    
    FileSystemKey(Project project, FileSystemConfiguration fsConf) {
        this.project = project;
        this.fsConf = fsConf;
    }
    
    public boolean equals(Object o) {
        
        if (o == null) return false;
        
        FileSystemKey comparand;
        try {
            comparand = (FileSystemKey) o;
        }
        catch (ClassCastException cce) {
            return false;
        }
        
        if (project.equals(comparand.getProject())) {
            // since fsConf can be null we have to deal with that separately
            if (fsConf == null) {
                if (comparand.getFileSystemConfiguration() == null)
                    return true;
                else
                    return false;
            }
            
            if (fsConf.equals(comparand.getFileSystemConfiguration()))
                return true;
        }
        
        return false;
    }
    
    Project getProject() {
        return project;
    }
    
    FileSystemConfiguration getFileSystemConfiguration() {
        return fsConf;
    }
}

/**
 * Closes the VFS manager when the project finishes.
 */
class CloseListener implements BuildListener {
    
    private FileSystemKey key;
    
    CloseListener(FileSystemKey key) {
        this.key = key;
    }
    
    public void buildFinished(BuildEvent event)
    {
        AntHelper.close(key);
    }

    public void buildStarted(BuildEvent event)
    {
    }

    public void messageLogged(BuildEvent event)
    {
    }

    public void targetFinished(BuildEvent event)
    {
    }

    public void targetStarted(BuildEvent event)
    {
    }

    public void taskFinished(BuildEvent event)
    {
    }

    public void taskStarted(BuildEvent event)
    {
    }
}

***** AntLogger.java *****

package org.apache.commons.vfs.ant;

import org.apache.commons.logging.*;
import org.apache.tools.ant.*;

/**
 * A commons-logging wrapper for Ant logging.
 */
public class AntLogger implements Log {
    
    private ProjectComponent pc;
    
    public AntLogger(ProjectComponent pc) {
        this.pc = pc;
    }
    
    public void debug(final Object o)
    {
        pc.log(String.valueOf(o), Project.MSG_DEBUG);
    }

    public void debug(Object o, Throwable throwable)
    {
        debug(o);
    }

    public void error(Object o)
    {
        pc.log(String.valueOf(o), Project.MSG_ERR);
    }

    public void error(Object o, Throwable throwable)
    {
        error(o);
    }

    public void fatal(Object o)
    {
        pc.log(String.valueOf(o), Project.MSG_ERR);
    }

    public void fatal(Object o, Throwable throwable)
    {
        fatal(o);
    }

    public void info(Object o)
    {
        pc.log(String.valueOf(o), Project.MSG_INFO);
    }

    public void info(Object o, Throwable throwable)
    {
        info(o);
    }

    public void trace(Object o)
    {
    }

    public void trace(Object o, Throwable throwable)
    {
    }

    public void warn(Object o)
    {
        pc.log(String.valueOf(o), Project.MSG_WARN);
    }

    public void warn(Object o, Throwable throwable)
    {
        warn(o);
    }

    public boolean isDebugEnabled()
    {
        return true;
    }

    public boolean isErrorEnabled()
    {
        return true;
    }

    public boolean isFatalEnabled()
    {
        return true;
    }

    public boolean isInfoEnabled()
    {
        return true;
    }

    public boolean isTraceEnabled()
    {
        return false;
    }

    public boolean isWarnEnabled()
    {
        return true;
    }
}

***** FileSystemConfiguration.java *****

package org.apache.commons.vfs;

import org.apache.commons.vfs.*;
import org.apache.tools.ant.*;
import java.util.*;

/**
 * Data structure that holds a fully qualified classname (name of the
 * FileSystemManager implementation class) and a map of parameters (name/value
 * pairs that are given to the FileSystemManager implementation class.
 */
public class FileSystemConfiguration {
    
    String className;
    Map parameters = new HashMap();
    
    public FileSystemConfiguration(String className) {
        this.className = className;
    }
    
    public String getClassName() {
        return className;
    }
    
    public void setParameter(String name, Object value) {
        parameters.put(name, value);
    }
    
    public Object getParameter(String name) {
        return parameters.get(name);
    }
    
    private Map getParameters() {
        return parameters;
    }
    
    public boolean equals(Object o) {
        
        if (o == null) return false;
        
        FileSystemConfiguration comparand;
        try {
            comparand = (FileSystemConfiguration) o;
        }
        catch (ClassCastException cce) {
            return false;
        }
        
        if (
            className.equals(comparand.getClassName()) &&
            parameters.equals(comparand.getParameters())
        )
            return true;
        else
            return false;
    }
}

***** VFS.java *****

/*
 * Copyright 2002, 2003,2004 The Apache Software Foundation.
 * 
 * Licensed 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.commons.vfs;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * The main entry point for the VFS.  Used to create {@link FileSystemManager}
 * instances.
 *
 * @author <a href="mailto:adammurdoch@apache.org">Adam Murdoch</a>
 * @version $Revision: 1.8 $ $Date: 2004/05/17 20:13:20 $
 */
public class VFS
{
    private static FileSystemManager defaultSharedInstance;

    private VFS()
    {
    }

    /**
     * Returns the default shared {@link FileSystemManager} instance.
     */
    public static synchronized FileSystemManager getManager()
        throws FileSystemException
    {
        if (defaultSharedInstance == null)
        {
            FileSystemConfiguration conf = new FileSystemConfiguration(
                "org.apache.commons.vfs.impl.StandardFileSystemManager"
            );
            defaultSharedInstance = createManager(conf);
        }
        return defaultSharedInstance;
    }
    
    /**
     * Creates a FileSystemManager instance.
     * 
     * @param conf is the FileSystemConfiguration used to create a
     * FileSystemManager. If it is null then the default shared manager is
     * returned.
     */
    public static FileSystemManager createManager(FileSystemConfiguration conf) throws FileSystemException
{
        
        if (conf == null)
            return getManager();
        
        try {
            Class clazz = Class.forName(conf.getClassName());
            FileSystemManager fsm = (FileSystemManager) clazz.newInstance();
            fsm.init(conf);
            return fsm;
        }
        catch(Exception e) {
            throw new FileSystemException("vfs/create-manager.error");
        }
    }
    
}

***** FileSystemManager - partially *****

/**
 * Called after instantiation and before use. Implementation can ignore
 * calls to this method after the first call and even throw a
 * RuntimeException if they so desire.
 */
void init(FileSystemConfiguration conf) throws FileSystemException;

/**
 * Release all the resources, close connections etc. After this method this
 * FileSystemManager is not expected to work anymore.
 */
void close();


---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Mime
View raw message