KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > services > fileextraction > JahiaFileExtractionServiceImpl


1 //
2
// ____.
3
// __/\ ______| |__/\. _______
4
// __ .____| | \ | +----+ \
5
// _______| /--| | | - \ _ | : - \_________
6
// \\______: :---| : : | : | \________>
7
// |__\---\_____________:______: :____|____:_____\
8
// /_____|
9
//
10
// . . . i n j a h i a w e t r u s t . . .
11
//
12

13 package org.jahia.services.fileextraction;
14
15 import java.io.File JavaDoc;
16 import java.io.FileInputStream JavaDoc;
17 import java.io.FileOutputStream JavaDoc;
18 import java.io.IOException JavaDoc;
19 import java.io.InputStream JavaDoc;
20 import java.io.ObjectInputStream JavaDoc;
21 import java.io.ObjectOutput JavaDoc;
22 import java.io.ObjectOutputStream JavaDoc;
23 import java.util.Properties JavaDoc;
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 /**
32  *
33  * @author hollis
34  *
35  * TODO To change the template for this generated type comment go to
36  * Window - Preferences - Java - Code Style - Code Templates
37  */

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 JavaDoc FileExtractors = null;
50
51     private BeanFactory fileExtractorsFactory = null;
52     
53     private String JavaDoc 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 JavaDoc settings = (Properties JavaDoc)bf.getBean("settings");
67         String JavaDoc 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 JavaDoc f = new File JavaDoc (tmpDiskPath);
75         if (!f.isDirectory ()) {
76             f.mkdir ();
77         }
78
79         // fileExtractors
80
settings = (Properties JavaDoc)bf.getBean("fileExtractors");
81         try {
82             if ( settings != null ){
83                 this.FileExtractors = (Properties JavaDoc)settings.clone();
84             } else {
85                 logger.debug("No file extractors found!");
86             }
87         } catch ( Throwable JavaDoc t ){
88             throw new JahiaInitializationException("Error loading file extraction config:", t);
89         }
90     }
91
92     /**
93      * Return a file extractor for a given content type
94      *
95      * @param contentType String
96      * @throws JahiaException
97      * @return FileExtractor
98      */

99     public FileExtractor getFileExtractor(String JavaDoc contentType)
100         throws JahiaException {
101         if ( contentType != null ){
102             String JavaDoc className = FileExtractors.getProperty(contentType);
103             if ( className != null ){
104                 try {
105                     Class JavaDoc c = Class.forName(className);
106                     return (FileExtractor)c.newInstance();
107                 } catch ( ClassNotFoundException JavaDoc cnfe ){
108                     logger.debug(cnfe);
109                 } catch ( InstantiationException JavaDoc ie ){
110                     logger.debug(ie);
111                 } catch ( IllegalAccessException JavaDoc iae ){
112                     logger.debug(iae);
113                 }
114             }
115         }
116         return null;
117     }
118     
119     /**
120      *
121      * @param contentType
122      * @param cacheKey , a unique key used to cache the extraction ( serializing on disk )
123      * @param lastModified
124      * @param allowCache, if true, use a cached extractio if any
125      * @param fileStream
126      * @return
127      * @throws Exception
128      */

129     public ExtractedDocument getExtractedDocument( String JavaDoc contentType,
130                                                    String JavaDoc cacheKey,
131                                                    long lastModified,
132                                                    boolean allowCache,
133                                                    InputStream JavaDoc fileStream)
134     throws Exception JavaDoc {
135         return getExtractedDocument(contentType, cacheKey,lastModified, allowCache, fileStream, null);
136     }
137
138     /**
139      *
140      * @param contentType
141      * @param cacheKey , a unique key used to cache the extraction ( serializing on disk )
142      * @param lastModified
143      * @param allowCache, if true, use a cached extractio if any
144      * @param fileStream
145      * @param charSet
146      * @return
147      * @throws Exception
148      */

149     public ExtractedDocument getExtractedDocument( String JavaDoc contentType,
150                                                    String JavaDoc cacheKey,
151                                                    long lastModified,
152                                                    boolean allowCache,
153                                                    InputStream JavaDoc fileStream,
154                                                    String JavaDoc charSet)
155     throws Exception JavaDoc {
156         FileExtractor extractor = this.getFileExtractor(contentType);
157         if ( extractor == null ){
158             return null;
159         }
160         ExtractedDocument extDoc = null;
161         try {
162             // try to load previously extracted data if the file has not changed
163
String JavaDoc formattedPath = JahiaTools.replacePattern(cacheKey,"/","\\");
164             formattedPath = JahiaTools.replacePattern(formattedPath,"\\","_");
165             String JavaDoc tmpFilePath = this.tmpDiskPath + File.separator
166                 + "jahia_tmpfile_" + formattedPath;
167
168             if ( allowCache ){
169                 try {
170                     // Deserialize from a file
171
File JavaDoc f = new File JavaDoc(tmpFilePath);
172                     if ( f.exists() && f.lastModified()>lastModified ){
173                         ObjectInputStream JavaDoc in = new
174                         ObjectInputStream JavaDoc(new
175                         FileInputStream JavaDoc(f));
176                         // Deserialize the object
177
extDoc = (ExtractedDocument) in.readObject();
178                         in.close();
179                         logger.info("Use previous extracted file " + tmpFilePath);
180                     }
181                 } catch (ClassNotFoundException JavaDoc e) {
182                     logger.debug(e);
183                 } catch (IOException JavaDoc e) {
184                     //logger.debug(e); file could not exist and it's not an error
185
}
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                         // Serialize to a file
195
ObjectOutput JavaDoc out = new ObjectOutputStream JavaDoc(new
196                             FileOutputStream JavaDoc(tmpFilePath));
197                         out.writeObject(extDoc);
198                         out.close();
199                     }
200                     catch (IOException JavaDoc e) {
201                         logger.error(e.getMessage());
202                     }
203                 }
204             }
205         } catch ( Throwable JavaDoc t ){
206             logger.error(t.getMessage());
207         }
208         return extDoc;
209     }
210
211 }
212
Popular Tags