1 7 package org.jboss.jms.client.container; 8 9 import java.util.HashSet ; 10 import java.util.Iterator ; 11 import java.util.Set ; 12 13 import org.jboss.aop.advice.Interceptor; 14 import org.jboss.aop.joinpoint.Invocation; 15 import org.jboss.aop.joinpoint.MethodInvocation; 16 import org.jboss.jms.client.Lifecycle; 17 import org.jboss.jms.container.Container; 18 19 26 public class ClosedInterceptor 27 implements Interceptor 28 { 29 31 32 private static final int NOT_CLOSED = 0; 33 34 35 private static final int IN_CLOSING = 1; 36 37 38 private static final int CLOSING = 2; 39 40 41 private static final int IN_CLOSE = 3; 42 43 44 private static final int CLOSED = -1; 45 46 48 49 private int state = NOT_CLOSED; 50 51 52 private int inuseCount = 0; 53 54 56 58 60 62 public String getName() 63 { 64 return "ClosedInterceptor"; 65 } 66 67 public Object invoke(Invocation invocation) throws Throwable 68 { 69 String methodName = ((MethodInvocation) invocation).getMethod().getName(); 70 boolean isClosing = methodName.equals("closing"); 71 boolean isClose = methodName.equals("close"); 72 73 if (isClosing) 74 { 75 if (checkClosingAlreadyDone()) 76 return null; 77 } 78 else if (isClose) 79 { 80 if(checkCloseAlreadyDone()) 81 return null; 82 } 83 else 84 inuse(); 85 86 if (isClosing) 87 maintainRelatives(invocation); 88 89 try 90 { 91 return invocation.invokeNext(); 92 } 93 finally 94 { 95 if (isClosing) 96 closing(); 97 else if (isClose) 98 closed(); 99 else 100 done(); 101 } 102 } 103 104 106 111 protected synchronized boolean checkClosingAlreadyDone() 112 throws Throwable 113 { 114 if (state != NOT_CLOSED) 115 return true; 116 state = IN_CLOSING; 117 return false; 118 } 119 120 123 protected synchronized void closing() 124 throws Throwable 125 { 126 state = CLOSING; 127 } 128 129 135 protected synchronized boolean checkCloseAlreadyDone() 136 throws Throwable 137 { 138 if (state != CLOSING) 139 return true; 140 while (inuseCount > 0) 141 wait(); 142 state = IN_CLOSE; 143 return false; 144 } 145 146 149 protected synchronized void closed() 150 throws Throwable 151 { 152 state = CLOSED; 153 } 154 155 158 protected synchronized void inuse() 159 throws Throwable 160 { 161 if (state != NOT_CLOSED) 162 throw new IllegalStateException ("Already closed"); 163 ++inuseCount; 164 } 165 166 169 protected synchronized void done() 170 throws Throwable 171 { 172 if (--inuseCount == 0) 173 notifyAll(); 174 } 175 176 181 protected void maintainRelatives(Invocation invocation) 182 { 183 Container container = Container.getContainer(invocation); 186 Set clone = null; 187 Set children = container.getChildren(); 188 synchronized (children) 189 { 190 clone = new HashSet (children); 191 } 192 193 for (Iterator i = clone.iterator(); i.hasNext();) 196 { 197 Container childContainer = (Container) i.next(); 198 Lifecycle child = (Lifecycle) childContainer.getProxy(); 199 try 200 { 201 child.closing(); 202 child.close(); 203 } 204 catch (Throwable ignored) 205 { 206 } 208 } 209 210 Container parent = container.getParent(); 212 if (parent != null) 213 parent.removeChild(container); 214 } 215 216 218 220 222 } 223 | Popular Tags |