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 javax.jbi.servicedesc.ServiceEndpoint;
020    import javax.xml.namespace.QName;
021    import javax.xml.parsers.DocumentBuilderFactory;
022    
023    import org.w3c.dom.Document;
024    import org.w3c.dom.DocumentFragment;
025    import org.w3c.dom.Element;
026    import org.w3c.dom.Text;
027    
028    public class ExternalEndpoint implements ServiceEndpoint {
029    
030        private QName eprElement;
031        private String locationUri;
032        private QName serviceName;
033        private String endpointName;
034        private QName[] interfaces = null;
035        
036        public ExternalEndpoint(QName eprElement, 
037                                String locationUri,
038                                QName serviceName, 
039                                String epName, 
040                                QName[] interfaces) {
041            this.eprElement = eprElement;
042            this.locationUri = locationUri;
043            this.endpointName = epName;
044            this.serviceName = serviceName;
045            this.interfaces = interfaces;
046        }
047    
048        public ExternalEndpoint(QName eprElement, 
049                                String locationUri,
050                                QName serviceName, 
051                                String epName, 
052                                QName interfaceName) {
053            this.eprElement = eprElement;
054            this.locationUri = locationUri;
055            this.endpointName = epName;
056            this.serviceName = serviceName;
057            if (interfaceName != null) {
058                this.interfaces = new QName[] { interfaceName };
059            }
060        }
061    
062        public DocumentFragment getAsReference(QName operationName) {
063            try {
064                DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
065                dbf.setNamespaceAware(true);
066                Document doc = dbf.newDocumentBuilder().newDocument();
067                DocumentFragment df = doc.createDocumentFragment();
068                Element e = doc.createElementNS(eprElement.getNamespaceURI(), eprElement.getLocalPart());
069                Text t = doc.createTextNode(locationUri);
070                e.appendChild(t);
071                df.appendChild(e);
072                return df;
073            } catch (Exception e) {
074                throw new RuntimeException("Could not create reference", e);
075            }
076        }
077    
078        public QName getServiceName() {
079            return serviceName;
080        }
081    
082        public String getEndpointName() {
083            return endpointName;
084        }
085    
086        public QName[] getInterfaces() {
087            return interfaces;
088        }
089    
090    }