Loading a jrxml file under Tomcat - java

I have a web app that uses JasperReports to power data export to Excel, PDF & CSV. It's a grails app using the DynamicJasperReports plugin and it uses a jrxml file as the template for the report.
This was working but has broken following some changes to our tomcat configuration. It still works locally in grails development mode.
FastReportBuilder drb = new FastReportBuilder()
drb.setTemplateFile("resources/reportTemplate.jrxml")
DynamicReport report = reportBuilder.call(drb)
JRDataSource ds = new JRMapCollectionDataSource(data)
JasperPrint jp = DynamicJasperHelper.generateJasperPrint(report, new ClassicLayoutManager(), ds)
ReportWriter reportWriter = ReportWriterFactory.getInstance().getReportWriter(jp, format, [:])
reportWriter.writeTo(response)
It throws this error:
java.lang.NullPointerException
at ar.com.fdvs.dj.core.DynamicJasperHelper.generateJasperDesign(DynamicJasperHelper.java:151)
at ar.com.fdvs.dj.core.DynamicJasperHelper.generateJasperReport(DynamicJasperHelper.java:448)
at ar.com.fdvs.dj.core.DynamicJasperHelper.generateJasperPrint(DynamicJasperHelper.java:234)
at ar.com.fdvs.dj.core.DynamicJasperHelper.generateJasperPrint(DynamicJasperHelper.java:187)
at com.giivpro.services.analytics.JasperReportsService.runReport(JasperReportsService.groovy:30)

I eventually realized this was not due to upgrading to tomcat7 as I originally thought but instead was due to changes in how the app was being deployed, made around the same time.
All the jar file dependencies of the app are now copied to /var/lib/tomcat/lib rather than being deployed inside the war file. Which means that the line in DynamicJasperHelper
URL url = DynamicJasperHelper.class.getClassLoader().getResource(dr.getTemplateFileName());
now failed due to security restrictions on the ClassLoader. The jars in /var/lib/tomcat/lib get loaded by a separate ClassLoader and are not able to load resources from specific WAR files.

Related

Updating the .bat file in Wildfly 10 Final not working?

Hi all I am new to jboss/wildfly world I am working on a java application which supports both tomcat and wildfly where I have a piece of code which changes the file content dynamically(from java code)...when I execute the following code in tomcat,it updates the file and restarts automatically...but when I tried the same code with wildfly I don't see any updates in file content.
NOTE : I am deploying the .WAR file from admin console (the file is in .WAR).
I tried restarting the wildfly server no changes same old file content.
Here is my piece of java code,
Path nioPath = Paths.get(XYZScript.getPath());
List<String> aLines = new ArrayList<>();
aLines.add("Content 1");
aLines.add("Content 2");
try {
Files.write(nioPath, aLines, ENCODING, StandardOpenOption.TRUNCATE_EXISTING, StandardOpenOption.WRITE);
} catch (IOException e) {
I found some links which says we need to redeploy the .WAR or use Overlay
But it should be programmatic the user will change files dynamically
During deployment JBoss / Wildfly unpacks the deployment under $JBOSS_HOME/standalone/tmp/vfs/deployment. It is used as a virtual file system (vfs). But it also gets deleted on undeployment.
In your case you will have to modify the war under $JBOSS_HOME/standalone/deployments and trigger a redeployment by using marker files like <war-name>.war.dodeploy. See here for more information on marker files.

Netbeans - GlassFish - Java EE Configuration File

Since it was really difficult for me to find an answer to this question I'm gonna post both the question and the answer I found to this problem.
Problem: How to use a configuration file in java while working with Netbeans and deploying into a GlassFish Server?
Main problem is to actually access the file (a lot of trouble with the path in which things as getResource, creating a new File and getting it's absolute path, and many other tricks didn't work).
In this particular case I wanted the file to be in my ejb Project.
Create a configuration File (e.g. "config.properties") in
ProjectName-ejb\src\conf
You will be able to see the file from Netbeans in your project configuration Files:
Insert all the properties you want:
Create an attribute in the class from which you will access the file like this: private final String BAD_WORDS_FILE_NAME = "\META-INF\config.properties";
Once your code is deployed to GlassFIsh, all conf files seem to be deployed to this META-INF folder:
Access Properties using sth like:
private String[] getBadWordsFromFile() throws IOException {
InputStream resourceAsStream = getClass().getClassLoader().getResourceAsStream(BAD_WORDS_FILE_NAME);
Properties properties = new Properties();
properties.load(resourceAsStream);
String badWordsAsString = properties.getProperty(BAD_WORDS_PROPERTY_NAME);
return badWordsAsString.split(BAD_WORDS_SEPARATOR);
}
This was the Solution I found, which worked but was only tested on a local machine... this might get some trouble on Release.

Mule - how to access files in src/main/resources in Java class when running in Studio and Standalone?

I have a Mule CE application that is using a Java component to transform a CSV file to XML. My Java class needs to access a flatpack XML file called map.xml - I have placed this in src/main/resources. My Java class is in src/main/java. I'm currently accessing the map.xml file in my Java class as follows:
fr = new FileReader("src/main/resources/map.xml");
This works fine in Mule Studio, but when I try and run this application in Mule Standalone, I get the following error:
java.io.FileNotFoundException: src/main/resources/map.xml (No such file or directory)
Is there a way I can make this file path mutual so that it will work in both Studio and Standalone? I have also tried simply fr = new FileReader("map.xml"); and this fails in Studio.
UPDATE
Through a combination of the answer from #Learner, and some info in this post, I've managed to find a solution to this problem. I've updated my Java class to the following and this has worked in both Studio and Standalone:
fr = new FileReader(MyClassName.class.getResource("/map.xml").getPath());
UPDATE
How to retieve mule-app.properties file? If same then will it work onCloudHub as well.
There are couple of ways to do this:
You may read resource as stream like this, files under src/main/resources are in your classpath
InputStream is = this.getClass().getResourceAsStream("map.xml");
The other recommended way is to create your transformer as a spring bean and inject external map.xml file dependency through spring.
Generally when you place in path in src/main/resources it comes under classpath ... In Standalone also it should get it from there ... if not, then could you place it in standalone conf folder where all properties files are kept and have a try

File upload to the server directory

I am uploading an excel file to the tomcat server. Which is saving inside my eclipse directory D:\workspace_Eclipse\.metadata\.plugins\org.eclipse.wst.server.core\tmp1\wtpwebapps\StatusPortal\Job_doc\abc.xls
When ever i am accessing this file its giving me file not found Exception \Job_doc\abc.xls.
Its could not able to find the path which is i am giving while accessing the file like
\Job_doc\abc.xls
I am giving the path \Job_doc\abc.xls while accessing.
This is because you are using a relative path. Eclipse will use the current working directory to be a temp location for deploying the webapp. So the file is uploaded to the folder relative to this path (This happens when you start the app from eclipse Run On Server. Define your paths as static constants(May be you can use absolute paths for testing). After testing you can use the relative paths on production deployment.
Still, you can do alternate way. Dont use the integrated tomcat server of Eclipse. Use a standalone server, use the descriptor file to link the webapp in workspace to tomcat. After the save, just reload the app in tomcat manager and try.
Try reading your file using ClassLoader as below:
InputStream inputStream =
getClass().getClassLoader().getResourceAsStream("/Job_doc/abc.xls");
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream ));
If you want to get the File object, then try as below:
URI uri = getClass().getClassLoader().getResource("/Job_doc/abc.xls").toURI();
File file = new File(uri);

Classpath Resource in Tomcat6 (Works in Jetty)

I'm having trouble with a legacy Web Application that I'm migrating to Maven3.
I need to obtain a file from the Classpath that in the directory structure is located in:
/src/main/resources/com/thinkglish/geoip/GeoIP.dat
When I create the .war file with the Maven build, I can confirm that this .dat file is located (as it should be) in:
WEB-INF/classes/com/thinkglish/geoip/GeoIP.dat
I'm trying two different approaches to get the resource from one of my classes, which implements javax.servlet.Filter:
ClassPathResource resource = new ClassPathResource("com/thinkglish/geoip/GeoIp.dat");
and
URL resource = getClass().getResource("/com/thinkglish/geoip/GeoIp.dat");
If I start the application using Maven's Jetty plugin, that works fine in both ways. However, when I deploy the application in a Tomcat and start the server, the resource cannot be located.
In the first case I get a java.io.FileNotFoundException: class path resource [com/thinkglish/geoip/GeoIp.dat] cannot be resolved to URL because it does not exist and in the second case the resource is null.
A curious thing about all this is that if I use one method or the other trying to obtain another resource from the Classpath (e.g. com/thinkglish/struts/i18n/MessageResources.properties or com/thinkglish/filter/LanguageFilter.class) it works without any problems.
Do you have any guess about this? Is it possible that the .dat extension has anything to do with this?
Edited - More data!
I added a new .properties mock file to the exact same directory in which the .dat file lives:
/src/main/resources/com/thinkglish/geoip/mock.properties
I tried to obtain it in Tomcat6 and it worked!
ClassPathResource resource = new ClassPathResource("com/thinkglish/geoip/mock.properties");
I'm starting to think that I need to do something else configuration-wise to make Tomcat6 accept the .dat file as a Classpath resource.
Thanks in advance!
I might be barking up completely the wrong tree here... but have you checked the capitalisation of GeoIP.dat / GeoIp.dat? Is Tomcat running on a case-sensitive OS?
Following should work:
String classpathLocation = "com/thinkglish/geoip/GeoIp.dat";
URL classpathResource = Thread.currentThread().getContextClassLoader().getResource(classpathLocation);
// Or:
InputStream input = Thread.currentThread().getContextClassLoader().getResourceAsStream(classpathLocation);

Categories

Resources