KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > clientproject > classpath > BootClassPathImplementation


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.clientproject.classpath;
20
21 import java.beans.PropertyChangeEvent JavaDoc;
22 import org.netbeans.modules.j2ee.clientproject.AppClientProjectUtil;
23 import org.netbeans.spi.java.classpath.ClassPathImplementation;
24 import org.netbeans.spi.java.classpath.PathResourceImplementation;
25 import org.netbeans.spi.java.classpath.support.ClassPathSupport;
26 import org.netbeans.api.java.platform.JavaPlatform;
27 import org.netbeans.api.java.platform.JavaPlatformManager;
28 import org.netbeans.api.java.classpath.ClassPath;
29
30 import java.beans.PropertyChangeListener JavaDoc;
31 import java.beans.PropertyChangeSupport JavaDoc;
32 import java.util.List JavaDoc;
33 import java.util.ArrayList JavaDoc;
34 import java.util.Collections JavaDoc;
35 import java.util.Iterator JavaDoc;
36 import org.netbeans.modules.j2ee.clientproject.ui.customizer.AppClientProjectProperties;
37 import org.netbeans.spi.project.support.ant.PropertyEvaluator;
38 import org.openide.util.WeakListeners;
39
40 final class BootClassPathImplementation implements ClassPathImplementation, PropertyChangeListener JavaDoc {
41
42     private static final String JavaDoc PLATFORM_ACTIVE = AppClientProjectProperties.JAVA_PLATFORM; //NOI18N
43
private static final String JavaDoc ANT_NAME = "platform.ant.name"; //NOI18N
44
private static final String JavaDoc J2SE = "j2se"; //NOI18N
45

46     private final PropertyEvaluator evaluator;
47     private JavaPlatformManager platformManager;
48     //name of project active platform
49
private String JavaDoc activePlatformName;
50     //active platform is valid (not broken reference)
51
private boolean isActivePlatformValid;
52     private List JavaDoc<PathResourceImplementation> resourcesCache;
53     private PropertyChangeSupport JavaDoc support = new PropertyChangeSupport JavaDoc(this);
54
55     public BootClassPathImplementation(PropertyEvaluator evaluator) {
56         assert evaluator != null;
57         this.evaluator = evaluator;
58         evaluator.addPropertyChangeListener(WeakListeners.propertyChange(this, evaluator));
59     }
60
61     public synchronized List JavaDoc /*<PathResourceImplementation>*/ getResources() {
62         if (this.resourcesCache == null) {
63             JavaPlatform jp = findActivePlatform ();
64             if (jp != null) {
65                 //TODO: May also listen on CP, but from Platform it should be fixed.
66
ClassPath cp = jp.getBootstrapLibraries();
67                 List JavaDoc entries = cp.entries();
68                 List JavaDoc<PathResourceImplementation> result = new ArrayList JavaDoc<PathResourceImplementation>(entries.size());
69                 for (Iterator JavaDoc it = entries.iterator(); it.hasNext();) {
70                     ClassPath.Entry entry = (ClassPath.Entry) it.next();
71                     result.add (ClassPathSupport.createResource(entry.getURL()));
72                 }
73                 resourcesCache = Collections.unmodifiableList (result);
74             }
75             else {
76                 resourcesCache = Collections.emptyList();
77             }
78         }
79         return this.resourcesCache;
80     }
81
82     public void addPropertyChangeListener(PropertyChangeListener JavaDoc listener) {
83         this.support.addPropertyChangeListener (listener);
84     }
85
86     public void removePropertyChangeListener(PropertyChangeListener JavaDoc listener) {
87         this.support.removePropertyChangeListener (listener);
88     }
89
90     private JavaPlatform findActivePlatform () {
91         if (this.platformManager == null) {
92             this.platformManager = JavaPlatformManager.getDefault();
93             this.platformManager.addPropertyChangeListener(WeakListeners.propertyChange(this, this.platformManager));
94         }
95         this.activePlatformName = evaluator.getProperty(PLATFORM_ACTIVE);
96         final JavaPlatform activePlatform = AppClientProjectUtil.getActivePlatform (this.activePlatformName);
97         this.isActivePlatformValid = activePlatform != null;
98         return activePlatform;
99     }
100     
101     public void propertyChange(PropertyChangeEvent JavaDoc evt) {
102         if (evt.getSource() == this.evaluator && evt.getPropertyName().equals(PLATFORM_ACTIVE)) {
103             //Active platform was changed
104
resetCache ();
105         }
106         else if (evt.getSource() == this.platformManager && JavaPlatformManager.PROP_INSTALLED_PLATFORMS.equals(evt.getPropertyName()) && activePlatformName != null) {
107             //Platform definitions were changed, check if the platform was not resolved or deleted
108
if (this.isActivePlatformValid) {
109                 if (AppClientProjectUtil.getActivePlatform (this.activePlatformName) == null) {
110                     //the platform was not removed
111
this.resetCache();
112                 }
113             }
114             else {
115                 if (AppClientProjectUtil.getActivePlatform (this.activePlatformName) != null) {
116                     this.resetCache();
117                 }
118             }
119         }
120     }
121     
122     /**
123      * Resets the cache and firesPropertyChange
124      */

125     private void resetCache () {
126         synchronized (this) {
127             resourcesCache = null;
128         }
129         support.firePropertyChange(PROP_RESOURCES, null, null);
130     }
131     
132 }
133
Popular Tags