📄 cm.java
字号:
**/
public long getPublicationLifetime(String dn, String fn) {
File file = getFile(dn, fn);
return expirer.getPublicationLifetime(file);
}
/**
* Returns the maximum duration in milliseconds for which this document
* should cached by those other than the publisher. This value is either
* the cache lifetime or the remaining lifetime of the document, whichever
* is less.
*
* @since 1.0
*
* @param dn contains the name of the folder
* @param fn contains the name of the file
* @return number of milliseconds until the file expires or -1 if the
* file is not recognized or already expired.
*/
public long getCacheLifetime(String dn, String fn) {
File file = getFile(dn, fn);
return expirer.getCacheLifetime(file);
}
/**
* Gets the list of all the files into the given folder
*
* @param dn contains the name of the folder
* @return String[] an array of Strings containing the name of the files
* @since 1.0
*/
public String[] getFileNames(String dn) {
if (dn == null) {
// why?!
if (LOG.isEnabledFor(Priority.DEBUG))
LOG.debug("null directory name");
return null;
} else {
File dir = new File(rootDir, dn);
return dir.list();
}
}
/**
* Returns the name of the oldest file in a given directory
*
* @param dn contains the name of the folder
* @return String returns the name of the oldest file
* @since 1.0
*/
public String getOldestFile(String dn) {
if (dn == null) {
if (LOG.isEnabledFor(Priority.DEBUG))
LOG.debug("null directory name");
return null;
}
try {
File dir = new File(rootDir, dn);
File [] list = dir.listFiles();
if (list == null || list.length == 0) {
if (LOG.isEnabledFor(Priority.DEBUG))
LOG.debug("no files in folder");
return null;
}
long oldestTime = list[0].lastModified();
File oldest = list[0];
File file = null;
long time = 0;
for (int i = 1; i < list.length; ++i) {
file = list[i];
time = file.lastModified();
if (time < oldestTime) {
// This one is more recent
oldest = list[i];
oldestTime = time;
}
}
return oldest.getName();
} catch (Exception e) {
if (LOG.isEnabledFor(Priority.WARN))
LOG.debug("Failure determining oldest file", e);
return null;
}
}
/**
* Returns true if the directory exists
*
* @param dn contains the name of the folder
* @return boolean
* @since 1.0
*/
public boolean isFolder(String dn) {
if (dn == null) {
return false;
}
File dir = new File(rootDir, dn);
return dir.exists() && dir.isDirectory();
}
/**
* Returns the inputStream of a specified file, in a specified dir
*
* @param dn directory name
* @param fn file name
* @return The inputStream value
* @exception IOException if an I/O error occurs
* @since 1.0
*/
public FileInputStream getInputStream(String dn, String fn)
throws IOException {
return new FileInputStream(getFile(dn, fn));
}
/**
* Convenience method; creates a dir if does not exist.
*
* @param dirName directory name
* @return File
* @exception IOException if an I/O error occurs
* @since 1.0
*/
private File initDir(String dirName)
throws IOException {
File dir = new File(rootDir, dirName);
if (!dir.exists()) {
// We need to create the directory
if (!dir.mkdirs()) {
throw new IOException("Cm cannot create directory " + dir);
}
}
return dir;
}
public void createFolder(String f) throws IOException {
createFolder(f, null);
}
/**
* Creates a new folder. Note that this function is idempotent.
*
* @param f directory name
* @throws IOException if an I/O error occurs.
* @since 1.0
*/
public synchronized void createFolder(String f, String [] elements) throws IOException {
// while this method creates the CmCache
File dir = initDir(f);
switch (searchMethod) {
case INDEX :
case CACHE :
if (elements != null ) {
if (caches.get(f) == null) {
CmCache cmc = new CmCache(elements, dir);
caches.put(f,cmc);
new Thread(cmc, f + " CmCache ").start();
}
}
break;
case RAW:
default :
// do nothing
break;
}
}
/**
* Generates a random file name
*
* @param dn directory name in which the tmp filename would be created
* @return temporary file name.
* @since 1.0
*/
public String createTmpName(String dn) {
String tryName;
File dir = new File(rootDir, dn);
do {
tryName = "cm" + Integer.toString(random.nextInt());
} while ((new File(dir, tryName)).exists());
return tryName;
}
/**
* Remove a file
*
* @param dn directory name
* @param fn file name
* @throws IOException if an I/O error occurs
* @since 1.0
*/
public void remove(String dn, String fn)
throws IOException {
File file = getFile(dn, fn);
try {
if ( searchMethod == CACHE ) {
((CmCache) caches.get(dn)).remove(fn);
}
expirer.cancelFileExpiration(file);
} finally {
if ( file.exists() )
if (!file.delete()) {
throw new IOException("Cm cannot remove the file " + file);
}
}
}
/**
* Tests if a file does exist
*
* @param dn directory name
* @param fn file name
* @return Description of the Returned Value
* @throws IOException if an I/O error occurs
* @since 1.0
*/
public boolean exists(String dn, String fn)
throws IOException {
File file = getFile(dn, fn);
return file.exists();
}
/**
* Restore a saved StructuredDocument.
*
* @param dn directory name
* @param fn file name
* @return StructuredDocument containing the file
* @throws IOException if an I/O error occurs
* was not possible.
* @since 1.0
*/
public StructuredDocument restore(String dn, String fn)
throws IOException {
File file = getFile(dn, fn);
return restoreFile(file);
}
/**
* Restore a saved byte array.
*
* @param dn directory name
* @param fn file name
* @return byte [] containing the file
* @throws IOException if an I/O error occurs
* @throws InvocationTargetException when restoring the document
* was not possible.
* @since 1.0
*/
public byte[] restoreBytes(String dn, String fn)
throws IOException {
File file = getFile(dn, fn);
InputStream ip = null;
byte[] buffer = null;
try {
ip = new FileInputStream(file);
int size = (int) file.length();
buffer = new byte[size];
int got = 0;
while (got < size) {
int res = ip.read(buffer, got, size - got);
if (-1 == res) {
ip.close();
return null;
}
got += res;
}
} finally {
if (ip != null) {
try {
ip.close();
} catch (IOException ignored) {
}
}
}
return buffer;
}
/**
* saves a StructuredDocument in specified dir, and file name
*
* @param dn directory name
* @param fn file name
* @param doc StructuredDocument to save
* @exception IOException if an I/O error occurs
* @since 1.0
*/
public void save(String dn, String fn, StructuredDocument doc)
throws IOException {
save(dn,
fn,
doc,
DiscoveryService.INFINITE_LIFETIME,
DiscoveryService.NO_EXPIRATION);
}
/**
* saves a StructuredDocument in specified dir, and file name, and associated doc timeouts
*
* @param dn directory name
* @param fn file name
* @param doc StructuredDocument to save
* @param timeoutInMyCache time after which the document is removed from
* the local cahce. Expressed in milliseconds relative to to now.
* @param timeoutForOthers time after which the document is removed from
* remote caches. Expressed in milliseconds relative to to now.
* @exception IOException if an I/O error occurs
* @since 1.0
*/
public void save(String dn,
String fn,
StructuredDocument doc,
long timeoutInMyCache,
long timeoutForOthers) throws IOException {
File file = getFile(dn, fn);
FileOutputStream op = null;
try {
// Initiate or renew the expiration schedule
expirer.scheduleFileExpiration(file,
timeoutInMyCache,
timeoutForOthers );
// save the new version
op = new FileOutputStream(file);
doc.sendToStream(op);
op.close();
op = null;
if (searchMethod == CACHE) {
((CmCache) caches.get(dn)).add(fn, doc);
}
} catch (IOException e) {
if (LOG.isEnabledFor(Priority.DEBUG))
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -