1 22 package org.jboss.test.kernel.deployment.jboss.beans.dependspojo; 23 24 import java.io.Serializable ; 25 26 import javax.naming.InitialContext ; 27 import javax.naming.NamingException ; 28 29 import org.jboss.naming.Util; 30 31 37 public class SimpleService implements Serializable 38 { 39 40 private static final long serialVersionUID = 1L; 41 42 public static final String BASE_JNDI_NAME = "test/kernel/deployment/depends/pojo"; 43 44 public static final String CONSTRUCTED = "CONSTRUCTED"; 45 public static final String CREATED = "CREATED"; 46 public static final String STARTED = "STARTED"; 47 public static final String STOPPED = "STOPPED"; 48 public static final String DESTROYED = "DESTROYED"; 49 public static final String FAILED = "FAILED"; 50 51 53 protected transient InitialContext ctx; 54 55 protected String name; 56 protected String [] depends; 57 protected String state = CONSTRUCTED; 58 59 61 public SimpleService(String name) throws NamingException 62 { 63 ctx = new InitialContext (); 65 66 this.name = name; 67 68 log("CTOR"); 69 } 70 71 73 public String getName() 74 { 75 return name; 76 } 77 78 public String [] getDepends() 79 { 80 return depends; 81 } 82 83 public String getState() 84 { 85 return state; 86 } 87 88 public void setDepends(String [] depends) throws NamingException 89 { 90 log("setDepends(" + depends + ")"); 91 this.depends = depends; 92 93 if (depends == null) 94 { 95 Util.unbind(ctx, getJndiName(name)); 96 log("Destroyed binding: " + name); 97 } 98 else 99 { 100 Util.rebind(ctx, getJndiName(name), this); 101 log("Created binding: " + name); 102 } 103 } 104 105 107 public void create() throws Exception 108 { 109 log("create()"); 110 state = CREATED; 111 checkDependencies(CREATED); 112 } 113 114 public void start() throws Exception 115 { 116 log("start()"); 117 state = STARTED; 118 checkDependencies(STARTED); 119 } 120 121 public void stop() throws Exception 122 { 123 log("stop()"); 124 state = STOPPED; 125 checkDependencies(STARTED); 126 } 127 128 public void destroy() throws Exception 129 { 130 log("destroy()"); 131 state = DESTROYED; 132 checkDependencies(STOPPED); 133 } 134 135 137 protected void log(Object message) 138 { 139 System.out.println(getName() + " - " + message); 140 } 141 142 protected String getJndiName(String name) 143 { 144 return BASE_JNDI_NAME + '/' + name; 145 } 146 147 protected void checkDependencies(String desiredState) 148 { 149 if (depends != null) 150 { 151 for (int i = 0; i < depends.length; i++) 152 { 153 try 154 { 155 SimpleService other = (SimpleService)ctx.lookup(getJndiName(depends[i])); 156 if (other.getState().equals(desiredState)) 157 { 158 log(depends[i] + " is " + other.getState()); 159 } 160 else 161 { 162 throw new IllegalStateException (depends[i] + " not " + desiredState); 163 } 164 } 165 catch (NamingException e) 166 { 167 throw new RuntimeException (depends[i] + " not bound\n"); 168 } 169 } 170 } 171 } 172 173 175 public String toString() 176 { 177 StringBuffer sbuf = new StringBuffer (); 178 sbuf 179 .append(getClass().getName()) 180 .append("[ name=").append(name) 181 .append(", state=").append(state); 182 if (depends != null) 183 { 184 sbuf.append(", depends=["); 185 for (int i = 0; i < depends.length; i++) 186 sbuf.append(' ').append(depends[i]); 187 sbuf.append(" ]"); 188 } 189 sbuf.append(" ]"); 190 191 return sbuf.toString(); 192 } 193 } 194 | Popular Tags |