[jdom-interest] Re: JDOM w/ Apache Axis (Jerry Jalenak)
Charles.Robinson at abbott.com
Charles.Robinson at abbott.com
Wed Apr 14 04:21:57 PDT 2004
I have had no problem using JDOM with Apache/AXIS although I do all my
deployment through jws files. Note: I am trying to write services that the
lowest common denominator (cf Visual Basic) can understand. Thus, for the
most part, my interface is String in, String out where the strings are
either just simple parameters or XML object trees represented as a string.
(This may in fact be your problem: you don't want to send an Element
across the wire but rather a String whose content is the XML for the
Element.)
I enclose the complete client super class which does the work. The encode
methods take an appropriate Java class tree (all of whose members are
subclasses of XMLType) and, using JDOM converts it to XML: see in
particular encodeAsString. The analyze methods do the inverse: take an XML tree as represented by
JDOM, parse it and return an object of type XMLType (which is probably a
tree of objects of type XMLType).
The method getResponseFromServer does the actual transmission to the server.
Hope this all helps.
package com.abbott.pprd.hts.sirna.xmlobjects;
import java.io.*;
import java.util.*;
import java.lang.reflect.*;
import org.jdom.*;
import org.jdom.input.*;
import org.jdom.output.*;
import java.net.*;
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
/**
* The <code>XMLType</code> class is an abstract base class
* performing creation and manipulation of java classes from stringified
XML sent from the axis/siRNA server.
* @version 0.01; 1/28/04
* @author C. Robinson
*/
abstract class XMLType {
/** The path of the objects from this package */
static final String objectPath = "xmlobjects.";
/** A Log4J logging instance for this class */
static final org.apache.commons.logging.Log LOG =
org.apache.commons.logging.LogFactory.getLog(XMLType.class);
/** The URL of the SOAP service supplying the muscle */
static final String urlString =
"http://ppdapf07.northamerica.intra.abbott.com:8071/axis/SIRNARelationalService.jws";
/** The XML attributes representing variable data for this class */
protected Hashtable attributes = new Hashtable();
/** The children of this class if this is a standard container class */
protected ArrayList children = null;
/**
* Generate an instance of the class represented by this XML element
*
* @param el an XML element
*
* @return an instantion of the class represented by the Element
*
*/
public XMLType analyze(Element el) throws Exception,Throwable {
analyzeAttributes(el);
analyzeChildren(el);
dirty = false;
return this;
}
/** Dirty is true if the object has been modified since fetch.
Initialized to true. */
boolean dirty = true;
/**
* Return the state of dirtyness
*
* @return the dirtyness state
*
*/
public boolean isDirty() {return dirty;}
/**
* Set the state of dirtyness
*
* @param x the dirtyness state
*
*/
public void setDirty(boolean x) {dirty = x;}
/**
* Get the attributes (name/value pairs)
*
* @return the Hashtable of attributes
*
*/
protected Hashtable getAttributes() {return attributes;}
protected void setAttributes(Hashtable h) {attributes=h;}
/**
* Save an attribute in the set of attribute (remove same if null or "")
* set dirty = true if old attribute and the new one disaagree
*
* @param attri the name of the attribute to be set
* @param value the value of the attribute to be set (null is represented
by null or "")
*
*/
public void putAttribute(String attri,String value) {
String x = getAttribute(attri);
if (x==null) {
if ((value == null) || value.equals("")) return;
} else if (x.equals(value)) {
return;
}
if ((value == null) || value.equals("")) {
attributes.remove(attri);
} else {
attributes.put(attri,value);
}
dirty = true;
}
public String getAttribute(String attri) {
return (String)attributes.get(attri);
}
public String encodeAsString() throws Exception {
StringWriter sw = new StringWriter();
Document outdoc = new Document(encode());
XMLOutputter outputter = new XMLOutputter("",true);
outputter.setIndent(" ");
outputter.output(outdoc, sw);
return sw.toString();
}
protected Element encode() throws Exception {
// LOG.info("encode "+this.getClass().getName());
StringTokenizer st = new
StringTokenizer(getClass().getName(),".");
String lastName ="?";
while (st.hasMoreTokens()) lastName=st.nextToken();
Element el = new Element(lastName);
encodeAttributes(el);
encodeChildren(el);
return el;
}
protected void encodeAttributes(Element el) {
for (Iterator i = attributes.keySet().iterator();i.hasNext();) {
String a = (String)i.next();
el.setAttribute(a,getAttribute(a));
}
}
protected void encodeChildren(Element el) throws Exception {
if (children != null) for (Iterator i =
children.iterator();i.hasNext();) {
XMLType child = (XMLType)i.next();
el.addContent(child.encode());
}
}
protected void analyzeAttributes(Element el) throws Exception {
List t = el.getAttributes();
for (Iterator i = t.iterator();i.hasNext();) {
Attribute a = (Attribute)i.next();
attributes.put(a.getName(),a.getValue());
}
}
protected void analyzeChildren(Element el) throws Exception, Throwable {
children = new ArrayList(); // all of type XMLType
List t = el.getChildren();
for (Iterator i = t.iterator();i.hasNext();) {
Element e = (Element)i.next();
String name = e.getName();
XMLType x =
(XMLType)Class.forName(objectPath+e.getName()).newInstance();
children.add(x.analyze(e));
}
}
public void putChild(XMLType x) throws Exception,Throwable {
if (children==null) children = getChildren(); // in case we are
only partially fetched
children.add(x);
}
public ArrayList getChildren() throws Exception {
if (children==null) children = new ArrayList(); // we have no
current chilren --> create a list for same
return children;
}
protected static String getResponseFromServer(String meth, Object[] args)
throws Exception {
// LOG.info("Processing "+meth);
Call call = (Call)new Service().createCall();
call.setOperationName(new
javax.xml.namespace.QName("http://soapinterop.org/", meth));
call.setTargetEndpointAddress(new URL(urlString));
return (String)call.invoke(args);
}
protected static int getIntResponseFromServer(String meth, Object[] args)
throws Exception {
// LOG.info("Processing "+meth);
Call call = (Call)new Service().createCall();
call.setOperationName(new
javax.xml.namespace.QName("http://soapinterop.org/", meth));
call.setTargetEndpointAddress(new URL(urlString));
Integer i = (Integer)call.invoke(args);
return i.intValue();
}
}
jdom-interest-request at jdom.org
Sent by: jdom-interest-admin at jdom.org
04/14/2004 01:01 AM
Please respond to jdom-interest
To: jdom-interest at jdom.org
cc:
Subject: jdom-interest digest, Vol 1 #1463 - 1 msg
Send jdom-interest mailing list submissions to
jdom-interest at jdom.org
To subscribe or unsubscribe via the World Wide Web, visit
http://lists.denveronline.net/mailman/listinfo/jdom-interest
or, via email, send a message with subject or body 'help' to
jdom-interest-request at jdom.org
You can reach the person managing the list at
jdom-interest-admin at jdom.org
When replying, please edit your Subject line so it is more specific
than "Re: Contents of jdom-interest digest..."
Today's Topics:
1. JDOM w/ Apache Axis (Jerry Jalenak)
--__--__--
Message: 1
From: "Jerry Jalenak" <Jerry.Jalenak at LABONE.com>
To: "'jdom-interest at jdom.org'" <jdom-interest at jdom.org>
Date: Tue, 13 Apr 2004 11:25:14 -0500
Subject: [jdom-interest] JDOM w/ Apache Axis
Greetings All!
I'm fairly new to using JDOM to generate XML, so bear with me. I'm in the
processing of writing a web service using Apache Axis. As part of my
'learning' process, I've been able to have a dotNet client hit my web
service and retrieve a basic String, a basic JavaBean, and an Array object
from the JavaBean. I'm now trying to generate a XML document using JDOM,
and pass it back to the client. Here's my basic 'HelloWorld'
implementation:
package ws;
import org.jdom.Element;
/**
* @author jjalenak
*
*/
public class HelloWorldImpl implements HelloWorld
{
/* (non-Javadoc)
* @see ws.HelloWorld#hello()
*/
public Document hello() throws Exception
{
Document _d = new Document();
Element _e = new
Element("root");
_e.setText("Hi There!");
_d.setRootElement(_e);
return _d;
}
/* (non-Javadoc)
* @see ws.HelloWorld#goodbye()
*/
public Document goodbye() throws Exception
{
Document _d = new Document();
Element _e = new
Element("root");
_e.setText("Good Bye!");
_d.setRootElement(_e);
return _d;
}
}
When I ran the WSDL2JAVA emitter, it created a 'Document' bean in my 'ws'
package, and removed the import for the org.jdom.Document. The web
service
deploys on Tomcat (5.0.18) without any problems. When I try to call the
web
service from my test Java web app, I get the following exception:
exception is (500)Internal Server Error
AxisFault
faultCode: {http://xml.apache.org/axis/}HTTP
faultSubcode:
faultString: (500)Internal Server Error
faultActor:
faultNode:
faultDetail:
{}string: return code: 500
Digging into this, I found what I think is the 'true' problem :
java.io.IOException: No serializer found for class org.jdom.Element in
registry org.apache.axis.encoding.TypeMappingImpl at 6c2308. It appears that
although WSDL2JAVA converted the Document object to a proper bean, it
didn't
do the Element object. I not quite sure where to go from here, so if
anyone
has some guidance, I'd sure appreciate it. Better yet, if someone has a
working example of an Axis-based webservice using JDOM that they can
share,
I'd also appreciate being able to get a look at it.
Thanks!
Jerry Jalenak
Development Manager, Web Publishing
LabOne, Inc.
10101 Renner Blvd.
Lenexa, KS 66219
(913) 577-1496
jerry.jalenak at labone.com
This transmission (and any information attached to it) may be confidential
and
is intended solely for the use of the individual or entity to which it is
addressed. If you are not the intended recipient or the person responsible
for
delivering the transmission to the intended recipient, be advised that you
have received this transmission in error and that any use, dissemination,
forwarding, printing, or copying of this information is strictly
prohibited.
If you have received this transmission in error, please immediately notify
LabOne at the following email address:
securityincidentreporting at labone.com
--__--__--
_______________________________________________
To control your jdom-interest membership:
http://lists.denveronline.net/mailman/options/jdom-interest/youraddr@yourhost.com
End of jdom-interest Digest
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://jdom.org/pipermail/jdom-interest/attachments/20040414/c01fccf5/attachment.htm
More information about the jdom-interest
mailing list