X-Git-Url: http://erislabs.net/gitweb/?a=blobdiff_plain;f=source%2Fmir%2Fgenerator%2Ftal%2FCachingFileLoader.java;h=c9f042b928d6360f45872115e3a6ec530672c89c;hb=b7ea95152eaddbf069564a5f2f117774165d36e6;hp=41256ab293d27f04ca7bb477b0d7d816a150990d;hpb=a841b85307a342bbaaf328ab2a79d9dbc4619b1f;p=mir.git diff --git a/source/mir/generator/tal/CachingFileLoader.java b/source/mir/generator/tal/CachingFileLoader.java index 41256ab2..c9f042b9 100755 --- a/source/mir/generator/tal/CachingFileLoader.java +++ b/source/mir/generator/tal/CachingFileLoader.java @@ -1,135 +1,138 @@ -/* - * Copyright (C) 2001, 2002 The Mir-coders group - * - * This file is part of Mir. - * - * Mir 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; either version 2 of the License, or - * (at your option) any later version. - * - * Mir 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 Mir; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - * - * In addition, as a special exception, The Mir-coders gives permission to link - * the code of this program with any library licensed under the Apache Software License, - * The Sun (tm) Java Advanced Imaging library (JAI), The Sun JIMI library - * (or with modified versions of the above that use the same license as the above), - * and distribute linked combinations including the two. You must obey the - * GNU General Public License in all respects for all of the code used other than - * the above mentioned libraries. If you modify this file, you may extend this - * exception to your version of the file, but you are not obligated to do so. - * If you do not wish to do so, delete this exception statement from your version. - */ -package mir.generator.tal; - -import java.io.File; -import java.io.FileInputStream; -import java.io.FileNotFoundException; -import java.io.InputStream; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.Vector; - -public class CachingFileLoader { - private Map cachedObjects; - private List history; - private int capacity; - private CachedFileObjectFactory factory; - - public CachingFileLoader(int aCapacity, CachedFileObjectFactory aFactory) { - capacity = aCapacity; - cachedObjects = new HashMap(); - history = new Vector(); - factory = aFactory; - } - - private void cacheObject(String aFilename, long aLastModified, Object anObject) { - synchronized (this) { - history.remove(aFilename); - history.add(aFilename); - while (history.size()>capacity && history.size()>0) { - history.remove(0); - } - cachedObjects.put(aFilename, new CachedObject(aFilename, aLastModified, anObject)); - } - } - - private Object constructObject(String aFilename) throws FileNotFoundException { - File file = new File(aFilename); - FileInputStream inputStream = new FileInputStream(file); - try { - Object result = factory.constructObject(inputStream); - cacheObject(aFilename, file.lastModified(), result); - - return result; - } - finally { - try { - inputStream.close(); - } - catch (Throwable t) { - } - } - } - - private void revatilize(String aFilename) { - synchronized (this) { - history.remove(aFilename); - history.add(aFilename); - } - } - - private class CachedObject { - private String filename; - private long lastModified; - private Object object; - - public CachedObject(String aFilename, long aLastModified, Object anObject) { - filename = aFilename; - lastModified = aLastModified; - object = anObject; - } - - public Object getObject() { - return object; - } - - public String getFilename() { - return filename; - } - - public long getLastModified() { - return lastModified; - } - } - - public Object retrieveFile(String aFilename) throws FileNotFoundException { - synchronized (this) { - if (!cachedObjects.containsKey(aFilename)) { - return constructObject(aFilename); - } - - CachedObject cachedObject = (CachedObject) cachedObjects.get(aFilename); - File file = new File(aFilename); - if (file.lastModified() != cachedObject.getLastModified()) { - return constructObject(aFilename); - } - - revatilize(aFilename); - - return cachedObject.getObject(); - } - } - - public interface CachedFileObjectFactory { - Object constructObject(InputStream aStream); - } +/* + * Copyright (C) 2001, 2002 The Mir-coders group + * + * This file is part of Mir. + * + * Mir 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; either version 2 of the License, or + * (at your option) any later version. + * + * Mir 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 Mir; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * In addition, as a special exception, The Mir-coders gives permission to link + * the code of this program with any library licensed under the Apache Software License, + * The Sun (tm) Java Advanced Imaging library (JAI), The Sun JIMI library + * (or with modified versions of the above that use the same license as the above), + * and distribute linked combinations including the two. You must obey the + * GNU General Public License in all respects for all of the code used other than + * the above mentioned libraries. If you modify this file, you may extend this + * exception to your version of the file, but you are not obligated to do so. + * If you do not wish to do so, delete this exception statement from your version. + */ + +package mir.generator.tal; + +import java.io.BufferedInputStream; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.InputStream; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +public class CachingFileLoader { + private Map cachedObjects; + private List history; + private int capacity; + private CachedFileObjectFactory factory; + + public CachingFileLoader(int aCapacity, CachedFileObjectFactory aFactory) { + capacity = aCapacity; + cachedObjects = new HashMap(); + history = new ArrayList(); + factory = aFactory; + } + + private void cacheObject(String aFilename, long aLastModified, Object anObject) { + synchronized (this) { + history.remove(aFilename); + history.add(aFilename); + while (history.size()>capacity && history.size()>0) { + history.remove(0); + } + cachedObjects.put(aFilename, new CachedObject(aFilename, aLastModified, anObject)); + } + } + + private Object constructObject(String aFilename) throws FileNotFoundException { + File file = new File(aFilename); + BufferedInputStream inputStream = new BufferedInputStream( + new FileInputStream(file), 8192); + try { + Object result = factory.constructObject(inputStream); + cacheObject(aFilename, file.lastModified(), result); + + return result; + } + finally { + try { + inputStream.close(); + } + catch (Throwable t) { + } + } + } + + private void revatilize(String aFilename) { + synchronized (this) { + history.remove(aFilename); + history.add(aFilename); + } + } + + private class CachedObject { + private String filename; + private long lastModified; + private Object object; + + public CachedObject(String aFilename, long aLastModified, Object anObject) { + filename = aFilename; + lastModified = aLastModified; + object = anObject; + } + + public Object getObject() { + return object; + } + + public String getFilename() { + return filename; + } + + public long getLastModified() { + return lastModified; + } + } + + public Object retrieveFile(String aFilename) throws FileNotFoundException { + synchronized (this) { + if (!cachedObjects.containsKey(aFilename)) { + return constructObject(aFilename); + } + + CachedObject cachedObject = (CachedObject) cachedObjects.get(aFilename); + File file = new File(aFilename); + if (file.lastModified() != cachedObject.getLastModified()) { + return constructObject(aFilename); + } + + revatilize(aFilename); + + return cachedObject.getObject(); + } + } + + public interface CachedFileObjectFactory { + Object constructObject(InputStream aStream); + } } \ No newline at end of file