1 17 18 package org.pentaho.plugin.eclipsebirt; 19 20 import java.io.File ; 21 import java.util.StringTokenizer ; 22 import java.util.logging.Level ; 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 workaroundProtocolHandler = null; 43 String imageDirectory = null; 44 String logDirectory = null; 45 46 public boolean startup(IPentahoSession session) { 47 48 imageDirectory = PentahoSystem.getApplicationContext().getFileOutputPath("system/tmp/BIRT"); 51 File dir = new File (imageDirectory); 52 try { 53 if (!dir.exists()) { 54 dir.mkdirs(); 55 } 56 } catch (Exception e) { 57 Logger.warn(BirtSystemListener.class.getName(), Messages.getErrorString("BIRT.ERROR_0011_DIRECTORY_CREATION_FAILED", imageDirectory)); } 59 60 logDirectory = PentahoSystem.getApplicationContext().getFileOutputPath("system/logs/BIRT"); 63 dir = new File (logDirectory); 64 try { 65 if (!dir.exists()) { 66 dir.mkdirs(); 67 } 68 } catch (Exception e) { 69 Logger.warn(BirtSystemListener.class.getName(), Messages.getErrorString("BIRT.ERROR_0011_DIRECTORY_CREATION_FAILED", logDirectory)); } 71 72 reportEngine = createBIRTEngine(); 74 BIRTReportComponent.reportEngine = reportEngine; 75 return( reportEngine != null ); 76 } 77 78 public void shutdown() { 79 80 BIRTReportComponent.reportEngine = null; 81 82 85 File dir = new File (imageDirectory); 86 try { 87 if (!dir.exists()) { 88 return; 89 } 90 File [] files = dir.listFiles(); 91 for (int i = 0; i < files.length; i++) { 92 files[i].delete(); 93 } 94 } 96 catch (Exception e) { 97 Logger.warn(BirtSystemListener.class.getName(), Messages.getErrorString("BIRT.ERROR_0012_CANT_COMPLETE_PURGE", logDirectory)); } 99 100 try { 101 reportEngine.shutdown(); 102 Platform.shutdown(); 103 104 if ( workaroundProtocolHandler != null ) { 106 System.setProperty( "java.protocol.handler.pkgs", workaroundProtocolHandler ); workaroundProtocolHandler = null; 108 } 109 } 111 catch ( Exception e ) { 112 } 114 115 } 116 117 118 121 private IReportEngine createBIRTEngine() { 122 123 try { 124 String birtHome = PentahoSystem.getApplicationContext().getSolutionPath("system/BIRT"); birtHome = birtHome.replaceAll("\\\\.\\\\", "\\\\"); 128 if (PentahoSystem.debug) 129 Logger.debug(BirtSystemListener.class.getName(), Messages.getString("BIRT.DEBUG_BIRT_HOME", birtHome)); 131 EngineConfig config = new EngineConfig(); 133 config.setEngineHome(birtHome); 135 String logDest = PentahoSystem.getApplicationContext().getFileOutputPath("system/logs/BIRT"); 138 logDest = ""; 140 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 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 String protocolHandler = System.getProperty("java.protocol.handler.pkgs"); if ( (protocolHandler != null) && (protocolHandler.indexOf( "org.jboss.net.protocol" ) != -1 ) ) { StringTokenizer tok = new StringTokenizer (protocolHandler, "|"); StringBuffer newProtocolHandler = new StringBuffer (); 167 String name; 168 while (tok.hasMoreElements()) { 169 name = tok.nextToken(); 170 if ( !name.equals("org.jboss.net.protocol") ) { newProtocolHandler.append( name ).append('|'); 172 } 173 } 174 newProtocolHandler.setLength( Math.max(0, newProtocolHandler.length() - 1) ); 176 workaroundProtocolHandler = System.setProperty( "java.protocol.handler.pkgs", newProtocolHandler.toString() ); } 178 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 error) { 187 error.printStackTrace(); 188 Logger.error(BirtSystemListener.class.getName(), Messages.getErrorString("BIRT.ERROR_0008_INVALID_CONFIGURATION")); } 190 return null; 191 } 192 193 194 195 } 196 | Popular Tags |