1 22 package org.jboss.aop.deployment; 23 24 import java.net.URL ; 25 26 import javax.management.InstanceNotFoundException ; 27 import javax.management.MBeanServer ; 28 import javax.management.Notification ; 29 import javax.management.NotificationListener ; 30 import javax.management.ObjectName ; 31 32 import org.jboss.mx.loading.HeirarchicalLoaderRepository3; 33 import org.jboss.mx.loading.LoaderRepository; 34 import org.jboss.mx.util.MBeanServerLocator; 35 36 41 public class LoaderRepositoryUrlUtil implements NotificationListener 42 { 43 final static MBeanServer SERVER; 44 final static ObjectName MAIN_LOADER_REPOSITORY_OBJECT_NAME; 45 final static LoaderRepository MAIN_LOADER_REPOSITORY; 46 static 47 { 48 SERVER = MBeanServerLocator.locateJBoss(); 49 try 50 { 51 MAIN_LOADER_REPOSITORY_OBJECT_NAME = new ObjectName ("JMImplementation:name=Default,service=LoaderRepository"); 52 MAIN_LOADER_REPOSITORY = (LoaderRepository)SERVER.invoke(MAIN_LOADER_REPOSITORY_OBJECT_NAME, "getInstance", new Object [0], new String [0]); 53 } 54 catch (Exception e) 55 { 56 throw new RuntimeException (e); 57 } 58 } 59 60 long currentSequenceNumber; 61 long lastSequenceNumber = -1; 62 URL [] urls; 63 64 public LoaderRepositoryUrlUtil() 65 { 66 try 67 { 68 SERVER.addNotificationListener(MAIN_LOADER_REPOSITORY_OBJECT_NAME, this, null, null); 69 } 70 catch (InstanceNotFoundException e) 71 { 72 throw new RuntimeException (e); 73 } 74 } 75 76 public synchronized void handleNotification(Notification notification, Object handback) 77 { 78 if (notification.getType().equals(LoaderRepository.CLASSLOADER_ADDED)) 79 { 80 currentSequenceNumber = notification.getSequenceNumber(); 81 } 82 else if (notification.getType().equals(LoaderRepository.CLASSLOADER_REMOVED)) 83 { 84 currentSequenceNumber = notification.getSequenceNumber(); 85 } 86 } 87 88 public synchronized UrlInfo getURLInfo(HeirarchicalLoaderRepository3 scopedLoader, UrlInfo urlInfo) 89 { 90 boolean changed = false; 91 if (lastSequenceNumber != currentSequenceNumber) 92 { 93 urls = MAIN_LOADER_REPOSITORY.getURLs(); 94 lastSequenceNumber = currentSequenceNumber; 95 changed = true; 96 } 97 if (!changed) 98 { 99 changed = urlInfo != null && (urlInfo.getSequenceNumber() != lastSequenceNumber); 100 } 101 if (urlInfo == null || changed) 102 { 103 URL [] localUrls = getLocalUrls(scopedLoader, urls); 104 urlInfo = new UrlInfo(urls, localUrls, lastSequenceNumber); 105 } 106 return urlInfo; 107 } 108 109 public long getCurrentSequenceNumber() 110 { 111 return currentSequenceNumber; 112 } 113 114 private URL [] getLocalUrls(HeirarchicalLoaderRepository3 scopedRepository, URL [] globalUrls) 115 { 116 URL [] scopedRepositoryUrls = scopedRepository.getURLs(); 117 118 int scopedLength = 0; 121 for (int i = 0 ; i < scopedRepositoryUrls.length ; i++) 122 { 123 URL scopedUrl = scopedRepositoryUrls[i]; 124 for (int j = 0 ; j < globalUrls.length ; j ++) 125 { 126 URL globalUrl = globalUrls[j]; 127 if (scopedRepositoryUrls[i].equals(globalUrls[j])) 128 { 129 scopedLength = i; 130 break; 131 } 132 } 133 if (scopedLength > 0) 134 { 135 break; 136 } 137 } 138 139 URL [] localUrls = new URL [scopedLength]; 140 System.arraycopy(scopedRepositoryUrls, 0, localUrls, 0, scopedLength); 141 return localUrls; 142 } 143 144 public class UrlInfo 145 { 146 147 URL [] globalUrls; 148 URL [] localUrls; 149 long sequenceNumber; 150 151 public UrlInfo(URL [] globalUrls, URL [] localUrls, long sequenceNumber) 152 { 153 super(); 154 this.globalUrls = globalUrls; 155 this.localUrls = localUrls; 156 this.sequenceNumber = sequenceNumber; 157 } 158 159 public URL [] getGlobalUrls() 160 { 161 return globalUrls; 162 } 163 164 public URL [] getLocalUrls() 165 { 166 return localUrls; 167 } 168 169 public long getSequenceNumber() 170 { 171 return sequenceNumber; 172 } 173 } 174 175 } 176 | Popular Tags |