Coverage Report - org.semispace.ws.client.SemiSpaceProxy
 
Classes in this File Line Coverage Branch Coverage Complexity
SemiSpaceProxy
0%
0/54
0%
0/6
2
 
 1  
 /*
 2  
  * ============================================================================
 3  
  *
 4  
  *  File:     SemiSpaceProxy.java
 5  
  *----------------------------------------------------------------------------
 6  
  *
 7  
  * Copyright 2008 Erlend Nossum
 8  
  *
 9  
  * Licensed under the Apache License, Version 2.0 (the "License"); 
 10  
  * you may not use this file except in compliance with the License. 
 11  
  * You may obtain a copy of the License at 
 12  
  *
 13  
  *   http://www.apache.org/licenses/LICENSE-2.0
 14  
  *
 15  
  * Unless required by applicable law or agreed to in writing, software
 16  
  * distributed under the License is distributed on an "AS IS" BASIS,
 17  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 18  
  * See the License for the specific language governing permissions and 
 19  
  * limitations under the License.
 20  
  *
 21  
  *  Description:  See javadoc below
 22  
  *
 23  
  *  Created:      Mar 4, 2008
 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  
  * Client side proxy of SemiSpace web services
 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  
      * Open WS connection to the defined end point. This should be 
 60  
      * something similar to <tt>http://localhost:8080/semispace-war/services/space</tt>.
 61  
      */
 62  
     public static synchronized SemiSpaceInterface retrieveSpace( String endpoint) {
 63  0
         if ( instance == null ) {
 64  0
             instance = retrieveSpace(readSpaceServiceAsStandardPort(endpoint));
 65  
             //instance = retrieveSpace(readSpaceServiceAsSpring());
 66  
         }
 67  
         
 68  0
         return instance;
 69  
     }
 70  
  
 71  
     /**
 72  
      * An alternative way of performing lookup
 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  
         // Add a port to the Service
 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  
      * This is a bit roundabout, but is used for junit purposes.
 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  
      * <b>Notify is illegal to use in proxy</b>
 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  
      * TODO Consider fixing that null is always returned.
 169  
      * @return will presently ALWAYS return null, even when operation was a success.
 170  
      * @see org.semispace.SemiSpaceInterface#write(java.lang.Object, long)
 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  
 }