001: /*
002: * Copyright 2006 Pentaho Corporation. All rights reserved.
003: * This software was developed by Pentaho Corporation and is provided under the terms
004: * of the Mozilla Public License, Version 1.1, or any later version. You may not use
005: * this file except in compliance with the license. If you need a copy of the license,
006: * please go to http://www.mozilla.org/MPL/MPL-1.1.txt. The Original Code is the Pentaho
007: * BI Platform. The Initial Developer is Pentaho Corporation.
008: *
009: * Software distributed under the Mozilla Public License is distributed on an "AS IS"
010: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. Please refer to
011: * the license for the specific language governing your rights and limitations.
012: *
013: * @created Jul 26, 2005
014: * @author Gretchen Moran
015: *
016: */
017:
018: package org.pentaho.plugin.eclipsebirt;
019:
020: import java.io.File;
021: import java.util.StringTokenizer;
022: import java.util.logging.Level;
023:
024: import org.eclipse.birt.core.framework.Platform;
025: import org.eclipse.birt.report.engine.api.EngineConfig;
026: import org.eclipse.birt.report.engine.api.HTMLRenderOption;
027: import org.eclipse.birt.report.engine.api.IRenderOption;
028: import org.eclipse.birt.report.engine.api.IReportEngine;
029: import org.eclipse.birt.report.engine.api.IReportEngineFactory;
030: import org.eclipse.birt.report.engine.api.RenderOption;
031:
032: import org.pentaho.core.session.IPentahoSession;
033: import org.pentaho.core.system.IPentahoSystemListener;
034: import org.pentaho.core.system.PentahoSystem;
035: import org.pentaho.messages.Messages;
036: import org.pentaho.util.logging.ILogger;
037: import org.pentaho.util.logging.Logger;
038:
039: public class BirtSystemListener implements IPentahoSystemListener {
040: private static IReportEngine reportEngine = null;
041:
042: private static String workaroundProtocolHandler = null; // Workaround for Eclipse bug 156877
043:
044: String imageDirectory = null;
045:
046: String logDirectory = null;
047:
048: public boolean startup(IPentahoSession session) {
049:
050: // Need directory for temporarily writing images for HTML reports
051: imageDirectory = PentahoSystem.getApplicationContext()
052: .getFileOutputPath("system/tmp/BIRT"); //$NON-NLS-1$
053:
054: File dir = new File(imageDirectory);
055: try {
056: if (!dir.exists()) {
057: dir.mkdirs();
058: }
059: } catch (Exception e) {
060: Logger
061: .warn(
062: BirtSystemListener.class.getName(),
063: Messages
064: .getErrorString(
065: "BIRT.ERROR_0011_DIRECTORY_CREATION_FAILED", imageDirectory)); //$NON-NLS-1$
066: }
067:
068: // Need directory for BIRT logging or BIRT blows chunks
069: logDirectory = PentahoSystem.getApplicationContext()
070: .getFileOutputPath("system/logs/BIRT"); //$NON-NLS-1$
071:
072: dir = new File(logDirectory);
073: try {
074: if (!dir.exists()) {
075: dir.mkdirs();
076: }
077: } catch (Exception e) {
078: Logger
079: .warn(
080: BirtSystemListener.class.getName(),
081: Messages
082: .getErrorString(
083: "BIRT.ERROR_0011_DIRECTORY_CREATION_FAILED", logDirectory)); //$NON-NLS-1$
084: }
085:
086: // Create a BIRT report engine configuration object
087: reportEngine = createBIRTEngine();
088: BIRTReportComponent.reportEngine = reportEngine;
089: return (reportEngine != null);
090: }
091:
092: public void shutdown() {
093:
094: BIRTReportComponent.reportEngine = null;
095:
096: // TODO: should this be moved to maintenance so the image directory can
097: // be purged more often?
098:
099: File dir = new File(imageDirectory);
100: try {
101: if (!dir.exists()) {
102: return;
103: }
104: File[] files = dir.listFiles();
105: for (int i = 0; i < files.length; i++) {
106: files[i].delete();
107: }
108: //dir.delete();
109: } catch (Exception e) {
110: Logger
111: .warn(
112: BirtSystemListener.class.getName(),
113: Messages
114: .getErrorString(
115: "BIRT.ERROR_0012_CANT_COMPLETE_PURGE", logDirectory)); //$NON-NLS-1$
116: }
117:
118: try {
119: // Bart Maertens, 14/11/2007: reportEngine.shutdown() is deprecated. Use destroy() instead.
120: //reportEngine.shutdown();
121: reportEngine.destroy();
122: Platform.shutdown();
123:
124: // Workaround for Eclipse bug 156877
125: if (workaroundProtocolHandler != null) {
126: System
127: .setProperty(
128: "java.protocol.handler.pkgs", workaroundProtocolHandler); //$NON-NLS-1$
129: workaroundProtocolHandler = null;
130: }
131: // End Workaround
132: } catch (Exception e) {
133: // Ignore since platform is exiting anyway
134: }
135:
136: }
137:
138: /*
139: * Create an instance of the EngineConfig class for the BIRT report engine.
140: */
141: private IReportEngine createBIRTEngine() {
142:
143: try {
144: // Get the global settings for the BIRT engine from our system settings
145: String birtHome = PentahoSystem.getApplicationContext()
146: .getSolutionPath("system/BIRT"); //$NON-NLS-1$
147: birtHome = birtHome.replaceAll("\\\\.\\\\", "\\\\"); //$NON-NLS-1$ //$NON-NLS-2$
148:
149: String birtResourcePath = PentahoSystem
150: .getApplicationContext().getSolutionPath(
151: "system/BIRT/resources");
152: birtResourcePath = birtResourcePath.replaceAll("\\\\.\\\\",
153: "\\\\");
154:
155: if (PentahoSystem.debug)
156: Logger.debug(BirtSystemListener.class.getName(),
157: Messages.getString(
158: "BIRT.DEBUG_BIRT_HOME", birtHome)); //$NON-NLS-1$
159:
160: // Create an appropriate Config object
161: EngineConfig config = new EngineConfig();
162: config.setEngineHome(birtHome); // Configuring where BIRT engine is installed
163:
164: config.setResourcePath(birtResourcePath);
165:
166: // Set the directory where the BIRT log files will go
167: String logDest = PentahoSystem.getApplicationContext()
168: .getFileOutputPath("system/logs/BIRT"); //$NON-NLS-1$
169:
170: logDest = ""; //$NON-NLS-1$
171:
172: // Set the logging level
173: int loggingLevel = Logger.getLogLevel();
174: if (loggingLevel == ILogger.TRACE) {
175: config.setLogConfig(logDest, Level.ALL);
176: } else if (loggingLevel == ILogger.DEBUG) {
177: config.setLogConfig(logDest, Level.FINE);
178: } else if (loggingLevel == ILogger.INFO) {
179: config.setLogConfig(logDest, Level.INFO);
180: } else if (loggingLevel == ILogger.WARN) {
181: config.setLogConfig(logDest, Level.WARNING);
182: } else if (loggingLevel == ILogger.ERROR) {
183: config.setLogConfig(logDest, Level.SEVERE);
184: } else if (loggingLevel == ILogger.FATAL) {
185: config.setLogConfig(logDest, Level.SEVERE);
186: }
187:
188: // Register new image handler
189: // Bart Maertens, 14/11/2007: Replace HTMLEmitterConfig with HTMLRenderOption.
190: // HTMLEmitterConfig emitterConfig = new HTMLEmitterConfig();
191: // emitterConfig.setActionHandler(new HTMLActionHandler());
192: // emitterConfig.setImageHandler(new HTMLServerImageHandler());
193: // config.getEmitterConfigs().put(RenderOptionBase.OUTPUT_FORMAT_HTML, emitterConfig);
194: IRenderOption option = new RenderOption();
195: option.setOutputFormat("html");
196: HTMLRenderOption renderOption = new HTMLRenderOption(option);
197: renderOption.setImageDirectory(imageDirectory);
198: config.getEmitterConfigs().put(
199: renderOption.OUTPUT_FORMAT_HTML, renderOption);
200:
201: // Workaround for Eclipse bug 156877
202: String protocolHandler = System
203: .getProperty("java.protocol.handler.pkgs"); //$NON-NLS-1$
204: if ((protocolHandler != null)
205: && (protocolHandler
206: .indexOf("org.jboss.net.protocol") != -1)) { //$NON-NLS-1$
207: StringTokenizer tok = new StringTokenizer(
208: protocolHandler, "|"); //$NON-NLS-1$
209: StringBuffer newProtocolHandler = new StringBuffer();
210: String name;
211: while (tok.hasMoreElements()) {
212: name = tok.nextToken();
213: if (!name.equals("org.jboss.net.protocol")) { //$NON-NLS-1$
214: newProtocolHandler.append(name).append('|');
215: }
216: }
217: newProtocolHandler.setLength(Math.max(0,
218: newProtocolHandler.length() - 1)); // chop the last '|'
219:
220: workaroundProtocolHandler = System
221: .setProperty(
222: "java.protocol.handler.pkgs", newProtocolHandler.toString()); //$NON-NLS-1$
223: }
224: // End Workaround
225:
226: Platform.startup(config);
227:
228: IReportEngineFactory factory = (IReportEngineFactory) Platform
229: .createFactoryObject(IReportEngineFactory.EXTENSION_REPORT_ENGINE_FACTORY);
230: IReportEngine engine = factory.createReportEngine(config);
231: return engine;
232: } catch (Throwable error) {
233: error.printStackTrace();
234: Logger
235: .error(
236: BirtSystemListener.class.getName(),
237: Messages
238: .getErrorString("BIRT.ERROR_0008_INVALID_CONFIGURATION")); //$NON-NLS-1$
239: }
240: return null;
241: }
242:
243: }
|