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