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.management.DeploymentException;
021    import javax.jbi.messaging.MessageExchange;
022    import javax.jbi.messaging.MessageExchange.Role;
023    import javax.jbi.servicedesc.ServiceEndpoint;
024    import javax.xml.namespace.QName;
025    
026    import org.apache.servicemix.common.DefaultComponent;
027    import org.apache.servicemix.common.ExternalEndpoint;
028    import org.apache.servicemix.common.ServiceMixComponent;
029    import org.apache.servicemix.common.ServiceUnit;
030    import org.apache.servicemix.jbi.resolver.URIResolver;
031    
032    public abstract class ConsumerEndpoint extends SimpleEndpoint {
033    
034        private ServiceEndpoint activated;
035        private QName targetInterface;
036        private QName targetService;
037        private String targetEndpoint;
038        private QName targetOperation;
039        private String targetUri;
040        
041        public ConsumerEndpoint() {
042        }
043    
044        public ConsumerEndpoint(ServiceUnit serviceUnit, QName service, String endpoint) {
045            super(serviceUnit, service, endpoint);
046        }
047    
048        public ConsumerEndpoint(DefaultComponent component, ServiceEndpoint endpoint) {
049            super(component.getServiceUnit(), endpoint.getServiceName(), endpoint.getEndpointName());
050        }
051    
052        public Role getRole() {
053            return Role.CONSUMER;
054        }
055    
056        public synchronized void start() throws Exception {
057            ServiceMixComponent component = getServiceUnit().getComponent();
058            ComponentContext ctx = component.getComponentContext();
059            activated = new ExternalEndpoint(component.getEPRElementName(),
060                                             getLocationURI(),
061                                             getService(),
062                                             getEndpoint(),
063                                             getInterfaceName());
064            ctx.registerExternalEndpoint(activated);
065        }
066    
067        public synchronized void stop() throws Exception {
068            ServiceMixComponent component = getServiceUnit().getComponent();
069            ComponentContext ctx = component.getComponentContext();
070            if (activated != null) {
071                ServiceEndpoint se = activated;
072                activated = null;
073                ctx.deregisterExternalEndpoint(se);
074            }
075        }
076    
077        public abstract String getLocationURI();
078    
079        /**
080         * @return the targetEndpoint
081         */
082        public String getTargetEndpoint() {
083            return targetEndpoint;
084        }
085    
086        /**
087         * @param targetEndpoint the targetEndpoint to set
088         */
089        public void setTargetEndpoint(String targetEndpoint) {
090            this.targetEndpoint = targetEndpoint;
091        }
092    
093        /**
094         * @return the targetInterface
095         */
096        public QName getTargetInterface() {
097            return targetInterface;
098        }
099    
100        /**
101         * @param targetInterface the targetInterface to set
102         */
103        public void setTargetInterface(QName targetInterface) {
104            this.targetInterface = targetInterface;
105        }
106    
107        /**
108         * @return the targetService
109         */
110        public QName getTargetService() {
111            return targetService;
112        }
113    
114        /**
115         * @param targetService the targetService to set
116         */
117        public void setTargetService(QName targetService) {
118            this.targetService = targetService;
119        }
120    
121        /**
122         * @return the targetOperation
123         */
124        public QName getTargetOperation() {
125            return targetOperation;
126        }
127    
128        /**
129         * @param targetOperation the targetOperation to set
130         */
131        public void setTargetOperation(QName targetOperation) {
132            this.targetOperation = targetOperation;
133        }
134    
135        /**
136         * @return the targetUri
137         */
138        public String getTargetUri() {
139            return targetUri;
140        }
141    
142        /**
143         * @param targetUri the targetUri to set
144         */
145        public void setTargetUri(String targetUri) {
146            this.targetUri = targetUri;
147        }
148        
149        protected void configureExchangeTarget(MessageExchange exchange) {
150            if (targetUri != null) {
151                URIResolver.configureExchange(exchange, getContext(), targetUri);
152            }
153            if (exchange.getInterfaceName() == null && targetInterface != null) {
154                exchange.setInterfaceName(targetInterface);
155            }
156            if (exchange.getOperation() == null && targetOperation != null) {
157                exchange.setOperation(targetOperation);
158            }
159            if (exchange.getService() == null && targetService != null) {
160                exchange.setService(targetService);
161                if (targetEndpoint != null) {
162                    ServiceEndpoint se = getContext().getEndpoint(targetService, targetEndpoint);
163                    if (se != null) {
164                        exchange.setEndpoint(se);
165                    } else {
166                        logger.warn("Target service (" + targetService + ") and endpoint (" + targetEndpoint + ")" + 
167                                " specified, but no matching endpoint found.  Only the service will be used for routing.");
168                    }
169                }
170            }
171        }
172    
173        public void validate() throws DeploymentException {
174            super.validate();
175            if (targetInterface == null && targetService == null && targetUri == null) {
176                throw new DeploymentException("targetInterface, targetService or targetUri should be specified");
177            }
178        }
179    }