⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 genericfactory.java

📁 p2p仿真器。开发者可以工作在覆盖层中进行创造和测试逻辑算法或者创建和测试新的服务。PlanetSim还可以将仿真代码平稳转换为在Internet上的实验代码
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
package planet.generic.commonapi.factory;

import java.util.Iterator;
import java.util.TreeMap;

import planet.commonapi.Application;
import planet.commonapi.EndPoint;
import planet.commonapi.Id;
import planet.commonapi.Message;
import planet.commonapi.Network;
import planet.commonapi.Node;
import planet.commonapi.NodeHandle;
import planet.commonapi.RouteMessage;
import planet.commonapi.behaviours.BehavioursFactory;
import planet.commonapi.behaviours.BehavioursFilter;
import planet.commonapi.behaviours.BehavioursInvoker;
import planet.commonapi.behaviours.BehavioursPattern;
import planet.commonapi.behaviours.BehavioursPool;
import planet.commonapi.behaviours.BehavioursRoleSelector;
import planet.commonapi.behaviours.exception.NoBehaviourDispatchedException;
import planet.commonapi.behaviours.exception.NoSuchBehaviourException;
import planet.commonapi.exception.InitializationException;
import planet.commonapi.factory.ApplicationFactory;
import planet.commonapi.factory.EndPointFactory;
import planet.commonapi.factory.IdFactory;
import planet.commonapi.factory.NetworkFactory;
import planet.commonapi.factory.NodeFactory;
import planet.commonapi.factory.NodeHandleFactory;
import planet.commonapi.factory.RouteMessagePool;
import planet.commonapi.results.ResultsConstraint;
import planet.commonapi.results.ResultsEdge;
import planet.commonapi.results.ResultsFactory;
import planet.commonapi.results.ResultsGenerator;
import planet.simulate.Logger;
import planet.util.KeyGen;
import planet.util.Properties;

/**
 * It is an abstraction class that follows the Factory Method pattern. It
 * offers different static methods to obtain differents instances of factories
 * and their related objects. 
 * <br><br>
 * From anywhere of this simulator one can use these methods to obtain the
 * required instance, using the current configuration attributes (using
 * loaded properties from planet.util.Properties).
 * 
 * @author <a href="mailto: jordi.pujol@estudiants.urv.es">Jordi Pujol</a>
 * 07-jul-2005
 */
public class GenericFactory {
    
    /**
     * The NetworkFactory that is used to invoke the 
     * NetworkFactory methods.
     */
    private static NetworkFactory factoryNetworkFactory = null;
    /**
     * The IdFactory that is used to invoke the 
     * IdFactory methods.
     */
    private static IdFactory factoryIdFactory = null;
    /**
     * The NodeHandleFactory that is used to invoke the nodehandle factory
     * methods.
     */
    private static NodeHandleFactory factoryNodeHandleFactory = null;
    /**
     * The NodeFactory that is used to invoke the node factory methods.
     */
    private static NodeFactory factoryNodeFactory = null;   
    /**
     * The ApplicationFactory that is used to invoke the application factory
     * methods.
     */
    private static ApplicationFactory factoryApplicationFactory = null;
    /**
     * The EndPointFactory that is used to invoke the endpoint factory methods.
     */
    private static EndPointFactory factoryEndPointFactory = null;
    /**
     * The BehavioursFactory that is used to invoke the behaviours factory methods.
     */
    private static BehavioursFactory behavioursFactory = null;
    /**
     * The BehavioursPool that is used to invoke the behaviours pool methods.
     */
    private static BehavioursPool behavioursPool = null;
    /**
     * The RouteMessagePool that is used to invoke the pool methods.
     */
    private static RouteMessagePool routeMessagePool = null;
    /**
     * The ResultsFactory instances for any results type loaded.
     */
    private static TreeMap resultsFactory = null;
    /**
     * The ResultsGenerator instances for any results type loaded.
     */
    private static TreeMap resultsGenerator = null;
    /**
     * Permits to generate an unique String key for RouteMessages that can send
     * any Node.
     */
    private static KeyGen keyGen = null;

    
    /**
     * Builds an instance of the specified class. Its internal behaviour
     * is the following:
     * <pre>
     *    return classReference.newInstance();
     * </pre>
     * @param classReference The class reference to use to build the new instance.
     * @return A new instance of the specified class.
     * @throws InitializationException if any error occurs during the 
     * initialization process, or when the <b>classReference</b> has a null value.
     */
    public static Object newInstance(Class classReference) throws InitializationException
    {
        if (classReference == null)
            throw new InitializationException("Using null class reference to build a new instance.");
        try {
            return classReference.newInstance();
        } catch (Exception e)
        {
            throw new InitializationException("Cannot build a new instance of '"+
                    classReference.getName()+"'.",e);
        }
    }
    
