KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > metadata > WeakProxyClassPathImplementation


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.modules.j2ee.metadata;
20
21 import org.netbeans.spi.java.classpath.ClassPathImplementation;
22
23 import java.util.List JavaDoc;
24 import java.util.ArrayList JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.util.Collections JavaDoc;
27 import java.beans.PropertyChangeListener JavaDoc;
28 import java.beans.PropertyChangeEvent JavaDoc;
29 import java.lang.ref.WeakReference JavaDoc;
30 import java.util.concurrent.CopyOnWriteArraySet JavaDoc;
31 import org.netbeans.spi.java.classpath.PathResourceImplementation;
32 import org.openide.util.Utilities;
33 import org.openide.util.WeakListeners;
34
35 /** ProxyClassPathImplementation provides read only proxy for ClassPathImplementations.
36  * The order of the resources is given by the order of its delegates.
37  * The proxy is designed to be used as a union of class paths.
38  * E.g. to be able to easily iterate or listen on all design resources = sources + compile resources
39  */

40 public class WeakProxyClassPathImplementation implements ClassPathImplementation {
41     
42     final private CopyOnWriteArraySet JavaDoc<WeakReference JavaDoc<ClassPathImplementation>> classPathRefs = new CopyOnWriteArraySet JavaDoc<WeakReference JavaDoc<ClassPathImplementation>>();
43     final private PropertyChangeListener JavaDoc classPathsListener = new DelegatesListener ();
44
45     private List JavaDoc<PathResourceImplementation> resourcesCache;
46     private ArrayList JavaDoc<PropertyChangeListener JavaDoc> listeners;
47
48     public WeakProxyClassPathImplementation (ClassPathImplementation[] classPaths) {
49         if (classPaths == null) {
50             throw new IllegalArgumentException JavaDoc ();
51         }
52         for (ClassPathImplementation classPath : classPaths) {
53             if (classPaths == null) {
54                 continue;
55             }
56             classPath.addPropertyChangeListener (WeakListeners.propertyChange(classPathsListener, classPath));
57             classPathRefs.add(new CleanupReference<ClassPathImplementation>(classPath));
58         }
59     }
60
61     public List JavaDoc <PathResourceImplementation> getResources() {
62         synchronized (this) {
63             if (this.resourcesCache != null) {
64                 return this.resourcesCache;
65             }
66         }
67         List JavaDoc<PathResourceImplementation> result = new ArrayList JavaDoc<PathResourceImplementation>(classPathRefs.size() * 10);
68         for (WeakReference JavaDoc<ClassPathImplementation> classPathRef : classPathRefs) {
69             ClassPathImplementation classPath = classPathRef.get();
70             if (classPath == null) {
71                 continue;
72             }
73             List JavaDoc<? extends PathResourceImplementation> subPath = classPath.getResources();
74             assert subPath != null : "ClassPathImplementation.getResources() returned null. ClassPathImplementation.class: "
75                 + classPath.getClass().toString() + " ClassPathImplementation: " + classPath.toString();
76             result.addAll(subPath);
77         }
78         synchronized (this) {
79             if (this.resourcesCache == null) {
80                 resourcesCache = Collections.unmodifiableList(result);
81             }
82             return this.resourcesCache;
83         }
84     }
85
86     public synchronized void addPropertyChangeListener(PropertyChangeListener JavaDoc listener) {
87         if (this.listeners == null)
88             this.listeners = new ArrayList JavaDoc<PropertyChangeListener JavaDoc>();
89         this.listeners.add (listener);
90     }
91
92     public synchronized void removePropertyChangeListener(PropertyChangeListener JavaDoc listener) {
93         if (this.listeners == null)
94             return;
95         this.listeners.remove (listener);
96     }
97     
98     public String JavaDoc toString () {
99         StringBuffer JavaDoc builder = new StringBuffer JavaDoc("["); //NOI18N
100
for (WeakReference JavaDoc<ClassPathImplementation> classPathRef : classPathRefs) {
101             ClassPathImplementation classPath = classPathRef.get();
102             if (classPath != null) {
103                 builder.append (classPath.toString());
104                 builder.append(", "); //NOI18N
105
}
106         }
107         builder.append ("]"); //NOI18N
108
return builder.toString ();
109     }
110     
111     private void change(String JavaDoc propertyName) {
112         Iterator JavaDoc it = null;
113         synchronized (this) {
114             WeakProxyClassPathImplementation.this.resourcesCache = null; //Clean the cache
115
if (WeakProxyClassPathImplementation.this.listeners == null) {
116                 return;
117             }
118             it = ((ArrayList JavaDoc)WeakProxyClassPathImplementation.this.listeners.clone()).iterator();
119         }
120         PropertyChangeEvent JavaDoc event = new PropertyChangeEvent JavaDoc (this, propertyName, null, null);
121         while (it.hasNext()) {
122             ((PropertyChangeListener JavaDoc)it.next()).propertyChange (event);
123         }
124     }
125
126     private class DelegatesListener implements PropertyChangeListener JavaDoc {
127
128         public void propertyChange(PropertyChangeEvent JavaDoc evt) {
129             change(evt.getPropertyName());
130         }
131     }
132     
133     private class CleanupReference<T> extends WeakReference JavaDoc<T> implements Runnable JavaDoc {
134         
135         public CleanupReference(T referent) {
136             super(referent, Utilities.activeReferenceQueue());
137         }
138         
139         public void run() {
140             classPathRefs.remove(this);
141             change(ClassPathImplementation.PROP_RESOURCES);
142         }
143     }
144
145 }
146
Popular Tags