1 13 package org.jahia.services.fileextraction; 14 15 import java.io.File ; 16 import java.io.FileInputStream ; 17 import java.io.FileOutputStream ; 18 import java.io.IOException ; 19 import java.io.InputStream ; 20 import java.io.ObjectInputStream ; 21 import java.io.ObjectOutput ; 22 import java.io.ObjectOutputStream ; 23 import java.util.Properties ; 24 25 import org.jahia.exceptions.JahiaException; 26 import org.jahia.exceptions.JahiaInitializationException; 27 import org.jahia.settings.SettingsBean; 28 import org.jahia.utils.JahiaTools; 29 import org.springframework.beans.factory.BeanFactory; 30 31 38 public class JahiaFileExtractionServiceImpl extends JahiaFileExtractionService { 39 40 private static org.apache.log4j.Logger logger = 41 org.apache.log4j.Logger.getLogger (JahiaFileExtractionServiceImpl.class); 42 43 static private JahiaFileExtractionServiceImpl instance = null; 44 45 protected JahiaFileExtractionServiceImpl() { 46 logger.info("***** Starting the JahiaFileExtractionServiceImpl *****" ); 47 } 48 49 private Properties FileExtractors = null; 50 51 private BeanFactory fileExtractorsFactory = null; 52 53 private String tmpDiskPath = null; 54 55 public static JahiaFileExtractionServiceImpl getInstance() { 56 if (instance == null) { 57 instance = new JahiaFileExtractionServiceImpl(); 58 } 59 return instance; 60 } 61 62 public void init( SettingsBean jSettings ) 63 throws JahiaInitializationException { 64 65 BeanFactory bf = org.jahia.bin.Jahia.getConfigBeanFactory(); 66 Properties settings = (Properties )bf.getBean("settings"); 67 String tmpDir = settings.getProperty("org.jahia.services.fileextractor.tmpdir"); 68 if ( tmpDir == null || "".equals(tmpDir) ){ 69 tmpDir = "fileextraction"; 70 } 71 tmpDiskPath = jSettings.getJahiaVarDiskPath () + 72 File.separator + tmpDir; 73 74 File f = new File (tmpDiskPath); 75 if (!f.isDirectory ()) { 76 f.mkdir (); 77 } 78 79 settings = (Properties )bf.getBean("fileExtractors"); 81 try { 82 if ( settings != null ){ 83 this.FileExtractors = (Properties )settings.clone(); 84 } else { 85 logger.debug("No file extractors found!"); 86 } 87 } catch ( Throwable t ){ 88 throw new JahiaInitializationException("Error loading file extraction config:", t); 89 } 90 } 91 92 99 public FileExtractor getFileExtractor(String contentType) 100 throws JahiaException { 101 if ( contentType != null ){ 102 String className = FileExtractors.getProperty(contentType); 103 if ( className != null ){ 104 try { 105 Class c = Class.forName(className); 106 return (FileExtractor)c.newInstance(); 107 } catch ( ClassNotFoundException cnfe ){ 108 logger.debug(cnfe); 109 } catch ( InstantiationException ie ){ 110 logger.debug(ie); 111 } catch ( IllegalAccessException iae ){ 112 logger.debug(iae); 113 } 114 } 115 } 116 return null; 117 } 118 119 129 public ExtractedDocument getExtractedDocument( String contentType, 130 String cacheKey, 131 long lastModified, 132 boolean allowCache, 133 InputStream fileStream) 134 throws Exception { 135 return getExtractedDocument(contentType, cacheKey,lastModified, allowCache, fileStream, null); 136 } 137 138 149 public ExtractedDocument getExtractedDocument( String contentType, 150 String cacheKey, 151 long lastModified, 152 boolean allowCache, 153 InputStream fileStream, 154 String charSet) 155 throws Exception { 156 FileExtractor extractor = this.getFileExtractor(contentType); 157 if ( extractor == null ){ 158 return null; 159 } 160 ExtractedDocument extDoc = null; 161 try { 162 String formattedPath = JahiaTools.replacePattern(cacheKey,"/","\\"); 164 formattedPath = JahiaTools.replacePattern(formattedPath,"\\","_"); 165 String tmpFilePath = this.tmpDiskPath + File.separator 166 + "jahia_tmpfile_" + formattedPath; 167 168 if ( allowCache ){ 169 try { 170 File f = new File (tmpFilePath); 172 if ( f.exists() && f.lastModified()>lastModified ){ 173 ObjectInputStream in = new 174 ObjectInputStream (new 175 FileInputStream (f)); 176 extDoc = (ExtractedDocument) in.readObject(); 178 in.close(); 179 logger.info("Use previous extracted file " + tmpFilePath); 180 } 181 } catch (ClassNotFoundException e) { 182 logger.debug(e); 183 } catch (IOException e) { 184 } 186 } 187 if ( extDoc == null ){ 188 extDoc = extractor.getExtractedDocument(cacheKey,lastModified,fileStream,charSet); 189 if ( extDoc == null ){ 190 extDoc = new ExtractedDocumentImpl(); 191 } 192 if ( allowCache ){ 193 try { 194 ObjectOutput out = new ObjectOutputStream (new 196 FileOutputStream (tmpFilePath)); 197 out.writeObject(extDoc); 198 out.close(); 199 } 200 catch (IOException e) { 201 logger.error(e.getMessage()); 202 } 203 } 204 } 205 } catch ( Throwable t ){ 206 logger.error(t.getMessage()); 207 } 208 return extDoc; 209 } 210 211 } 212 | Popular Tags |