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.component;
018
019 import java.io.File;
020 import java.io.FilenameFilter;
021 import java.net.MalformedURLException;
022 import java.net.URL;
023
024 import javax.jbi.management.DeploymentException;
025 import javax.xml.bind.JAXBContext;
026 import javax.xml.bind.JAXBException;
027
028 import org.apache.servicemix.common.AbstractDeployer;
029 import org.apache.servicemix.common.Deployer;
030 import org.apache.servicemix.common.Endpoint;
031 import org.apache.servicemix.common.ServiceMixComponent;
032 import org.apache.servicemix.common.ServiceUnit;
033 import org.oasis_open.docs.wsn.b_2.CreatePullPoint;
034 import org.oasis_open.docs.wsn.b_2.Subscribe;
035 import org.oasis_open.docs.wsn.br_2.RegisterPublisher;
036 import org.oasis_open.docs.wsn.brw_2.NotificationBroker;
037
038 public class WSNDeployer extends AbstractDeployer implements Deployer {
039
040 protected FilenameFilter filter;
041
042 protected JAXBContext context;
043
044 public WSNDeployer(ServiceMixComponent component) {
045 super(component);
046 filter = new XmlFilter();
047 try {
048 context = WSNEndpoint.createJAXBContext(NotificationBroker.class);
049 } catch (JAXBException e) {
050 throw new RuntimeException("Could not create jaxb context", e);
051 }
052 }
053
054 public boolean canDeploy(String serviceUnitName, String serviceUnitRootPath) {
055 File[] xmls = new File(serviceUnitRootPath).listFiles(filter);
056 return xmls != null && xmls.length > 0;
057 }
058
059 public ServiceUnit deploy(String serviceUnitName, String serviceUnitRootPath) throws DeploymentException {
060 File[] xmls = new File(serviceUnitRootPath).listFiles(filter);
061 if (xmls == null || xmls.length == 0) {
062 throw failure("deploy", "No xml files found", null);
063 }
064 WSNServiceUnit su = new WSNServiceUnit();
065 su.setComponent(component);
066 su.setName(serviceUnitName);
067 su.setRootPath(serviceUnitRootPath);
068 for (int i = 0; i < xmls.length; i++) {
069 Endpoint ep;
070 URL url;
071 try {
072 url = xmls[i].toURL();
073 } catch (MalformedURLException e) {
074 // TODO Auto-generated catch block
075 throw new DeploymentException("Error deploying xml file", e);
076 }
077 ep = createEndpoint(url);
078 ep.setServiceUnit(su);
079 validate(ep);
080 su.addEndpoint(ep);
081 }
082 if (su.getEndpoints().size() == 0) {
083 throw failure("deploy", "Invalid wsdl: no endpoints found", null);
084 }
085 return su;
086 }
087
088 public Endpoint createEndpoint(URL url) throws DeploymentException {
089 Object request = null;
090 try {
091 request = context.createUnmarshaller().unmarshal(url);
092 } catch (JAXBException e) {
093 throw failure("deploy", "Invalid xml", e);
094 }
095 return createEndpoint(request);
096 }
097
098 public Endpoint createEndpoint(Object request) throws DeploymentException {
099 if (request instanceof Subscribe) {
100 return new WSNSubscriptionEndpoint((Subscribe) request);
101 } else if (request instanceof CreatePullPoint) {
102 return new WSNPullPointEndpoint((CreatePullPoint) request);
103 } else if (request instanceof RegisterPublisher) {
104 return new WSNPublisherEndpoint((RegisterPublisher) request);
105 } else {
106 throw failure("deploy", "Unsupported request " + request.getClass().getName(), null);
107 }
108 }
109
110 public static class XmlFilter implements FilenameFilter {
111
112 public boolean accept(File dir, String name) {
113 return name.endsWith(".xml");
114 }
115
116 }
117
118 }