jetspeedurlmanagerservice.java

来自「jetspeed源代码」· Java 代码 · 共 466 行 · 第 1/2 页

JAVA
466
字号
            }
        }
    }

    /**
     * Register or replace an URL record. All records are keyed to
     * the imutable URL of URLInfo.
     * 
     * @param info the info record to store
     */
    public void register( URLInfo info ) {
        if ( info != null) {
            synchronized (urls) {
                if( getInfo( info.getURL() ) == null ) 
                    urls.put( info.getURL().intern(), info );
            }
        }
    }

    /**
     * Unregister an URL from the repository
     * 
     * @param url the url to remove
     */
    public void unregister( String url ) {
        if ( url != null ) {
            synchronized (urls) {
                urls.remove( url.intern() );
            }
        }
    }

    /**
     * Get the information record stored in the database about
     * an URL.
     * 
     * @param url the url whose record is sought
     * @return the description record found in the repository or null.
     */
    public URLInfo getInfo( String url ) {
        URLInfo info = null;

        if ( url != null ) {
            synchronized(urls) {
                info = (URLInfo)urls.get( url.intern() );
            }
        }

        return info;
    }

    /**
     * Test whether the URL is currently believed to be OK by this 
     * repository.
     * 
     * @param url the url to be tested
     * @return false is the url is known by this repository and has
     * a status indicating an error, true otherwise.
     */
    public boolean isOK( String url ) {
        URLInfo info = getInfo( url );

        // we don't know this URL, play it safe and say it's good
        if ( info == null ) return true;

        return ( info.getStatus() == URLManagerService.STATUS_OK );
    }

    /**
     * List of the current known URLs in the repository
     *
     * @return a List of URL strings known to this repository
     */
    public List list() {
        synchronized (urls) {
            return new Vector( urls.keySet() );
        }
    }

    /**
     * List of the current known URLs in the repository which have 
     * the given status.
     *
     * @param status the status to be retrieved. May be 
     * {@link URLManagerService#STATUS_ANY} to indicate any status
     * @return a List of URL strings known to this repository with this status
     */
    public List list( int status ) {
        Vector result = new Vector();

        synchronized (urls) {
            Iterator i = urls.entrySet().iterator();
            while( i.hasNext() ) {
                Map.Entry entry = (Map.Entry)i.next();
                URLInfo info = (URLInfo)entry.getValue();
                if ( ( info.getStatus() & status ) != 0 ) {
                    result.addElement( entry.getKey() );
                }
            }
        }

        return result;
    }

    /**
     * Load the persisted state of the repository from disk
     */
    private synchronized void load() {

        Map store = new HashMap();
        Configuration config = null;

        logger.info( "Restoring the URLs from disk: " + path );

        try {
            config = new Configuration( path );

            int count = 1;
            String url = null;

            while ( ( url = ( config
                              .getString("entry."+count+".url") ) ) != null ) {
                //Intern the url to ensure we can use "==" to compare
                //and synchronize on it
                url = url.intern();
                int status = config.getInteger("entry."+count+".status", URLManagerService.STATUS_OK );
                if( store.get( url ) == null )
                    store.put( url, new URLInfo( url, status ) );
                count++;
            }

            logger.info( "URLManager loaded " + count + " urls" );                    

        } catch ( Exception e ) {
            logger.error( "Could not restore URLManager state", e );
            return;
        } finally {
            // set the loaded store as the new store
            this.urls = store;
        }

    }

    /**
     * Persist the state of the repository on disk in a properties file
     */
    private synchronized void save() {
        PrintWriter pw = null ;

        try {

            File propfile = new File(path); // FileWriter doesn't always do this
            propfile.getParentFile().mkdirs();
            propfile.createNewFile();

            pw = new PrintWriter( new BufferedWriter( new FileWriter( propfile ) ) );
            synchronized (urls) {
                Iterator i = urls.values().iterator();
                int entryNum = 1;
                while( i.hasNext() ) {
                    URLInfo info = (URLInfo)i.next();
                    pw.print( "entry." );
                    pw.print( entryNum );
                    pw.print( ".url=" );
                    writeEscaped( pw, info.getURL() );
                    pw.println( "" );
                    pw.print( "entry." );
                    pw.print( entryNum );
                    pw.print( ".status=" );
                    pw.print( info.getStatus() );
                    pw.println( "" );
                    entryNum++;
                }
            }
        } 
        catch ( Throwable t ) 
        {
            logger.error( "Impossible to save URLManager state to "+path, t );
        } 
        finally 
        {
            if( pw != null )
            {
                pw.close();
            }
        }
    }

    /**
     * Return the port of a proxy
     * @param protocol The protocol that the proxy supports, e.g. 'http'
     * @return The port number (1-65535), or -1 if no port was specified (= use default)
     */
    public int getProxyPort( String protocol ) {
        Integer proxyPort = (Integer)proxies.get( (protocol + ".port").toLowerCase() );

        if (proxyPort != null)
            return proxyPort.intValue();
        else
            return -1;
    }

    /**
     * Return a proxy's hostname
     * @param protocol The protocol that the proxy supports, e.g. 'http'
     * @return The hostname of the proxy, or <code>null</code> if no proxy is specified for this protocol
     */
    public String getProxyHost( String protocol ) {
        String proxyHost = (String)proxies.get( (protocol + ".host").toLowerCase() );

        return proxyHost;
    }

    /**
     * <p>Escape values when saving.
     * Appends a String to a StringBuffer, escaping commas.</p>
     * <p>We assume that commas are unescaped.</p>
     * @param sink a StringBuffer to write output
     * @param element a value to be written
     */
    protected void writeEscaped( PrintWriter sink, String element ) {
        int upTo = element.indexOf(",");
        if( upTo == -1 ) {
            sink.print( element );
            return;
        }
        sink.print( element.substring( 0, upTo ) );
        sink.print( "\\," );
        writeEscaped( sink, element.substring( upTo+1, element.length() ) );
        return;
    }
}

⌨️ 快捷键说明

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