| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | |
|
| 20 | |
|
| 21 | |
|
| 22 | |
|
| 23 | |
|
| 24 | |
|
| 25 | |
|
| 26 | |
|
| 27 | |
package org.semispace.ws; |
| 28 | |
|
| 29 | |
import java.io.StringReader; |
| 30 | |
import java.util.HashMap; |
| 31 | |
import java.util.Map; |
| 32 | |
|
| 33 | |
import javax.jws.WebService; |
| 34 | |
import javax.xml.parsers.DocumentBuilderFactory; |
| 35 | |
|
| 36 | |
import org.slf4j.Logger; |
| 37 | |
import org.slf4j.LoggerFactory; |
| 38 | |
import org.semispace.Holder; |
| 39 | |
import org.semispace.SemiSpace; |
| 40 | |
import org.w3c.dom.Document; |
| 41 | |
import org.w3c.dom.Node; |
| 42 | |
import org.w3c.dom.NodeList; |
| 43 | |
import org.xml.sax.InputSource; |
| 44 | |
|
| 45 | |
|
| 46 | |
|
| 47 | |
|
| 48 | |
|
| 49 | |
|
| 50 | |
|
| 51 | |
@WebService(endpointInterface = "org.semispace.ws.WsSpace") |
| 52 | 0 | public class WsSpaceImpl implements WsSpace { |
| 53 | 0 | private static final Logger log = LoggerFactory.getLogger(WsSpaceImpl.class); |
| 54 | |
|
| 55 | |
private SemiSpace space; |
| 56 | |
|
| 57 | |
|
| 58 | |
public void setSpace(SemiSpace space) { |
| 59 | 0 | this.space = space; |
| 60 | 0 | } |
| 61 | |
|
| 62 | |
public void write(String contents, long timeToLiveMs) { |
| 63 | 0 | makeSureSpaceIsPresent(); |
| 64 | 0 | Holder elem = retrievePropertiesFromXml(contents); |
| 65 | 0 | space.writeToElements(elem.getClassName(),timeToLiveMs, elem.getXml(), elem.getSearchMap()); |
| 66 | 0 | } |
| 67 | |
|
| 68 | |
private void makeSureSpaceIsPresent() { |
| 69 | 0 | if ( space == null ) { |
| 70 | 0 | String error = "Erroneous initialization - need space."; |
| 71 | 0 | log.error(error); |
| 72 | 0 | throw new RuntimeException(error); |
| 73 | |
} |
| 74 | 0 | } |
| 75 | |
|
| 76 | |
public String read(String template, long queryLifeMs) { |
| 77 | 0 | makeSureSpaceIsPresent(); |
| 78 | 0 | Holder elem = retrievePropertiesFromXml(template ); |
| 79 | 0 | String found = space.findOrWaitLeaseForTemplate(elem.getSearchMap(), queryLifeMs, false); |
| 80 | 0 | return found; |
| 81 | |
} |
| 82 | |
|
| 83 | |
public String readIfExists(String template) { |
| 84 | 0 | makeSureSpaceIsPresent(); |
| 85 | 0 | return read( template, 0); |
| 86 | |
} |
| 87 | |
|
| 88 | |
public String take(String template, long queryLifeMs) { |
| 89 | 0 | makeSureSpaceIsPresent(); |
| 90 | 0 | Holder elem = retrievePropertiesFromXml(template ); |
| 91 | 0 | String found = space.findOrWaitLeaseForTemplate(elem.getSearchMap(), queryLifeMs, true); |
| 92 | 0 | return found; |
| 93 | |
} |
| 94 | |
|
| 95 | |
public String takeIfExists(String template) { |
| 96 | 0 | makeSureSpaceIsPresent(); |
| 97 | 0 | return take( template, 0); |
| 98 | |
} |
| 99 | |
|
| 100 | |
|
| 101 | |
|
| 102 | |
|
| 103 | |
|
| 104 | |
|
| 105 | |
|
| 106 | |
protected Holder retrievePropertiesFromXml(String xmlsource) { |
| 107 | |
|
| 108 | 0 | InputSource is = new InputSource( new StringReader(xmlsource)); |
| 109 | 0 | DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); |
| 110 | |
|
| 111 | 0 | Document doc = null; |
| 112 | |
try { |
| 113 | 0 | doc = factory.newDocumentBuilder().parse( is ); |
| 114 | 0 | } catch (Exception e) { |
| 115 | 0 | log.error("Returning null, due to exception parsing xml: "+xmlsource, e); |
| 116 | 0 | return null; |
| 117 | 0 | } |
| 118 | 0 | String doctype = doc.getDocumentElement().getNodeName(); |
| 119 | |
|
| 120 | 0 | Map<String, String> map = new HashMap<String, String>(); |
| 121 | |
|
| 122 | 0 | NodeList children = doc.getDocumentElement().getChildNodes(); |
| 123 | 0 | for ( int i=0 ; i < children.getLength() ; i++) { |
| 124 | 0 | Node node = children.item( i ); |
| 125 | |
|
| 126 | |
|
| 127 | 0 | if ( node.getNodeType() == Node.ELEMENT_NODE && node.getChildNodes().getLength() > 0) { |
| 128 | 0 | String name = node.getNodeName(); |
| 129 | 0 | String value = node.getChildNodes().item(0).getNodeValue(); |
| 130 | |
|
| 131 | 0 | if( value != null ) { |
| 132 | 0 | map.put(name,value); |
| 133 | |
} |
| 134 | |
} |
| 135 | |
} |
| 136 | 0 | map.put("class", doctype); |
| 137 | |
|
| 138 | 0 | Holder holder = new Holder(xmlsource, -1, doctype, -1, map ); |
| 139 | 0 | return holder; |
| 140 | |
} |
| 141 | |
} |