wood burning stoves 2.0*
The moose likes Web Services and the fly likes Reading external XML file from an Axis Web Service Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Web Services
Bookmark "Reading external XML file from an Axis Web Service" Watch "Reading external XML file from an Axis Web Service" New topic
Author

Reading external XML file from an Axis Web Service

Bharat Ruparel
Ranch Hand

Joined: Jul 30, 2003
Posts: 493
I have deployed an Axis 1.2.1 web service within Tomcat 5.0.28 container. The web service is supposed to read an XML file. I have used Castor to serialize the XML file into Java Classes. I developed this application under Eclipse as a standalone Java application. It works perfectly. When I try to move it to the Axis Web Service running within the Tomcat Container however, I ran into trouble. Specifically, the Web Service cannot find the XML file (mapping.xml and the datafile containing XML data to be read into Java classes). I did some research and tried to copy this file just about everywhere, i.e. in the WEB-INF directory, in the classes directory, etc. but no luck, the web service cannot find it. I understand that within the Servlet container you get the path using getRealPath function of the ServletConfig object. However, that does not work either. I have seen various postings to the effect that Axis MsgContext object can give the ServletContext object, but do not know how. I would really appreciate it if someone can post a code snippet showing how to read the path for a file to be read from within an Axis web service running within the Tomcat container. The imports will help too.
Thanks in advance.
Bharat


SCJP,SCJD,SCWCD,SCBCD,SCDJWS,SCEA
Gerardo Tasistro
Ranch Hand

Joined: Feb 08, 2005
Posts: 362
Are you using ?

// Load Mapping
Mapping mapping = new Mapping();
mapping.loadMapping("mapping.xml");

And putting the mapping.xml in the same directory the .java file that uses it is placed?

Did you check the spelling on the name and path. I've had some problems with wierd extensions getting added (particularly on Windows systems) when you edit the XML in some third party tool.
Bharat Ruparel
Ranch Hand

Joined: Jul 30, 2003
Posts: 493
Hello Gerardo,
Thanks for your quick response:

You wrote:

re you using ?

// Load Mapping
Mapping mapping = new Mapping();
mapping.loadMapping("mapping.xml");

The answer is yes:

Here is a snippet of my code that is attempting to read the mapping file:

package sc.webservices.services.castor;

import org.exolab.castor.xml.*;
import org.exolab.castor.mapping.*;

... Other imports

import java.io.FileReader;
import java.util.List;
import java.util.Iterator;


/**
* @author bruparel
* This class is used to transfer data read from the XML files to the
* ForceEntityArray used by the BlueForce/RedForce forces
*/
public class ForcesCastorMapper {


public ForcesCastorMapper(){ };

public static ForceEntity[] getForceEntityArray() {

ForceEntity[] forceEntityArray = null;
try {
// -- Load a mapping file
System.out.println("Before reading the mapping.xml file");
// Scott, this is what I was trying to resolve. I will work on it some more
// basically, we need to be able to get the servlet context here.
// System.out.println("WEB-INF real path:" +getServletContext().getRealPath("/WEB-INF"));
// System.out.println("axis real path:" +getServletContext().getRealPath("/axis"));

Mapping mapping = new Mapping();
mapping.loadMapping("/home/bruparel/mapping.xml");
System.out.println("After reading the mapping.xml file. Starting Unmarshaller");
Unmarshaller un = new Unmarshaller(Entities.class);
un.setMapping( mapping );


Note in the above code that I had to hard code the path to my mapping.xml file? I tried putting this file in the same directory within the Tomcat/Axis directories where the ForcesCastorMapper class resides, but it did not work. I even tried putting "./" in front of mapping.xml to make it read from the current directory but it didn't work either. You will see from my code that I have commented out the getRealPath code that I was trying to get to work, but gave up in frustration. The ServletContext needs to be obtained from Axis MsgContext object. I don't know how.

Thanks for trying to help.

Regards,
Bharat
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

I don't recommend using getRealPath("/") in a servlet app unless you know that it will never need to be supported in a container that runs webapps from a packed war file.

That being said, if you want to get a reference to the ServletContext from within an Object being consumed by Axis, implement ServiceLifeCycle.
You should then be able to locate the servlet context with code similar to the following:



Since this has everything to do with Apache Axis and isn't really Servlet specific, I'm going to move it. I'm guessing that you'll find the greatest number of Axis gurus in the Webservice forum so I'll move it there.
[ January 30, 2006: Message edited by: Ben Souther ]

Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Balaji Loganathan
author and deputy
Bartender

Joined: Jul 13, 2001
Posts: 3150
Bharat,
In addition to what Ben said, also have a look at the question (how to access http auth info..) in our http://faq.javaranch.com/view?WebServicesFaq
Hope you managed to solve your problem by now.


Spritle Software Blogs
Bharat Ruparel
Ranch Hand

Joined: Jul 30, 2003
Posts: 493
Hello Ben and Balaji,
Thank you both. The problem is resolved. I appreciate your help.
Regards,
Bharat
 
wood burning stoves
 
subject: Reading external XML file from an Axis Web Service