KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > ejbjarproject > EjbJarSources


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
20 package org.netbeans.modules.j2ee.ejbjarproject;
21
22 import java.beans.PropertyChangeListener JavaDoc;
23 import java.beans.PropertyChangeEvent JavaDoc;
24 import java.io.File JavaDoc;
25 import java.util.ArrayList JavaDoc;
26 import java.util.List JavaDoc;
27 import javax.swing.event.ChangeEvent JavaDoc;
28 import javax.swing.event.ChangeListener JavaDoc;
29 import org.netbeans.modules.j2ee.ejbjarproject.ui.EjbJarLogicalViewProvider;
30 import org.netbeans.modules.j2ee.ejbjarproject.ui.customizer.EjbJarProjectProperties;
31 import org.openide.filesystems.FileUtil;
32 import org.openide.util.Mutex;
33 import org.netbeans.api.project.Sources;
34 import org.netbeans.api.project.SourceGroup;
35 import org.netbeans.api.project.ProjectManager;
36 import org.netbeans.api.project.FileOwnerQuery;
37 import org.netbeans.api.java.project.JavaProjectConstants;
38 import org.netbeans.spi.project.support.ant.SourcesHelper;
39 import org.netbeans.spi.project.support.ant.AntProjectHelper;
40 import org.netbeans.spi.project.support.ant.PropertyEvaluator;
41
42 public class EjbJarSources implements Sources, PropertyChangeListener JavaDoc, ChangeListener JavaDoc {
43
44     private static final String JavaDoc BUILD_DIR_PROP = "${" + EjbJarProjectProperties.BUILD_DIR + "}"; //NOI18N
45
private static final String JavaDoc DIST_DIR_PROP = "${" + EjbJarProjectProperties.DIST_DIR + "}"; //NOI18N
46

47     private final AntProjectHelper helper;
48     private final PropertyEvaluator evaluator;
49     private final SourceRoots sourceRoots;
50     private final SourceRoots testRoots;
51     private Sources delegate;
52     /**
53      * Flag to forbid multiple invocation of {@link SourcesHelper#registerExternalRoots}
54      **/

55     private boolean externalRootsRegistered;
56     private final List JavaDoc/*<ChangeListener>*/ listeners = new ArrayList JavaDoc();
57     private SourcesHelper sourcesHelper;
58
59     EjbJarSources(AntProjectHelper helper, PropertyEvaluator evaluator,
60                 SourceRoots sourceRoots, SourceRoots testRoots) {
61         this.helper = helper;
62         this.evaluator = evaluator;
63         this.sourceRoots = sourceRoots;
64         this.testRoots = testRoots;
65         this.sourceRoots.addPropertyChangeListener(this);
66         this.testRoots.addPropertyChangeListener(this);
67         this.evaluator.addPropertyChangeListener(this);
68         initSources(); // have to register external build roots eagerly
69
}
70
71     /**
72      * Returns an array of SourceGroup of given type. It delegates to {@link SourcesHelper}.
73      * This method firstly acquire the {@link ProjectManager#mutex} in read mode then it enters
74      * into the synchronized block to ensure that just one instance of the {@link SourcesHelper}
75      * is created. These instance is cleared also in the synchronized block by the
76      * {@link EjbJarSources#fireChange} method.
77      */

78     public SourceGroup[] getSourceGroups(final String JavaDoc type) {
79         return (SourceGroup[]) ProjectManager.mutex().readAccess(new Mutex.Action() {
80             public Object JavaDoc run() {
81                 Sources _delegate;
82                 synchronized (EjbJarSources.this) {
83                     if (delegate == null) {
84                         delegate = initSources();
85                         delegate.addChangeListener(EjbJarSources.this);
86                     }
87                     _delegate = delegate;
88                 }
89                 return _delegate.getSourceGroups(type);
90             }
91         });
92     }
93
94     private Sources initSources() {
95         sourcesHelper = new SourcesHelper(helper, evaluator);
96         File JavaDoc projectDir = FileUtil.toFile(this.helper.getProjectDirectory());
97         String JavaDoc[] propNames = sourceRoots.getRootProperties();
98         String JavaDoc[] rootNames = sourceRoots.getRootNames();
99         for (int i = 0; i < propNames.length; i++) {
100             String JavaDoc displayName = rootNames[i];
101             String JavaDoc prop = "${" + propNames[i] + "}";
102             if (displayName.length() ==0) {
103                 //If the prop is src.dir use the default name
104
if ("src.dir".equals(propNames[i])) { //NOI18N
105
displayName = SourceRoots.DEFAULT_SOURCE_LABEL;
106                 }
107                 else {
108                     //If the name is not given, it should be either a relative path in the project dir
109
//or absolute path when the root is not under the project dir
110
File JavaDoc sourceRoot = helper.resolveFile(evaluator.evaluate(prop));
111                     if (sourceRoot != null) {
112                         String JavaDoc srPath = sourceRoot.getAbsolutePath();
113                         String JavaDoc pdPath = projectDir.getAbsolutePath() + File.separatorChar;
114                         if (srPath.startsWith(pdPath)) {
115                             displayName = srPath.substring(pdPath.length());
116                         }
117                         else {
118                             displayName = sourceRoot.getAbsolutePath();
119                         }
120                     }
121                     else {
122                         displayName = SourceRoots.DEFAULT_SOURCE_LABEL;
123                     }
124                 }
125             }
126             sourcesHelper.addPrincipalSourceRoot(prop, displayName, /*XXX*/null, null);
127             sourcesHelper.addTypedSourceRoot(prop, JavaProjectConstants.SOURCES_TYPE_JAVA, displayName, /*XXX*/null, null);
128         }
129         propNames = testRoots.getRootProperties();
130         rootNames = testRoots.getRootNames();
131         for (int i = 0; i < propNames.length; i++) {
132             String JavaDoc displayName = rootNames[i];
133             String JavaDoc prop = "${" + propNames[i] + "}";
134             if (displayName.length() ==0) {
135                 //If the prop is test.src.dir use the default name
136
if ("test.src.dir".equals(propNames[i])) { //NOI18N
137
displayName = SourceRoots.DEFAULT_TEST_LABEL;
138                 }
139                 else {
140                     //If the name is not given, it should be either a relative path in the project dir
141
//or absolute path when the root is not under the project dir
142
File JavaDoc sourceRoot = helper.resolveFile(evaluator.evaluate(prop));
143                     if (sourceRoot != null) {
144                         String JavaDoc srPath = sourceRoot.getAbsolutePath();
145                         String JavaDoc pdPath = projectDir.getAbsolutePath() + File.separatorChar;
146                         if (srPath.startsWith(pdPath)) {
147                             displayName = srPath.substring(pdPath.length());
148                         }
149                         else {
150                             displayName = sourceRoot.getAbsolutePath();
151                         }
152                     }
153                     else {
154                         displayName = SourceRoots.DEFAULT_TEST_LABEL;
155                     }
156                 }
157             }
158             sourcesHelper.addPrincipalSourceRoot(prop, displayName, /*XXX*/null, null);
159             sourcesHelper.addTypedSourceRoot(prop, JavaProjectConstants.SOURCES_TYPE_JAVA, displayName, /*XXX*/null, null);
160         }
161         
162         // Configuration Files
163
String JavaDoc configFilesLabel = org.openide.util.NbBundle.getMessage(EjbJarLogicalViewProvider.class, "LBL_Node_DocBase"); //NOI18N
164
sourcesHelper.addPrincipalSourceRoot("${"+EjbJarProjectProperties.META_INF+"}", configFilesLabel, /*XXX*/null, null);
165         
166         sourcesHelper.addNonSourceRoot(BUILD_DIR_PROP);
167         sourcesHelper.addNonSourceRoot(DIST_DIR_PROP);
168         
169         externalRootsRegistered = false;
170         ProjectManager.mutex().postWriteRequest(new Runnable JavaDoc() {
171             public void run() {
172                 if (!externalRootsRegistered) {
173                     sourcesHelper.registerExternalRoots(FileOwnerQuery.EXTERNAL_ALGORITHM_TRANSIENT);
174                     externalRootsRegistered = true;
175                 }
176             }
177         });
178         return sourcesHelper.createSources();
179     }
180
181     public void addChangeListener(ChangeListener JavaDoc changeListener) {
182         synchronized (listeners) {
183             listeners.add(changeListener);
184         }
185     }
186
187     public void removeChangeListener(ChangeListener JavaDoc changeListener) {
188         synchronized (listeners) {
189             listeners.remove(changeListener);
190         }
191     }
192
193     private void fireChange() {
194         ChangeListener JavaDoc[] _listeners;
195         synchronized (this) {
196             if (delegate != null) {
197                 delegate.removeChangeListener(this);
198                 delegate = null;
199             }
200         }
201         synchronized (listeners) {
202             if (listeners.isEmpty()) {
203                 return;
204             }
205             _listeners = (ChangeListener JavaDoc[])listeners.toArray(new ChangeListener JavaDoc[listeners.size()]);
206         }
207         ChangeEvent JavaDoc ev = new ChangeEvent JavaDoc(this);
208         for (int i = 0; i < _listeners.length; i++) {
209             _listeners[i].stateChanged(ev);
210         }
211     }
212     
213     public void propertyChange(PropertyChangeEvent JavaDoc evt) {
214         String JavaDoc propName = evt.getPropertyName();
215         if (SourceRoots.PROP_ROOT_PROPERTIES.equals(propName) || EjbJarProjectProperties.BUILD_DIR.equals(propName) || EjbJarProjectProperties.DIST_DIR.equals(propName))
216             this.fireChange();
217     }
218
219     public void stateChanged (ChangeEvent JavaDoc event) {
220         this.fireChange();
221     }
222
223 }
224
Popular Tags