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 org.apache.commons.logging.Log;
020    import org.apache.servicemix.common.ServiceUnit;
021    import org.apache.servicemix.common.Endpoint;
022    import org.apache.servicemix.common.EndpointSupport;
023    
024    import org.w3c.dom.Document;
025    
026    import javax.jbi.management.DeploymentException;
027    import javax.jbi.messaging.MessageExchange;
028    import javax.jbi.messaging.MessagingException;
029    import javax.jbi.messaging.MessageExchange.Role;
030    import javax.wsdl.Definition;
031    import javax.xml.namespace.QName;
032    
033    public abstract class AbstractEndpoint implements Endpoint {
034    
035        protected QName service;
036        protected String endpoint;
037        protected QName interfaceName;
038        protected Document description;
039        protected Definition definition;
040        protected ServiceUnit serviceUnit;
041        protected Log logger;
042        private String key;
043    
044        public AbstractEndpoint() {
045        }
046    
047        public AbstractEndpoint(ServiceUnit serviceUnit, QName service, String endpoint) {
048            this.serviceUnit = serviceUnit;
049            this.logger = serviceUnit.getComponent().getLogger();
050            this.service = service;
051            this.endpoint = endpoint;
052        }
053    
054        /**
055         * @return Returns the endpoint.
056         */
057        public String getEndpoint() {
058            return endpoint;
059        }
060    
061        /**
062         * Sets the name of the endpoint.
063         *
064         * @param endpoint a string specifiying the name of the endpoint
065         * @org.apache.xbean.Property description="the name of the endpoint"
066         */
067        public void setEndpoint(String endpoint) {
068            this.endpoint = endpoint;
069            this.key = null;
070        }
071    
072        /**
073         * @return Returns the service.
074         */
075        public QName getService() {
076            return service;
077        }
078    
079        /**
080         * Sets the name of the service the endpoint exposes.
081         *
082         * @param service a QName specifiying the name of the service
083         * @org.apache.xbean.Property description="the QName of the service exposed by the endpoint"
084         */
085        public void setService(QName service) {
086            this.service = service;
087            this.key = null;
088        }
089    
090        /**
091         * @return Returns the role.
092         */
093        public abstract Role getRole();
094    
095        /**
096         * @return Returns the description.
097         */
098        public Document getDescription() {
099            return description;
100        }
101    
102        /**
103         * Associates an XML document with the endpoint. The XML document describes the endpoint and is typically found in the service
104         * unit packaging.
105         *
106         * @param description a <code>Document</code> describing the endpoint
107         * @org.apache.xbean.Property description="an XML document describing the endpoint"
108         */
109        public void setDescription(Document description) {
110            this.description = description;
111        }
112    
113        /**
114         * @return Returns the interfaceName.
115         */
116        public QName getInterfaceName() {
117            return interfaceName;
118        }
119    
120        /**
121         * Sets the QName of the interface exposed by the endpoint.
122         *
123         * @param interfaceName a QName specifiying the name of the interface
124         * @org.apache.xbean.Property description="the QName of the interface exposed by the endpoint"
125         */
126        public void setInterfaceName(QName interfaceName) {
127            this.interfaceName = interfaceName;
128        }
129    
130        /**
131         * @return Returns the serviceUnit.
132         */
133        public ServiceUnit getServiceUnit() {
134            return serviceUnit;
135        }
136    
137        /**
138         * Associates an endpoint with a service unit. The service unit is used by the container to manage the endpoint's lifecycle.
139         *
140         * @param serviceUnit a <code>ServiceUnit</code> to which the endpoint will be associated
141         * @org.apache.xbean.Property description="the service unit responsible for the endpoint"
142         */
143        public void setServiceUnit(ServiceUnit serviceUnit) {
144            this.serviceUnit = serviceUnit;
145            this.logger = serviceUnit.getComponent().getLogger();
146        }
147    
148        public boolean isExchangeOkay(MessageExchange exchange) {
149            // TODO: We could check the MEP here
150            return true;
151        }
152    
153        public void prepareExchange(MessageExchange exchange) throws MessagingException {
154            getServiceUnit().getComponent().prepareExchange(exchange, this);
155        }
156    
157        public abstract void activate() throws Exception;
158    
159        public abstract void start() throws Exception;
160    
161        public abstract void stop() throws Exception;
162    
163        public abstract void deactivate() throws Exception;
164    
165        public abstract void process(MessageExchange exchange) throws Exception;
166    
167        public String toString() {
168            return "Endpoint[service: " + service + ", " + "endpoint: " + endpoint + ", " + "role: "
169                   + (getRole() == Role.PROVIDER ? "provider" : "consumer") + "]";
170        }
171    
172        /**
173         * Validate the endpoint at either deployment time for statically defined endpoints or at runtime for dynamic endpoints
174         *
175         * @throws DeploymentException
176         */
177        public void validate() throws DeploymentException {
178        }
179    
180        public Definition getDefinition() {
181            return definition;
182        }
183    
184        public void setDefinition(Definition definition) {
185            this.definition = definition;
186        }
187    
188        public String getKey() {
189            if (key == null) {
190                if (service == null) {
191                    throw new IllegalArgumentException("Endpoint: " + this + " has no service name defined");
192                }
193                if (endpoint == null) {
194                    throw new IllegalArgumentException("Endpoint: " + this + " has no endpoint name defined");
195                }
196                key = EndpointSupport.getKey(service, endpoint);
197            }
198            return key;
199        }
200    
201    }