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.bean.BeanExchange;
032 import org.apache.camel.impl.DefaultEndpoint;
033
034 /**
035 * @version $Revision:520964 $
036 */
037 public class RmiEndpoint extends DefaultEndpoint<BeanExchange> {
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 BeanExchange createExchange() {
054 return new BeanExchange(getContext());
055 }
056
057 public Consumer<BeanExchange> 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 }
061 return new RmiConsumer(this, processor);
062 }
063
064 public Producer<BeanExchange> createProducer() throws Exception {
065 return new RmiProducer(this);
066 }
067
068 public String getName() {
069 String path = uri.getPath();
070 if (path == null) {
071 path = uri.getSchemeSpecificPart();
072 }
073 return path;
074 }
075
076 public Registry getRegistry() throws RemoteException {
077 if (uri.getHost() != null) {
078 if (uri.getPort() == -1) {
079 return LocateRegistry.getRegistry(uri.getHost());
080 } else {
081 return LocateRegistry.getRegistry(uri.getHost(), uri.getPort());
082 }
083 } else {
084 return LocateRegistry.getRegistry();
085 }
086 }
087
088 public List<Class> getRemoteInterfaces() {
089 return remoteInterfaces;
090 }
091
092 public void setRemoteInterfaces(List<Class> remoteInterfaces) {
093 this.remoteInterfaces = remoteInterfaces;
094 if (classLoader == null && !remoteInterfaces.isEmpty()) {
095 classLoader = remoteInterfaces.get(0).getClassLoader();
096 }
097 }
098
099 public void setRemoteInterfaces(Class... remoteInterfaces) {
100 setRemoteInterfaces(Arrays.asList(remoteInterfaces));
101 }
102
103 public ClassLoader getClassLoader() {
104 return classLoader;
105 }
106
107 public void setClassLoader(ClassLoader classLoader) {
108 this.classLoader = classLoader;
109 }
110
111 public int getPort() {
112 return port;
113 }
114
115 public void setPort(int port) {
116 this.port = port;
117 }
118
119 }