📄 cmssynchronize.java
字号:
hashFile = null;
}
}
}
/**
* Synchronizes a file in the virtual or the server filesystem.
*
* @param vfsFile the file to synchronize.
* @exception CmsException the CmsException is thrown if something goes wrong.
*/
private void synchronizeFile(CmsFile vfsFile)
throws CmsException{
File sfsFile = null;
CmsFile updVfsFile = null;
int action = C_SYNC_NONE;
String path = m_synchronizePath+vfsFile.getAbsolutePath().substring(0,vfsFile.getAbsolutePath().lastIndexOf("/"));
String fileName = vfsFile.getAbsolutePath().substring(vfsFile.getAbsolutePath().lastIndexOf("/")+1);
sfsFile = new File(path, fileName);
if (!sfsFile.exists()){
// if the file does not exist in SFS, it has to be created
createNewLocalFile(sfsFile);
sfsFile = new File(path, fileName);
action = C_SYNC_SFS;
} else {
// if the file exists in SFS, compare the versions of the file
action = compareDate(vfsFile.getAbsolutePath(), vfsFile.getDateLastModified(), sfsFile.lastModified());
}
switch (action){
case 1 :
// the file from VFS has changed, so update the SFS
try {
writeFileByte(vfsFile.getContents(), sfsFile);
m_synchronizeList.putDates(vfsFile.getAbsolutePath(), vfsFile.getDateLastModified(), sfsFile.lastModified());
} catch (Exception e){
throw new CmsException("["+this.getClass().getName()+"]"+" Error while updating server filesystem",e);
}
break;
case 2 :
// the file from SFS has changed, so update the VFS
try {
byte [] content = getFileBytes(sfsFile);
m_cms.lockResource(vfsFile.getAbsolutePath(), true);
updVfsFile = m_cms.readFile(vfsFile.getAbsolutePath());
updVfsFile.setContents(content);
m_cms.writeFile(updVfsFile);
m_synchronizeList.putDates(vfsFile.getAbsolutePath(), m_cms.readFile(updVfsFile.getAbsolutePath()).getDateLastModified(), sfsFile.lastModified());
} catch (Exception e) {
throw new CmsException("["+this.getClass().getName()+"]"+" Error while updating virtual filesystem",e);
}
break;
case 3 :
// both files have changed, so copy the existing file in the SFS to a backup-file
// and update the file from VFS to SFS
File backupFile = new File(path,"$"+fileName);
if (copyServerFile(sfsFile, backupFile)){
// file has copied successfully, now copy the file from VFS
try {
writeFileByte(vfsFile.getContents(), sfsFile);
m_synchronizeList.putDates(vfsFile.getAbsolutePath(), vfsFile.getDateLastModified(), sfsFile.lastModified());
} catch (Exception e) {
throw new CmsException("["+this.getClass().getName()+"]"+" Error while updating server filesystem",e);
}
} else {
// throw an exception if the file could not be renamed
throw new CmsException("["+this.getClass().getName()+"]"+" The file "+sfsFile.getName()+" could not be copied");
}
break;
default :
// do nothing
break;
}
}
/**
* Compares the date of the resource in the virtual and the server filesystem
* with the date in the synchronize list
*
* @param resourceName the resource to compare.
* @param vfsDate the date of the resource in the virtual filesystem.
* @param sfsDate the date of the resource in the server filesystem.
* @exception CmsException the CmsException is thrown if something goes wrong.
* @return int the number of the action for synchronizeFile
*/
private int compareDate(String resourceName, long vfsDate, long sfsDate)
throws CmsException{
long syncVfsDate = m_synchronizeList.getVfsDate(resourceName);
long syncSfsDate = m_synchronizeList.getSfsDate(resourceName);
if (syncSfsDate == 0){
return C_SYNC_SFS;
}
if (vfsDate > syncVfsDate){
if (sfsDate > syncSfsDate){
return C_SYNC_SFSNEW;
} else {
return C_SYNC_SFS;
}
} else {
if (sfsDate > syncSfsDate){
return C_SYNC_VFS;
} else {
return C_SYNC_NONE;
}
}
}
/**
* Creates the new file on the SFS
*
* @param newFile the file that has to be created.
* @exception CmsException the CmsException is thrown if something goes wrong.
*/
private void createNewLocalFile(File newFile) throws CmsException {
FileOutputStream fOut = null;
if (newFile.exists()){
throw new CmsException("["+this.getClass().getName()+"] "+newFile.getPath()+" already exists on filesystem");
}
try {
File parentFolder = new File(newFile.getPath().replace('/', newFile.separatorChar).substring(0,newFile.getPath().lastIndexOf(newFile.separator)));
parentFolder.mkdirs();
if (parentFolder.exists()){
fOut = new FileOutputStream(newFile);
} else {
throw new CmsException("["+this.getClass().getName()+"]"+" Cannot create directories for "+newFile.getPath());
}
} catch (IOException e){
throw new CmsException("["+this.getClass().getName()+"]"+" Cannot create file "+newFile.getPath()+" on filesystem", e);
} finally {
if (fOut != null){
try {
fOut.close();
} catch (IOException e){
}
}
}
}
/**
* this copies a file on the server filesystem
*
* @param fromFile the file that has to be copied.
* @param toFile the copy of the file fromFile.
* @return boolean if the file could be copied
*/
private boolean copyServerFile(File fromFile, File toFile){
FileInputStream fIn = null;
FileOutputStream fOut = null;
try {
fIn = new FileInputStream(fromFile);
fOut = new FileOutputStream(toFile);
synchronized (fIn){
synchronized (fOut){
byte[] buffer = new byte[256];
while (true){
int bytesRead = fIn.read(buffer);
if (bytesRead == -1){
break;
}
fOut.write(buffer, 0, bytesRead);
}
}
}
return true;
} catch (IOException e){
return false;
} finally {
try {
fIn.close();
} catch (IOException e){
}
try {
fOut.close();
} catch (IOException e){
}
}
}
/**
* Deletes the folder and all subresources from SFS
* The folder has to be empty before it can be deleted
*
* @param delFolder the folder that has to be deleted.
* @return boolean is true if all directories could be deleted
*/
private boolean deleteDirectory (File delFolder){
String[] diskFiles = delFolder.list();
File currentFile;
String resourceName;
for (int i = 0; i < diskFiles.length; i++){
currentFile = new File(delFolder, diskFiles[i]);
if (currentFile.isDirectory()){
deleteDirectory(currentFile);
if (!currentFile.delete()){
return false;
}
resourceName = currentFile.getParent().substring(m_synchronizePath.length())+currentFile.getName()+currentFile.separatorChar;
} else {
if (!currentFile.delete()){
return false;
}
resourceName = currentFile.getParent().substring(m_synchronizePath.length(), currentFile.getParent().lastIndexOf(currentFile.separatorChar)+1)+currentFile.getName();
// if a file is deleted it has to be removed from the synchronize list
m_synchronizeList.remove(resourceName);
}
}
return delFolder.delete();
}
/**
* Returns a byte array containing the content of the file.
*
* @param filename The name of the file to read.
* @return bytes[] The content of the file.
*/
private byte[] getFileBytes(File file)
throws Exception{
byte[] buffer = null;
FileInputStream fileStream = null;
int charsRead;
int size;
try {
fileStream = new FileInputStream(file);
charsRead = 0;
size = new Long(file.length()).intValue();
buffer = new byte[size];
while(charsRead < size) {
charsRead += fileStream.read(buffer, charsRead, size - charsRead);
}
return buffer;
} catch (IOException e) {
throw e;
} finally {
try {
if (fileStream != null)
fileStream.close();
} catch (IOException e) {
}
}
}
/**
* Gets the file type for the filename.
*
* @param filename the resource to get the type.
* @exception CmsException the CmsException is thrown if something goes wrong.
* @return String the type for the resource
*/
private String getFileType(String filename)
throws CmsException {
String suffix = filename.substring(filename.lastIndexOf('.')+1);
suffix = suffix.toLowerCase(); // file extension of filename
// read the known file extensions from the database
Hashtable extensions = m_cms.readFileExtensions();
String resType = new String();
if (extensions != null) {
resType = (String) extensions.get(suffix);
}
if (resType == null) {
resType = "plain";
}
return resType;
}
/**
* This writes the byte content of a resource to the file on the server filesystem
*
* @param content the content of the file in the VFS.
* @param file the file in SFS that has to be updated with content.
* @exception Exception the Exception is thrown if something goes wrong.
*/
private void writeFileByte(byte[] content, File file)
throws Exception {
FileOutputStream fOut = null;
DataOutputStream dOut = null;
try {
// write the content to the file in server filesystem
fOut = new FileOutputStream(file);
dOut = new DataOutputStream(fOut);
dOut.write(content);
dOut.flush();
} catch (IOException e) {
throw e;
} finally {
try {
if (fOut != null)
fOut.close();
} catch (IOException e) {
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -