1 17 package org.apache.servicemix.wsn.component; 18 19 import java.io.File ; 20 import java.io.FilenameFilter ; 21 import java.net.MalformedURLException ; 22 import java.net.URL ; 23 import java.util.ArrayList ; 24 import java.util.Iterator ; 25 import java.util.List ; 26 27 import javax.jbi.management.DeploymentException; 28 import javax.jbi.management.LifeCycleMBean; 29 import javax.jbi.messaging.MessageExchange.Role; 30 import javax.xml.bind.JAXBContext; 31 import javax.xml.bind.JAXBException; 32 import javax.xml.namespace.QName ; 33 34 import org.apache.activemq.util.IdGenerator; 35 import org.apache.servicemix.common.AbstractDeployer; 36 import org.apache.servicemix.common.BaseComponent; 37 import org.apache.servicemix.common.Deployer; 38 import org.apache.servicemix.common.Endpoint; 39 import org.apache.servicemix.common.ExchangeProcessor; 40 import org.apache.servicemix.common.ServiceUnit; 41 import org.apache.servicemix.wsn.EndpointManager; 42 import org.apache.servicemix.wsn.EndpointRegistrationException; 43 import org.apache.servicemix.wsn.jaxws.NotificationBroker; 44 import org.apache.servicemix.wsn.jbi.JbiNotificationBroker; 45 import org.apache.servicemix.wsn.jms.JmsCreatePullPoint; 46 import org.oasis_open.docs.wsn.b_2.CreatePullPoint; 47 import org.oasis_open.docs.wsn.b_2.CreatePullPointResponse; 48 import org.oasis_open.docs.wsn.b_2.Subscribe; 49 import org.oasis_open.docs.wsn.b_2.SubscribeResponse; 50 import org.oasis_open.docs.wsn.br_2.RegisterPublisher; 51 import org.oasis_open.docs.wsn.br_2.RegisterPublisherResponse; 52 53 public class WSNDeployer extends AbstractDeployer implements Deployer { 54 55 protected FilenameFilter filter; 56 protected JAXBContext context; 57 58 public WSNDeployer(BaseComponent component) { 59 super(component); 60 filter = new XmlFilter(); 61 try { 62 context = WSNEndpoint.createJAXBContext(NotificationBroker.class); 63 } catch (JAXBException e) { 64 throw new RuntimeException ("Could not create jaxb context", e); 65 } 66 } 67 68 public boolean canDeploy(String serviceUnitName, String serviceUnitRootPath) { 69 File [] xmls = new File (serviceUnitRootPath).listFiles(filter); 70 return xmls != null && xmls.length > 0; 71 } 72 73 public ServiceUnit deploy(String serviceUnitName, String serviceUnitRootPath) throws DeploymentException { 74 File [] xmls = new File (serviceUnitRootPath).listFiles(filter); 75 if (xmls == null || xmls.length == 0) { 76 throw failure("deploy", "No wsdl found", null); 77 } 78 WSNServiceUnit su = new WSNServiceUnit(); 79 su.setComponent(component); 80 su.setName(serviceUnitName); 81 su.setRootPath(serviceUnitRootPath); 82 for (int i = 0; i < xmls.length; i++) { 83 Endpoint ep; 84 URL url; 85 try { 86 url = xmls[i].toURL(); 87 } catch (MalformedURLException e) { 88 throw new DeploymentException("Error deploying xml file", e); 90 } 91 ep = createEndpoint(url); 92 ep.setServiceUnit(su); 93 su.addEndpoint(ep); 94 } 95 if (su.getEndpoints().size() == 0) { 96 throw failure("deploy", "Invalid wsdl: no endpoints found", null); 97 } 98 return su; 99 } 100 101 public Endpoint createEndpoint(URL url) throws DeploymentException { 102 Object request = null; 103 try { 104 request = context.createUnmarshaller().unmarshal(url); 105 } catch (JAXBException e) { 106 throw failure("deploy", "Invalid xml", e); 107 } 108 return createEndpoint(request); 109 } 110 111 public Endpoint createEndpoint(Object request) throws DeploymentException { 112 if (request instanceof Subscribe) { 113 return new WSNSubscriptionEndpoint((Subscribe) request); 114 } else if (request instanceof CreatePullPoint) { 115 return new WSNPullPointEndpoint((CreatePullPoint) request); 116 } else if (request instanceof RegisterPublisher) { 117 return new WSNPublisherEndpoint((RegisterPublisher) request); 118 } else { 119 throw failure("deploy", "Unsupported request " + request.getClass().getName(), null); 120 } 121 } 122 123 public class WSNSubscriptionEndpoint extends Endpoint implements EndpointManager { 124 125 private Subscribe request; 126 private SubscribeResponse response; 127 128 public WSNSubscriptionEndpoint(Subscribe request) throws DeploymentException { 129 this.service = new QName ("http://servicemix.org/wsnotification", "Subscription"); 130 this.endpoint = new IdGenerator().generateSanitizedId(); 131 this.request = request; 132 } 133 134 @Override 135 public Role getRole() { 136 return Role.CONSUMER; 137 } 138 139 @Override 140 public void activate() throws Exception { 141 JbiNotificationBroker broker = ((WSNLifeCycle) serviceUnit.getComponent().getLifeCycle()).getNotificationBroker(); 142 response = broker.handleSubscribe(request, this); 143 } 144 145 @Override 146 public void deactivate() throws Exception { 147 JbiNotificationBroker broker = ((WSNLifeCycle) serviceUnit.getComponent().getLifeCycle()).getNotificationBroker(); 148 broker.unsubscribe(response.getSubscriptionReference().getAddress().getValue()); 149 } 150 151 @Override 152 public ExchangeProcessor getProcessor() { 153 return null; 154 } 155 156 public Object register(String address, Object service) throws EndpointRegistrationException { 157 return null; 158 } 159 160 public void unregister(Object endpoint) throws EndpointRegistrationException { 161 } 162 163 } 164 165 public class WSNPullPointEndpoint extends Endpoint implements EndpointManager { 166 167 private CreatePullPoint request; 168 private CreatePullPointResponse response; 169 170 public WSNPullPointEndpoint(CreatePullPoint request) throws DeploymentException { 171 this.service = new QName ("http://servicemix.org/wsnotification", "Subscription"); 172 this.endpoint = new IdGenerator().generateSanitizedId(); 173 this.request = request; 174 } 175 176 @Override 177 public Role getRole() { 178 return Role.PROVIDER; 179 } 180 181 @Override 182 public void activate() throws Exception { 183 JmsCreatePullPoint createPullPoint = ((WSNLifeCycle) serviceUnit.getComponent().getLifeCycle()).getCreatePullPoint(); 184 response = createPullPoint.createPullPoint(request); 185 } 186 187 @Override 188 public void deactivate() throws Exception { 189 JmsCreatePullPoint createPullPoint = ((WSNLifeCycle) serviceUnit.getComponent().getLifeCycle()).getCreatePullPoint(); 190 createPullPoint.destroyPullPoint(response.getPullPoint().getAddress().getValue()); 191 } 192 193 @Override 194 public ExchangeProcessor getProcessor() { 195 return null; 196 } 197 198 public Object register(String address, Object service) throws EndpointRegistrationException { 199 return null; 200 } 201 202 public void unregister(Object endpoint) throws EndpointRegistrationException { 203 } 204 205 } 206 207 public static class WSNPublisherEndpoint extends Endpoint implements EndpointManager { 208 209 private RegisterPublisher request; 210 private RegisterPublisherResponse response; 211 212 public WSNPublisherEndpoint(RegisterPublisher request) { 213 this.service = new QName ("http://servicemix.org/wsnotification", "Publisher"); 214 this.endpoint = new IdGenerator().generateSanitizedId(); 215 this.request = request; 216 } 217 218 @Override 219 public Role getRole() { 220 return Role.CONSUMER; 221 } 222 223 @Override 224 public void activate() throws Exception { 225 JbiNotificationBroker broker = ((WSNLifeCycle) serviceUnit.getComponent().getLifeCycle()).getNotificationBroker(); 226 response = broker.handleRegisterPublisher(request, this); 227 } 228 229 @Override 230 public void deactivate() throws Exception { 231 JbiNotificationBroker broker = ((WSNLifeCycle) serviceUnit.getComponent().getLifeCycle()).getNotificationBroker(); 232 broker.unsubscribe(response.getPublisherRegistrationReference().getAddress().getValue()); 233 } 234 235 @Override 236 public ExchangeProcessor getProcessor() { 237 return null; 238 } 239 240 public Object register(String address, Object service) throws EndpointRegistrationException { 241 return null; 242 } 243 244 public void unregister(Object endpoint) throws EndpointRegistrationException { 245 } 246 247 } 248 249 public static class WSNServiceUnit extends ServiceUnit { 250 public void start() throws Exception { 251 List <Endpoint> activated = new ArrayList <Endpoint>(); 252 try { 253 for (Iterator iter = getEndpoints().iterator(); iter.hasNext();) { 254 Endpoint endpoint = (Endpoint) iter.next(); 255 if (endpoint instanceof WSNPullPointEndpoint) { 256 endpoint.activate(); 257 activated.add(endpoint); 258 } 259 } 260 for (Iterator iter = getEndpoints().iterator(); iter.hasNext();) { 261 Endpoint endpoint = (Endpoint) iter.next(); 262 if (endpoint instanceof WSNSubscriptionEndpoint) { 263 endpoint.activate(); 264 activated.add(endpoint); 265 } 266 } 267 this.status = LifeCycleMBean.STARTED; 268 } catch (Exception e) { 269 for (Endpoint endpoint : activated) { 271 try { 272 endpoint.deactivate(); 273 } catch (Exception e2) { 274 } 276 } 277 throw e; 278 } 279 } 280 } 281 282 public static class XmlFilter implements FilenameFilter { 283 284 public boolean accept(File dir, String name) { 285 return name.endsWith(".xml"); 286 } 287 288 } 289 290 } 291 | Popular Tags |