| Method from org.jboss.net.axis.server.EntityBeanDeserializer Detail: |
public Map getOptions() {
return options;
}
|
public static EntityBeanDeserializer.BeanPropertyDescriptor[] getPd(Class javaType) {
BeanPropertyDescriptor[] pd;
try {
PropertyDescriptor[] rawPd =
Introspector.getBeanInfo(javaType).getPropertyDescriptors();
pd = BeanPropertyDescriptor.processPropertyDescriptors(rawPd, javaType);
} catch (Exception e) {
// this should never happen
throw new RuntimeException(e.getMessage());
}
return pd;
}
Create a BeanPropertyDescriptor array for the indicated class. |
protected String getStringOption(String key,
String def) {
String value = (String) options.get(key);
if (value == null) {
value = def;
}
return value;
}
returns an option string with a default |
protected void initialize() throws SAXException {
if (!initialized) {
initialized = true;
try {
//
// Extract home from jndiName
//
this.home =
new InitialContext().lookup(
getStringOption("JndiName", javaType.getName() + "Home"));
//
// Extract find method from name and sig
//
String findMethodName = getStringOption("FindMethodName", "findByPrimaryKey");
String findMethodSignatureString =
getStringOption("FindMethodSignature", "java.lang.String");
List findMethodSignatureClasses = new java.util.ArrayList(1);
StringTokenizer tokenizer = new StringTokenizer(findMethodSignatureString, ";");
while (tokenizer.hasMoreTokens()) {
findMethodSignatureClasses.add(
Thread.currentThread().getContextClassLoader().loadClass(
tokenizer.nextToken()));
}
this.findMethod =
home.getClass().getMethod(
findMethodName,
(Class[]) findMethodSignatureClasses.toArray(
new Class[findMethodSignatureClasses.size()]));
//
// Do some reasonable preprocessing
//
// Get a list of the bean properties
BeanPropertyDescriptor[] pd = getPd(javaType);
// loop through properties and grab the names for later
for (int i = 0; i < pd.length; i++) {
BeanPropertyDescriptor descriptor = pd[i];
propertyMap.put(descriptor.getName(), descriptor);
propertyMap.put(JavaUtils.xmlNameToJava(descriptor.getName()), descriptor);
}
typeDesc = TypeDesc.getTypeDescForClass(javaType);
//
// Next prepare the elements we need to call the finder
//
String findMethodElements = getStringOption("FindMethodElements", "name");
tokenizer = new StringTokenizer(findMethodElements, ";");
while (tokenizer.hasMoreElements()) {
if (typeDesc != null) {
this.findElements.add(typeDesc.getAttributeNameForField(tokenizer.nextToken()));
} else {
this.findElements.add(new QName("", tokenizer.nextToken()));
}
}
this.findObjects = new Object[findElements.size()];
} catch (NamingException e) {
throw new SAXException("Could not lookup home.", e);
} catch (ClassNotFoundException e) {
throw new SAXException("Could not find signature class.", e);
} catch (NoSuchMethodException e) {
throw new SAXException("Could not find finder method.", e);
}
}
}
initialize the deserializer |
public void onEndElement(String namespace,
String localName,
DeserializationContext context) throws SAXException {
try {
value = findMethod.invoke(home, findObjects);
Iterator allSetters = fieldSetters.iterator();
while (allSetters.hasNext()) {
((BeanPropertyTarget) allSetters.next()).setReal(value);
}
fieldSetters = null;
} catch (InvocationTargetException e) {
throw new SAXException("Encountered exception " + e.getTargetException());
} catch (IllegalAccessException e) {
throw new SAXException("Encountered exception " + e);
}
super.onEndElement(namespace, localName, context);
}
|
public SOAPHandler onStartChild(String namespace,
String localName,
String prefix,
Attributes attributes,
DeserializationContext context) throws SAXException {
BeanPropertyDescriptor propDesc = null;
if (typeDesc != null) {
QName elemQName = new QName(namespace, localName);
String fieldName = typeDesc.getFieldNameForElement(elemQName,false);
propDesc = (BeanPropertyDescriptor) propertyMap.get(fieldName);
}
if (propDesc == null) {
// look for a field by this name.
propDesc = (BeanPropertyDescriptor) propertyMap.get(localName);
}
if (propDesc == null) {
// look for a field by the "adjusted" name.
propDesc =
(BeanPropertyDescriptor) propertyMap.get(JavaUtils.xmlNameToJava(localName));
}
if (propDesc == null) {
// No such field
throw new SAXException(
Messages.getMessage("badElem00", javaType.getName(), localName));
}
// Determine the QName for this child element.
// Look at the type attribute specified. If this fails,
// use the javaType of the property to get the type qname.
QName qn = context.getTypeFromAttributes(namespace, localName, attributes);
// get the deserializer
Deserializer dSer = context.getDeserializerForType(qn);
// If no deserializer, use the base DeserializerImpl.
// There may not be enough information yet to choose the
// specific deserializer.
if (dSer == null) {
dSer = new DeserializerImpl();
// determine a default type for this child element
TypeMapping tm = context.getTypeMapping();
Class type = propDesc.getType();
dSer.setDefaultType(tm.getTypeQName(type));
}
QName elementQName = new QName(namespace, localName);
if (findElements.contains(elementQName)) {
dSer.registerValueTarget(
new FindPropertyTarget(findElements.indexOf(elementQName)));
} else if (propDesc.getWriteMethod().getParameterTypes().length == 1) {
// Success! Register the target and deserializer.
collectionIndex = -1;
dSer.registerValueTarget(new BeanPropertyTarget(propDesc));
} else {
// Success! This is a collection of properties so use the index
collectionIndex++;
dSer.registerValueTarget(new BeanPropertyTarget(propDesc, collectionIndex));
}
return (SOAPHandler) dSer;
}
Deserializer interface called on each child element encountered in
the XML stream. |
public void onStartElement(String namespace,
String localName,
String qName,
Attributes attributes,
DeserializationContext context) throws SAXException {
initialize();
if (typeDesc == null)
return;
// loop through the attributes and set bean properties that
// correspond to attributes
for (int i = 0; i < attributes.getLength(); i++) {
QName attrQName = new QName(attributes.getURI(i), attributes.getLocalName(i));
String fieldName = typeDesc.getFieldNameForAttribute(attrQName);
if (fieldName == null)
continue;
String attrName = attributes.getLocalName(i);
// look for the attribute property
BeanPropertyDescriptor bpd =
(BeanPropertyDescriptor) propertyMap.get(fieldName);
if (bpd != null) {
if (bpd.getWriteMethod() == null)
continue;
// determine the QName for this child element
TypeMapping tm = context.getTypeMapping();
Class type = bpd.getType();
QName qn = tm.getTypeQName(type);
if (qn == null)
throw new SAXException(Messages.getMessage("unregistered00", type.toString()));
// get the deserializer
Deserializer dSer = context.getDeserializerForType(qn);
if (dSer == null)
throw new SAXException(Messages.getMessage("noDeser00", type.toString()));
if (!(dSer instanceof SimpleDeserializer))
throw new SAXException(
Messages.getMessage("AttrNotSimpleType00", bpd.getName(), type.toString()));
if (findElements.contains(attrQName)) {
dSer.registerValueTarget(
new FindPropertyTarget(findElements.indexOf(attrQName)));
} else if (bpd.getWriteMethod().getParameterTypes().length == 1) {
// Success! Create an object from the string and set
// it in the bean
try {
Object val = ((SimpleDeserializer) dSer).makeValue(attributes.getValue(i));
bpd.getWriteMethod().invoke(value, new Object[] { val });
} catch (Exception e) {
throw new SAXException(e);
}
}
} // if
} // attribute loop
}
Set the bean properties that correspond to element attributes.
This method is invoked after startElement when the element requires
deserialization (i.e. the element is not an href and the value is not nil.) |
public void setOptions(Map options) {
this.options = options;
}
|