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.wsn;
018    
019    import java.util.Iterator;
020    import java.util.Map;
021    import java.util.concurrent.ConcurrentHashMap;
022    
023    import javax.jws.WebMethod;
024    import javax.jws.WebParam;
025    import javax.jws.WebResult;
026    import javax.jws.WebService;
027    import javax.xml.namespace.QName;
028    
029    import org.w3c.dom.Element;
030    
031    import org.apache.commons.logging.Log;
032    import org.apache.commons.logging.LogFactory;
033    import org.apache.servicemix.id.IdGenerator;
034    import org.apache.servicemix.common.util.DOMUtil;
035    import org.apache.servicemix.wsn.client.AbstractWSAClient;
036    import org.oasis_open.docs.wsn.b_2.CreatePullPointResponse;
037    import org.oasis_open.docs.wsn.b_2.UnableToCreatePullPointFaultType;
038    import org.oasis_open.docs.wsn.bw_2.CreatePullPoint;
039    import org.oasis_open.docs.wsn.bw_2.UnableToCreatePullPointFault;
040    import org.oasis_open.docs.wsn.bw_2.UnableToDestroyPullPointFault;
041    
042    @WebService(endpointInterface = "org.oasis_open.docs.wsn.bw_2.CreatePullPoint")
043    public abstract class AbstractCreatePullPoint extends AbstractEndpoint implements CreatePullPoint {
044    
045        private static Log log = LogFactory.getLog(AbstractCreatePullPoint.class);
046    
047        private IdGenerator idGenerator;
048    
049        private Map<String, AbstractPullPoint> pullPoints;
050    
051        public AbstractCreatePullPoint(String name) {
052            super(name);
053            idGenerator = new IdGenerator();
054            pullPoints = new ConcurrentHashMap<String, AbstractPullPoint>();
055        }
056    
057        public void init() throws Exception {
058            register();
059        }
060    
061        public void destroy() throws Exception {
062            unregister();
063        }
064    
065        @Override
066        protected String createAddress() {
067            return "http://servicemix.org/wsnotification/CreatePullPoint/" + getName();
068        }
069    
070        @WebMethod(operationName = "CreatePullPoint")
071        @WebResult(name = "CreatePullPointResponse", 
072                   targetNamespace = "http://docs.oasis-open.org/wsn/b-2", 
073                   partName = "CreatePullPointResponse")
074        public CreatePullPointResponse createPullPoint(
075                @WebParam(name = "CreatePullPoint", 
076                          targetNamespace = "http://docs.oasis-open.org/wsn/b-2", 
077                          partName = "CreatePullPointRequest")
078                org.oasis_open.docs.wsn.b_2.CreatePullPoint createPullPointRequest) throws UnableToCreatePullPointFault {
079    
080            log.debug("CreatePullEndpoint");
081            return handleCreatePullPoint(createPullPointRequest, null);
082        }
083    
084        public CreatePullPointResponse handleCreatePullPoint(
085                org.oasis_open.docs.wsn.b_2.CreatePullPoint createPullPointRequest, 
086                EndpointManager manager) throws UnableToCreatePullPointFault {
087            AbstractPullPoint pullPoint = null;
088            boolean success = false;
089            try {
090                pullPoint = createPullPoint(createPullPointName(createPullPointRequest));
091                for (Iterator it = createPullPointRequest.getAny().iterator(); it.hasNext();) {
092                    Element el = (Element) it.next();
093                    if ("address".equals(el.getLocalName())
094                            && "http://servicemix.apache.org/wsn2005/1.0".equals(el.getNamespaceURI())) {
095                        String address = DOMUtil.getElementText(el).trim();
096                        pullPoint.setAddress(address);
097                    }
098                }
099                pullPoint.setCreatePullPoint(this);
100                pullPoints.put(pullPoint.getAddress(), pullPoint);
101                pullPoint.create(createPullPointRequest);
102                if (manager != null) {
103                    pullPoint.setManager(manager);
104                }
105                pullPoint.register();
106                CreatePullPointResponse response = new CreatePullPointResponse();
107                response.setPullPoint(AbstractWSAClient.createWSA(pullPoint.getAddress()));
108                success = true;
109                return response;
110            } catch (EndpointRegistrationException e) {
111                log.warn("Unable to register new endpoint", e);
112                UnableToCreatePullPointFaultType fault = new UnableToCreatePullPointFaultType();
113                throw new UnableToCreatePullPointFault("Unable to register new endpoint", fault, e);
114            } finally {
115                if (!success && pullPoint != null) {
116                    pullPoints.remove(pullPoint.getAddress());
117                    try {
118                        pullPoint.destroy();
119                    } catch (UnableToDestroyPullPointFault e) {
120                        log.info("Error destroying pullPoint", e);
121                    }
122                }
123            }
124        }
125    
126        protected String createPullPointName(org.oasis_open.docs.wsn.b_2.CreatePullPoint createPullPointRequest) {
127            // Let the creator decide which pull point name to use
128            String name = null;
129            for (Iterator it = createPullPointRequest.getAny().iterator(); it.hasNext();) {
130                Element el = (Element) it.next();
131                if ("name".equals(el.getLocalName())
132                        && "http://servicemix.apache.org/wsn2005/1.0".equals(el.getNamespaceURI())) {
133                    name = DOMUtil.getElementText(el).trim();
134                }
135            }
136            if (name == null) {
137                // If no name is given, just generate one
138                name = idGenerator.generateSanitizedId();
139            }
140            return name;
141        }
142    
143        public void destroyPullPoint(String address) throws UnableToDestroyPullPointFault {
144            AbstractPullPoint pullPoint = pullPoints.remove(address);
145            if (pullPoint != null) {
146                pullPoint.destroy();
147            }
148        }
149    
150        protected abstract AbstractPullPoint createPullPoint(String name);
151    
152    }