View Javadoc
1   package org.exoplatform.services.cms.jodconverter.impl;
2   
3   import org.artofsolving.jodconverter.OfficeDocumentConverter;
4   import org.artofsolving.jodconverter.document.DocumentFamily;
5   import org.artofsolving.jodconverter.document.DocumentFormat;
6   import org.artofsolving.jodconverter.document.DocumentFormatRegistry;
7   import org.artofsolving.jodconverter.document.SimpleDocumentFormatRegistry;
8   import org.artofsolving.jodconverter.office.DefaultOfficeManagerConfiguration;
9   import org.artofsolving.jodconverter.office.OfficeException;
10  import org.artofsolving.jodconverter.office.OfficeManager;
11  import org.exoplatform.container.xml.InitParams;
12  import org.exoplatform.services.cms.jodconverter.JodConverterService;
13  import org.exoplatform.services.log.ExoLogger;
14  import org.exoplatform.services.log.Log;
15  import org.picocontainer.Startable;
16  
17  import java.io.File;
18  import java.util.Collections;
19  
20  /**
21   * {@inheritDoc}
22   */
23  public class JodConverterServiceImpl implements JodConverterService, Startable {
24  
25    private OfficeManager officeManager = null;
26    private OfficeDocumentConverter documentConverter = null;
27    private boolean enable = false;
28  
29    private static final Log LOG = ExoLogger.getLogger(JodConverterServiceImpl.class.getName());
30  
31    public JodConverterServiceImpl(InitParams initParams) throws Exception {
32      int ports[];
33      String enableJod = System.getProperty("wcm.jodconverter.enable");
34      if(enableJod == null || enableJod.isEmpty()) {
35        enable = true;
36      } else {
37        enable = Boolean.parseBoolean(enableJod);
38      }
39      if(enable) {
40        
41        String officeHomeParam = initParams.getValueParam("officeHome").getValue();
42        String portNumbers = initParams.getValueParam("port").getValue();
43        String taskQueueTimeout = initParams.getValueParam("taskQueueTimeout").getValue();
44        String taskExecutionTimeout = initParams.getValueParam("taskExecutionTimeout").getValue();
45        String maxTasksPerProcess = initParams.getValueParam("maxTasksPerProcess").getValue();
46        String retryTimeout = initParams.getValueParam("retryTimeout").getValue();
47        
48        DefaultOfficeManagerConfiguration configuration = new DefaultOfficeManagerConfiguration();
49        if (portNumbers != null) {
50          try {
51            String[] portsList = portNumbers.split(",");
52            ports = new int[portsList.length];
53            
54            for (int i = 0; i < portsList.length; i++) {
55              ports[i] = Integer.parseInt(portsList[i].trim());
56            }
57            configuration.setPortNumbers(ports);
58          } catch (NumberFormatException nfe) {
59            if (LOG.isWarnEnabled()) {
60              LOG.warn("Wrong configuration ==> Use default portNumbers value of DefaultOfficeManagerConfiguration");
61            }
62          }
63        }
64        // in case of not setting office home, JODConverter will use system default
65        // office home by using OfficeUtils.getDefaultOfficeHome();
66        if (officeHomeParam != null && officeHomeParam.trim().length() != 0) {
67          try {
68            configuration.setOfficeHome(officeHomeParam);
69          } catch (IllegalArgumentException iae) {
70            if (LOG.isWarnEnabled()) {
71              LOG.warn("Wrong configuration ==> Use default officeHome value of DefaultOfficeManagerConfiguration");
72            }
73          }
74        }
75        
76        if (taskQueueTimeout != null) {
77          try {
78            configuration.setTaskQueueTimeout(Long.parseLong(taskQueueTimeout));
79          } catch (NumberFormatException nfe) {
80            if (LOG.isWarnEnabled()) {
81              LOG.warn("Wrong configuration ==> Use default taskQueueTimeout value of DefaultOfficeManagerConfiguration");
82            }
83          }
84        }
85        
86        if (taskExecutionTimeout != null) {
87          try {
88            configuration.setTaskExecutionTimeout(Long.parseLong(taskExecutionTimeout));
89          } catch (NumberFormatException nfe) {
90            if (LOG.isWarnEnabled()) {
91              LOG.warn("Wrong configuration ==> Use default taskExecutionTimeout value of DefaultOfficeManagerConfiguration");
92            }
93          }
94        }
95        
96        if (retryTimeout != null) {
97          try {
98            configuration.setRetryTimeout(Long.parseLong(retryTimeout));
99          } catch (NumberFormatException nfe) {
100           if (LOG.isWarnEnabled()) {
101             LOG.warn("Wrong configuration ==> Use default retryTimeout value of DefaultOfficeManagerConfiguration");
102           }
103         }
104       }
105       
106       if (maxTasksPerProcess != null) {
107         try {
108           configuration.setMaxTasksPerProcess(Integer.parseInt(maxTasksPerProcess));
109         } catch (NumberFormatException nfe) {
110           if (LOG.isWarnEnabled()) {
111             LOG.warn("Wrong configuration ==> Use default maxTasksPerProcess value of DefaultOfficeManagerConfiguration");
112           }
113         }
114       }
115       
116       try {
117         officeManager = configuration.buildOfficeManager();
118         documentConverter = new OfficeDocumentConverter(officeManager);
119         DocumentFormatRegistry documentFormatRegistry = documentConverter.getFormatRegistry();
120         if (documentFormatRegistry instanceof SimpleDocumentFormatRegistry) {
121           DocumentFormat jpg = new DocumentFormat("JPEG Image", "jpg", "image/jpeg");
122           jpg.setInputFamily(DocumentFamily.DRAWING);
123           jpg.setStoreProperties(DocumentFamily.TEXT, Collections.singletonMap("FilterName", "writer_jpg_Export"));
124           ((SimpleDocumentFormatRegistry) documentFormatRegistry).addFormat(jpg);
125         } else {
126           LOG.warn("Can't add a specific document format for thumbnail generation from a Word Document.");
127         }
128       } catch (IllegalStateException ise) {
129         if (LOG.isErrorEnabled()) {
130           LOG.equals(ise.getMessage());
131         }
132       }
133     }
134   }
135 
136   public void start() {
137     if(!enable) {
138       LOG.info("JODConverter is disabled. To view office files in Activity Stream or Content Explorer, you need to change EXO_JODCONVERTER_ENABLE=true in " 
139             + "customization setting file");
140       return;  
141     }
142     try {
143       if (officeManager != null) {
144         officeManager.start();
145       }
146     } catch (OfficeException oe) {
147       if (LOG.isErrorEnabled()) {
148         LOG.error("Exception when start Office Service: ",oe);
149       }
150     }
151   }
152 
153   public void stop() {
154     if(!enable) {
155       LOG.info("JODConverter is disabled. To view office files in Activity Stream or Content Explorer, you need to change EXO_JODCONVERTER_ENABLE=true in " 
156             + "customization setting file");
157       return;
158     }
159     try {
160       if (officeManager != null) {
161         officeManager.stop();
162       }
163     } catch (OfficeException oe) {
164       if (LOG.isErrorEnabled()) {
165         LOG.error("Exception when stop Office Service: ",oe);
166       }
167     }
168   }
169 
170 /**
171  * {@inheritDoc}
172  */
173   public boolean convert(File input, File output, String outputFormat) throws OfficeException {
174     if(!enable) {
175       LOG.debug("JodConverter is disabled so you cannot view this document! " +
176               "To enable it, please change wcm.jodconverter.enable=true in configuration.properties file");
177       return false;
178     }
179     if (officeManager != null && officeManager.isRunning()) {
180       if (documentConverter != null) {
181         DocumentFormat documentFormat = documentConverter.getFormatRegistry().getFormatByExtension(outputFormat);
182         if (documentFormat == null) {
183           LOG.warn("Can't convert file {} because no corresponding document conversion for extension '{}'", input.getPath(), outputFormat);
184           return false;
185         }
186     	try {
187           documentConverter.convert(input, output, documentFormat);
188           return true;
189     	} catch (Exception e){
190           LOG.warn("Failed to convert file {} to '{}'", input.getPath(), outputFormat, e);
191           return false;
192     	}
193       }
194       return false;
195     } 
196     if (LOG.isWarnEnabled()) {
197       LOG.warn("this OfficeManager is currently stopped!");
198     }
199     return false;
200   }
201 
202 }