001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.apache.camel.component.rmi;
018    
019    import java.net.URI;
020    import java.net.URISyntaxException;
021    import java.rmi.RemoteException;
022    import java.rmi.registry.LocateRegistry;
023    import java.rmi.registry.Registry;
024    import java.util.Arrays;
025    import java.util.List;
026    
027    import org.apache.camel.Consumer;
028    import org.apache.camel.Processor;
029    import org.apache.camel.Producer;
030    import org.apache.camel.RuntimeCamelException;
031    import org.apache.camel.component.pojo.PojoExchange;
032    import org.apache.camel.impl.DefaultEndpoint;
033    
034    /**
035     * @version $Revision:520964 $
036     */
037    public class RmiEndpoint extends DefaultEndpoint<PojoExchange> {
038    
039            private List<Class> remoteInterfaces;
040            private ClassLoader classLoader;
041            private URI uri;
042            private int port;
043    
044            protected RmiEndpoint(String endpointUri, RmiComponent component) throws URISyntaxException {
045                    super(endpointUri, component);
046                    this.uri = new URI(endpointUri);
047            }
048    
049            public boolean isSingleton() {
050                    return false;
051            }
052    
053            public PojoExchange createExchange() {
054                    return new PojoExchange(getContext());
055            }
056    
057            public Consumer<PojoExchange> createConsumer(Processor processor) throws Exception {
058                    if( remoteInterfaces == null || remoteInterfaces.size()==0 )
059                            throw new RuntimeCamelException("To create an RMI consumer, the RMI endpoint's remoteInterfaces property must be be configured.");
060                    return new RmiConsumer(this, processor);
061            }
062    
063            public Producer<PojoExchange> createProducer() throws Exception {
064                    return new RmiProducer(this);
065            }
066    
067            public String getName() {
068                    String path = uri.getPath();
069                    if( path == null )
070                            path = uri.getSchemeSpecificPart();
071                    return path;
072            }
073    
074            public Registry getRegistry() throws RemoteException {
075                    if( uri.getHost()!=null ) {
076                            if( uri.getPort() == -1 ) {
077                                    return LocateRegistry.getRegistry(uri.getHost());
078                            } else {
079                                    return LocateRegistry.getRegistry(uri.getHost(), uri.getPort());                                
080                            }
081                    } else {
082                            return LocateRegistry.getRegistry();
083                    }
084            }
085    
086            public List<Class> getRemoteInterfaces() {
087                    return remoteInterfaces;
088            }
089    
090            public void setRemoteInterfaces(List<Class> remoteInterfaces) {
091                    this.remoteInterfaces = remoteInterfaces;
092                    if( classLoader== null && !remoteInterfaces.isEmpty() ) {
093                            classLoader = remoteInterfaces.get(0).getClassLoader();
094                    }
095            }
096            public void setRemoteInterfaces(Class... remoteInterfaces) {
097                    setRemoteInterfaces(Arrays.asList(remoteInterfaces));           
098            }
099    
100            public ClassLoader getClassLoader() {
101                    return classLoader;
102            }
103            public void setClassLoader(ClassLoader classLoader) {
104                    this.classLoader = classLoader;
105            }
106    
107            public int getPort() {
108                    return port;
109            }
110    
111            public void setPort(int port) {
112                    this.port = port;
113            }
114    
115    
116    }