| 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.client; |
| 28 | |
|
| 29 | |
import javax.xml.namespace.QName; |
| 30 | |
import javax.xml.ws.Service; |
| 31 | |
import javax.xml.ws.soap.SOAPBinding; |
| 32 | |
|
| 33 | |
import org.slf4j.Logger; |
| 34 | |
import org.slf4j.LoggerFactory; |
| 35 | |
import org.semispace.SemiEventListener; |
| 36 | |
import org.semispace.SemiEventRegistration; |
| 37 | |
import org.semispace.SemiLease; |
| 38 | |
import org.semispace.SemiSpaceInterface; |
| 39 | |
import org.semispace.ws.WsSpace; |
| 40 | |
import org.springframework.context.support.ClassPathXmlApplicationContext; |
| 41 | |
|
| 42 | |
import com.thoughtworks.xstream.XStream; |
| 43 | |
|
| 44 | |
|
| 45 | |
|
| 46 | |
|
| 47 | |
public class SemiSpaceProxy implements SemiSpaceInterface { |
| 48 | 0 | private static final Logger log = LoggerFactory.getLogger( SemiSpaceProxy.class ); |
| 49 | |
|
| 50 | 0 | private static SemiSpaceProxy instance = null; |
| 51 | |
private WsSpace space; |
| 52 | |
private XStream xstream; |
| 53 | |
|
| 54 | 0 | private SemiSpaceProxy() { |
| 55 | 0 | xstream = new XStream(); |
| 56 | 0 | } |
| 57 | |
|
| 58 | |
|
| 59 | |
|
| 60 | |
|
| 61 | |
|
| 62 | |
public static synchronized SemiSpaceInterface retrieveSpace( String endpoint) { |
| 63 | 0 | if ( instance == null ) { |
| 64 | 0 | instance = retrieveSpace(readSpaceServiceAsStandardPort(endpoint)); |
| 65 | |
|
| 66 | |
} |
| 67 | |
|
| 68 | 0 | return instance; |
| 69 | |
} |
| 70 | |
|
| 71 | |
|
| 72 | |
|
| 73 | |
|
| 74 | |
protected static WsSpace readSpaceServiceAsSpring() { |
| 75 | 0 | ClassPathXmlApplicationContext context = |
| 76 | |
new ClassPathXmlApplicationContext(new String[] {"org/semispace/ws/client/space-bean.xml"}); |
| 77 | 0 | WsSpace hw = (WsSpace) context.getBean("space"); |
| 78 | 0 | return hw; |
| 79 | |
} |
| 80 | |
|
| 81 | |
protected static WsSpace readSpaceServiceAsStandardPort() { |
| 82 | 0 | return readSpaceServiceAsStandardPort("http://localhost:8080/semispace-war/services/space"); |
| 83 | |
} |
| 84 | |
|
| 85 | |
protected static WsSpace readSpaceServiceAsStandardPort( String endpointAddress ) { |
| 86 | 0 | QName SERVICE_NAME = new QName("http://ws.semispace.org/", "WsSpace"); |
| 87 | 0 | QName PORT_NAME = new QName("http://ws.semispace.org/", "WsSpacePort"); |
| 88 | |
|
| 89 | 0 | Service service = Service.create(SERVICE_NAME); |
| 90 | |
|
| 91 | |
|
| 92 | 0 | service.addPort(PORT_NAME, SOAPBinding.SOAP11HTTP_BINDING, endpointAddress); |
| 93 | |
|
| 94 | 0 | WsSpace hw = service.getPort(WsSpace.class); |
| 95 | 0 | return hw; |
| 96 | |
} |
| 97 | |
|
| 98 | |
|
| 99 | |
|
| 100 | |
|
| 101 | |
protected static synchronized SemiSpaceProxy retrieveSpace(WsSpace space ) { |
| 102 | 0 | if ( instance == null ) { |
| 103 | 0 | instance = new SemiSpaceProxy(); |
| 104 | 0 | instance.space = space; |
| 105 | |
} |
| 106 | 0 | return instance; |
| 107 | |
} |
| 108 | |
|
| 109 | |
|
| 110 | |
|
| 111 | |
|
| 112 | |
public SemiEventRegistration notify(Object template, SemiEventListener listener, long duration) { |
| 113 | 0 | throw new SemiSpaceProxyException("Illegal to use notify in space proxy.", null); |
| 114 | |
} |
| 115 | |
|
| 116 | |
public Object read(Object template, long duration) { |
| 117 | |
String xml; |
| 118 | |
try { |
| 119 | 0 | xml = space.read( toXml( template ), duration); |
| 120 | 0 | } catch (Exception e) { |
| 121 | 0 | throw new SemiSpaceProxyException("Could not read due to connection error.", e); |
| 122 | 0 | } |
| 123 | 0 | return fromXml( xml ); |
| 124 | |
} |
| 125 | |
|
| 126 | |
private Object fromXml(String xml) { |
| 127 | 0 | if ( xml == null ) { |
| 128 | 0 | return null; |
| 129 | |
} |
| 130 | 0 | return xstream.fromXML(xml); |
| 131 | |
} |
| 132 | |
|
| 133 | |
private String toXml(Object template) { |
| 134 | 0 | return xstream.toXML(template); |
| 135 | |
} |
| 136 | |
|
| 137 | |
public Object readIfExists(Object template) { |
| 138 | |
String xml; |
| 139 | |
try { |
| 140 | 0 | xml = space.readIfExists( toXml( template )); |
| 141 | 0 | } catch (Exception e) { |
| 142 | 0 | throw new SemiSpaceProxyException("Could not read due to connection error.", e); |
| 143 | 0 | } |
| 144 | 0 | return fromXml( xml ); |
| 145 | |
} |
| 146 | |
|
| 147 | |
public Object take(Object template, long duration) { |
| 148 | |
String xml; |
| 149 | |
try { |
| 150 | 0 | xml = space.take(toXml( template ), duration); |
| 151 | 0 | } catch (RuntimeException e) { |
| 152 | 0 | throw new SemiSpaceProxyException("Could not take due to connection error.", e); |
| 153 | 0 | } |
| 154 | 0 | return fromXml( xml ); |
| 155 | |
} |
| 156 | |
|
| 157 | |
public Object takeIfExists(Object template) { |
| 158 | |
String xml; |
| 159 | |
try { |
| 160 | 0 | xml = space.takeIfExists(toXml( template )); |
| 161 | 0 | } catch (RuntimeException e) { |
| 162 | 0 | throw new SemiSpaceProxyException("Could not take due to connection error.", e); |
| 163 | 0 | } |
| 164 | 0 | return fromXml( xml ); |
| 165 | |
} |
| 166 | |
|
| 167 | |
|
| 168 | |
|
| 169 | |
|
| 170 | |
|
| 171 | |
|
| 172 | |
public SemiLease write(Object obj, long duration) { |
| 173 | |
try { |
| 174 | 0 | space.write(toXml(obj), duration); |
| 175 | 0 | } catch (RuntimeException e) { |
| 176 | 0 | throw new SemiSpaceProxyException("Could not write due to connection error.", e); |
| 177 | 0 | } |
| 178 | 0 | return null; |
| 179 | |
} |
| 180 | |
|
| 181 | |
protected void setSpace(WsSpace space) { |
| 182 | 0 | this.space = space; |
| 183 | 0 | } |
| 184 | |
|
| 185 | |
} |