BIRT with Struts2

normalpetsSoftware and s/w Development

Nov 4, 2013 (3 years and 11 months ago)

130 views

BIRT with Struts2



I have created a Dynamic Web

A
pplication in Struts 2 framework to display a sample BIRT report

example in details steps
.


In the Eclipse workspace namely “heliosspaces”

as shown below
, I have created a Dynamic Web
application with the name
web application content and name
“BirtStruts2BEX” with

src


as the
source folder and

WebContent


as the web
folder.


The structure of the web application is as follows:






B.1
-

LIB Folder

Contents


The lib folder has the following libraries

with 52 files in lib

folder
.


(a)

Stru
t
s
2
.1 Librari
es

(b)

BIRT 2.6.1 Libraries

(c)

Spring

3.0.4

Libraries
.

(d)

Apache Commons Logging 1.1.1 Libraries


They are


(a) Struts 2.1 libraries




(b)
BIRT

2.6.1
runtime libraries




(c) Spring 3.0.4 Libraries.




(d) Apache Commons Logging 1.1.1 Libraries




B.2
-

B
IRT Plugins and Configuration

Contents:


Create a folder “platform” under WEB
-
INF/lib.
C
opy the contents of BIRT runtime “plugins” and
“configuration” directory folder and its contents to “WEB
-
INF/lib/platform”.


The folder content should look like below:




The classes folder should be created as part of the project

structure indicated below
.


B.3


Classes Folder Contents:


Make sure the classes folder is directed to “BirtStruts2BEX/WebCo
ntent/WEB
-
INF/classes” so
that the contents of the src folder will be compiled and copied to this folder.





B.4


Struts.xml Contents:


The struts.xml file resides in “
\
BirtStruts2BEX
\
src” folder and will be copied to classes folder
during compilation process in
Eclipse IDE. When the struts.xml files is copied to classes fol
der, it
will be part of the application classpath.


<!DOCTYPE struts PUBLIC


"
-
//Apache Software Foundation//DTD Struts Configuration 2.0//EN"


"http://struts.apache.org/dtds/struts
-
2.0.dtd">

<struts>


<package name="BirtStruts2" extends="struts
-
d
efault">



<action name="BirtStruts2" class="org.birt.struts2.BirtStruts2">



<result type="stream">




<param name="contentType">text/html</param>




<param name="inputName">inputStream</param>



</result>



</action>




</package>

</struts>


See the f
ile structure:






B.5


applicationContext.xml Contents:



<?xml version="1.0" encoding="UTF
-
8"?>

<beans xmlns="http://www.springframework.org/schema/beans"


xmlns:xsi="http://www.w3.org/2001/XMLSchema
-
instance"


xsi:schemaLocation="


http://www.springframework.org/schema/beans


http://www.springframework.org/schema/beans/spring
-
beans.xsd">


</beans>


The file applicationConte
xt.xml file will reside in the folder WEB
-
INF folder as shown.



B.6


web.xml Contents:



<?xml version="1.0" encoding="UTF
-
8"?>

<web
-
app id="WebApp_9" version="2.4" xmlns="http://java.sun
.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema
-
instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web
-
app_2_4.xsd">



<display
-
name>Struts Blank</display
-
name>



<filter>


<filter
-
name>
struts2</filter
-
name>


<filter
-
class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter
-
class>


</filter>



<!
--

Loads the Spring web application context
--
>


<listener>



<listener
-
class>org.springframework.web.context.ContextLoaderListener</listener
-
class>


</listener>



<filter
-
mapping>


<filter
-
name>struts2</filter
-
name>


