/*
* This program is free software; you can redistribute it and/or modify it under the
* terms of the GNU Lesser General Public License, version 2.1 as published by the Free Software
* Foundation.
*
* You should have received a copy of the GNU Lesser General Public License along with this
* program; if not, you can obtain a copy at http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html
* or from the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU Lesser General Public License for more details.
*
* Copyright 2005 - 2009 Pentaho Corporation. All rights reserved.
*
*
* @created Jul 26, 2005
* @author Gretchen Moran
*
*/
package org.pentaho.platform.plugin.action.eclipsebirt;
import java.io.File;
import java.util.StringTokenizer;
import java.util.logging.Level;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.eclipse.birt.core.exception.BirtException;
import org.eclipse.birt.core.framework.Platform;
import org.eclipse.birt.report.engine.api.EngineConfig;
import org.eclipse.birt.report.engine.api.HTMLRenderOption;
import org.eclipse.birt.report.engine.api.IRenderOption;
import org.eclipse.birt.report.engine.api.IReportEngine;
import org.eclipse.birt.report.engine.api.IReportEngineFactory;
import org.eclipse.birt.report.engine.api.RenderOption;
import org.pentaho.platform.api.engine.ILogger;
import org.pentaho.platform.api.engine.IPentahoSession;
import org.pentaho.platform.api.engine.IPentahoSystemListener;
import org.pentaho.platform.engine.core.system.PentahoSystem;
import org.pentaho.platform.plugin.action.messages.Messages;
import org.pentaho.platform.util.logging.Logger;
public class BirtSystemListener implements IPentahoSystemListener {
private static IReportEngine reportEngine = null;
private static final Log logger = LogFactory.getLog(BirtSystemListener.class);
private static String workaroundProtocolHandler = null; // Workaround for Eclipse bug 156877
String imageDirectory = null;
String logDirectory = null;
public boolean startup(final IPentahoSession session) {
// Need directory for temporarily writing images for HTML reports
imageDirectory = PentahoSystem.getApplicationContext().getFileOutputPath("system/tmp/BIRT"); //$NON-NLS-1$
File dir = new File(imageDirectory);
try {
if (!dir.exists()) {
dir.mkdirs();
}
} catch (Exception e) {
Logger.warn(BirtSystemListener.class.getName(), Messages.getInstance().getErrorString(
"BIRT.ERROR_0011_DIRECTORY_CREATION_FAILED", imageDirectory)); //$NON-NLS-1$
}
// Need directory for BIRT logging or BIRT blows chunks
logDirectory = PentahoSystem.getApplicationContext().getFileOutputPath("system/logs/BIRT"); //$NON-NLS-1$
dir = new File(logDirectory);
try {
if (!dir.exists()) {
dir.mkdirs();
}
} catch (Exception e) {
Logger.warn(BirtSystemListener.class.getName(), Messages.getInstance().getErrorString(
"BIRT.ERROR_0011_DIRECTORY_CREATION_FAILED", logDirectory)); //$NON-NLS-1$
}
// Create a BIRT report engine configuration object
BirtSystemListener.reportEngine = createBIRTEngine();
BIRTReportComponent.reportEngine = BirtSystemListener.reportEngine;
return (BirtSystemListener.reportEngine != null);
}
public void shutdown() {
BIRTReportComponent.reportEngine = null;
// TODO: should this be moved to maintenance so the image directory can
// be purged more often?
File dir = new File(imageDirectory);
try {
if (!dir.exists()) {
return;
}
File[] files = dir.listFiles();
for (File element : files) {
element.delete();
}
//dir.delete();
} catch (Exception e) {
Logger.warn(BirtSystemListener.class.getName(), Messages.getInstance().getErrorString(
"BIRT.ERROR_0012_CANT_COMPLETE_PURGE", logDirectory)); //$NON-NLS-1$
}
try {
// Bart Maertens, 14/11/2007: reportEngine.shutdown() is deprecated. Use destroy() instead.
//reportEngine.shutdown();
BirtSystemListener.reportEngine.destroy();
Platform.shutdown();
// Workaround for Eclipse bug 156877
if (BirtSystemListener.workaroundProtocolHandler != null) {
System.setProperty("java.protocol.handler.pkgs", BirtSystemListener.workaroundProtocolHandler); //$NON-NLS-1$
BirtSystemListener.workaroundProtocolHandler = null;
}
// End Workaround
} catch (Exception e) {
// Ignore since platform is exiting anyway
}
}
/*
* Create an instance of the EngineConfig class for the BIRT report engine.
*/
private IReportEngine createBIRTEngine() {
try {
// Get the global settings for the BIRT engine from our system settings
String birtHome = PentahoSystem.getApplicationContext().getSolutionPath("system/BIRT"); //$NON-NLS-1$
birtHome = birtHome.replaceAll("\\\\.\\\\", "\\\\"); //$NON-NLS-1$ //$NON-NLS-2$
String birtResourcePath = PentahoSystem.getApplicationContext().getSolutionPath("system/BIRT/resources"); //$NON-NLS-1$
birtResourcePath = birtResourcePath.replaceAll("\\\\.\\\\", "\\\\"); //$NON-NLS-1$//$NON-NLS-2$
if (PentahoSystem.debug) {
Logger.debug(BirtSystemListener.class.getName(), Messages.getInstance().getString("BIRT.DEBUG_BIRT_HOME", birtHome)); //$NON-NLS-1$
}
// Create an appropriate Config object
EngineConfig config = new EngineConfig();
config.setEngineHome(birtHome); // Configuring where BIRT engine is installed
config.setResourcePath(birtResourcePath);
// Set the directory where the BIRT log files will go
String logDest = PentahoSystem.getApplicationContext().getFileOutputPath("system/logs/BIRT"); //$NON-NLS-1$
logDest = ""; //$NON-NLS-1$
// Set the logging level
int loggingLevel = Logger.getLogLevel();
if (loggingLevel == ILogger.TRACE) {
config.setLogConfig(logDest, Level.ALL);
} else if (loggingLevel == ILogger.DEBUG) {
config.setLogConfig(logDest, Level.FINE);
} else if (loggingLevel == ILogger.INFO) {
config.setLogConfig(logDest, Level.INFO);
} else if (loggingLevel == ILogger.WARN) {
config.setLogConfig(logDest, Level.WARNING);
} else if (loggingLevel == ILogger.ERROR) {
config.setLogConfig(logDest, Level.SEVERE);
} else if (loggingLevel == ILogger.FATAL) {
config.setLogConfig(logDest, Level.SEVERE);
}
// Register new image handler
// Bart Maertens, 14/11/2007: Replace HTMLEmitterConfig with HTMLRenderOption.
// HTMLEmitterConfig emitterConfig = new HTMLEmitterConfig();
// emitterConfig.setActionHandler(new HTMLActionHandler());
// emitterConfig.setImageHandler(new HTMLServerImageHandler());
// config.getEmitterConfigs().put(RenderOptionBase.OUTPUT_FORMAT_HTML, emitterConfig);
IRenderOption option = new RenderOption();
option.setOutputFormat("html"); //$NON-NLS-1$
HTMLRenderOption renderOption = new HTMLRenderOption(option);
renderOption.setImageDirectory(imageDirectory);
config.getEmitterConfigs().put(IRenderOption.OUTPUT_FORMAT_HTML, renderOption);
// Workaround for Eclipse bug 156877
String protocolHandler = System.getProperty("java.protocol.handler.pkgs"); //$NON-NLS-1$
if ((protocolHandler != null) && (protocolHandler.indexOf("org.jboss.net.protocol") != -1)) { //$NON-NLS-1$
StringTokenizer tok = new StringTokenizer(protocolHandler, "|"); //$NON-NLS-1$
StringBuffer newProtocolHandler = new StringBuffer();
String name;
while (tok.hasMoreElements()) {
name = tok.nextToken();
if (!name.equals("org.jboss.net.protocol")) { //$NON-NLS-1$
newProtocolHandler.append(name).append('|');
}
}
newProtocolHandler.setLength(Math.max(0, newProtocolHandler.length() - 1)); // chop the last '|'
BirtSystemListener.workaroundProtocolHandler = System.setProperty(
"java.protocol.handler.pkgs", newProtocolHandler.toString()); //$NON-NLS-1$
}
// End Workaround
Platform.startup(config);
IReportEngineFactory factory = (IReportEngineFactory) Platform
.createFactoryObject(IReportEngineFactory.EXTENSION_REPORT_ENGINE_FACTORY);
IReportEngine engine = factory.createReportEngine(config);
return engine;
} catch (BirtException be) {
BirtSystemListener.logger.error(null, be);
Logger
.error(BirtSystemListener.class.getName(), Messages.getInstance().getErrorString("BIRT.ERROR_0008_INVALID_CONFIGURATION")); //$NON-NLS-1$
}
return null;
}
}