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    
028    import org.w3c.dom.Element;
029    
030    import org.apache.activemq.util.IdGenerator;
031    import org.apache.commons.logging.Log;
032    import org.apache.commons.logging.LogFactory;
033    import org.apache.servicemix.jbi.util.DOMUtil;
034    import org.apache.servicemix.wsn.jaxws.CreatePullPoint;
035    import org.apache.servicemix.wsn.jaxws.UnableToCreatePullPointFault;
036    import org.apache.servicemix.wsn.jaxws.UnableToDestroyPullPointFault;
037    import org.oasis_open.docs.wsn.b_2.CreatePullPointResponse;
038    import org.oasis_open.docs.wsn.b_2.UnableToCreatePullPointFaultType;
039    
040    @WebService(endpointInterface = "org.apache.servicemix.wsn.jaxws.CreatePullPoint")
041    public abstract class AbstractCreatePullPoint extends AbstractEndpoint implements CreatePullPoint {
042    
043        private static Log log = LogFactory.getLog(AbstractCreatePullPoint.class);
044    
045        private IdGenerator idGenerator;
046    
047        private Map<String, AbstractPullPoint> pullPoints;
048    
049        public AbstractCreatePullPoint(String name) {
050            super(name);
051            idGenerator = new IdGenerator();
052            pullPoints = new ConcurrentHashMap<String, AbstractPullPoint>();
053        }
054    
055        public void init() throws Exception {
056            register();
057        }
058    
059        public void destroy() throws Exception {
060            unregister();
061        }
062    
063        @Override
064        protected String createAddress() {
065            return "http://servicemix.org/wsnotification/CreatePullPoint/" + getName();
066        }
067    
068        @WebMethod(operationName = "CreatePullPoint")
069        @WebResult(name = "CreatePullPointResponse", 
070                   targetNamespace = "http://docs.oasis-open.org/wsn/b-2", 
071                   partName = "CreatePullPointResponse")
072        public CreatePullPointResponse createPullPoint(
073                @WebParam(name = "CreatePullPoint", 
074                          targetNamespace = "http://docs.oasis-open.org/wsn/b-2", 
075                          partName = "CreatePullPointRequest")
076                org.oasis_open.docs.wsn.b_2.CreatePullPoint createPullPointRequest) throws UnableToCreatePullPointFault {
077    
078            log.debug("CreatePullEndpoint");
079            return handleCreatePullPoint(createPullPointRequest, null);
080        }
081    
082        public CreatePullPointResponse handleCreatePullPoint(
083                org.oasis_open.docs.wsn.b_2.CreatePullPoint createPullPointRequest, 
084                EndpointManager manager) throws UnableToCreatePullPointFault {
085            AbstractPullPoint pullPoint = null;
086            boolean success = false;
087            try {
088                pullPoint = createPullPoint(idGenerator.generateSanitizedId());
089                for (Iterator it = createPullPointRequest.getAny().iterator(); it.hasNext();) {
090                    Element el = (Element) it.next();
091                    if ("address".equals(el.getLocalName())
092                            && "http://servicemix.apache.org/wsn2005/1.0".equals(el.getNamespaceURI())) {
093                        String address = DOMUtil.getElementText(el).trim();
094                        pullPoint.setAddress(address);
095                    }
096                }
097                pullPoint.setCreatePullPoint(this);
098                pullPoints.put(pullPoint.getAddress(), pullPoint);
099                pullPoint.create(createPullPointRequest);
100                if (manager != null) {
101                    pullPoint.setManager(manager);
102                }
103                pullPoint.register();
104                CreatePullPointResponse response = new CreatePullPointResponse();
105                response.setPullPoint(createEndpointReference(pullPoint.getAddress()));
106                success = true;
107                return response;
108            } catch (EndpointRegistrationException e) {
109                UnableToCreatePullPointFaultType fault = new UnableToCreatePullPointFaultType();
110                throw new UnableToCreatePullPointFault("Unable to register new endpoint", fault, e);
111            } finally {
112                if (!success && pullPoint != null) {
113                    pullPoints.remove(pullPoint.getAddress());
114                    try {
115                        pullPoint.destroy();
116                    } catch (UnableToDestroyPullPointFault e) {
117                        log.info("Error destroying pullPoint", e);
118                    }
119                }
120            }
121        }
122    
123        public void destroyPullPoint(String address) throws UnableToDestroyPullPointFault {
124            AbstractPullPoint pullPoint = pullPoints.remove(address);
125            if (pullPoint != null) {
126                pullPoint.destroy();
127            }
128        }
129    
130        protected abstract AbstractPullPoint createPullPoint(String name);
131    
132    }