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.ExchangeStatus;
021    import javax.jbi.messaging.InOnly;
022    import javax.jbi.messaging.MessageExchange;
023    import javax.jbi.messaging.NormalizedMessage;
024    import javax.jbi.messaging.RobustInOnly;
025    import javax.jbi.messaging.MessageExchange.Role;
026    import javax.jbi.servicedesc.ServiceEndpoint;
027    import javax.xml.namespace.QName;
028    
029    import org.apache.servicemix.JbiConstants;
030    import org.apache.servicemix.common.DefaultComponent;
031    import org.apache.servicemix.common.ServiceUnit;
032    
033    public abstract class ProviderEndpoint extends SimpleEndpoint {
034    
035        private ServiceEndpoint activated;
036    
037    
038        public ProviderEndpoint() {
039        }
040    
041        public ProviderEndpoint(ServiceUnit serviceUnit, QName service, String endpoint) {
042            super(serviceUnit, service, endpoint);
043        }
044    
045        public ProviderEndpoint(DefaultComponent component, ServiceEndpoint endpoint) {
046            super(component.getServiceUnit(), endpoint.getServiceName(), endpoint.getEndpointName());
047        }
048    
049        /* (non-Javadoc)
050         * @see org.apache.servicemix.common.Endpoint#getRole()
051         */
052        public Role getRole() {
053            return Role.PROVIDER;
054        }
055    
056        public void start() throws Exception {
057            ComponentContext ctx = getServiceUnit().getComponent().getComponentContext();
058            activated = ctx.activateEndpoint(service, endpoint);
059        }
060    
061        public void stop() throws Exception {
062            if (activated == null) {
063                throw new IllegalStateException("Endpoint not activated: " + this);
064            }
065            ServiceEndpoint ep = activated;
066            activated = null;
067            ComponentContext ctx = getServiceUnit().getComponent().getComponentContext();
068            ctx.deactivateEndpoint(ep);
069        }
070    
071        /**
072         * A default implementation of the message processor which checks the status of the exchange
073         * and if its valid will dispatch to either {@link #processInOnly(MessageExchange,NormalizedMessage)} for
074         * an {@link InOnly} or {@link RobustInOnly} message exchange otherwise the
075         * {@link #processInOut(MessageExchange,NormalizedMessage,NormalizedMessage)}
076         * method will be invoked
077         *
078         * @param exchange the message exchange
079         * @throws Exception
080         */
081        public void process(MessageExchange exchange) throws Exception {
082            // The component acts as a provider, this means that another component has requested our service
083            // As this exchange is active, this is either an in or a fault (out are sent by this component)
084            if (exchange.getRole() == Role.PROVIDER) {
085                // Exchange is finished
086                if (exchange.getStatus() == ExchangeStatus.DONE) {
087                    return;
088                // Exchange has been aborted with an exception
089                } else if (exchange.getStatus() == ExchangeStatus.ERROR) {
090                    return;
091                // Exchange is active
092                } else {
093                    NormalizedMessage in;
094                    // Fault message
095                    if (exchange.getFault() != null) {
096                        done(exchange);
097                    // In message
098                    } else if ((in = exchange.getMessage("in")) != null) {
099                        if (exchange instanceof InOnly || exchange instanceof RobustInOnly) {
100                            processInOnly(exchange, in);
101                            done(exchange);
102                        }
103                        else {
104                            NormalizedMessage out = exchange.getMessage("out");
105                            if (out == null) {
106                                out = exchange.createMessage();
107                                exchange.setMessage(out, "out");
108                            }
109                            processInOut(exchange, in, out);
110                            boolean txSync = exchange.isTransacted() && Boolean.TRUE.equals(exchange.getProperty(JbiConstants.SEND_SYNC));
111                            if (txSync) {
112                                sendSync(exchange);
113                            } else {
114                                send(exchange);
115                            }
116                        }
117                    // This is not compliant with the default MEPs
118                    } else {
119                        throw new IllegalStateException("Provider exchange is ACTIVE, but no in or fault is provided");
120                    }
121                }
122            // Unsupported role: this should never happen has we never create exchanges
123            } else {
124                throw new IllegalStateException("Unsupported role: " + exchange.getRole());
125            }
126        }
127    
128    
129        protected void processInOnly(MessageExchange exchange, NormalizedMessage in) throws Exception {
130            throw new UnsupportedOperationException("Unsupported MEP: " + exchange.getPattern());
131        }
132    
133        protected void processInOut(MessageExchange exchange, NormalizedMessage in, NormalizedMessage out) throws Exception {
134            throw new UnsupportedOperationException("Unsupported MEP: " + exchange.getPattern());
135        }
136        
137    }