📄 jetspeeddiskcache.java
字号:
URLManager.register( local,
URLManager.STATUS_OK,
"Local added" );
}
logger.info("Returning local cached URL");
return dce;
}
//only return for URLs that are cacheable and that are based on URLs
//that are remote
if ( DiskCacheUtils.isCacheable( url ) ) {
if ( ( DiskCacheUtils.isCached( this, url ) == false ) || force ) {
//Log.info( "DiskCache: MISS - fectching document: " + url );
//if it doesn't exist then pull it down from a URL and save it to a file
// SGP We can arrive here either because force was true or
// because force is false and the url is not cached.
// We must force load in both cases
this.add( url, true );
}
return this.getEntry(url, force);
//return new JetspeedDiskCacheEntry( DiskCacheUtils.getFile( this, url ) );
} else {
//else it is a remote URL and can not be cached.
logger.info( "DiskCache: this URL can't be stored in cache... providing it directly." + url );
return new JetspeedDiskCacheEntry( url );
}
}
/**
Get an Entry given a Reader and the URL from which it has been fetched.
@see DiskCache#getEntry( String url, Reader is )
*/
public DiskCacheEntry getEntry( String url,
Reader is ) throws IOException {
String uri = URIEncoder.encode( url );
String oldfilename = this.getRoot() + "/old." + uri;
String filename = DiskCacheUtils.getFile( this, url ).getAbsolutePath();
String newfilename = this.getRoot() + "/new." + uri;
File file = new File( DiskCacheUtils.getFile( this, url ).getAbsolutePath() );
File newfile = new File( newfilename);
OutputStreamWriter os = new OutputStreamWriter (new FileOutputStream( newfile ), "utf-8" );
//now process the InputStream...
char chars[] = new char[200];
int readCount = 0;
while( ( readCount = is.read( chars )) > 0 ) {
os.write(chars, 0, readCount);
}
is.close();
os.close();
File oldfile = new File( oldfilename);
if(oldfile.exists())
oldfile.delete();
if(newfile.exists() && newfile.length() > 0) {
file = new File( filename );
file.renameTo(oldfile);
newfile.renameTo(file);
}
try {
if( oldfile.exists() )
oldfile.delete();
} catch (Exception e) {
logger.info("Exception " +
e.getMessage() +
" while deleting " + oldfilename, e);
}
JetspeedDiskCacheEntry dce = (JetspeedDiskCacheEntry) entries.get(url.intern());
if (dce != null )
{
dce.setFile( file );
return dce;
} else {
return this.getEntry(url, false);
}
}
/**
@see DiskCache#remove( String url )
*/
public void remove( String url ) throws IOException {
String uri = URIEncoder.encode( url );
if( DiskCacheUtils.isCached( this, url ) ) {
entries.remove(url.intern());
URLManager.unregister( url.intern() );
File file = DiskCacheUtils.getFile( this, url );
if(file.exists()) {
file.delete();
}
}
String oldfilename = this.getRoot() + "/old." + uri;
File file = new File(oldfilename);
if(file.exists()) {
file.delete();
}
String newfilename = this.getRoot() + "/new." + uri;
file = new File(newfilename);
if(file.exists()) {
file.delete();
}
}
/**
@see DiskCache#add( String url )
*/
public void add( String url ) throws IOException {
add( url, false );
}
/**
@see DiskCache#add( String url )
*/
public void add( String url, boolean force ) throws IOException {
String interned = url.intern();
this.fetch( url,
DiskCacheUtils.getFile( this, url ).getAbsolutePath(),
force );
if(entries.get(interned) != null ) return;
entries.put(interned, new JetspeedDiskCacheEntry(interned));
URLManager.register( interned,
URLManager.STATUS_OK,
"Added by Program" );
}
/**
@see DiskCache#fetch( String url, String cache )
@param url the url to retrieve
@param cache what file to store it in.
*/
public String fetch( String url,
String cache ) throws IOException {
return fetch( url, cache, false );
}
/**
Pulls in the remote URL from the net and saves it to disk
@see DiskCache#fetch( String url, String cache )
@param url the url to retrieve
@param cache what file to store it in.
*/
public String fetch( String url,
String cache,
boolean force ) throws IOException {
if (url == null) {
throw new IllegalArgumentException("url cannot be null");
}
if (cache == null) {
throw new IllegalArgumentException("cache cannot be null");
}
try {
//The URL fecther will try to get the URL or it will throw
//an Exception here.
Reader is = URLFetcher.fetch( url, force );
OutputStreamWriter os = new OutputStreamWriter( new FileOutputStream( cache ),
"utf-8" );
//now process the InputStream...
char chars[] = new char[200];
int readCount = 0;
while( ( readCount = is.read( chars )) > 0 ) {
os.write(chars, 0, readCount);
}
is.close();
os.close();
} catch (MalformedURLException e) {
logger.error("Error in URL", e );
}
return cache;
}
/**
@see DiskCache#refresh
*/
public void refresh( String url ) {
ThreadPool.process( new URLFetcherDownloader( url ) );
}
/**
Return the default instance of the JetspeedDiskCache cache.
*/
public static JetspeedDiskCache getInstance() {
return JetspeedDiskCache.getInstance( DEFAULT_CACHE_DIRECTORY );
}
/**
Return the default instance of the JetspeedDiskCache cache.
@param location A directory to store the cache at.
*/
public static JetspeedDiskCache getInstance( String directory ) {
synchronized(JetspeedDiskCache.instances) {
JetspeedDiskCache cache = (JetspeedDiskCache)JetspeedDiskCache.instances.get(directory);
if (cache == null) {
cache = new JetspeedDiskCache(directory);
JetspeedDiskCache.instances.put( directory, cache );
logger.info("DISK CACHE: Initing cache for " + directory);
cache.initEntries();
logger.info("DISK CACHE: Inited cache:" + directory);
}
return cache;
}
}
/**
*/
public boolean isCached(String url)
{
return entries.containsKey(url.intern());
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -