Files
alfresco-community-repo/source/java/org/alfresco/repo/content/caching/CacheFileProps.java
Alan Davis 84841e063f Merged 5.2.N (5.2.1) to HEAD (5.2)
125788 rmunteanu: Merged 5.1.N (5.1.2) to 5.2.N (5.2.1)
      125606 rmunteanu: Merged 5.1.1 (5.1.1) to 5.1.N (5.1.2)
         125515 slanglois: MNT-16155 Update source headers - add new Copyrights for Java and JSP source files + automatic check in the build


git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@127810 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
2016-06-03 17:08:06 +00:00

202 lines
5.7 KiB
Java

/*
* #%L
* Alfresco Repository
* %%
* Copyright (C) 2005 - 2016 Alfresco Software Limited
* %%
* This file is part of the Alfresco software.
* If the software was purchased under a paid Alfresco license, the terms of
* the paid license agreement will prevail. Otherwise, the software is
* provided under the following open source license terms:
*
* Alfresco is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Alfresco 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 Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Alfresco. If not, see <http://www.gnu.org/licenses/>.
* #L%
*/
package org.alfresco.repo.content.caching;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.util.Properties;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
/**
* Manage a cache file's associated properties.
*
* @author Matt Ward
*/
public class CacheFileProps
{
private static final String CONTENT_URL = "contentUrl";
private static final String DELETE_WATCH_COUNT = "deleteWatchCount";
private static final Log log = LogFactory.getLog(CacheFileProps.class);
private final Properties properties = new Properties();
private final File cacheFile;
private final File propsFile;
/**
* Construct a CacheFileProps specifying which cache file the properties belong to.
*
* @param cacheFile - cache file
*/
public CacheFileProps(File cacheFile)
{
this.cacheFile = cacheFile;
this.propsFile = fileForCacheFile();
}
/**
* Load properties from the cache file's associated properties file.
*/
public void load()
{
properties.clear();
if (propsFile.exists())
{
try
{
BufferedReader reader = new BufferedReader(new FileReader(propsFile));
properties.load(reader);
reader.close();
}
catch (FileNotFoundException error)
{
log.error("File disappeared after exists() check: " + cacheFile);
}
catch (IOException e)
{
throw new RuntimeException("Unable to read properties file " + cacheFile, e);
}
}
}
/**
* Save properties to the cache file's associated properties file.
*/
public void store()
{
BufferedOutputStream out = null;
try
{
out = new BufferedOutputStream(new FileOutputStream(propsFile));
properties.store(out, "Properties for " + cacheFile);
}
catch(FileNotFoundException e)
{
throw new RuntimeException("Couldn't create output stream for file: " + propsFile, e);
}
catch(IOException e)
{
throw new RuntimeException("Couldn't write file: " + propsFile, e);
}
finally
{
try
{
if (out != null) out.close();
}
catch(IOException e)
{
// Couldn't close it, just log that it wasn't possible.
if (log.isErrorEnabled())
{
log.error("Couldn't close file: " + propsFile);
}
}
}
}
/**
* Delete the cache file's associated properties file.
*/
public void delete()
{
propsFile.delete();
}
/**
* Does a properties file exist for the cache file?
*
* @return true if the file exists
*/
public boolean exists()
{
return propsFile.exists();
}
/**
* Size of the properties file or 0 if it does not exist.
*
* @return file size in bytes.
*/
public long fileSize()
{
return propsFile.length();
}
/**
* Set the value of the contentUrl property.
*
* @param url String
*/
public void setContentUrl(String url)
{
properties.setProperty(CONTENT_URL, url);
}
/**
* Get the value of the contentUrl property.
*
* @return contentUrl
*/
public String getContentUrl()
{
return properties.getProperty(CONTENT_URL);
}
/**
* Set the value of the deleteWatchCount property.
*
* @param watchCount Integer
*/
public void setDeleteWatchCount(Integer watchCount)
{
properties.setProperty(DELETE_WATCH_COUNT, watchCount.toString());
}
/**
* Get the value of the deleteWatchCount property.
*
* @return deleteWatchCount
*/
public Integer getDeleteWatchCount()
{
String watchCountStr = properties.getProperty(DELETE_WATCH_COUNT, "0");
return Integer.parseInt(watchCountStr);
}
// Generate the path for the properties file, based upon the cache file's path.
private File fileForCacheFile()
{
return new File(cacheFile.getAbsolutePath() + ".properties");
}
}