Simboss asked for notes on IRC just now ... here is the "Example" file I started people working on (just so I could see what was under discussion). This probably needs to be supplemented with a bit of javadoc editing help.

The first file (DefaultFactory) was started as the "one finder to rule them all) Tthe second file (ShapefileDataStore) is a worked example of the approach used, please review the constructors)

Cheers,
Jody
package org.geotools;

import java.net.URL;

import org.geotools.factory.AbstractFactory;
import org.geotools.factory.Factory;
import org.geotools.factory.Hints;

public class ShapefileDataStore {
    
    public static Hints.Key FEATURE_TYPE_FACTORY = new 
Hints.Key(FeatureTypeFactory.class);
    private URL url;
    private Object featureTypeFactory;
    
    public ShapefileDataStore(URL url) {
        this(url, new ShapeFeatureTypeFactory());
    }

    public ShapefileDataStore(URL url, Hints hints) {
        this( url, FactoryFinder.factory( FEATURE_TYPE_FACTORY, hints, 
ShapeFeatureTypeFactory.class ) );
    }
    public ShapefileDataStore(URL url, FeatureTypeFactory factory) {
        this.url = url;
        featureTypeFactory = factory;
    }
 
    public void setFeatureTypeFactory( FeatureTypeFactory factory ){
         featureTypeFactory = factory;//DefaultFactory.ensure( factory, 
FeatureTypeFactory.class );
    }
    
    public void run( ) {
        FeautreTypeFactory factory = featureTypeFactory;
        
        if ( factory == null ) {
            factory = new ShapeFeatureTypeFactory();
        }
    }
    
    public static void main(String args[]){
        FactoryFinder.setOrdering( PojoFeatureTypeFactory.class, 
FeatureTypeFactory.class );
    }
}

class PojoFeatureTypeFactory extends FeatureTypeFactory {
}
class ShapeFeatureTypeFactory extends FeatureTypeFactory {
    public ShapeFeatureTypeFactory(){}
}
class FeatureTypeFactory {    
}
class FactoryFinder {
    public static FeatureTypeFactory factory(Hints.Key key, Hints hints, Class 
myDefault ){
        if( hints.containsKey( key )){
            return getFactory( key.getValueClass(), hints );            
        }
            
        if (factory.getClass().getName().startsWith("org.geotools")){
            return new ShapeFeatureTypeFactory();    
        }
        if( true ){ // do we have a user supplied "global" setting - say 
PojoFeatureTypeFactory
            return factory;
        }
        
    }
    public static FeatureTypeFactory getFactory(Class class1, Hints hints) {
        return null;
    }
    
}

main( ) {
    FactoryFinder.addFactoryRegister( new ServiceIterator );
    
}
/*
 *    GeoTools - OpenSource mapping toolkit
 *    http://geotools.org
 *    (C) 2006-2006, GeoTools Project Managment Committee (PMC)
 *
 *    This library is free software; you can redistribute it and/or
 *    modify it under the terms of the GNU Lesser General Public
 *    License as published by the Free Software Foundation;
 *    version 2.1 of the License.
 *
 *    This library is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *    Lesser General Public License for more details.
 */
package org.geotools;

import org.geotools.factory.Factory;

/**
 * Ease of use class for for Factory handling.
 * <p>
 * This class is intended for:
 * <ul>
 * <li><b>geotools users:</b> please use this class to find a "default" factory
 * implementation.</li>
 * <li><b>geotools implementators:</b> please use this class to ensure you have 
an
 * appropriate factory implementation.</li>
 * </ul>
 * Examples:<pre><code>
 * </code></pre>
 *
 * @author Jody Garnett, Refractions Research Inc.
 */
public class DefaultFactory {
    /**
     * User helper method to locate a shared "default" factory.
     * <p>
     * The default factory's are set once for your entire application.
     * <p>
     * You can set defaults directly for the entrie factory:<pre><code>
     *    DefaultFactory.
     * </pre></code>
     * 
     *  default( FactoryType type, class Type ) method.
     * Or you can make use of Pico or Spring contains as illustrated in the 
ext/pico and ext/spring.
     * </p>
     * @param factoryType
     * @return
     */
    public static Factory lookup( Class factoryType ){
       return null; 
    }

    /**
     * Implementor helper method for constructor injection.
     * <p>
     * Example of BestPractice:<pre><code>
     * 
     * public ShapefileDataStore( URL url ){
     *       this( url, new ShapeFeatureTypeFactory() );
     * }
     * 
     * public ShapefileDataStore( URL url, Hints hints){
     *       FeatureTypeFactory factory = FactoryFinder.getFactory( 
FeatureTypeFactory.class, hints);
     *       if (!(factory instanceof ShapeFeatureTypeFactory)) {
     *           // CASE 1:
     *           factory = new ShapeFeatureTypeFactory();
     *           
     *           // CASE 2:
     *           hints.put(FEATURE_TYPE_FACTORY, ShapeFeatureTypeFactory.class);
     *           factory = FactoryFinder.getFactory( FeatureTypeFactory.class, 
hints);
     *       } 
     *       this( url, FactoryFinder.getFactory( FeatureTypeFactory.class, 
hints, ShapeFeatureTypeFactory.class) );
     *       
     *       // -1 default feature factory type
     *       // 0 shapefile feature factory type
     *       // 1 pojo feature factory
     * }
     * public ShapefileDataStore( URL url, FeatureTypeFactory factory){
     *       this.url = url;
     *       featureTypeFactory = DefaultFactory.confirm( factory, 
FeatureTypeFactory.class ); 
     * }
     * </code></pre>
     * @param factory
     * @param factoryType
     * @return
     */
    public static Factory comfirm( Factory factory, Class factoryType ){
        if( factory != null ) {
            return factory;
        }
        if( factoryType.isInstance( factory )){
            throw new ClassCastException("Requred a "+factoryType );
        }
        return find( factoryType );        
    }
    /**
     * Implementor helper method for setter injection.
     * <p>
     * Example of BestPractice:<pre><code>
     * public void setFeatureTypeFactory( factory ){
     *       featureTypeFactory = DefaultFactory.ensure( factory, 
FeatureTypeFactory.class );
     * }
     * </code></pre>
     * 
     * @param factory
     * @param factoryType
     * @return
     */
    public static Factory ensure( Object factory, Class factoryType ){
        if( factory == null ) {
            throw new NullPointerException("Requred a "+factoryType );
        }
        if( factoryType.isInstance( factory )){
            throw new ClassCastException("Requred a "+factoryType );
        }
        return factory;
    }
    
    /**
     * Register this provided factory as a "default" for the entire library.
     * <p>
     * Please use this method to set a "default" as shown below:<pre><code>
     * public static void main( String args[] ){
     *     DefaultFactory.register( new PojoFeatureFactory() );
     * }
     * </pre></code>
     * 
     * Please note that your "default" will only be used as needed, many things 
like ShapefileDataStore
     * will make use of their own defaults that are "optimized" for efficiency. 
To take control in these
     * situtations please do the following:<pre></code>
     *      ShapefileDataStore shape = new ShapefileDataStore( url );
     *      shape.setFeatureFactory( new PojoFeatureFactory() );
     * </code></pre>
     * 
     * @param factory
     */
    public static void register( Factory factory ){
        
    }
    public static void register( Class factoryType ){
        
    }
    public static void synchornize( ContainerAdapter container ){
        
    }
    /**
     * This interface 
     *
     */
    public static interface ContainerAdapter {
        
    }
}
-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys -- and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
_______________________________________________
Geotools-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/geotools-devel

Reply via email to