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.model;
018
019 import java.util.Collections;
020 import java.util.List;
021
022 import javax.xml.bind.annotation.XmlAccessType;
023 import javax.xml.bind.annotation.XmlAccessorType;
024 import javax.xml.bind.annotation.XmlAttribute;
025 import javax.xml.bind.annotation.XmlTransient;
026
027 import org.apache.camel.Endpoint;
028 import org.apache.camel.ExchangePattern;
029 import org.apache.camel.Processor;
030 import org.apache.camel.processor.SendProcessor;
031 import org.apache.camel.spi.RouteContext;
032 import org.apache.camel.util.ObjectHelper;
033
034 /**
035 * Base class for sending to an endpoint with an optional {@link ExchangePattern}
036 *
037 * @version $Revision: 794544 $
038 */
039 //@XmlType(name = "sendType")
040 @XmlAccessorType(XmlAccessType.FIELD)
041 public class SendDefinition<Type extends ProcessorDefinition> extends ProcessorDefinition<Type> {
042 @XmlAttribute(required = false)
043 protected String uri;
044 @XmlAttribute(required = false)
045 protected String ref;
046 @XmlTransient
047 protected Endpoint endpoint;
048
049 public SendDefinition() {
050 }
051
052 public SendDefinition(String uri) {
053 this.uri = uri;
054 }
055
056 @Override
057 public Processor createProcessor(RouteContext routeContext) throws Exception {
058 Endpoint endpoint = resolveEndpoint(routeContext);
059 return new SendProcessor(endpoint, getPattern());
060 }
061
062 public Endpoint resolveEndpoint(RouteContext context) {
063 if (endpoint == null) {
064 endpoint = context.resolveEndpoint(getUri(), getRef());
065 }
066 return endpoint;
067 }
068
069 // Properties
070 // -----------------------------------------------------------------------
071 public String getRef() {
072 return ref;
073 }
074
075 public void setRef(String ref) {
076 this.ref = ref;
077 }
078
079 public String getUri() {
080 return uri;
081 }
082
083 public void setUri(String uri) {
084 this.uri = uri;
085 }
086
087 public Endpoint getEndpoint() {
088 return endpoint;
089 }
090
091 public void setEndpoint(Endpoint endpoint) {
092 this.endpoint = endpoint;
093 }
094
095 public ExchangePattern getPattern() {
096 return null;
097 }
098
099 @SuppressWarnings("unchecked")
100 public List<ProcessorDefinition> getOutputs() {
101 return Collections.EMPTY_LIST;
102 }
103
104 /**
105 * Returns the endpoint URI or the name of the reference to it
106 */
107 public Object getUriOrRef() {
108 String uri = getUri();
109 if (ObjectHelper.isEmpty(uri)) {
110 return uri;
111 } else if (endpoint != null) {
112 return endpoint.getEndpointUri();
113 }
114 return getRef();
115 }
116
117 @Override
118 public String getLabel() {
119 return FromDefinition.description(getUri(), getRef(), getEndpoint());
120 }
121 }