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