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.spring;
018    
019    import java.io.InputStream;
020    
021    import javax.xml.bind.JAXBContext;
022    import javax.xml.namespace.QName;
023    
024    import org.springframework.beans.factory.FactoryBean;
025    import org.springframework.beans.factory.InitializingBean;
026    import org.springframework.core.io.Resource;
027    import org.apache.servicemix.wsn.component.WSNDeployableEndpoint;
028    import org.apache.servicemix.wsn.component.WSNEndpoint;
029    import org.apache.servicemix.wsn.component.WSNSubscriptionEndpoint;
030    import org.apache.servicemix.wsn.component.WSNPullPointEndpoint;
031    import org.apache.servicemix.wsn.component.WSNPublisherEndpoint;
032    import org.oasis_open.docs.wsn.brw_2.NotificationBroker;
033    import org.oasis_open.docs.wsn.b_2.Subscribe;
034    import org.oasis_open.docs.wsn.b_2.CreatePullPoint;
035    import org.oasis_open.docs.wsn.br_2.RegisterPublisher;
036    
037    /**
038     * @org.apache.xbean.XBean element="endpoint"
039     */
040    public class WSNDeployableEndpointFactoryBean implements FactoryBean, InitializingBean {
041    
042        private QName service;
043        private String endpoint;
044        private Resource resource;
045        private Object request;
046    
047        public QName getService() {
048            return service;
049        }
050    
051        public void setService(QName service) {
052            this.service = service;
053        }
054    
055        public String getEndpoint() {
056            return endpoint;
057        }
058    
059        public void setEndpoint(String endpoint) {
060            this.endpoint = endpoint;
061        }
062    
063        public Resource getResource() {
064            return resource;
065        }
066    
067        public void setResource(Resource resource) {
068            this.resource = resource;
069        }
070    
071        public Object getRequest() {
072            return request;
073        }
074    
075        public void setRequest(Object request) {
076            this.request = request;
077        }
078    
079        public void afterPropertiesSet() throws Exception {
080            if (request == null && resource == null) {
081                throw new IllegalStateException("One of request or resource properties must be set");
082            }
083        }
084    
085        public Object getObject() throws Exception {
086            if (request == null && resource != null) {
087                JAXBContext context = WSNEndpoint.createJAXBContext(NotificationBroker.class);
088                InputStream is = resource.getInputStream();
089                try {
090                    request = context.createUnmarshaller().unmarshal(is);
091                } finally {
092                    is.close();
093                }
094            }
095            if (request instanceof Subscribe) {
096                return new WSNSubscriptionEndpoint((Subscribe) request, service, endpoint);
097            } else if (request instanceof CreatePullPoint) {
098                return new WSNPullPointEndpoint((CreatePullPoint) request, service, endpoint);
099            } else if (request instanceof RegisterPublisher) {
100                return new WSNPublisherEndpoint((RegisterPublisher) request, service, endpoint);
101            } else {
102                throw new IllegalStateException("Unrecognized request of type " + request.getClass());
103            }
104        }
105    
106        public Class getObjectType() {
107            return WSNDeployableEndpoint.class;
108        }
109    
110        public boolean isSingleton() {
111            return false;
112        }
113    }