KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > earproject > BrokenProjectSupport


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.earproject;
21
22 import java.io.IOException JavaDoc;
23 import java.util.Collection JavaDoc;
24 import java.util.HashSet JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.util.List JavaDoc;
27 import java.util.Set JavaDoc;
28 import javax.swing.event.ChangeEvent JavaDoc;
29 import javax.swing.event.ChangeListener JavaDoc;
30 import org.netbeans.api.project.ProjectManager;
31 import org.netbeans.api.project.ant.AntArtifact;
32 import org.netbeans.modules.j2ee.earproject.ui.customizer.VisualClassPathItem;
33 import org.netbeans.spi.project.support.ant.AntProjectHelper;
34 import org.netbeans.spi.project.support.ant.EditableProperties;
35 import org.openide.ErrorManager;
36 import org.openide.filesystems.FileChangeAdapter;
37 import org.openide.filesystems.FileChangeListener;
38 import org.openide.filesystems.FileEvent;
39 import org.openide.filesystems.FileObject;
40 import org.openide.filesystems.FileRenameEvent;
41
42 /**
43  * TODO comments the whole class.
44  *
45  * @see org.netbeans.api.project.Project#getLookup
46  * @author Martin Krauskopf
47  */

48 public final class BrokenProjectSupport {
49     
50     private final EarProject project;
51     private final Set JavaDoc<ChangeListener JavaDoc> listeners;
52     private final FileChangeListener artifactListener;
53     private final Collection JavaDoc<FileObject> watchedArtifacts = new HashSet JavaDoc<FileObject>();
54     
55     BrokenProjectSupport(final EarProject project) {
56         this.project = project;
57         this.listeners = new HashSet JavaDoc<ChangeListener JavaDoc>(1);
58         this.artifactListener = new ArtifactListener();
59     }
60     
61     public boolean hasBrokenArtifacts() {
62         boolean brokenArtifacts = false;
63         List JavaDoc<VisualClassPathItem> vcpis = project.getProjectProperties().getJarContentAdditional();
64         for (VisualClassPathItem vcpi : vcpis) {
65             Object JavaDoc obj = vcpi.getObject();
66             if (!(obj instanceof AntArtifact)) {
67                 continue;
68             }
69             AntArtifact aa = (AntArtifact) obj;
70             FileObject script = aa.getScriptFile();
71             if (script == null || !script.isValid()) {
72                 brokenArtifacts = true;
73                 break;
74             }
75         }
76         return brokenArtifacts;
77     }
78     
79     public void watchAntArtifact(final AntArtifact artifact) {
80         FileObject artFile = artifact.getScriptFile();
81         watchedArtifacts.add(artFile);
82         artFile.addFileChangeListener(artifactListener);
83     }
84     
85     public void cleanUp() {
86         for (FileObject artFile : watchedArtifacts) {
87             artFile.removeFileChangeListener(artifactListener);
88         }
89         watchedArtifacts.clear();
90     }
91     
92     public void addChangeListener(ChangeListener JavaDoc l) {
93         synchronized (listeners) {
94             listeners.add(l);
95         }
96     }
97     
98     public void removeChangeListener(ChangeListener JavaDoc l) {
99         synchronized (listeners) {
100             listeners.remove(l);
101         }
102     }
103     
104     private void fireChangeEvent() {
105         Iterator JavaDoc<ChangeListener JavaDoc> it;
106         synchronized (listeners) {
107             it = new HashSet JavaDoc<ChangeListener JavaDoc>(listeners).iterator();
108         }
109         ChangeEvent JavaDoc ev = new ChangeEvent JavaDoc(this);
110         while (it.hasNext()) {
111             it.next().stateChanged(ev);
112         }
113     }
114     
115     /**
116      * Tries to fix/adjust broken artifacts' references.
117      * <p>Acquires write access from {@link ProjectManager#mutex}.</p>
118      */

119     public void adjustReferences() {
120         ProjectManager.mutex().writeAccess(new Runnable JavaDoc() {
121             public void run() {
122                 for (VisualClassPathItem vcpi : project.getProjectProperties().getJarContentAdditional()) {
123                     if (vcpi.getObject() instanceof AntArtifact) {
124                         String JavaDoc raw = vcpi.getRaw();
125                         if (raw.matches("^\\$\\{reference\\..*\\}")) { // NOI18N
126
String JavaDoc currEvaluated = project.evaluator().evaluate(raw);
127                             FileObject currEvaluatedFO = project.getAntProjectHelper().resolveFileObject(currEvaluated);
128                             if (currEvaluatedFO == null) {
129                                 String JavaDoc referenceKey = raw.substring(2, raw.length() - 1); // without $ and curly brackets // NOI18N
130
adjustReference(vcpi, referenceKey);
131                             }
132                         }
133                     }
134                 }
135             }
136         });
137     }
138     
139     private void adjustReference(final VisualClassPathItem vcpi, final String JavaDoc referenceKey) {
140         EditableProperties prjProps = project.getAntProjectHelper().getProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH);
141         String JavaDoc referenceValue = prjProps.getProperty(referenceKey);
142         if (referenceValue.matches("^\\$\\{project\\..*\\}.+")) { // NOI18N
143
String JavaDoc prjReference = referenceValue.substring(0, referenceValue.indexOf('}', 2) + 1);
144             String JavaDoc relPath = vcpi.getEvaluated();
145             prjProps.setProperty(referenceKey, prjReference + '/' + relPath);
146         }
147         project.getAntProjectHelper().putProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH, prjProps);
148         try {
149             ProjectManager.getDefault().saveProject(project);
150         } catch (IOException JavaDoc ioe) {
151             ErrorManager.getDefault().notify(ErrorManager.WARNING, ioe);
152         }
153     }
154     
155     private class ArtifactListener extends FileChangeAdapter {
156         
157         public void fileDeleted(FileEvent fe) {
158             fireChangeEvent();
159         }
160         
161         public void fileRenamed(FileRenameEvent fe) {
162             fireChangeEvent();
163         }
164         
165     }
166     
167 }
168
Popular Tags