ch.claudio.pediacache.model
Class Cache

java.lang.Object
  extended by ch.claudio.pediacache.model.Cache

public class Cache
extends java.lang.Object

Note: Cached pages should be refreshed when the user is online. For this look for pages with lastRead much less than lastFetched.

Version:
$Id: Cache.java 163 2006-11-12 11:10:11Z claudio $
Author:
Claudio Nieder

Copyright (C) 2006 Claudio Nieder <private@claudio.ch>, CH-8610 Uster

This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; version 2 of the License.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA


Field Summary
static java.lang.String httpContentType
          http content-type header
static java.lang.String httpLastModified
          http Last-Modified header
static java.lang.String httpLocation
          http Location header
static java.lang.String httpReferer
          http Location header
static java.lang.String httpUserAgent
          http User-Agent header
 
Constructor Summary
Cache()
           
 
Method Summary
static java.net.URL decodeURL(java.lang.String url, java.lang.String query)
          Decode a Base64 string.
static void deleteAllPages()
          Deletes all pages in the cache.
static java.lang.String encodeURL(java.net.URL url)
          Base 64 encode an URL so that it can be safely used as part of another URL
static CachedPage fetchPage(java.net.URL url, Source associatedSource, int priority)
          Get page from cache or from Internet.
static int getCacheSize()
          Get the number of pages in the cache.
static long getDBSize()
          Get the size of the database file.
static java.util.List<CachedPage> getLastPages()
          Retrieve the last few pages.
static java.util.List<SearchResult> getLastSearches()
          Retrieve the last few pages.
static CachedPage getPage(java.net.URL url)
          Retrieve a page from cache given the URL.
static int getQueueSize()
          Get the size of the queue
static java.util.List<Source> getSelectedSources()
          Get the sources selected by the user.
static java.util.List<Source> getSourcesSorted()
          Get all sources
static int getTotalSize()
          Get the number of pages in the cache.
static void initSource()
          Initialise sources.
static void main(java.lang.String[] args)
          Note: executing this class as main class will cause the expiry thread to be initialized and thus the JVM will not terminate.
static void selectSource(java.lang.String host)
          Add the source with a given host name to the preferred ones
static void setUserAgent(java.lang.String userAgentArg)
          Sets the user agent to use for requests.
static void startPingThread()
          Start a thread which scans for the selected wiki sites and maintains their online status.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

httpContentType

public static final java.lang.String httpContentType
http content-type header

See Also:
Constant Field Values

httpLocation

public static final java.lang.String httpLocation
http Location header

See Also:
Constant Field Values

httpReferer

public static final java.lang.String httpReferer
http Location header

See Also:
Constant Field Values

httpLastModified

public static final java.lang.String httpLastModified
http Last-Modified header

See Also:
Constant Field Values

httpUserAgent

public static final java.lang.String httpUserAgent
http User-Agent header

See Also:
Constant Field Values
Constructor Detail

Cache

public Cache()
Method Detail

startPingThread

public static void startPingThread()
Start a thread which scans for the selected wiki sites and maintains their online status.


initSource

public static void initSource()
Initialise sources.


getSourcesSorted

public static java.util.List<Source> getSourcesSorted()
Get all sources

Returns:
source

getSelectedSources

public static java.util.List<Source> getSelectedSources()
Get the sources selected by the user.

Returns:
sources

selectSource

public static void selectSource(java.lang.String host)
Add the source with a given host name to the preferred ones

Parameters:
host - host name of the source

getDBSize

public static long getDBSize()
Get the size of the database file.

Returns:
size in byte

getLastPages

public static java.util.List<CachedPage> getLastPages()
Retrieve the last few pages.

Returns:
page content or null.

getLastSearches

public static java.util.List<SearchResult> getLastSearches()
Retrieve the last few pages.

Returns:
page content or null.

getPage

public static CachedPage getPage(java.net.URL url)
Retrieve a page from cache given the URL. Returns null if no page is not found in the Cache.

Parameters:
url - page URL
Returns:
page content or null.

setUserAgent

public static void setUserAgent(java.lang.String userAgentArg)
Sets the user agent to use for requests.

Parameters:
userAgentArg - string

fetchPage

public static CachedPage fetchPage(java.net.URL url,
                                   Source associatedSource,
                                   int priority)
Get page from cache or from Internet. If the page is not in the cache and the site is online then it is fetched and stored in the cache. If the page is in the cache and the site is online, then it is refetched if newer. If the page is not in the cache and the site is not online, then the current result is returned.

Parameters:
url - to get
associatedSource - Source for this URL may be null
priority - of this request. 0 if request from user.
Returns:
page or null if not in cache and not able to retrieve

encodeURL

public static java.lang.String encodeURL(java.net.URL url)
Base 64 encode an URL so that it can be safely used as part of another URL

Parameters:
url - to encode
Returns:
encoded url

decodeURL

public static java.net.URL decodeURL(java.lang.String url,
                                     java.lang.String query)
                              throws java.io.UnsupportedEncodingException,
                                     java.net.MalformedURLException
Decode a Base64 string. Note: It is up to the caller to verify that the returned string is really a valid URL. If a query string is supplied it is appended with a "?" to the URL.

Parameters:
url - to decode
query - optional query string
Returns:
decoded url
Throws:
java.io.UnsupportedEncodingException
java.net.MalformedURLException

getQueueSize

public static int getQueueSize()
Get the size of the queue

Returns:
number of pages waiting to be fetched.

getCacheSize

public static int getCacheSize()
Get the number of pages in the cache.

Returns:
number of pages waiting to be fetched.

getTotalSize

public static int getTotalSize()
Get the number of pages in the cache.

Returns:
number of pages waiting to be fetched.

deleteAllPages

public static void deleteAllPages()
Deletes all pages in the cache. This includes queued pages. For debugging only.


main

public static void main(java.lang.String[] args)
Note: executing this class as main class will cause the expiry thread to be initialized and thus the JVM will not terminate.

Parameters:
args - ignored