<url
-
pattern>/*</url
-
pattern>


</filter
-
mapping>



<welcome
-
file
-
list>



<welcome
-
file>index.html</welcome
-
file>


</welcome
-
file
-
list>


</web
-
app>


The file web.xml file will reside in the folder WEB
-
INF folder as shown.




B.7



WebContent directory Conten
ts:



The Web content has 3 files that need to be present. They are


(a)

Index.html

(b)

BirtConfig.properties

(c)

Test.jsp


The file contents are added in the following list:







B.8


src directory Contents:


There are 3 classes in this example and they are


(a)

Birt Engine


Initial BIRT class

(b)

BirtStruts2.java


Action component for Birt Report View

(c)

RunBirt.java


Class to run Birt


The structure of the Birt class is as follows:






B.9


images directory Contents:

No content is available in images directory.


B.10


report directory Contents:

The report directory has one sample rpt file and it is “TopNPercent.rptdesign”.


B.11


Classes:


__________________________________________________

package org.birt.struts2;

import java.io.InputStream;

import java.io.OutputStream;

import java.io.ByteArray
OutputStream;

import java.io.ByteArrayInputStream;

import org.apache.struts2.util.ServletContextAware;

import org.apache.struts2.interceptor.ServletRequestAware;

import javax.servlet.ServletContext;

import javax.servlet.http.HttpServletRequest;


import co
m.opensymphony.xwork2.ActionSupport;

public class BirtStruts2 extends ActionSupport implements ServletContextAware, ServletRequestAware {



private ByteArrayInputStream inputStream;



private ByteArrayOutputStream oStream;


public ByteArrayInputStream getI
nputStream() {



return inputStream;


}


private HttpServletRequest request;


public void setServletRequest(HttpServletRequest request) {



this.request = request;


}


private ServletContext context;


public void setServletContext(ServletContext context) {



this.context = context;


}


public String execute() throws Exception {




RunBirt rb = new RunBirt();



inputStream = new ByteArrayInputStream(rb.runReport(this.context, this.request));




ret
urn SUCCESS;


}


}

__________________________________________________


package org.birt.struts2;


import java.io.InputStream;

import java.io.IOException;

import java.util.Properties;

import java.util.logging.Level;


import org.eclipse.birt.report.engine.ap
i.EngineConstants;

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;

import org.eclipse.birt.report.e
ngine.api.ReportEngine;

import javax.servlet.http.HttpServletRequest;


public class BirtEngine {



private static IReportEngine birtEngine = null;



private static Properties configProps = new Properties();



private final static String configFile = "BirtC
onfig.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))





{






leve
l = Level.FINE;





} else if ("FINER".equalsIgnoreCase(logLevel))





{






level = Level.FINER;





} else if ("FINEST".equalsIgnoreCase(logLevel))





{






level = Level.FINEST;





} else if ("OFF".equalsIgnoreCase(logLevel))





{






level = L
evel.OFF;





}





config.setLogConfig(configProps.getProperty("logDirectory"), level);




}





config.getAppContext().put(EngineConstants.APPCONTEXT_CLASSLOADER_KEY,
BirtEngine.class.getClassLoader());




//config.setEngineHome("C:
\
\
install
\
\
personall
\
\
birt
\
\
birt
-
runtime
-
2_6_1
\
\
birt
-
runtime
-
2_6_1
\
\
ReportEngine");




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( c
onfig );





}



return birtEngine;


}



public static synchronized void destroyBirtEngine() {



if (birtEngine == null) {




return;



}





birtEngine.destroy();



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();



}



}


}

__________________________________________________


package org.birt.struts2
;

import javax.servlet.ServletContext;

import javax.servlet.ServletConfig;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;


import org.eclipse.birt.report.engine.api.EngineConstants;

import org.eclipse.birt.report.engine.api.HTMLRenderOption;

import org.eclipse.birt.report.engine.api.IReportRunnable;

import org.eclipse.birt.report.engine.api.IRunAndRenderTask;

import org.eclips
e.birt.report.engine.api.IReportEngine;

import org.eclipse.birt.report.engine.api.HTMLServerImageHandler;

import org.eclipse.birt.report.engine.api.PDFRenderOption;

import org.eclipse.birt.report.engine.api.RenderOption;

import java.io.ByteArrayOutputStrea
m;


public class RunBirt{

private IReportEngine birtReportEngine = null;


public byte[] runReport(ServletContext sc, HttpServletRequest req) throws ServletException{




this.birtReportEngine = BirtEngine.getBirtEngine(sc);






IReportRunnable design;



tr
y



{




//Open report design




design = birtReportEngine.openReportDesign(
sc.getRealPath("/reports/TopNPercent.rptdesign") );




//create task to run and render report




IRunAndRenderTask task = birtReportEngine.createRunAndRenderTask( design );










//task.getAppContext().put("BIRT_VIEWER_HTTPSERVLET_REQUEST", req );







if( req.getParameter("TopCount") != null ){





task.setParameterValue("Top Count",
Integer.valueOf(req.getParameter("TopCount")));




}




if( req.getParameter("TopPercentage")
!= null ){





task.setParameterValue("Top Percentage",
Float.valueOf(req.getParameter("TopPercentage")));




}




//set output options




HTMLRenderOption options = new HTMLRenderOption();




options.setOutputFormat(HTMLRenderOption.OUTPUT_FORMAT_HTML);




ByteArrayOutputStream oStream = new ByteArrayOutputStream();




options.setOutputStream(oStream);




options.setImageHandler(new HTMLServerImageHandler());




options.setBaseImageURL(req.getContextPath()+"/images");




options.setImageDirectory(sc.getRe
alPath("/images"));







task.setRenderOption(options);















//run report




task.run();




task.close();




return oStream.toByteArray();







}catch (Exception e){








e.printStackTrace();




throw new ServletException( e );



}


}

}



B.12



Project Contents have been enclosed in the document for
reference and can be added to Eclipse as a project. This zip file does
not contain lib folder contents (54 files) and need to be added.
Also
the BIRT runtime libraries need to be added as this is a

50MB file
size.