Coverage Report - org.semispace.ws.WsSpaceImpl
 
Classes in this File Line Coverage Branch Coverage Complexity
WsSpaceImpl
0%
0/46
0%
0/10
2
 
 1  
 /*
 2  
  * ============================================================================
 3  
  *
 4  
  *  File:     WsSpace.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;
 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  
  * Implementation of unauthenticated space access.
 47  
  *  <p>
 48  
  * You need to set the space.
 49  
  * </p>
 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  
     /** For the benefit of spring */
 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  
      * Protected for the benefit of junit test(s)
 103  
      * @return A <b>temporary</b> holder object containing the relevant elements found in the source.
 104  
      * @see WsSpaceImpl#retrievePropertiesFromObject
 105  
      */
 106  
     protected Holder retrievePropertiesFromXml(String xmlsource) {
 107  
         // InputStream is = new StringConverterBufferedInputStream( new FileInputStream( tmpfile ) );
 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  
             //log.info("Got node "+node.getNodeName()+" which contains "+node.getNodeValue());
 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  
                 //log.info("This is an element node with "+node.getChildNodes().getLength()+" children which is "+value);
 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  
 }