利用BIRT API生成报表例子3
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
package com.test; import java.io.InputStream; import java.io.IOException; import java.util.Properties; import java.util.logging.Level; import org.eclipse.birt.report.engine.api.EngineConfig; import org.eclipse.birt.report.engine.api.IReportEngine; import javax.servlet.*; import org.eclipse.birt.core.framework.PlatformServletContext; import org.eclipse.birt.core.framework.IPlatformContext; import org.eclipse.birt.core.framework.Platform; import org.eclipse.birt.core.exception.BirtException; import org.eclipse.birt.report.engine.api.IReportEngineFactory; public class BirtEngine { private static IReportEngine birtEngine = null; private static Properties configProps = new Properties(); private final static String configFile = "BirtConfig.properties"; public static synchronized void initBirtConfig() { loadEngineProps(); } public static synchronized IReportEngine getBirtEngine(ServletContext sc) { if (birtEngine == null) { EngineConfig config = new EngineConfig(); if( configProps != null){ String logLevel = configProps.getProperty("logLevel"); Level level = Level.OFF; if ("SEVERE".equalsIgnoreCase(logLevel)) { level = Level.SEVERE; } else if ("WARNING".equalsIgnoreCase(logLevel)) { level = Level.WARNING; } else if ("INFO".equalsIgnoreCase(logLevel)) { level = Level.INFO; } else if ("CONFIG".equalsIgnoreCase(logLevel)) { level = Level.CONFIG; } else if ("FINE".equalsIgnoreCase(logLevel)) { level = Level.FINE; } else if ("FINER".equalsIgnoreCase(logLevel)) { level = Level.FINER; } else if ("FINEST".equalsIgnoreCase(logLevel)) { level = Level.FINEST; } else if ("OFF".equalsIgnoreCase(logLevel)) { level = Level.OFF; } config.setLogConfig(configProps.getProperty("logDirectory"), level); } config.setEngineHome(""); IPlatformContext context = new PlatformServletContext( sc ); config.setPlatformContext( context ); try { Platform.startup( config ); } catch ( BirtException e ) { e.printStackTrace( ); } IReportEngineFactory factory = (IReportEngineFactory) Platform .createFactoryObject( IReportEngineFactory.EXTENSION_REPORT_ENGINE_FACTORY ); birtEngine = factory.createReportEngine( config ); } return birtEngine; } public static synchronized void destroyBirtEngine() { if (birtEngine == null) { return; } birtEngine.shutdown(); Platform.shutdown(); birtEngine = null; } public Object clone() throws CloneNotSupportedException { throw new CloneNotSupportedException(); } private static void loadEngineProps() { try { //Config File must be in classpath ClassLoader cl = Thread.currentThread ().getContextClassLoader(); InputStream in = null; in = cl.getResourceAsStream (configFile); configProps.load(in); in.close(); } catch (IOException e) { e.printStackTrace(); } } } |
