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
035: ExchangeProcessor {
036:
037: private DeliveryChannel channel;
038: private MessageExchangeFactory exchangeFactory;
039: private ComponentContext context;
040:
041: public SimpleEndpoint() {
042: }
043:
044: public SimpleEndpoint(ServiceUnit serviceUnit, QName service,
045: String endpoint) {
046: super (serviceUnit, service, endpoint);
047: }
048:
049: public SimpleEndpoint(DefaultComponent component,
050: ServiceEndpoint endpoint) {
051: super (component.getServiceUnit(), endpoint.getServiceName(),
052: endpoint.getEndpointName());
053: }
054:
055: public synchronized void activate() throws Exception {
056: context = new EndpointComponentContext(this );
057: channel = context.getDeliveryChannel();
058: exchangeFactory = channel.createExchangeFactory();
059: start();
060: }
061:
062: public synchronized void deactivate() throws Exception {
063: stop();
064: }
065:
066: public ExchangeProcessor getProcessor() {
067: return this ;
068: }
069:
070: protected void send(MessageExchange me) throws MessagingException {
071: channel.send(me);
072: }
073:
074: protected void sendSync(MessageExchange me)
075: throws MessagingException {
076: if (!channel.sendSync(me)) {
077: throw new MessagingException("SendSync failed");
078: }
079: }
080:
081: protected void done(MessageExchange me) throws MessagingException {
082: me.setStatus(ExchangeStatus.DONE);
083: send(me);
084: }
085:
086: protected void fail(MessageExchange me, Exception error)
087: throws MessagingException {
088: me.setError(error);
089: send(me);
090: }
091:
092: /**
093: * @return the exchangeFactory
094: */
095: public MessageExchangeFactory getExchangeFactory() {
096: return exchangeFactory;
097: }
098:
099: /**
100: * @return the channel
101: */
102: public DeliveryChannel getChannel() {
103: return channel;
104: }
105:
106: /**
107: * @return the context
108: */
109: public ComponentContext getContext() {
110: return context;
111: }
112:
113: }
|