KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > aop > deployment > LoaderRepositoryUrlUtil


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.aop.deployment;
23
24 import java.net.URL JavaDoc;
25
26 import javax.management.InstanceNotFoundException JavaDoc;
27 import javax.management.MBeanServer JavaDoc;
28 import javax.management.Notification JavaDoc;
29 import javax.management.NotificationListener JavaDoc;
30 import javax.management.ObjectName JavaDoc;
31
32 import org.jboss.mx.loading.HeirarchicalLoaderRepository3;
33 import org.jboss.mx.loading.LoaderRepository;
34 import org.jboss.mx.util.MBeanServerLocator;
35
36 /**
37  *
38  * @author <a HREF="kabir.khan@jboss.com">Kabir Khan</a>
39  * @version $Revision: 1.1 $
40  */

41 public class LoaderRepositoryUrlUtil implements NotificationListener JavaDoc
42 {
43    final static MBeanServer JavaDoc SERVER;
44    final static ObjectName JavaDoc 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 JavaDoc("JMImplementation:name=Default,service=LoaderRepository");
52          MAIN_LOADER_REPOSITORY = (LoaderRepository)SERVER.invoke(MAIN_LOADER_REPOSITORY_OBJECT_NAME, "getInstance", new Object JavaDoc[0], new String JavaDoc[0]);
53       }
54       catch (Exception JavaDoc e)
55       {
56          throw new RuntimeException JavaDoc(e);
57       }
58    }
59    
60    long currentSequenceNumber;
61    long lastSequenceNumber = -1;
62    URL JavaDoc[] urls;
63    
64    public LoaderRepositoryUrlUtil()
65    {
66       try
67       {
68          SERVER.addNotificationListener(MAIN_LOADER_REPOSITORY_OBJECT_NAME, this, null, null);
69       }
70       catch (InstanceNotFoundException JavaDoc e)
71       {
72          throw new RuntimeException JavaDoc(e);
73       }
74    }
75
76    public synchronized void handleNotification(Notification JavaDoc notification, Object JavaDoc 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 JavaDoc[] 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 JavaDoc[] getLocalUrls(HeirarchicalLoaderRepository3 scopedRepository, URL JavaDoc[] globalUrls)
115    {
116       URL JavaDoc[] scopedRepositoryUrls = scopedRepository.getURLs();
117
118       //This is a bit of a hack, since this relies on the order of the urls returned by HeirarchicalLoaderRepository3
119
//My urls, followed by parent urls.
120
int scopedLength = 0;
121       for (int i = 0 ; i < scopedRepositoryUrls.length ; i++)
122       {
123          URL JavaDoc scopedUrl = scopedRepositoryUrls[i];
124          for (int j = 0 ; j < globalUrls.length ; j ++)
125          {
126             URL JavaDoc 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 JavaDoc[] localUrls = new URL JavaDoc[scopedLength];
140       System.arraycopy(scopedRepositoryUrls, 0, localUrls, 0, scopedLength);
141       return localUrls;
142    }
143    
144    public class UrlInfo
145    {
146       
147       URL JavaDoc[] globalUrls;
148       URL JavaDoc[] localUrls;
149       long sequenceNumber;
150
151       public UrlInfo(URL JavaDoc[] globalUrls, URL JavaDoc[] localUrls, long sequenceNumber)
152       {
153          super();
154          this.globalUrls = globalUrls;
155          this.localUrls = localUrls;
156          this.sequenceNumber = sequenceNumber;
157       }
158       
159       public URL JavaDoc[] getGlobalUrls()
160       {
161          return globalUrls;
162       }
163       
164       public URL JavaDoc[] getLocalUrls()
165       {
166          return localUrls;
167       }
168
169       public long getSequenceNumber()
170       {
171          return sequenceNumber;
172       }
173    }
174    
175 }
176
Popular Tags