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

📄 properties.java

📁 p2p仿真器。开发者可以工作在覆盖层中进行创造和测试逻辑算法或者创建和测试新的服务。PlanetSim还可以将仿真代码平稳转换为在Internet上的实验代码
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
        Properties.overlayPropertiesInstance.postinit(properties);
        if (Properties.overlayWithBehaviours)
            Properties.behavioursPropertiesInstance.postinit(properties);
        if (Properties.activatedResults)
            postInitResults();
    }
    
    /**
     * Resets all attributes to its initial values or null by default.
     */
    private static void resetAttributes()
    {
        /* Factories attributes: */
        factoriesNetworkFactory        = null;
        factoriesIdFactory             = null;
        factoriesNodeHandleFactory     = null;
        factoriesNodeFactory           = null;
        factoriesRouteMessagePool      = null;
        
        factoriesNetwork               = null;
        factoriesNodeHandle            = null;
        factoriesRouteMessage          = null;
        factoriesNetworkTopology       = null;
        factoriesNetworkSize           = 0;
        
        factoriesApplicationFactory    = null;
        factoriesEndPointFactory       = null;

        factoriesApplication           = null;
        factoriesEndPoint              = null;

        /* Simulator attributes: */
        simulatorSimulationSteps       = 0;
        simulatorLogLevel              = 0;
        simulatorPrintLevel            = 0;
        simulatorEnvironment           = null;
        simulatorQueueSize             = 0;
        simulatorProcessedMessages     = 0;

        simulatorEventFile             = null;
        
        /* Serialization attributes: */
        serializedInputFile            = null;
        serializedOutputFile           = null;
        serializedOutputFileReplaced   = true;
        
        /* Behaviours attributes: */
        behavioursFactory              = null; 
        behavioursPool                 = null;
        behavioursRoleSelector         = null;
        behavioursInvoker              = null;
        behavioursFilter               = null;
        behavioursPattern              = null;
        behavioursProperties           = null;
        behavioursPropertiesInstance   = null;
        behavioursNumberOfTypes        = 0;
        behavioursNumberOfModes        = 0;

        /* Overlay attributes: */
        overlayId                      = null;
        overlayNode                    = null;
        overlayProperties              = null;
        overlayPropertiesInstance      = null;
        overlayWithBehaviours          = false;
        
        /* Results attributes: */
        resultsFactory                 = null;
        resultsEdge                    = null;
        resultsConstraint              = null;
        resultsGenerator               = null;
        resultsProperties              = null;
        resultsPropertiesInstance      = null;
        resultsUniqueName              = null;

        /* Internal attributes: */
        properties                     = null;
        activatedApplicationLevel      = false;
        activatedEvents                = false;
        activatedSerialization         = false;
        activatedResults               = false;
    }
    
    /**
     * Makes the postinitialization process for each result properties.
     * @throws InitializationException if an error occurs during
     * the initialization of the different properties.
     * @see planet.util.PropertiesInitializer#postinit(planet.util.PropertiesWrapper)
     */
    private static void postInitResults() throws InitializationException
    {
        for (int i=0; i < Properties.resultsPropertiesInstance.size(); i++)
        {
            ((PropertiesInitializer)Properties.resultsPropertiesInstance.get(i)).postinit(properties);
        }
    }
    
    /**
     * Loads the entire specific properties file for the current simulation.
     * The steps to do it are the followings:
     * <ol type="1">
     * <li> Loads the <b>masterFilename</b> with the filename of the specific properties file to use.</li>
     * <li> Loads the specific properties file with the attributes for the current simulation.</li>
     * </ol>
     * @param masterFilename Master filename where is specified the filename
     * with the specific properties file for the current simulation.
     * @param mainPropertyName The key that appears into the <b>masterFilename</b>
     * with the final filename with required configuration.
     * @return The java.util.Properties with all specific properties for the current simulation.
     * @throws InitializationException if occurs any error opening the files or loading the properties.
     */
    private static PropertiesWrapper loadProperties(String masterFilename, String mainPropertyName) throws InitializationException
    {
        FileInputStream fis = null;
        PropertiesWrapper prop = new PropertiesWrapper(); 
        
        //loads the master properties file, with the name of the specific properties file
        try {
            fis = new FileInputStream(masterFilename);
            prop.load(fis);
            fis.close();
        } catch (FileNotFoundException e) {
            throw new InitializationException("The file '"+masterFilename+"' not exists",e);
        } catch (IOException e) {
            throw new InitializationException("Cannot load the file '"+masterFilename+"'",e);
        }
        
        //loads the specified properties file for the current simulation
        String filename = prop.getProperty(mainPropertyName);
        if (filename == null)
            throw new InitializationException("The properties file '"+masterFilename+"' doesn't contain the main property '"+mainPropertyName+"'");
        prop.clear();
        try {
            fis = new FileInputStream(filename);
            prop.load(fis);
            fis.close();
        } catch (FileNotFoundException e) {
            throw new InitializationException("The properties file '"+filename+"' specified in the master file not exists",e);
        } catch (IOException e) {
            throw new InitializationException("Cannot load the properties file '"+filename+"' specified in the master file",e);
        }
        return prop;
    }

    /**
     * Builds an instance of the PropertiesInitializer implementation and loads
     * its specific attributes.
     * @param classReference Class reference to use to build a new instance.
     * @return A new built and initialized instance of the related class.
     * @throws InitializationException if any error occurs during the 
     * initialization.
     */
    private static PropertiesInitializer loadPropertiesInitializer(Class classReference) throws InitializationException
    {
        PropertiesInitializer props = null;
        try {
            props = (PropertiesInitializer)classReference.newInstance();
            props.init(properties); //loads its specific properties
            return props;
        } catch (Exception e)
        {
            throw new InitializationException("Cannot build an instance of PropertiesInitializer named '"+classReference.getName()+"'",e);
        }
    }

    /**
     * Load the base implementation classes to run the current simulation.
     * @throws InitializationException if any error has ocurred during the
     * loading process.
     */
    private static void loadFactoriesAttributes() throws InitializationException
    {
        /* Factories classes: */
        Properties.factoriesNetworkFactory    = properties.getPropertyAsClass(FACTORIES_NETWORKFACTORY);
        Properties.factoriesIdFactory         = properties.getPropertyAsClass (FACTORIES_IDFACTORY);
        Properties.factoriesNodeHandleFactory = properties.getPropertyAsClass (FACTORIES_NODEHANDLEFACTORY);
        Properties.factoriesNodeFactory       = properties.getPropertyAsClass (FACTORIES_NODEFACTORY);
        Properties.factoriesRouteMessagePool  = properties.getPropertyAsClass (FACTORIES_ROUTEMESSAGEPOOL);

        /* Specific classes: */
        Properties.factoriesNetwork           = properties.getPropertyAsClass (FACTORIES_NETWORK);
        Properties.factoriesNodeHandle        = properties.getPropertyAsClass (FACTORIES_NODEHANDLE);
        Properties.factoriesRouteMessage      = properties.getPropertyAsClass (FACTORIES_ROUTEMESSAGE);
        
        /* Other attributes: */
        Properties.factoriesNetworkTopology   = properties.getProperty(FACTORIES_NETWORKTOPOLOGY);
        Properties.factoriesNetworkSize       = properties.getPropertyAsInt(FACTORIES_NETWORKSIZE);
        if (Properties.factoriesNetworkSize < 0) {
            throw new InitializationException("The network size '"+Properties.factoriesNetworkSize+"' are invalid. Must be equal to or greater than zero.");
        }
        
        //testing interfaces implementation
        Interfaces.ensureImplementedInterfaceOrClass(Properties.factoriesNetworkFactory,   Interfaces.FACTORIES_NETWORKFACTORY,   FACTORIES_NETWORKFACTORY);
        Interfaces.ensureImplementedInterfaceOrClass(Properties.factoriesIdFactory,        Interfaces.FACTORIES_IDFACTORY,        FACTORIES_IDFACTORY);
        Interfaces.ensureImplementedInterfaceOrClass(Properties.factoriesNodeHandleFactory,Interfaces.FACTORIES_NODEHANDLEFACTORY,FACTORIES_NODEHANDLEFACTORY);
        Interfaces.ensureImplementedInterfaceOrClass(Properties.factoriesNodeFactory,      Interfaces.FACTORIES_NODEFACTORY,      FACTORIES_NODEFACTORY);
        Interfaces.ensureImplementedInterfaceOrClass(Properties.factoriesRouteMessagePool, Interfaces.FACTORIES_ROUTEMESSAGEPOOL, FACTORIES_ROUTEMESSAGEPOOL);
        
        Interfaces.ensureImplementedInterfaceOrClass(Properties.factoriesNetwork,          Interfaces.FACTORIES_NETWORK,          FACTORIES_NETWORK);
        Interfaces.ensureImplementedInterfaceOrClass(Properties.factoriesNodeHandle,       Interfaces.FACTORIES_NODEHANDLE,       FACTORIES_NODEHANDLE);
        Interfaces.ensureImplementedInterfaceOrClass(Properties.factoriesRouteMessage,     Interfaces.FACTORIES_ROUTEMESSAGE,     FACTORIES_ROUTEMESSAGE);
    }

    /**
     * Loads all specific simulator attributes from the current properties.
     * @throws InitializationException if occurs any error during the loading process
     * of the attributes.
     */
    private static void loadSimulatorAttributes() throws InitializationException
    {
        Properties.simulatorSimulationSteps   = properties.getPropertyAsInt(SIMULATOR_SIMULATION_STEPS);
        Properties.simulatorLogLevel          = properties.getPropertyAsInt(SIMULATOR_LOG_LEVEL);
        Properties.simulatorPrintLevel        = properties.getPropertyAsInt(SIMULATOR_PRINT_LEVEL);
        Properties.simulatorEnvironment       = properties.getProperty(SIMULATOR_ENVIRONMENT);
        Properties.simulatorQueueSize         = properties.getPropertyAsInt(SIMULATOR_QUEUE_SIZE);
        Properties.simulatorProcessedMessages = properties.getPropertyAsInt(SIMULATOR_PROCESSED_MESSAGES);
        
        //testing the correctness of the values
        ensureValidSimulatorEnvironment(Properties.simulatorEnvironment);
        ensureValidPrintLevel(Properties.simulatorPrintLevel);
    }
    
    /**
     * Test if the <b>environment</b> is a valid simulator environment.
     * @param environment The value to be tested.
     * @throws InitializationException if the <b>environment</b> has an 
     * incorrect value.
     */
    private static void ensureValidSimulatorEnvironment(String environment) throws InitializationException
    {
        if (environment!=null && 
            (!environment.equals(Properties.SIMULATOR_EXPERIMENTAL_ENVIRONMENT) &&
             !environment.equals(Properties.SIMULATOR_SIMULATION_ENVIRONMENT)))
            throw new InitializationException("The environment specified at property '"+
                    Properties.SIMULATOR_ENVIRONMENT+"' are unknown (found '"+environment+"'). The correct values are: "+
                    Properties.SIMULATOR_EXPERIMENTAL_ENVIRONMENT+", "+
                    Properties.SIMULATOR_SIMULATION_ENVIRONMENT);
    }
    
    /**
     * Test if the <b>printLevel</b> is a valid print level.
     * @param printLevel The value to be tested.
     * @throws InitializationException if the <b>printLevel</b> has an 
     * incorrect value.
     */
    private static void ensureValidPrintLevel(int printLevel) throws InitializationException
    {
        switch (printLevel)
        {
        case SIMULATOR_NO_PRINT:
        case SIMULATOR_PRETTY_PRINT:
        case SIMULATOR_FULL_PRINT: return;
        default: throw new InitializationException("The print level specified at property '"
                + Properties.SIMULATOR_PRINT_LEVEL+"' are unknown. The correct values are: 0, 1, 2");
        }
    }

    
    /**
     * Loads all specific overlay attributes from the current properties.
     * @throws InitializationException if occurs any error during the loading process
     * of the attributes.
     */
    private static void loadOverlayAttributes() throws InitializationException
    {
        Properties.overlayId                 = properties.getPropertyAsClass(OVERLAY_ID);
        Properties.overlayNode               = properties.getPropertyAsClass(OVERLAY_NODE);
        Properties.overlayProperties         = properties.getPropertyAsClass(OVERLAY_PROPERTIES);
        Properties.overlayWithBehaviours     = properties.getPropertyAsBoolean(OVERLAY_WITH_BEHAVIOURS);
        
        //testing the correctness of the values
        Interfaces.ensureImplementedInterfaceOrClass(Properties.overlayId,        Interfaces.OVERLAY_ID,        OVERLAY_ID);
        Interfaces.ensureImplementedInterfaceOrClass(Properties.overlayNode,      Interfaces.OVERLAY_NODE,      OVERLAY_NODE);
        Interfaces.ensureImplementedInterfaceOrClass(Properties.overlayProperties,Interfaces.OVERLAY_PROPERTIES,OVERLAY_PROPERTIES);
        
        //ending the loading process
        Properties.overlayPropertiesInstance = (OverlayProperties)loadPropertiesInitializer(Properties.overlayProperties);
    }

    /**
     * Loads the specific behaviours attributes for the current simulation.
     * Only should be loaded if the current overlay use them.
     * @throws InitializationException if any error has ocurred during the 
     * loading process.
     */
    private static void loadBehavioursAttributes() throws InitializationException
    {
        Properties.behavioursFactory            = properties.getPropertyAsClass(BEHAVIOURS_FACTORY);
        Properties.behavioursPool               = properties.getPropertyAsClass(BEHAVIOURS_POOL);
        Properties.behavioursRoleSelector       = properties.getPropertyAsClass(BEHAVIOURS_ROLESELECTOR);
        Properties.behavioursInvoker            = properties.getPropertyAsClass(BEHAVIOURS_INVOKER);
        Properties.behavioursFilter             = properties.getPropertyAsClass(BEHAVIOURS_FILTER);
        Properties.behavioursPattern            = properties.getPropertyAsClass(BEHAVIOURS_PATTERN);
        Properties.behavioursProperties         = properties.getPropertyAsClass(BEHAVIOURS_PROPERTIES);
        
        Properties.behavioursNumberOfTypes      = properties.getPropertyAsInt(BEHAVIOURS_NUMBEROFTYPES);
        Properties.behavioursNumberOfModes      = properties.getPropertyAsInt(BEHAVIOURS_NUMBEROFMODES);
        
        //testing the correctness of the values
        Interfaces.ensureImplementedInterfaceOrClass(Properties.behavioursFactory,     Interfaces.BEHAVIOURS_FACTORY,     BEHAVIOURS_FACTORY);
        Interfaces.ensureImplementedInterfaceOrClass(Properties.behavioursPool,        Interfaces.BEHAVIOURS_POOL,        BEHAVIOURS_POOL);
        Interfaces.ensureImplementedInterfaceOrClass(Properties.behavioursRoleSelector,Interfaces.BEHAVIOURS_ROLESELECTOR,BEHAVIOURS_ROLESELECTOR);
        Interfaces.ensureImplementedInterfaceOrClass(Properties.behavioursInvoker,     Interfaces.BEHAVIOURS_INVOKER,     BEHAVIOURS_INVOKER);
        Interfaces.ensureImplementedInterfaceOrClass(Properties.behavioursFilter,      Interfaces.BEHAVIOURS_FILTER,      BEHAVIOURS_FILTER);
        Interfaces.ensureImplementedInterfaceOrClass(Properties.behavioursPattern,     Interfaces.BEHAVIOURS_PATTERN,     BEHAVIOURS_PATTERN);
        Interfaces.ensureImplementedInterfaceOrClass(Properties.behavioursProperties,  Interfaces.BEHAVIOURS_PROPERTIES,  BEHAVIOURS_PROPERTIES);
        
        //ending the loading process
        Properties.behavioursPropertiesInstance = loadPropertiesInitializer(Properties.behavioursProperties);

⌨️ 快捷键说明

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