1 22 package org.jboss.aop.deployment; 23 24 import java.io.File ; 25 import java.net.URL ; 26 import java.util.Iterator ; 27 28 import javax.management.MBeanServer ; 29 import javax.management.MalformedObjectNameException ; 30 import javax.management.Notification ; 31 import javax.management.ObjectName ; 32 33 import org.jboss.aop.AspectAnnotationLoader; 34 import org.jboss.aop.AspectManager; 35 import org.jboss.aop.AspectXmlLoader; 36 import org.jboss.deployment.DeploymentException; 37 import org.jboss.deployment.DeploymentInfo; 38 import org.jboss.deployment.DeploymentState; 39 import org.jboss.deployment.SubDeployer; 40 import org.jboss.deployment.SubDeployerSupport; 41 import org.jboss.util.file.ArchiveBrowser; 42 import org.jboss.util.file.ClassFileFilter; 43 44 53 public class AspectDeployer 54 extends SubDeployerSupport 55 implements SubDeployer, AspectDeployerMBean 56 { 57 58 63 public AspectDeployer() 64 { 65 initializeMainDeployer(); 66 } 67 68 74 protected void initializeMainDeployer() 75 { 76 setSuffixes(new String []{".aop", "-aop.xml"}); 77 setRelativeOrder(100); 78 } 79 80 86 public boolean accepts(DeploymentInfo di) 87 { 88 String urlStr = di.url.toString(); 89 return urlStr.endsWith(".aop") || urlStr.endsWith(".aop/") || 90 urlStr.endsWith("-aop.xml"); 91 } 92 93 100 public void init(DeploymentInfo di) 101 throws DeploymentException 102 { 103 try 104 { 105 if (di.watch == null) 106 { 107 if (di.url.getProtocol().equals("file")) 109 { 110 File file = new File (di.url.getFile()); 111 112 if (!file.isDirectory()) 114 { 115 di.watch = di.url; 116 } 117 else 119 { 120 di.watch = new URL (di.url, "META-INF/jboss-aop.xml"); 121 } 122 } 123 else 124 { 125 di.watch = di.url; 127 } 128 } 129 } 130 catch (Exception e) 131 { 132 log.error("failed to parse AOP document: ", e); 133 throw new DeploymentException(e); 134 } 135 super.init(di); 136 } 137 138 145 public void create(DeploymentInfo di) 146 throws DeploymentException 147 { 148 ClassLoader old = Thread.currentThread().getContextClassLoader(); 149 try 150 { 151 URL docURL = getDocUrl(di); 152 ClassLoader scl = getScopedClassLoader(di, docURL); 153 154 if (scl != null) 155 { 156 log.info("AOP deployment is scoped using classloader " + scl); 157 } 158 159 Thread.currentThread().setContextClassLoader(di.ucl); 160 if (!di.isXML) 161 { 162 Iterator it = ArchiveBrowser.getBrowser(di.localUrl, new ClassFileFilter()); 163 AspectManager manager = (scl != null) ? AspectManager.instance(scl) : AspectManager.instance(); 164 AspectAnnotationLoader loader = new AspectAnnotationLoader(manager); 165 loader.setClassLoader(scl); 166 loader.deployInputStreamIterator(it); 167 } 168 169 AspectXmlLoader.deployXML(docURL, scl); 170 Notification msg = new Notification ("AOP Deploy", this, getNextNotificationSequenceNumber()); 171 sendNotification(msg); 172 log.debug("Deployed AOP: " + di.url); 173 } 174 catch (Exception ex) 175 { 176 ex.printStackTrace(); 177 throw new DeploymentException(ex); 178 } 179 finally 180 { 181 Thread.currentThread().setContextClassLoader(old); 182 } 183 } 184 185 192 public void start(DeploymentInfo di) throws DeploymentException 193 { 194 } 195 196 207 public void stop(DeploymentInfo di) 208 { 210 if (di.state != DeploymentState.STARTED) 213 { 214 log.debug("Ignoring request to stop '" + di.url + "', current state: " + di.state); 215 return; 216 } 217 218 log.debug("undeploying document " + di.url); 219 ClassLoader old = Thread.currentThread().getContextClassLoader(); 220 try 221 { 222 Thread.currentThread().setContextClassLoader(di.ucl); 223 if (!di.isXML) 224 { 225 Iterator it = ArchiveBrowser.getBrowser(di.localUrl, new ClassFileFilter()); 226 AspectAnnotationLoader loader = new AspectAnnotationLoader(AspectManager.instance()); 227 loader.undeployInputStreamIterator(it); 228 229 } 230 231 URL docURL = getDocUrl(di); 232 AspectXmlLoader.undeployXML(docURL); 234 AspectManager.instance().unregisterClassLoader(di.ucl); 235 236 241 Notification msg = new Notification ("AOP Undeploy", this, getNextNotificationSequenceNumber()); 242 sendNotification(msg); 243 } 244 catch (Exception ex) 245 { 246 log.error("failed to stop", ex); 247 } 248 finally 249 { 250 Thread.currentThread().setContextClassLoader(old); 251 } 252 } 253 254 260 public void destroy(DeploymentInfo di) 261 { 263 } 264 265 271 protected void startService() throws Exception 272 { 273 super.startService(); 274 } 275 276 protected ObjectName getObjectName(MBeanServer server, ObjectName name) 277 throws MalformedObjectNameException 278 { 279 return name == null ? OBJECT_NAME : name; 280 } 281 282 private URL getDocUrl(DeploymentInfo di) throws DeploymentException 283 { 284 URL docURL = di.localUrl; 285 if (di.isXML == false) 286 docURL = di.localCl.findResource("META-INF/jboss-aop.xml"); 287 if (docURL == null) 289 throw new DeploymentException("Failed to find META-INF/jboss-aop.xml"); 290 return docURL; 291 } 292 293 private ClassLoader getScopedClassLoader(DeploymentInfo di, URL docUrl) 294 { 295 if (JBossScopedClassLoaderHelper.isScopedClassLoader(di.ucl)) 299 { 300 return di.ucl; 301 } 302 303 return null; 304 } 305 } | Popular Tags |