• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Reading external XML file from an Axis Web Service

 
Ranch Hand
Posts: 493
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 362
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 493
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
author and deputy
Posts: 3150
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Bharat Ruparel
Ranch Hand
Posts: 493
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Ben and Balaji,
Thank you both. The problem is resolved. I appreciate your help.
Regards,
Bharat
reply
    Bookmark Topic Watch Topic
  • New Topic