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;
018    
019    import org.apache.commons.logging.Log;
020    import org.w3c.dom.Document;
021    
022    import javax.jbi.management.DeploymentException;
023    import javax.jbi.messaging.MessageExchange;
024    import javax.jbi.messaging.MessageExchange.Role;
025    import javax.wsdl.Definition;
026    import javax.xml.namespace.QName;
027    
028    public abstract class Endpoint {
029    
030        protected QName service;
031        protected String endpoint;
032        protected QName interfaceName;
033        protected Document description;
034        protected Definition definition;
035        protected ServiceUnit serviceUnit;
036        protected Log logger;
037        private String key;
038        
039        public Endpoint() {
040        }
041        
042        public Endpoint(ServiceUnit serviceUnit, QName service, String endpoint) {
043            this.serviceUnit = serviceUnit;
044            this.logger = serviceUnit.getComponent().getLogger();
045            this.service = service;
046            this.endpoint = endpoint;
047        }
048        
049        /**
050         * @return Returns the endpoint.
051         */
052        public String getEndpoint() {
053            return endpoint;
054        }
055        /**
056         * @param endpoint The endpoint to set.
057         */
058        public void setEndpoint(String endpoint) {
059            this.endpoint = endpoint;
060            this.key = null;
061        }
062        /**
063         * @return Returns the service.
064         */
065        public QName getService() {
066            return service;
067        }
068        /**
069         * @param service The service to set.
070         */
071        public void setService(QName service) {
072            this.service = service;
073            this.key = null;
074        }
075        /**
076         * @return Returns the role.
077         */
078        public abstract Role getRole();
079        /**
080         * @return Returns the description.
081         */
082        public Document getDescription() {
083            return description;
084        }
085        /**
086         * @param description The description to set.
087         */
088        public void setDescription(Document description) {
089            this.description = description;
090        }
091        /**
092         * @return Returns the interfaceName.
093         */
094        public QName getInterfaceName() {
095            return interfaceName;
096        }
097        /**
098         * @param interfaceName The interfaceName to set.
099         */
100        public void setInterfaceName(QName interfaceName) {
101            this.interfaceName = interfaceName;
102        }
103        /**
104         * @return Returns the serviceUnit.
105         */
106        public ServiceUnit getServiceUnit() {
107            return serviceUnit;
108        }
109    
110        /**
111         * @param serviceUnit The serviceUnit to set.
112         */
113        public void setServiceUnit(ServiceUnit serviceUnit) {
114            this.serviceUnit = serviceUnit;
115            this.logger = serviceUnit.component.getLogger();
116        }
117    
118        public boolean isExchangeOkay(MessageExchange exchange) {
119            // TODO: We could check the MEP here 
120            return true;
121        }
122    
123        public abstract void activate() throws Exception;
124        
125        public abstract void deactivate() throws Exception;
126    
127        public abstract ExchangeProcessor getProcessor();
128        
129        public String toString() {
130            return "Endpoint[service: " + service + ", " + 
131                            "endpoint: " + endpoint + ", " +
132                            "role: " + (getRole() == Role.PROVIDER ? "provider" : "consumer") + "]";
133        }
134    
135        /**
136         * Validate the endpoint at either deployment time for statically defined endpoints or at runtime for dynamic endpoints
137         * 
138         * @throws DeploymentException
139         */
140        public void validate() throws DeploymentException {
141        }
142        
143        public Definition getDefinition() {
144            return definition;
145        }
146    
147        public void setDefinition(Definition definition) {
148            this.definition = definition;
149        }
150        
151        String getKey() {
152            if (key == null) {
153                if (service == null) {
154                    throw new IllegalArgumentException("Endpoint: " + this + " has no service name defined");
155                }
156                if (endpoint == null) {
157                    throw new IllegalArgumentException("Endpoint: " + this + " has no endpoint name defined");
158                }
159                key = EndpointSupport.getKey(service, endpoint);
160            }
161            return key;
162        }
163    
164    }