Coverage Report - org.apache.camel.component.rmi.RmiConsumer
 
Classes in this File Line Coverage Branch Coverage Complexity
RmiConsumer
71% 
100% 
0
 
 1  
 /**
 2  
  * Licensed to the Apache Software Foundation (ASF) under one or more
 3  
  * contributor license agreements.  See the NOTICE file distributed with
 4  
  * this work for additional information regarding copyright ownership.
 5  
  * The ASF licenses this file to You under the Apache License, Version 2.0
 6  
  * (the "License"); you may not use this file except in compliance with
 7  
  * the License.  You may obtain a copy of the License at
 8  
  *
 9  
  *      http://www.apache.org/licenses/LICENSE-2.0
 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.apache.camel.component.rmi;
 18  
 
 19  
 import java.lang.reflect.InvocationHandler;
 20  
 import java.lang.reflect.InvocationTargetException;
 21  
 import java.lang.reflect.Method;
 22  
 import java.lang.reflect.Proxy;
 23  
 import java.rmi.Remote;
 24  
 import java.rmi.registry.Registry;
 25  
 import java.rmi.server.UnicastRemoteObject;
 26  
 
 27  
 import org.apache.camel.Processor;
 28  
 import org.apache.camel.component.bean.BeanExchange;
 29  
 import org.apache.camel.component.bean.BeanInvocation;
 30  
 import org.apache.camel.impl.DefaultConsumer;
 31  
 
 32  
 /**
 33  
  * A {@link Consumer} which uses RMI's {@see UnicastRemoteObject} to consume
 34  
  * method invocations.
 35  
  * 
 36  
  * @version $Revision: 533758 $
 37  
  */
 38  
 public class RmiConsumer extends DefaultConsumer<BeanExchange> implements InvocationHandler {
 39  
 
 40  
     private final RmiEndpoint endpoint;
 41  
     private Remote stub;
 42  
     private Remote proxy;
 43  
 
 44  
     public RmiConsumer(RmiEndpoint endpoint, Processor processor) {
 45  1
         super(endpoint, processor);
 46  1
         this.endpoint = endpoint;
 47  
 
 48  1
     }
 49  
 
 50  
     @Override
 51  
     protected void doStart() throws Exception {
 52  1
         Class[] interfaces = new Class[endpoint.getRemoteInterfaces().size()];
 53  1
         endpoint.getRemoteInterfaces().toArray(interfaces);
 54  1
         proxy = (Remote)Proxy.newProxyInstance(endpoint.getClassLoader(), interfaces, this);
 55  1
         stub = UnicastRemoteObject.exportObject(proxy, endpoint.getPort());
 56  
 
 57  
         try {
 58  1
             Registry registry = endpoint.getRegistry();
 59  1
             String name = endpoint.getName();
 60  1
             registry.bind(name, stub);
 61  
 
 62  0
         } catch (Exception e) { // Registration might fail.. clean up..
 63  
             try {
 64  0
                 UnicastRemoteObject.unexportObject(stub, true);
 65  0
             } catch (Throwable ignore) {
 66  0
             }
 67  0
             stub = null;
 68  0
             throw e;
 69  1
         }
 70  1
         super.doStart();
 71  1
     }
 72  
 
 73  
     @Override
 74  
     protected void doStop() throws Exception {
 75  1
         super.doStop();
 76  
         try {
 77  1
             Registry registry = endpoint.getRegistry();
 78  1
             registry.unbind(endpoint.getName());
 79  0
         } catch (Throwable e) { // do our best to unregister
 80  1
         }
 81  1
         UnicastRemoteObject.unexportObject(proxy, true);
 82  1
     }
 83  
 
 84  
     public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
 85  1
         if (!isStarted()) {
 86  0
             throw new IllegalStateException("The endpoint is not active: " + getEndpoint().getEndpointUri());
 87  
         }
 88  1
         BeanInvocation invocation = new BeanInvocation(proxy, method, args);
 89  1
         BeanExchange exchange = getEndpoint().createExchange();
 90  1
         exchange.setInvocation(invocation);
 91  1
         getProcessor().process(exchange);
 92  1
         Throwable fault = exchange.getException();
 93  1
         if (fault != null) {
 94  0
             throw new InvocationTargetException(fault);
 95  
         }
 96  1
         return exchange.getOut().getBody();
 97  
     }
 98  
 
 99  
     public Remote getProxy() {
 100  0
         return proxy;
 101  
     }
 102  
 
 103  
     public Remote getStub() {
 104  0
         return stub;
 105  
     }
 106  
 }