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.servicemix.common.endpoints;
018
019 import javax.jbi.component.ComponentContext;
020 import javax.jbi.messaging.DeliveryChannel;
021 import javax.jbi.messaging.ExchangeStatus;
022 import javax.jbi.messaging.MessageExchange;
023 import javax.jbi.messaging.MessageExchangeFactory;
024 import javax.jbi.messaging.MessagingException;
025 import javax.jbi.servicedesc.ServiceEndpoint;
026 import javax.xml.namespace.QName;
027
028 import org.apache.servicemix.common.DefaultComponent;
029 import org.apache.servicemix.common.Endpoint;
030 import org.apache.servicemix.common.EndpointComponentContext;
031 import org.apache.servicemix.common.ExchangeProcessor;
032 import org.apache.servicemix.common.ServiceUnit;
033
034 public abstract class SimpleEndpoint extends Endpoint implements ExchangeProcessor {
035
036 private DeliveryChannel channel;
037 private MessageExchangeFactory exchangeFactory;
038 private ComponentContext context;
039
040 public SimpleEndpoint() {
041 }
042
043 public SimpleEndpoint(ServiceUnit serviceUnit, QName service, String endpoint) {
044 super(serviceUnit, service, endpoint);
045 }
046
047 public SimpleEndpoint(DefaultComponent component, ServiceEndpoint endpoint) {
048 super(component.getServiceUnit(), endpoint.getServiceName(), endpoint.getEndpointName());
049 }
050
051 public synchronized void activate() throws Exception {
052 context = new EndpointComponentContext(this);
053 channel = context.getDeliveryChannel();
054 exchangeFactory = channel.createExchangeFactory();
055 start();
056 }
057
058 public synchronized void deactivate() throws Exception {
059 stop();
060 }
061
062 public ExchangeProcessor getProcessor() {
063 return this;
064 }
065
066 protected void send(MessageExchange me) throws MessagingException {
067 channel.send(me);
068 }
069
070 protected void sendSync(MessageExchange me) throws MessagingException {
071 if (!channel.sendSync(me)) {
072 throw new MessagingException("SendSync failed");
073 }
074 }
075
076 protected void done(MessageExchange me) throws MessagingException {
077 me.setStatus(ExchangeStatus.DONE);
078 send(me);
079 }
080
081 protected void fail(MessageExchange me, Exception error) throws MessagingException {
082 me.setError(error);
083 send(me);
084 }
085
086 /**
087 * @return the exchangeFactory
088 */
089 public MessageExchangeFactory getExchangeFactory() {
090 return exchangeFactory;
091 }
092
093 /**
094 * @return the channel
095 */
096 public DeliveryChannel getChannel() {
097 return channel;
098 }
099
100 /**
101 * @return the context
102 */
103 public ComponentContext getContext() {
104 return context;
105 }
106
107 }