urlfetcher.java

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

JAVA
464
字号
                         return false;
                 }
                 addRealtimeURL( url );

                 //only update this if the URL on which it is based is newer 
                 //than the one on disk.
                 URL sock;
                   
                 // Determine the URL's protocol
                 String protocol = url.substring(0, url.indexOf(":/"));
           
                 // Check if a proxy is set. If no port is set, use the default port (-1)
                 String proxyHost = URLManager.getProxyHost( protocol );
                 if (proxyHost != null)
                 {
                     // Open the URL using a proxy
                     sock = new URL(protocol,
                                    proxyHost,
                                    URLManager.getProxyPort( protocol ),
                                    url);
                 }
                 else
                 {	
                     sock = new URL( url );
                 }

                 URLConnection conn = null;
                 conn = sock.openConnection();

                 File file = dce.getFile();
                 long mod = dce.getLastModified();
                 long filesize = 0;
                 if(file != null)
                 {
                     filesize = file.length();
                 }

                 if(mod > 0 || filesize > 0)
                     conn.setIfModifiedSince(mod);
                   
                 conn.connect();
                 long last = conn.getLastModified();
                 long expires = conn.getExpiration();
                 int clength = conn.getContentLength();
                 int respCode = 200;
                 if(conn instanceof HttpURLConnection) {
                     respCode = ( ( HttpURLConnection )conn ).getResponseCode();
                 }
                   
                 if (respCode != 304 /*NOT MODIFIED*/ && 
                     (clength == -1 || clength > 0) && 
                     (  last == 0 || 
                       last > dce.getLastModified()) ) {

                     logger.info( "URLFetcher: Found updated URL: " + 
                               url +
                               " Modified " + last + " Expires: " + expires +
                               " CLength: " + clength ); 
                
                     //force this URL to update.

                     JetspeedDiskCache.getInstance().getEntry( url, getReader( conn ) );
                     //Trying to deal with a problem under FreeBSD
                     conn.getInputStream().close();

                     //Set the last modified and expiration times for entry
                     //FIXME: 0 is used in FileWatcher to mean not initialized...
                     if(last > 0)
                         dce.setLastModified(last);    
                     else
                         dce.setLastModified( System.currentTimeMillis() );
                     dce.setExpirationTime(expires);


                     //removeRealtimeURL( url ); (done in finally)
                     return true;
                     //now make sure that the entry that depends on this HREF
                     //is updated in the PortletFactory.
                 } else {

                     if(last > 0)
                         dce.setLastModified(last);    
                     else
                         dce.setLastModified( System.currentTimeMillis() );
                     dce.setExpirationTime(expires);
                           
                       
                     logger.info( "DiskCacheDaemon: URL still valid: " + url +
                               " Modified " + last + " Expires: " + expires +
                               " CLength: " + clength); 
                     //removeRealtimeURL( url ); (done in finally)
                     return false;
                 }
             } catch (Throwable e) {
                 //Add as a Bad URL
                 logger.error("Throwable",  e);
                 URLManager.register( url,
                                      URLManagerService.STATUS_BAD,
                                      e.toString() );
             } finally {
                 removeRealtimeURL( url );
             }
                   
         } else {
             logger.info( "URLFetcher: Cache miss during validation! Forcing url: " + url ); 
             removeRealtimeURL( url );
             JetspeedDiskCache.getInstance().getEntry( url, true );
             return true;
         }
           return false;
                    
    }


    /**
     *
     * Return a Reader for a given HTTP connection.
     * If the connection first line contains a XML declaration
     * with encoding, honor this encoding.
     * If not, use the encoding from the HTTP connection,
     * taking ISO-8859-1 as default.
     *
    */
    static final Reader getReader( URLConnection conn )
        throws IOException, UnsupportedEncodingException {
        String enc = conn.getContentEncoding();
        if( enc == null ) {
            enc = "ISO-8859-1";
        }
        // Some XML files come with a encoding attribute inside,
        // different than the HTTP encoding. We will have
        // to start reading the Reader, read the attribute and rewind 
        // the stream, generating a new reader with the "true" encoding
        BufferedInputStream is = new BufferedInputStream( conn.getInputStream() );
        //If document is XML, find the encoding and give it priority over
        //the one returned by the connection

        //we mark for resetting later. We need a big number to ensure
        // stack of streams don't read it to fill buffers.
        is.mark( 20480 );
        BufferedReader asciiReader = new BufferedReader( new InputStreamReader( is, "ASCII" ) );
        String decl = asciiReader.readLine();
        //System.err.println( "Line: " + decl );
        String key = "encoding=\"";
        //decl nul means that the connection got reset...
        if( decl != null ) {
            int off = decl.indexOf( key );
            if( off > 0 ) {
                enc = decl.substring( off + key.length(), 
                                      decl.indexOf( '"' , off + key.length()) );
            }
        }
        logger.info("URLFetcher: found URL with encoding -> " + enc );
        //Reset the bytes read
        is.reset();
        Reader rdr = new InputStreamReader( is,
                                            enc );
        return rdr;
    }


    
    /**
    Add a URL that is downloading in realtime
    */
    static final void addRealtimeURL( String url ) {
        synchronized( realtime_urls )
        {
            Vector threads = (Vector) realtime_urls.get( url);
            if(threads != null)
               {
                if(!threads.contains(Thread.currentThread()))
                   {
                     threads.addElement(Thread.currentThread() );
                   }
               } else {
                threads = new Vector();
                threads.addElement(Thread.currentThread());
                realtime_urls.put( url, threads  );
               }
        }
        
    }
    
    /**
    Remove a URL because it isn't downloading anymore.
    */
    static final void removeRealtimeURL( String url ) {
        synchronized( realtime_urls )
        {
           Vector threads = (Vector) realtime_urls.get( url);
           if(threads != null)
               synchronized( threads  )
                   {
                    Thread realLoader = (Thread) threads.firstElement();
                    if(realLoader == Thread.currentThread())
                    {
                      synchronized(url.intern())
                     {
                      realtime_urls.remove(url);
                      url.intern().notifyAll();
                      }          
                     } else {
                     threads.removeElement(Thread.currentThread());
                     }
                    }
        }
        
    }

    /**
    Return true if this URL isn't downloading in realtime.
    */
    static final boolean isRealtimeURL( String url ) {

        synchronized( realtime_urls ) {
            return realtime_urls.get( url ) != null;
        }
            
    }

    /**
    Return the list of realtime URLs for debug
    */
    public static final Hashtable getRealtimeURLs() {
        synchronized(realtime_urls) {
            return realtime_urls;
        }
    }
    
}

⌨️ 快捷键说明

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