KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > pentaho > plugin > eclipsebirt > BirtSystemListener


1 /*
2  * Copyright 2006 Pentaho Corporation. All rights reserved.
3  * This software was developed by Pentaho Corporation and is provided under the terms
4  * of the Mozilla Public License, Version 1.1, or any later version. You may not use
5  * this file except in compliance with the license. If you need a copy of the license,
6  * please go to http://www.mozilla.org/MPL/MPL-1.1.txt. The Original Code is the Pentaho
7  * BI Platform. The Initial Developer is Pentaho Corporation.
8  *
9  * Software distributed under the Mozilla Public License is distributed on an "AS IS"
10  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. Please refer to
11  * the license for the specific language governing your rights and limitations.
12  *
13  * @created Jul 26, 2005
14  * @author Gretchen Moran
15  *
16  */

17
18 package org.pentaho.plugin.eclipsebirt;
19
20 import java.io.File JavaDoc;
21 import java.util.StringTokenizer JavaDoc;
22 import java.util.logging.Level JavaDoc;
23
24 import org.eclipse.birt.core.framework.Platform;
25 import org.eclipse.birt.report.engine.api.EngineConfig;
26 import org.eclipse.birt.report.engine.api.HTMLActionHandler;
27 import org.eclipse.birt.report.engine.api.HTMLEmitterConfig;
28 import org.eclipse.birt.report.engine.api.HTMLServerImageHandler;
29 import org.eclipse.birt.report.engine.api.IReportEngine;
30 import org.eclipse.birt.report.engine.api.IReportEngineFactory;
31 import org.eclipse.birt.report.engine.api.RenderOptionBase;
32 import org.pentaho.core.session.IPentahoSession;
33 import org.pentaho.core.system.IPentahoSystemListener;
34 import org.pentaho.core.system.PentahoSystem;
35 import org.pentaho.messages.Messages;
36 import org.pentaho.util.logging.ILogger;
37 import org.pentaho.util.logging.Logger;
38
39 public class BirtSystemListener implements IPentahoSystemListener {
40     private static IReportEngine reportEngine = null;
41     private static String JavaDoc workaroundProtocolHandler = null; // Workaround for Eclipse bug 156877
42

43     String JavaDoc imageDirectory = null;
44     String JavaDoc logDirectory = null;
45
46     public boolean startup(IPentahoSession session) {
47
48         // Need directory for temporarily writing images for HTML reports
49
imageDirectory = PentahoSystem.getApplicationContext().getFileOutputPath("system/tmp/BIRT"); //$NON-NLS-1$
50

51         File JavaDoc dir = new File JavaDoc(imageDirectory);
52         try {
53             if (!dir.exists()) {
54                 dir.mkdirs();
55             }
56         } catch (Exception JavaDoc e) {
57             Logger.warn(BirtSystemListener.class.getName(), Messages.getErrorString("BIRT.ERROR_0011_DIRECTORY_CREATION_FAILED", imageDirectory)); //$NON-NLS-1$
58
}
59
60         // Need directory for BIRT logging or BIRT blows chunks
61
logDirectory = PentahoSystem.getApplicationContext().getFileOutputPath("system/logs/BIRT"); //$NON-NLS-1$
62

63         dir = new File JavaDoc(logDirectory);
64         try {
65             if (!dir.exists()) {
66                 dir.mkdirs();
67             }
68         } catch (Exception JavaDoc e) {
69             Logger.warn(BirtSystemListener.class.getName(), Messages.getErrorString("BIRT.ERROR_0011_DIRECTORY_CREATION_FAILED", logDirectory)); //$NON-NLS-1$
70
}
71         
72         // Create a BIRT report engine configuration object
73
reportEngine = createBIRTEngine();
74         BIRTReportComponent.reportEngine = reportEngine;
75         return( reportEngine != null );
76     }
77
78     public void shutdown() {
79
80         BIRTReportComponent.reportEngine = null;
81
82         // TODO: should this be moved to maintenance so the image directory can
83
// be purged more often?
84

85         File JavaDoc dir = new File JavaDoc(imageDirectory);
86         try {
87             if (!dir.exists()) {
88                 return;
89             }
90             File JavaDoc[] files = dir.listFiles();
91             for (int i = 0; i < files.length; i++) {
92                 files[i].delete();
93             }
94             //dir.delete();
95
}
96         catch (Exception JavaDoc e) {
97             Logger.warn(BirtSystemListener.class.getName(), Messages.getErrorString("BIRT.ERROR_0012_CANT_COMPLETE_PURGE", logDirectory)); //$NON-NLS-1$
98
}
99         
100         try {
101             reportEngine.shutdown();
102             Platform.shutdown();
103             
104             // Workaround for Eclipse bug 156877
105
if ( workaroundProtocolHandler != null ) {
106                 System.setProperty( "java.protocol.handler.pkgs", workaroundProtocolHandler ); //$NON-NLS-1$
107
workaroundProtocolHandler = null;
108             }
109             // End Workaround
110
}
111         catch ( Exception JavaDoc e ) {
112             // Ignore since platform is exiting anyway
113
}
114
115     }
116     
117     
118     /*
119      * Create an instance of the EngineConfig class for the BIRT report engine.
120      */

121     private IReportEngine createBIRTEngine() {
122
123         try {
124             // Get the global settings for the BIRT engine from our system settings
125
String JavaDoc birtHome = PentahoSystem.getApplicationContext().getSolutionPath("system/BIRT"); //$NON-NLS-1$
126
birtHome = birtHome.replaceAll("\\\\.\\\\", "\\\\"); //$NON-NLS-1$ //$NON-NLS-2$
127

128             if (PentahoSystem.debug)
129                 Logger.debug(BirtSystemListener.class.getName(), Messages.getString("BIRT.DEBUG_BIRT_HOME", birtHome)); //$NON-NLS-1$
130

131             // Create an appropriate Config object
132
EngineConfig config = new EngineConfig();
133             config.setEngineHome(birtHome); // Configuring where BIRT engine is installed
134

135             // Set the directory where the BIRT log files will go
136
String JavaDoc logDest = PentahoSystem.getApplicationContext().getFileOutputPath("system/logs/BIRT"); //$NON-NLS-1$
137

138             logDest = ""; //$NON-NLS-1$
139

140             // Set the logging level
141
int loggingLevel = Logger.getLogLevel();
142             if (loggingLevel == ILogger.TRACE) {
143                 config.setLogConfig(logDest, Level.ALL);
144             } else if (loggingLevel == ILogger.DEBUG) {
145                 config.setLogConfig(logDest, Level.FINE);
146             } else if (loggingLevel == ILogger.INFO) {
147                 config.setLogConfig(logDest, Level.INFO);
148             } else if (loggingLevel == ILogger.WARN) {
149                 config.setLogConfig(logDest, Level.WARNING);
150             } else if (loggingLevel == ILogger.ERROR) {
151                 config.setLogConfig(logDest, Level.SEVERE);
152             } else if (loggingLevel == ILogger.FATAL) {
153                 config.setLogConfig(logDest, Level.SEVERE);
154             }
155
156             // Register new image handler
157
HTMLEmitterConfig emitterConfig = new HTMLEmitterConfig( );
158             emitterConfig.setActionHandler( new HTMLActionHandler( ) );
159             emitterConfig.setImageHandler( new HTMLServerImageHandler( ) );
160             config.getEmitterConfigs( ).put( RenderOptionBase.OUTPUT_FORMAT_HTML, emitterConfig );
161             
162             // Workaround for Eclipse bug 156877
163
String JavaDoc protocolHandler = System.getProperty("java.protocol.handler.pkgs"); //$NON-NLS-1$
164
if ( (protocolHandler != null) && (protocolHandler.indexOf( "org.jboss.net.protocol" ) != -1 ) ) { //$NON-NLS-1$
165
StringTokenizer JavaDoc tok = new StringTokenizer JavaDoc(protocolHandler, "|"); //$NON-NLS-1$
166
StringBuffer JavaDoc newProtocolHandler = new StringBuffer JavaDoc();
167                 String JavaDoc name;
168                 while (tok.hasMoreElements()) {
169                     name = tok.nextToken();
170                     if ( !name.equals("org.jboss.net.protocol") ) { //$NON-NLS-1$
171
newProtocolHandler.append( name ).append('|');
172                     }
173                 }
174                 newProtocolHandler.setLength( Math.max(0, newProtocolHandler.length() - 1) ); // chop the last '|'
175

176                 workaroundProtocolHandler = System.setProperty( "java.protocol.handler.pkgs", newProtocolHandler.toString() ); //$NON-NLS-1$
177
}
178             // End Workaround
179

180             Platform.startup( config );
181
182             IReportEngineFactory factory = (IReportEngineFactory) Platform.createFactoryObject( IReportEngineFactory.EXTENSION_REPORT_ENGINE_FACTORY );
183             IReportEngine engine = factory.createReportEngine( config );
184             return engine;
185         }
186         catch (Throwable JavaDoc error) {
187             error.printStackTrace();
188             Logger.error(BirtSystemListener.class.getName(), Messages.getErrorString("BIRT.ERROR_0008_INVALID_CONFIGURATION")); //$NON-NLS-1$
189
}
190         return null;
191     }
192
193
194     
195 }
196
Popular Tags