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

📄 jasen.java

📁 spam source codejasen-0.9jASEN - java Anti Spam ENgine.zip 如标题所示
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
                inetAddressResolver = (InetAddressResolver)Class.forName(config.getInetResolver(), true, loader).newInstance();

            // Error handler
            if(config.getErrorHandler() != null) {
                errorHandler = (JasenErrorHandler)Class.forName(config.getErrorHandler(), true, loader).newInstance();
            }
            else
            {
                errorHandler = new SystemErrorHandler();
            }

            // Set the handler into the broker
            ErrorHandlerBroker.getInstance().setErrorHandler(errorHandler);

            // Create and initialise the plugins
            if(config.getPluginConfigurations() != null) {

                logger.debug("Initialising plugins");

                // Make sure they are sorted
                Collections.sort(config.getPluginConfigurations());

                Iterator i = config.getPluginConfigurations().iterator();

                plugins = new Vector(config.getPluginConfigurations().size());

                JasenPlugin plugin = null;
                ProbabilityCalculator pluginCalc = null;
                PluginContainer container = null;
                Properties props = null;
                JasenPluginConfiguration pluginConfig = null;

                while(i.hasNext()) {
                    pluginConfig = (JasenPluginConfiguration)i.next();

                    plugin = (JasenPlugin)Class.forName(pluginConfig.getType(), true, loader).newInstance();

                    logger.debug("Initialising plugin: " + plugin.getClass().getName());

                    // initialise the plugin
                    if(pluginConfig.getProperties() != null) {
                        props = new Properties();
                        props.load(this.getClass().getClassLoader().getResourceAsStream(pluginConfig.getProperties()));
                    }
                    else {
                        props = null;
                    }

                    plugin.init(props);

                    if(pluginConfig.getCalculator() != null) {
                        pluginCalc = (ProbabilityCalculator)Class.forName(pluginConfig.getCalculator(), true, loader).newInstance();
                    }

                    // Add the plugin to a container
                    container = new PluginContainer();
                    container.setName(pluginConfig.getName());
                    container.setDisplayName(pluginConfig.getDisplayName());
                    container.setDescription(pluginConfig.getDescription());
                    container.setCalculator(pluginCalc);
                    container.setPlugin(plugin);

                    // Add the container to the list
                    plugins.add(container);
                }
            }

            if(config.getAutoUpdateEnabled() != null && config.getAutoUpdateEnabled().trim().equalsIgnoreCase("true")) {

                logger.debug("Initialising auto update engine");

                // create the configuration
                JasenAutoUpdateConfiguration jc = new JasenAutoUpdateConfiguration();
                jc.setCheckOnStartup(new Boolean(config.getAutoUpdateCheckOnStartup()).booleanValue());
                jc.setErrorHandler((JasenErrorHandler)Class.forName(config.getAutoUpdateErrorHandler(), true, loader).newInstance());
                jc.setFrequency(Long.parseLong(config.getAutoUpdateFrequency()));
                jc.setParcel(config.getAutoUpdateParcel());
                jc.setReadBuffer(Integer.parseInt(config.getAutoUpdateReadBuffer()));
                jc.setReadTimeout(Long.parseLong(config.getAutoUpdateReadTimout()));
                jc.setUpdateURL(new URL(config.getAutoUpdateUrl()));

                // initialise and start the update manager
                JasenAutoUpdateManager.getInstance().init(jc);
            }
        }
        catch (IOException e)
        {
            throw new JasenException(e);
        }
        catch (SAXException e)
        {
            throw new JasenException(e);
        }
        catch (InstantiationException e)
        {
            throw new JasenException(e);
        }
        catch (IllegalAccessException e)
        {
            throw new JasenException(e);
        }
        catch (ClassNotFoundException e)
        {
            throw new JasenException(e);
        }
    }

    /**
     * Calls destroy on any registered plugins.
     * <br/>
     * This should be called prior to any calling application exiting to ensure any resources
     *  obtained by plugins are closed or returned.
     */
    public void destroy() {
        logger.debug("Shutting down jASEN engine");

        if(plugins != null) {
            JasenPlugin plugin = null;
            for (int i = 0; i < plugins.size(); i++) {

                plugin = ((PluginContainer)plugins.get(i)).getPlugin();

                logger.debug("Destroying plugin: " + plugin.getClass().getName());

                try {
                    plugin.destroy();
                }
                catch (JasenException e) {
                    errorHandler.handleException(e);
                }
            }
        }

        logger.debug("Shutting down auto update engine");
        if(JasenAutoUpdateManager.getInstance().isInitialized()) {
            JasenAutoUpdateManager.getInstance().destroy();
        }

        logger.debug("jASEN engine shut down");
    }

    /**
     * Restarts the engine.  This simply calls destroy() then init()
     * <br/>
     * NOTE: This method will fail to produce accurate results if the engine was originally initialized with anything other
     * than the default (parameterless) init() method.
     * @throws JasenException
     */
    public void restart() throws JasenException {
        logger.debug("Restarting jASEN engine");
        destroy();
        init();
    }

    /**
     * Scans the given message.
     * <p>
     * 	All plugins will execute regardless of the probability (or computed total probability) discovered from any single plugin or combination thereof
     * </p>
     * @param mm The MimeMessage to be scanned
     * @param ignored A list of plugin names which will be ignored during the scan.  May be null
     * @return The results of the scan as a JasenScanResult
     * @throws JasenException
     */
    public JasenScanResult scan(MimeMessage mm, String[] ignored) throws JasenException {
        return scan(mm, NO_THRESHOLD, ignored);
    }

    /**
     * Scans the message without a threshold specified
     * @param mm The MimeMessage to be scanned
     * @param message A pre-parsed JasenMessage
     * @param ignored A list of plugin names which will be ignored during the scan.  May be null
     * @return The results of the scan as a JasenScanResult
     * @throws JasenException
     */
    public JasenScanResult scan(MimeMessage mm, JasenMessage message, String[] ignored) throws JasenException {
        return scan(mm, message, NO_THRESHOLD, ignored);
    }


    /**
     * Scans the given mime message using the already mime-parsed JasenMessage.
     * <p>
     * This implementation allows calling applications to implement their own JasenMessage by passing it to the scan engine.
     * </p>
     * @param mm The MimeMessage to be scanned
     * @param message A pre-parsed JasenMessage
     * @param threshold The thresholds.  If any one plugin yields a result >= threshold, scanning is ceased and the result returned immediately
     * @param ignored A list of plugin names which will be ignored during the scan.  May be null
     * @return The results of the scan as a JasenScanResult
     * @throws JasenException
     */
    public JasenScanResult scan(MimeMessage mm, JasenMessage message, float threshold, String[] ignored) throws JasenException {

        StandardScanResult result = null;
        ReceivedHeaderParser parser = null;

        try
        {
            // Create a result instance
            result = (StandardScanResult) scanResultClass.newInstance();

            // and a mimeParser
            parser = (ReceivedHeaderParser) headerParserClass.newInstance();

        }
        catch (InstantiationException e)
        {
            throw new JasenException(e);
        }
        catch (IllegalAccessException e)
        {
            throw new JasenException(e);
        }

        //  Wrap the message so we can capture mimeParser data and store tokenized text
        JasenMessageWrapper wrapper = new JasenMessageWrapper(message);

        wrapper.setEngine(this);

        // Parse the HTML part and extract the tokens
        SpamHTMLParser htmlParser = new SpamHTMLParser();

        // Now, tokenize the html and text parts of the message
        ParserData parserData = htmlParser.parse(mm, wrapper, tokenizer);

        // Now, loop through the plugins in order and test...
        if(plugins != null) {

            PluginContainer container = null;
            JasenTestResult pluginResult = null;

            double[] probabilities = new double[plugins.size()];
            double[] pointProbabilities = null;
            int points = 0;
            float min = 0.0f;
            float max = 0.0f;
            int pointThreshold = 1;
            float prob = 0.5f;
            double finalProbability = 0.5d;
            boolean thresholdReached = false;

            String[][] testResults = new String[plugins.size()][4];

            long time;
            
            // Ensure the ignore list is sorted for the binary search
            if(ignored != null) {
                Arrays.sort(ignored);
            }

            for (int i = 0; i < plugins.size(); i++)
            {
                time = System.currentTimeMillis();

                container = (PluginContainer)plugins.get(i);
                
                // Determine if we should ignore this plugin
                if(ignored != null && ignored.length > 0 && Arrays.binarySearch(ignored, container.getName()) > -1) {
                    probabilities[i] =  JasenEngineConfiguration.getInstance().getGuess();
                    
                    testResults[i][RESULT_INDEX_PROBABILITY] = String.valueOf(probabilities[i]);  
                    testResults[i][RESULT_INDEX_TIME] = "0";
                    testResults[i][RESULT_INDEX_NAME] = container.getName();
                    testResults[i][RESULT_INDEX_PROBABILITY] = container.getDisplayName();
                }
                else
                {
                    try
                    {
                        pluginResult = container.getPlugin().test(this, mm, wrapper, parserData, parser);
                    }
                    catch (JasenException e)
                    {
                        // We couldn't execute this plugin... record the error and continue
                        ErrorHandlerBroker.getInstance().getErrorHandler().handleException(e);

                        probabilities[i] = JasenEngineConfiguration.getInstance().getGuess();
                    }

                    testResults[i][RESULT_INDEX_NAME] = container.getName();
                    testResults[i][RESULT_INDEX_DISPLAY] = container.getDisplayName();

                    if(pluginResult != null) {
                        if(pluginResult.isAbsolute()) {
                            // We know it's definately spam
                            result.setProbability(1.0d);

                            testResults[i][RESULT_INDEX_PROBABILITY] = "1.0";

                            // Stop processing here
                            return result;
                        }
                        else
                        {
                            probabilities[i] = pluginResult.calculateProbability();
                            testResults[i][RESULT_INDEX_PROBABILITY] = String.valueOf(probabilities[i]);
                        }
                    }
                    else
                    {
                        throw new JasenException("JasenTestResult cannot be null");
                    }

                    // If we have a threshold, compute now
                    if(threshold != NO_THRESHOLD) {
                        finalProbability = calculate(probabilities, 0, i+1);

                        if(finalProbability >= threshold) {
                            thresholdReached = true;
                            break;
                        }
                    }                      
                }

                testResults[i][RESULT_INDEX_TIME] = String.valueOf(System.currentTimeMillis() - time);
            }

⌨️ 快捷键说明

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