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.util.List;
020 import java.util.ArrayList;
021 import java.util.Iterator;
022
023 import javax.jbi.management.LifeCycleMBean;
024
025 import org.apache.servicemix.common.ServiceUnit;
026 import org.apache.servicemix.common.Endpoint;
027 import org.apache.servicemix.common.DefaultServiceUnit;
028
029 public class WSNServiceUnit extends DefaultServiceUnit {
030
031 @Override
032 public void init() throws Exception {
033 if (this.status == LifeCycleMBean.SHUTDOWN) {
034 List<Endpoint> activated = new ArrayList<Endpoint>();
035 try {
036 for (Iterator iter = getEndpoints().iterator(); iter.hasNext();) {
037 Endpoint endpoint = (Endpoint) iter.next();
038 if (endpoint instanceof WSNPullPointEndpoint) {
039 endpoint.activate();
040 activated.add(endpoint);
041 }
042 }
043 for (Iterator iter = getEndpoints().iterator(); iter.hasNext();) {
044 Endpoint endpoint = (Endpoint) iter.next();
045 if (endpoint instanceof WSNSubscriptionEndpoint) {
046 endpoint.activate();
047 activated.add(endpoint);
048 }
049 }
050 for (Iterator iter = getEndpoints().iterator(); iter.hasNext();) {
051 Endpoint endpoint = (Endpoint) iter.next();
052 if (endpoint instanceof WSNPublisherEndpoint) {
053 endpoint.activate();
054 activated.add(endpoint);
055 }
056 }
057 this.status = LifeCycleMBean.STOPPED;
058 } catch (Exception e) {
059 // Deactivate activated endpoints
060 for (Endpoint endpoint : activated) {
061 try {
062 endpoint.deactivate();
063 } catch (Exception e2) {
064 // do nothing
065 }
066 }
067 throw e;
068 }
069 }
070 }
071
072 @Override
073 public void start() throws Exception {
074 if (this.status == LifeCycleMBean.STOPPED) {
075 List<Endpoint> started = new ArrayList<Endpoint>();
076 try {
077 for (Iterator iter = getEndpoints().iterator(); iter.hasNext();) {
078 Endpoint endpoint = (Endpoint) iter.next();
079 if (endpoint instanceof WSNPullPointEndpoint) {
080 endpoint.start();
081 started.add(endpoint);
082 }
083 }
084 for (Iterator iter = getEndpoints().iterator(); iter.hasNext();) {
085 Endpoint endpoint = (Endpoint) iter.next();
086 if (endpoint instanceof WSNSubscriptionEndpoint) {
087 endpoint.start();
088 started.add(endpoint);
089 }
090 }
091 for (Iterator iter = getEndpoints().iterator(); iter.hasNext();) {
092 Endpoint endpoint = (Endpoint) iter.next();
093 if (endpoint instanceof WSNPublisherEndpoint) {
094 endpoint.start();
095 started.add(endpoint);
096 }
097 }
098 this.status = LifeCycleMBean.STARTED;
099 } catch (Exception e) {
100 // Deactivate activated endpoints
101 for (Endpoint endpoint : started) {
102 try {
103 endpoint.stop();
104 } catch (Exception e2) {
105 // do nothing
106 }
107 }
108 throw e;
109 }
110 }
111 }
112
113 }