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.bean.support;
018    
019    import javax.jbi.component.ComponentContext;
020    import javax.jbi.messaging.MessageExchange;
021    import javax.jbi.messaging.MessagingException;
022    import javax.jbi.servicedesc.ServiceEndpoint;
023    import javax.xml.namespace.QName;
024    
025    import org.apache.servicemix.jbi.resolver.URIResolver;
026    import org.springframework.beans.factory.InitializingBean;
027    
028    /**
029     * An ExchangeTarget may be used to specify the target of an exchange,
030     * while retaining all the JBI features (interface based routing, service
031     * name based routing or endpoint routing).
032     *   
033     * @author gnodet
034     * @version $Revision: 663580 $
035     * @org.apache.xbean.XBean element="exchange-target"
036     */
037    public class ExchangeTarget implements InitializingBean {
038    
039        private QName interf;
040    
041        private QName operation;
042    
043        private QName service;
044    
045        private String endpoint;
046        
047        private String uri;
048    
049        /**
050         * @return Returns the endpoint.
051         */
052        public String getEndpoint() {
053            return endpoint;
054        }
055    
056        /**
057         * @param endpoint
058         *            The endpoint to set.
059         */
060        public void setEndpoint(String endpoint) {
061            this.endpoint = endpoint;
062        }
063    
064        /**
065         * @return Returns the interface name.
066         */
067        public QName getInterface() {
068            return interf;
069        }
070    
071        /**
072         * @param interface name
073         *            The interface name to set.
074         */
075        public void setInterface(QName itf) {
076            this.interf = itf;
077        }
078    
079        /**
080         * @return Returns the operation name.
081         */
082        public QName getOperation() {
083            return operation;
084        }
085    
086        /**
087         * @param operation
088         *            The operation to set.
089         */
090        public void setOperation(QName operation) {
091            this.operation = operation;
092        }
093    
094        /**
095         * @return Returns the service.
096         */
097        public QName getService() {
098            return service;
099        }
100    
101        /**
102         * @param service
103         *            The service to set.
104         */
105        public void setService(QName service) {
106            this.service = service;
107        }
108    
109        /**
110         * @return the uri
111         */
112        public String getUri() {
113            return uri;
114        }
115    
116        /**
117         * @param uri the uri to set
118         */
119        public void setUri(String uri) {
120            this.uri = uri;
121        }
122    
123        /**
124         * Configures the target on the newly created exchange 
125         * @param exchange the exchange to configure
126         * @throws MessagingException if the target could not be configured
127         */
128        public void configureTarget(MessageExchange exchange, ComponentContext context) throws MessagingException {
129            if (interf == null && service == null && uri == null) {
130                throw new MessagingException("interface, service or uri should be specified");
131            }
132            if (uri != null) {
133                URIResolver.configureExchange(exchange, context, uri);
134            }
135            if (interf != null) {
136                exchange.setInterfaceName(interf);
137            }
138            if (operation != null) {
139                exchange.setOperation(operation);
140            }
141            if (service != null) {
142                exchange.setService(service);
143                if (endpoint != null) {
144                    ServiceEndpoint se = context.getEndpoint(service, endpoint);
145                    exchange.setEndpoint(se);
146                }
147            }
148        }
149    
150        /* (non-Javadoc)
151         * @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet()
152         */
153        public void afterPropertiesSet() throws Exception {
154            if (interf == null && service == null && uri == null) {
155                throw new MessagingException("interface, service or uri should be specified");
156            }
157        }
158    
159    }