    /**
     * Initialize the GenericFactory to load the default properties file. This
     * way permits return the different factories with the same base properties:
     * the factories obtained with the methods withot parameters returns the
     * same class of instances.
     * 
     * @throws InitializationException
     *             if occurs any problem during initialization.
     */
    public static void init() throws InitializationException {
        //Initialization process
        
        //---> Pre-initialization (don't comment!!)
        preinit();
        
        //---> Updating data into factories
        update();
        
        if (Properties.isApplicationLevelActivated())
        {
            factoryApplicationFactory = buildApplicationFactory();
            factoryEndPointFactory    = buildEndPointFactory();
        }
        
        //---> Behaviours
        if (Properties.overlayWithBehaviours)
        {
            behavioursFactory         = buildBehavioursFactory();
        }

        //---> Results
        if (Properties.isResultsActivated())
        {
            resultsFactory            = new TreeMap();
            resultsGenerator          = new TreeMap();
            Iterator it = Properties.resultsUniqueName.keySet().iterator();
            String resultsName = null;
            ResultsFactory factory = null;
            while (it.hasNext())
            {
                resultsName = (String)it.next();
                factory = buildResultsFactory(resultsName);
                resultsFactory.put(resultsName,factory);
                resultsGenerator.put(resultsName,factory.buildGenerator());
            }
        }
        
        //---> others
        keyGen                    = new KeyGen(0, Integer.MAX_VALUE);
        Logger.setLevel(Properties.simulatorLogLevel);
    }

    /**
     * Updates all main factories with the required but uninitialized instance.
     * @throws InitializationException if occurs some error during the
     * initialization.
     */
    private static void preinit() throws InitializationException 
    {
        factoryNetworkFactory     = (NetworkFactory)newInstance(Properties.factoriesNetworkFactory);
        factoryIdFactory          = (IdFactory)newInstance(Properties.factoriesIdFactory);
        factoryNodeHandleFactory  = (NodeHandleFactory)newInstance(Properties.factoriesNodeHandleFactory);
        factoryNodeFactory        = (NodeFactory)newInstance(Properties.factoriesNodeFactory);
        routeMessagePool          = (RouteMessagePool)newInstance(Properties.factoriesRouteMessagePool);
    }

    /**
     * Updates the internal attributes with the required parameters for
     * fully functional factories.
     * @throws InitializationException if some error occurs during the
     * initialization.
     */
    private static void update() throws InitializationException
    {
        factoryNetworkFactory.setValues(Properties.factoriesNetwork,Properties.factoriesNetworkSize, 
                factoryNodeFactory,Properties.factoriesNetworkTopology);
        factoryIdFactory.setValues(Properties.overlayId,Properties.factoriesNetworkTopology,
                Properties.factoriesNetworkSize);
        factoryNodeHandleFactory.setValues(Properties.factoriesNodeHandle);
        factoryNodeFactory.setValues(factoryIdFactory,Properties.overlayNode);
    }

    /**
     * Make the post initializations to leave the GenericFactory complete.
     * @throws InitializationException if some error occurs during the
     * initialization.
     */
    public static void postinit() throws InitializationException
    {
        //---> Behaviours
        if (Properties.overlayWithBehaviours)
        {
            behavioursPool            = buildBehavioursPool();
        }

    }
    
    
    /* ************* NETWORK RELATED METHODS **********************************/
    
    /**
     * Builds a new instance of NetworkFactory.
     * 
     * @return A new instance of the NetworkFactory default implementation.
     * @throws InitializationException
     *             if occurs some problem with the factory initialization.
     */
    public static NetworkFactory buildNetworkFactory()
            throws InitializationException {
        return ((NetworkFactory)newInstance(Properties.factoriesNetworkFactory))
            .setValues(Properties.factoriesNetwork,Properties.factoriesNetworkSize, 
                    factoryNodeFactory,Properties.factoriesNetworkTopology);
    }
    
    /**
     * @see planet.commonapi.factory.NetworkFactory#buildNetwork()
     */
    public static Network buildNetwork() throws InitializationException {
        return factoryNetworkFactory.buildNetwork();
    }

    /**
     * @see planet.commonapi.factory.NetworkFactory#buildNetwork(int)
     */
    public static Network buildNetwork(int size) throws InitializationException {
        return factoryNetworkFactory.buildNetwork(size);
    }

    /**
     * @see planet.commonapi.factory.NetworkFactory#buildNetwork(int,
     *      planet.commonapi.factory.NodeFactory)
     */
    public static Network buildNetwork(int size, NodeFactory nodeFactory)
            throws InitializationException {
        return factoryNetworkFactory.buildNetwork(size, nodeFactory);
    }

    /**
     * @see planet.commonapi.factory.NetworkFactory#buildNetwork(int,
     *      java.lang.String)
     */
    public static Network buildNetwork(int size, String topology)
            throws InitializationException {
        return factoryNetworkFactory.buildNetwork(size, topology);
    }

    /**
     * @see planet.commonapi.factory.NetworkFactory#buildNetwork(int,
     *      planet.commonapi.factory.NodeFactory, java.lang.String)
     */
    public static Network buildNetwork(int size, NodeFactory nodeFactory,
            String topology) throws InitializationException {
        return factoryNetworkFactory.buildNetwork(size, nodeFactory, topology);
    }
    
    /* ************************* ID RELATED METHODS ***************************/
    
    /**

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -