Coverage Report - org.kuali.rice.ksb.messaging.RemotedServiceHolder
 
Classes in this File Line Coverage Branch Coverage Complexity
RemotedServiceHolder
0%
0/38
0%
0/8
2.5
 
 1  
 /*
 2  
  * Copyright 2005-2007 The Kuali Foundation
 3  
  *
 4  
  *
 5  
  * Licensed under the Educational Community License, Version 2.0 (the "License");
 6  
  * you may not use this file except in compliance with the License.
 7  
  * You may obtain a copy of the License at
 8  
  *
 9  
  * http://www.opensource.org/licenses/ecl2.php
 10  
  *
 11  
  * Unless required by applicable law or agreed to in writing, software
 12  
  * distributed under the License is distributed on an "AS IS" BASIS,
 13  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 14  
  * See the License for the specific language governing permissions and
 15  
  * limitations under the License.
 16  
  */
 17  
 package org.kuali.rice.ksb.messaging;
 18  
 
 19  
 import java.io.ByteArrayInputStream;
 20  
 import java.io.ByteArrayOutputStream;
 21  
 import java.io.IOException;
 22  
 import java.io.ObjectInputStream;
 23  
 import java.io.ObjectOutput;
 24  
 import java.io.ObjectOutputStream;
 25  
 
 26  
 import org.apache.log4j.Logger;
 27  
 import org.kuali.rice.core.config.ConfigContext;
 28  
 import org.kuali.rice.core.exception.RiceRuntimeException;
 29  
 import org.kuali.rice.ksb.messaging.serviceconnectors.ServiceConnectorFactory;
 30  
 
 31  
 
 32  
 /**
 33  
  * Holds the reference to an endpoint of a service as well as the {@link ServiceInfo}
 34  
  * that defines the service.  Provides lazy loading of services at call time.
 35  
  *
 36  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 37  
  */
 38  
 public class RemotedServiceHolder implements ServiceHolder {
 39  
 
 40  
 
 41  0
             private static final Logger LOG = Logger.getLogger(RemotedServiceHolder.class);
 42  
 
 43  
         private Object service;
 44  
         private ServiceInfo serviceInfo;
 45  
 
 46  0
         public RemotedServiceHolder(ServiceInfo entry) {
 47  0
                 this.setServiceInfo(entry);
 48  0
         }
 49  
 
 50  
         public ServiceInfo getServiceInfo() {
 51  0
                 return this.serviceInfo;
 52  
         }
 53  
 
 54  
         public void setServiceInfo(ServiceInfo entry) {
 55  0
             if (ConfigContext.getCurrentContextConfig().getDevMode()) {
 56  0
                 this.serviceInfo = cloneServiceInfo(entry);
 57  
             } else {
 58  0
                 this.serviceInfo = entry;
 59  
             }
 60  0
         }
 61  
 
 62  
         public Object getService() throws Exception {
 63  0
             synchronized (this) {
 64  0
                 if (this.service == null) {
 65  0
                     this.setService(ServiceConnectorFactory.getServiceConnector(serviceInfo).getService());
 66  
                 }
 67  0
             }
 68  0
             return this.service;
 69  
         }
 70  
 
 71  
         public void setService(Object service) {
 72  0
                 this.service = service;
 73  0
         }
 74  
 
 75  
         /**
 76  
          * this is a hack so we don't mess with the {@link ServiceInfo} on the deployment side of things from the
 77  
          * {@link RemotedServiceRegistry}. We need the service in the {@link ServiceInfo} used on the client to be null but
 78  
          * it can't be for the server side stuff - solution serialize the object just like it was put in a datastore.
 79  
          *
 80  
          * @param serviceInfo
 81  
          * @return
 82  
          * @throws Exception
 83  
          */
 84  
     private static ServiceInfo cloneServiceInfo(final ServiceInfo serviceInfo) {
 85  
 
 86  0
         ObjectInputStream in = null;
 87  0
         ObjectOutput out = null;
 88  0
         Object tempService = serviceInfo.getServiceDefinition().getService();
 89  
 
 90  
         try {
 91  0
             serviceInfo.getServiceDefinition().setService(null);
 92  0
             ByteArrayOutputStream bos = new ByteArrayOutputStream();
 93  0
             out = new ObjectOutputStream(bos);
 94  0
             out.writeObject(serviceInfo);
 95  
 
 96  0
             in = new ObjectInputStream(new ByteArrayInputStream(bos.toByteArray()));
 97  0
             return (ServiceInfo) in.readObject();
 98  0
         } catch (Exception e) {
 99  0
             throw new RiceRuntimeException(e);
 100  
         } finally {
 101  
 
 102  0
             serviceInfo.getServiceDefinition().setService(tempService);
 103  0
             if (in != null) {
 104  
                 try {
 105  0
                     in.close();
 106  0
                 } catch (IOException ioe) {
 107  0
                     LOG.info("failed to close InputStream", ioe);
 108  0
                 }
 109  
             }
 110  0
             if (out != null) {
 111  
                 try {
 112  0
                 out.close();
 113  0
                 } catch (IOException ioe) {
 114  0
                     LOG.info("Failed to close OutputStream", ioe);
 115  0
                 }
 116  
             }
 117  
 
 118  
         }
 119  
     }
 120  
 }