All Downloads are FREE. Search and download functionalities are using the official Maven repository.

src.main.java.com.eva.properties.Proxy Maven / Gradle / Ivy

Go to download

Advanced properties with object factories, references and inheritance.

There is a newer version: 0.3
Show newest version
/*
 * $Id: Proxy.java 43 2007-02-27 21:30:21Z max $
 * 
 * Copyright (c) 2006-2007 Maximilian Antoni. All rights reserved.
 * 
 * This software is licensed as described in the file LICENSE.txt, which you
 * should have received as part of this distribution. The terms are also
 * available at http://www.maxantoni.de/projects/eva-properties/license.txt.
 */
package com.eva.properties;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.logging.Level;

/**
 * is a proxy for another properties file referenced by a path.
 * 
 * @author Max Antoni
 * @version $Revision: 43 $
 */
public class Proxy implements Replaceable {

    private static interface Type {
        Object resolve(Properties inParent, Context inContext);
    }
    
    private static class TypePath implements Type {
        private String path;
        
        TypePath(String inPath) {
            path = inPath;
        }
        
        /*
         * @see com.eva.properties.Proxy.Type#resolve(
         *      com.eva.properties.Properties, com.eva.properties.Context)
         */
        public Object resolve(Properties inParent, Context inContext) {
            String replacedPath = Replacer.replace(path, inContext);
            DataSource dataSource;
            try {
                dataSource = new DataSource(replacedPath);
            }
            catch(FileNotFoundException e) {
                /*
                 * This might happen a couple of times when a switch is
                 * processed.
                 */
                Log.INSTANCE.log(Level.FINE, "PropertiesProxy.fileNotFound",
                        replacedPath);
                return NoProperties.INSTANCE;
            }
            if(Log.INSTANCE.isLoggable(Level.CONFIG)) {
                Log.INSTANCE.log(Level.CONFIG,
                        "PropertiesProxy.readingProperties", dataSource
                                .toString());
            }
            try {
                if(inParent == null) {
                    return PropertiesParser.read(dataSource, false);
                }
                return PropertiesParser.read(inParent, dataSource);
            }
            catch(IOException e) {
                throw new PropertiesException(
                        "IOException while reading super properties "
                                + replacedPath + ": " + e.getMessage());
            }
            catch(PropertiesException e) {
                throw new PropertiesException(
                        "PropertiesException while reading super properties "
                                + replacedPath + ": " + e.getMessage());
            }
        }
        
        /*
         * @see java.lang.Object#toString()
         */
        public String toString() {
            return "&\"" + path + "\"";
        }
    }
    
    private static class TypeReplaceable implements Type {
        private Replaceable replaceable;
        
        TypeReplaceable(Replaceable inReplaceable) {
            replaceable = inReplaceable;
        }
        
        /*
         * @see com.eva.properties.Proxy.Type#resolve(
         *      com.eva.properties.Properties, com.eva.properties.Context)
         */
        public Object resolve(Properties inParent, Context inContext) {
            return replaceable.replace(inContext);
        }
        
        /*
         * @see java.lang.Object#toString()
         */
        public String toString() {
            return "&" + replaceable.toString();
        }
        
    }
    
    private Object object;
    private Properties parent;
    private Type type;
    
    /**
     * creates a proxy with a path.
     * 
     * @param inPath the path.
     */
    public Proxy(String inPath) {
        if(inPath == null) {
            throw new NullPointerException("Path cannot be null.");
        }
        if("".equals(inPath)) {
            throw new IllegalArgumentException("Path cannot be empty.");
        }
        type = new TypePath(inPath);
    }
    
    Proxy(Properties inParent, Replaceable inReplaceable) {
        parent = inParent;
        if(inReplaceable == null) {
            throw new NullPointerException("Replaceable cannot be null.");
        }
        type = new TypeReplaceable(inReplaceable);
    }
    
    /**
     * creates a proxy with parent properties and a path.
     *  
     * @param inParent the parent properties. 
     * @param inPath the path.
     */
    Proxy(Properties inParent, String inPath) {
        this(inPath);
        parent = inParent;
    }

    /**
     * creates a proxy with parent properties and a type.
     *  
     * @param inParent the parent properties. 
     * @param inType the type.
     */
    private Proxy(Properties inParent, Type inType) {
        this(inType);
        parent = inParent;
    }
    
    private Proxy(Type inType) {
        type = inType;
    }
    
    /*
     * @see com.eva.properties.Replaceable#copy(com.eva.properties.Properties)
     */
    public Replaceable copy(Properties inParent) {
        Proxy proxy = new Proxy(inParent, type);
        if(object != null) {
            if(object instanceof Replaceable) {
                proxy.object = ((Replaceable) object).copy(inParent);
            }
            else {
                proxy.object = object;
            }
        }
        return proxy;
    }
    
    /* 
     * @see com.eva.properties.Replaceable#replace(com.eva.properties.Context)
     */
    public Object replace(Context inContext) throws PropertiesException {
        if(object == null) {
            /*
             * Set object to NoProperties instance to avoid recursive call when
             * a proxy is used for "super" and inside the super reference
             * something needs to be replaced.
             */
            object = NoProperties.INSTANCE;
            object = type.resolve(parent, inContext);
        }
        return object;
    }

    /* 
     * @see java.lang.Object#toString()
     */
    public String toString() {
        return type.toString();
    }

    /*
     * @see com.eva.properties.Replaceable#toString(java.lang.StringBuffer,
     *      java.lang.String)
     */
    public void write(Writer inoutWriter) {
        inoutWriter.append(type.toString());
        inoutWriter.append("\n");
    }

    /**
     * sets the parent properties for this proxy.
     * 
     * @param inParent the parent properties.
     */
    void setParent(Properties inParent) {
        parent = inParent;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy