View Javadoc

1   /*
2    * Copyright (c) 2005, The K-Wf Grid Consortium
3    * Fraunhofer Institute for Computer Architecture and Software Technology
4    * See http://www.kwfgrid.eu and http://www.first.fraunhofer.de for more details.
5    */
6   package net.kwfgrid.gwes.uiproxy.jdom;
7   
8   import org.jaxen.*;
9   import org.jaxen.jdom.*;
10  
11  import org.jdom.*;
12  import net.kwfgrid.gwes.uiproxy.SequenceBuffer;
13  import net.kwfgrid.gwes.uiproxy.BufferException;
14  
15  /**
16     Implementation of <code>SequenceBuffer</code> for sequenced XML messages in the JDOM object model.
17     The sequence number of the messages must be accessible by an XPath query. The jaxen XPath engine
18     is used to query incoming messages for their sequence number.<br><br>
19     This buffer supports the following properties:
20     <ul>
21     <li>Buffer.size</li>
22     <li>SequenceBuffer.first</li>
23     <li>JDOMSequenceBuffer.xpath_sequenceNumber</li>
24     <li>JDOMSequenceBuffer.xpath_shouldHandle</li>
25     </ul>
26     The JDOMSequenceBuffer.* properties must be set at initialization time of the buffer.
27   */
28  public abstract class JDOMSequenceBuffer extends SequenceBuffer {
29      public static final String XPATH_SEQUENCE_NUMBER_PROPERTY_KEY = "JDOMSequenceBuffer.xpath_sequenceNumber";
30      public static final String XPATH_SHOULD_HANDLE_PROPERTY_KEY = "JDOMSequenceBuffer.xpath_shouldHandle";
31  
32      XPath _xpath_shouldhandle;
33      XPath _xpath_sequencenumber;
34  
35      /**
36         Constructor.
37         @param xpath The XPath expression to access the sequence number inside of an incoming message.
38         @exception InstantiationException If the XPath string could not be parsed.
39       */
40      public JDOMSequenceBuffer(String xpath) throws InstantiationException {
41  	_xpath_shouldhandle = null;
42  	_xpath_sequencenumber = null;
43      }
44  
45      public synchronized void setProperty(String key, String value) throws BufferException {
46  	if (XPATH_SEQUENCE_NUMBER_PROPERTY_KEY.equals(key)) {
47  	    try {
48  		_xpath_sequencenumber = DocumentNavigator.getInstance().parseXPath(value);
49  	    } catch (Exception x) {
50  		_exception = new BufferException(this, "Error parsing XPath expression "+value+" to access sequence numbers.", x);
51  	    } 
52  	} else if (XPATH_SHOULD_HANDLE_PROPERTY_KEY.equals(key)) {
53  	    try {
54  		_xpath_shouldhandle = DocumentNavigator.getInstance().parseXPath(value);
55  	    } catch (Exception x) {
56  		_exception = new BufferException(this, "Error parsing XPath expression "+value+" to determine message type.", x);
57  	    } 
58  	} else {
59  	    super.setProperty(key, value);
60  	}
61  	if (_exception!=null) throw _exception;
62      }
63  
64  
65      /**
66         @exception IllegalStateException If the property <code>JDOMSequenceBuffer.xpath_sequenceNumber</code> is not set.
67       */
68      protected int getSequenceNumber(Object message) throws JaxenException, NumberFormatException, IllegalStateException {
69  	if (_xpath_sequencenumber==null) 
70  	    throw new IllegalStateException("Property JDOMSequenceBuffer.xpath_sequenceNumber not set.");
71  
72  	Element jdom = (Element)message;
73  	String seq = _xpath_sequencenumber.stringValueOf(jdom);
74  	return Integer.parseInt(seq);
75      }
76  
77      /**
78         This buffer handles messages which are instance of <code>org.jdom.Element</code>.
79         Additionally the XPath expression provided through property <code>JDOMSequenceBuffer.xpath_shouldHandle</code> must
80         evaluate to true for the incoming message.
81         @exception IllegalStateException If the property <code>JDOMSequenceBuffer.xpath_shouldHandle</code> is not set.
82       */
83      protected boolean shouldHandle(Object message) throws JaxenException, IllegalStateException {
84  	if (_xpath_shouldhandle==null) 
85  	    throw new IllegalStateException("Property JDOMSequenceBuffer.xpath_shouldHandle not set.");
86  
87  	if (message instanceof Element) {
88  	    return _xpath_shouldhandle.booleanValueOf(message);
89  	} else {
90  	    return false;
91  	}
92      }
93  }