📄 jahiatextfilebaseservice.java
字号:
//// ____.// __/\ ______| |__/\. _______// __ .____| | \ | +----+ \// _______| /--| | | - \ _ | : - \_________// \\______: :---| : : | : | \________>// |__\---\_____________:______: :____|____:_____\// /_____|//// . . . i n j a h i a w e t r u s t . . .////// JahiaTextFileBaseServices// EV 18.11.2000// DJ 06.01.2001 - added cache//// init()// composeJahiaFileName()// loadContents()// saveContents()// fileExists()// readFile()// writeFile()//package org.jahia.services.files;import java.io.*; // File access methodsimport java.util.*; // Vectorimport org.jahia.utils.*; // JahiaConsoleimport org.jahia.settings.*; // JahiaPrivateSettingsimport org.jahia.services.files.*; // JahiaService Interfaceimport org.jahia.services.usermanager.*;import org.jahia.exceptions.*;import org.jahia.data.cache.*; // JahiaCacheimport org.jahia.services.cache.*; // JahiaCacheFactorypublic class JahiaTextFileBaseService extends JahiaTextFileService { private static String serviceName = "JahiaTextFileService"; private static String jahiaDataDiskPath = ""; private static JahiaTextFileBaseService theObject = null; private static JahiaCache cache_id_text; // jahiaID_pageID_fieldID -> String private static Cache_id_text_IO cache_id_text_IO; /*** * constructor * EV 18.11.2000 * */ protected JahiaTextFileBaseService() { JahiaConsole.println( "Service", "***** Starting " + serviceName + " *****" ); } // end constructor /*** * getInstance * EV 31.10.2000 * */ public static synchronized JahiaTextFileBaseService getInstance() { if (theObject == null) { theObject = new JahiaTextFileBaseService(); } return theObject; } // end getInstance /*** * init * EV 18.11.2000 * */ public void init( JahiaPrivateSettings jSettings ) throws JahiaInitializationException { this.jahiaDataDiskPath = jSettings.jahiaFilesBigTextDiskPath; File filePath = new File( this.jahiaDataDiskPath ); if (!filePath.exists()) { JahiaConsole.println( "TextFileManager", "Creating " + this.jahiaDataDiskPath + " path " ); filePath.mkdirs(); } cache_id_text_IO = new Cache_id_text_IO(); cache_id_text = JahiaCacheFactory.getInstance().createJahiaCache("TextFileCache","Caches the BIGTEXTs stored on the disk as files", cache_id_text_IO, 1000); } // end init /*** * loadContents * EV 18.11.2000 * called by load_field_from_resultset (FieldsDBManager) * */ public String loadContents( int jahiaID, int pageID, int fieldID, String fieldValue ) throws JahiaException { String fileName = FileUtils.getInstance().composeJahiaFileName( jahiaDataDiskPath, jahiaID, pageID, fieldID ); String fileContents = ""; if (FileUtils.getInstance().fileExists( fileName )) { fileContents = (String)cache_id_text.getValue(fileName, fileName); } else { cache_id_text.setValue(fieldValue, fileName, fileName); fileContents = fieldValue; } return fileContents; } // end loadContents /*** * saveContents * EV 18.11.2000 * EV 18.11.2000 returns the file name * */ public String saveContents( int jahiaID, int pageID, int fieldID, String fieldValue ) throws JahiaException { String fileName = FileUtils.getInstance().composeJahiaFileName( jahiaDataDiskPath, jahiaID, pageID, fieldID ); cache_id_text.setValue(fieldValue, fileName, fileName); return fileName; } // end loadContents /*** * getFileName * YG 29.08.2001 * returns the file name * */ public String getFileName( int jahiaID, int pageID, int fieldID) throws JahiaException { String fileName = FileUtils.getInstance().composeJahiaFileName( jahiaDataDiskPath, jahiaID, pageID, fieldID ); return fileName; } // end loadContents /** * set the service name */ public void setName(String name){ serviceName = name; }///////////////////////////////// CacheIO objects /////////////////////////////////////// /** * CacheIO methods for cache_id_text * DJ 06.01.2001 */ private class Cache_id_text_IO implements JahiaCacheIO { // LOAD public Object loadValue (String cacheID, Object paramsIO) throws JahiaException { // JahiaConsole.println ("Cache_id_text_IO", "reading from disk : "+ cacheID +" : " ); return FileUtils.getInstance().readFile( (String)paramsIO ); } // SAVE public void saveValue (Object theObject, String cacheID, Object paramsIO) throws JahiaException { //JahiaConsole.println ("Cache_id_text_IO", "saving on disk : "+ cacheID +" : " ); FileUtils.getInstance().writeFile( (String)paramsIO, (String)theObject ); } // DELETE public void deleteValue (String cacheID, Object paramsIO) throws JahiaException { } } //-------------------------------------------------------------------------- /** * Copy all big text of a site in a gived folder * This folder must be a valid folder * * @param int siteID the id of the requested site * @param String destFolder the destination Folder * @return the number of duplicated files, -1 on error * @author NK */ public int copySiteBigText(int siteID, String destFolder) throws IOException{ File f = new File(destFolder); if ( !f.isDirectory() || !f.canWrite() ){ return -1; } String destFolderPath = destFolder + File.separator; f = null; f = new File(jahiaDataDiskPath); if ( !f.isDirectory() || !f.canRead() ){ return -1; } File[] files = f.listFiles(); if ( files.length == 0 ){ return 0; } String siteLabel = siteID + "-"; int nb = files.length; File destFile = null; int nbCopy = 0; for ( int i=0 ; i<nb ; i++ ){ if ( files[i].getName().startsWith(siteLabel) ){ destFile = new File(destFolderPath+files[i].getName()); try { FileInputStream fileInput = new FileInputStream( files[i] ); FileOutputStream fileOutput = new FileOutputStream( destFile ); JahiaTools.copyStream( fileInput, fileOutput ); fileInput = null; fileOutput = null; nbCopy += 1; } catch ( java.io.FileNotFoundException fnfe ){ // } } } return nbCopy; } //-------------------------------------------------------------------------- /** * Delete all big text files of a site * * @param int siteID the id of the requested site * @param User the user must be a root user * @return false on error * @author NK */ public boolean deleteSiteBigText(int siteID, JahiaUser user) throws IOException{ if ( !user.isAdminMember(0) ){ return false; } File f = new File(jahiaDataDiskPath); if ( !f.isDirectory() || !f.canWrite() ){ return false; } File[] files = f.listFiles(); String siteLabel = siteID + "-"; int nb = files.length; for ( int i=0 ; i<nb ; i++ ){ if ( files[i].getName().startsWith(siteLabel) ){ files[i].delete(); } } return true; }} // end JahiaTextFileBaseService
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -