1 17 package org.apache.servicemix.common; 18 19 import java.util.ArrayList ; 20 import java.util.Collection ; 21 import java.util.HashMap ; 22 import java.util.Iterator ; 23 import java.util.List ; 24 import java.util.Map ; 25 26 import javax.jbi.JBIException; 27 import javax.jbi.management.LifeCycleMBean; 28 29 public class ServiceUnit { 30 31 protected BaseComponent component; 32 protected String name; 33 protected String rootPath; 34 protected String status = LifeCycleMBean.SHUTDOWN; 35 protected Map endpoints = new HashMap (); 36 37 public ServiceUnit() { 38 } 39 40 public ServiceUnit(BaseComponent component) { 41 this.component = component; 42 } 43 44 public void start() throws Exception { 45 List activated = new ArrayList (); 47 try { 48 for (Iterator iter = getEndpoints().iterator(); iter.hasNext();) { 49 Endpoint endpoint = (Endpoint) iter.next(); 50 endpoint.activate(); 51 activated.add(endpoint); 52 } 53 this.status = LifeCycleMBean.STARTED; 54 } catch (Exception e) { 55 for (Iterator iter = activated.iterator(); iter.hasNext();) { 57 try { 58 Endpoint endpoint = (Endpoint) iter.next(); 59 endpoint.deactivate(); 60 } catch (Exception e2) { 61 } 63 } 64 throw e; 65 } 66 } 67 68 public void stop() throws Exception { 69 this.status = LifeCycleMBean.STOPPED; 70 Exception exception = null; 72 for (Iterator iter = getEndpoints().iterator(); iter.hasNext();) { 73 Endpoint endpoint = (Endpoint) iter.next(); 74 try { 75 endpoint.deactivate(); 76 } catch (Exception e) { 77 exception = e; 78 } 79 } 80 if (exception != null) { 81 throw exception; 82 } 83 } 84 85 public void shutDown() throws JBIException { 86 this.status = LifeCycleMBean.SHUTDOWN; 87 } 88 89 public String getCurrentState() { 90 return status; 91 } 92 93 public String getName() { 94 return name; 95 } 96 97 public void setName(String name) { 98 this.name = name; 99 } 100 101 public String getRootPath() { 102 return rootPath; 103 } 104 105 public void setRootPath(String rootPath) { 106 this.rootPath = rootPath; 107 } 108 109 112 public BaseComponent getComponent() { 113 return component; 114 } 115 116 119 public void setComponent(BaseComponent component) { 120 this.component = component; 121 } 122 123 public Collection getEndpoints() { 124 return this.endpoints.values(); 125 } 126 127 public void addEndpoint(Endpoint endpoint) { 128 String key = EndpointSupport.getKey(endpoint); 129 if (this.endpoints.put(key, endpoint) != null) { 130 throw new IllegalStateException ("More than one endpoint found in the SU for key: " + key); 131 } 132 } 133 134 public Endpoint getEndpoint(String key) { 135 return (Endpoint) this.endpoints.get(key); 136 } 137 138 139 } 140 | Popular Tags |