001/*
002 * (C) Copyright 2006-20011 Nuxeo SAS (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     Nuxeo - initial API and implementation
016 *
017 */
018
019package org.nuxeo.ecm.platform.reporting.engine;
020
021import java.io.IOException;
022import java.io.InputStream;
023import java.util.Properties;
024import java.util.logging.Level;
025
026import org.apache.commons.logging.Log;
027import org.apache.commons.logging.LogFactory;
028import org.eclipse.birt.core.exception.BirtException;
029import org.eclipse.birt.core.framework.Platform;
030import org.eclipse.birt.report.engine.api.EngineConfig;
031import org.eclipse.birt.report.engine.api.IReportEngine;
032import org.eclipse.birt.report.engine.api.IReportEngineFactory;
033import org.eclipse.birt.report.model.api.DesignConfig;
034import org.eclipse.birt.report.model.api.IDesignEngine;
035import org.eclipse.birt.report.model.api.IDesignEngineFactory;
036
037/**
038 * This is a Singleton used to trigger BIRT deployment and get access to the Reporting and Design engine
039 *
040 * @author Tiry (tdelprat@nuxeo.com)
041 */
042public class BirtEngine {
043
044    private static Log log = LogFactory.getLog(BirtEngine.class);
045
046    private static IReportEngine birtEngine = null;
047
048    private static IDesignEngine birtDesignEngine = null;
049
050    private static Properties configProps = new Properties();
051
052    private final static String configFile = "BirtConfig.properties";
053
054    public static synchronized void initBirtConfig() {
055        loadEngineProps();
056    }
057
058    public static synchronized IReportEngine getBirtEngine() {
059        if (birtEngine == null) {
060            EngineConfig config = new EngineConfig();
061            if (configProps != null) {
062                String logLevel = configProps.getProperty("logLevel");
063                Level level = Level.OFF;
064                if ("SEVERE".equalsIgnoreCase(logLevel)) {
065                    level = Level.SEVERE;
066                } else if ("WARNING".equalsIgnoreCase(logLevel)) {
067                    level = Level.WARNING;
068                } else if ("INFO".equalsIgnoreCase(logLevel)) {
069                    level = Level.INFO;
070                } else if ("CONFIG".equalsIgnoreCase(logLevel)) {
071                    level = Level.CONFIG;
072                } else if ("FINE".equalsIgnoreCase(logLevel)) {
073                    level = Level.FINE;
074                } else if ("FINER".equalsIgnoreCase(logLevel)) {
075                    level = Level.FINER;
076                } else if ("FINEST".equalsIgnoreCase(logLevel)) {
077                    level = Level.FINEST;
078                } else if ("OFF".equalsIgnoreCase(logLevel)) {
079                    level = Level.OFF;
080                }
081
082                config.setLogConfig(configProps.getProperty("logDirectory"), level);
083            }
084
085            config.setEngineHome("");
086
087            try {
088                Platform.startup(config);
089            } catch (BirtException e) {
090                log.error("Cannot startup birt", e);
091            }
092
093            IReportEngineFactory factory = (IReportEngineFactory) Platform.createFactoryObject(IReportEngineFactory.EXTENSION_REPORT_ENGINE_FACTORY);
094            birtEngine = factory.createReportEngine(config);
095
096            DesignConfig dconfig = new DesignConfig();
097            IDesignEngineFactory df = (IDesignEngineFactory) Platform.createFactoryObject(IDesignEngineFactory.EXTENSION_DESIGN_ENGINE_FACTORY);
098            birtDesignEngine = df.createDesignEngine(dconfig);
099        }
100        return birtEngine;
101    }
102
103    public static synchronized IDesignEngine getBirtDesignEngine() {
104        if (birtDesignEngine == null) {
105            getBirtEngine();
106        }
107        return birtDesignEngine;
108    }
109
110    public static synchronized void destroyBirtEngine() {
111        if (birtEngine == null) {
112            return;
113        }
114
115        birtEngine.shutdown();
116        Platform.shutdown();
117        birtEngine = null;
118
119    }
120
121    public Object clone() throws CloneNotSupportedException {
122        throw new CloneNotSupportedException();
123    }
124
125    private static void loadEngineProps() {
126        try {
127            // Config File must be in classpath
128            ClassLoader cl = Thread.currentThread().getContextClassLoader();
129            InputStream in = null;
130            in = cl.getResourceAsStream(configFile);
131            if (in != null) {
132                configProps.load(in);
133                in.close();
134            }
135        } catch (IOException e) {
136            e.printStackTrace();
137        }
138
139    }
140}