Added
Link Here
|
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.api.project.ant; |
21 |
|
22 |
import org.netbeans.spi.project.ant.AntBuildExtenderImplementation; |
23 |
import java.util.ArrayList; |
24 |
import java.util.Collection; |
25 |
import java.util.Collections; |
26 |
import java.util.HashMap; |
27 |
import java.util.HashSet; |
28 |
import java.util.List; |
29 |
import java.util.Map; |
30 |
import java.util.Set; |
31 |
import java.util.TreeMap; |
32 |
import javax.xml.parsers.DocumentBuilder; |
33 |
import javax.xml.parsers.DocumentBuilderFactory; |
34 |
import javax.xml.parsers.ParserConfigurationException; |
35 |
import org.netbeans.api.project.FileOwnerQuery; |
36 |
import org.netbeans.spi.project.ant.AntBuildExtenderImplementation; |
37 |
import org.netbeans.spi.project.support.ant.AntProjectHelper; |
38 |
import org.openide.filesystems.FileObject; |
39 |
import org.openide.filesystems.FileUtil; |
40 |
import org.w3c.dom.Document; |
41 |
import org.w3c.dom.Element; |
42 |
import org.w3c.dom.NodeList; |
43 |
|
44 |
/** |
45 |
* Allows extending the project's build script with 3rd party additions. |
46 |
* Check the Project's lookup to see if the feature is supported by a given Ant project type. |
47 |
* Typical usage: |
48 |
* <ul> |
49 |
* <li>Lookup the instance of AntBuildExtender in the project at hand</li> |
50 |
* <li>Create the external build script file with your targets and configuration</li> |
51 |
* <li>Use the AntBuildExtender to wire your script and targets into the main build lifecycle</li> |
52 |
* <li>Call {@link org.netbeans.api.project.ProjectManager#saveProject} to persist the changes and |
53 |
* regenerate the main build script</li> |
54 |
* </ul> |
55 |
* |
56 |
* Please note that it's easy to break the build script functionality and any script extensions |
57 |
* shall be done with care. A few rules to follow: |
58 |
* <ul> |
59 |
* <li>Pick a reasonably unique extension id</li> |
60 |
* <li>Prefix target names and properties you define in your extension with the extension id to prevent clashes.</li> |
61 |
* </ul> |
62 |
* @author mkleint |
63 |
* @since org.netbeans.modules.project.ant 1.16 |
64 |
*/ |
65 |
public final class AntBuildExtender { |
66 |
private HashMap<String, Extension> extensions; |
67 |
private AntBuildExtenderImplementation implementation; |
68 |
|
69 |
static { |
70 |
AntBuildExtenderAccessorImpl.createAccesor(); |
71 |
} |
72 |
|
73 |
AntBuildExtender(AntBuildExtenderImplementation implementation) { |
74 |
this.implementation = implementation; |
75 |
} |
76 |
|
77 |
/** |
78 |
* Get a list of target names in the main build script that are allowed to be |
79 |
* extended by adding the "depends" attribute definition to them. |
80 |
* @return list of target names |
81 |
*/ |
82 |
public List<String> getExtendableTargets() { |
83 |
List<String> targets = new ArrayList<String>(); |
84 |
targets.addAll(implementation.getExtendableTargets()); |
85 |
targets = Collections.unmodifiableList(targets); |
86 |
return targets; |
87 |
} |
88 |
|
89 |
/** |
90 |
* Adds a new build script extension. |
91 |
* @param id identification of the extension |
92 |
* @param extensionXml fileobject referencing the build script for the extension, |
93 |
* needs to be located in nbproject directory or below. |
94 |
* @return the newly created extension. |
95 |
*/ |
96 |
public synchronized Extension addExtension(String id, FileObject extensionXml) { |
97 |
assert extensionXml != null; |
98 |
assert extensionXml.isValid() && extensionXml.isData(); |
99 |
//TODO assert the owner is the same as the owner of this instance of entender. |
100 |
assert FileOwnerQuery.getOwner(extensionXml) == implementation.getOwningProject(); |
101 |
FileObject nbproj = implementation.getOwningProject().getProjectDirectory().getFileObject(AntProjectHelper.PROJECT_XML_PATH).getParent(); |
102 |
assert FileUtil.isParentOf(nbproj, extensionXml); |
103 |
if (extensions == null) { |
104 |
readProjectMetadata(); |
105 |
} |
106 |
if (extensions.get(id) != null) { |
107 |
throw new IllegalStateException("Extension with id '" + id + "' already exists."); |
108 |
} |
109 |
Extension ex = new Extension(id, extensionXml, FileUtil.getRelativePath(nbproj, extensionXml)); |
110 |
extensions.put(id, ex); |
111 |
updateProjectMetadata(); |
112 |
return ex; |
113 |
} |
114 |
/** |
115 |
* Remove an existing build script extension. Make sure to remove the extension's script file |
116 |
* before/after removing the extension. |
117 |
* @param id identification of the extension |
118 |
*/ |
119 |
public synchronized void removeExtension(String id) { |
120 |
if (extensions == null) { |
121 |
readProjectMetadata(); |
122 |
} |
123 |
if (extensions.get(id) == null) { |
124 |
// oh well, just ignore. |
125 |
return; |
126 |
} |
127 |
extensions.remove(id); |
128 |
updateProjectMetadata(); |
129 |
} |
130 |
|
131 |
/** |
132 |
* Get an extension by the id. |
133 |
* @param id identification token |
134 |
* @return Extention with the given id or null if not found. |
135 |
*/ |
136 |
public synchronized Extension getExtension(String id) { |
137 |
if (extensions == null) { |
138 |
readProjectMetadata(); |
139 |
} |
140 |
return extensions.get(id); |
141 |
} |
142 |
|
143 |
|
144 |
synchronized Set<Extension> getExtensions() { |
145 |
Set<Extension> ext = new HashSet<Extension>(); |
146 |
if (extensions == null) { |
147 |
readProjectMetadata(); |
148 |
} |
149 |
ext.addAll(extensions.values()); |
150 |
return ext; |
151 |
} |
152 |
|
153 |
private static final DocumentBuilder db; |
154 |
static { |
155 |
try { |
156 |
db = DocumentBuilderFactory.newInstance().newDocumentBuilder(); |
157 |
} catch (ParserConfigurationException e) { |
158 |
throw new AssertionError(e); |
159 |
} |
160 |
} |
161 |
private static Document createNewDocument() { |
162 |
// #50198: for thread safety, use a separate document. |
163 |
// Using XMLUtil.createDocument is much too slow. |
164 |
synchronized (db) { |
165 |
return db.newDocument(); |
166 |
} |
167 |
} |
168 |
|
169 |
|
170 |
private void updateProjectMetadata() { |
171 |
Document doc = createNewDocument(); |
172 |
Element root = doc.createElement(AntBuildExtenderImplementation.ELEMENT_ROOT); |
173 |
if (extensions != null) { |
174 |
FileObject nbproj = implementation.getOwningProject().getProjectDirectory().getFileObject(AntProjectHelper.PROJECT_XML_PATH).getParent(); |
175 |
for (Extension ext : extensions.values()) { |
176 |
Element child = doc.createElement(AntBuildExtenderImplementation.ELEMENT_EXTENSION); |
177 |
child.setAttribute(AntBuildExtenderImplementation.ATTR_ID, ext.id); |
178 |
String relPath = FileUtil.getRelativePath(nbproj, ext.file); |
179 |
assert relPath != null; |
180 |
child.setAttribute(AntBuildExtenderImplementation.ATTR_FILE, relPath); |
181 |
root.appendChild(child); |
182 |
for (String target : ext.dependencies.keySet()) { |
183 |
for (String depTarget : ext.dependencies.get(target)) { |
184 |
Element dep = doc.createElement(AntBuildExtenderImplementation.ELEMENT_DEPENDENCY); |
185 |
dep.setAttribute(AntBuildExtenderImplementation.ATTR_TARGET, target); |
186 |
dep.setAttribute(AntBuildExtenderImplementation.ATTR_DEPENDSON, depTarget); |
187 |
child.appendChild(dep); |
188 |
} |
189 |
} |
190 |
} |
191 |
} |
192 |
implementation.updateBuildExtensionMetadata(root); |
193 |
} |
194 |
|
195 |
private void readProjectMetadata() { |
196 |
Element cfgEl = implementation.getBuildExtensionMetadata(); |
197 |
List<String> rootIds = new ArrayList<String>(); |
198 |
FileObject nbproj = implementation.getOwningProject().getProjectDirectory().getFileObject(AntProjectHelper.PROJECT_XML_PATH).getParent(); |
199 |
extensions = new HashMap<String, Extension>(); |
200 |
if (cfgEl != null) { |
201 |
String namespace = cfgEl.getNamespaceURI(); |
202 |
NodeList roots = cfgEl.getElementsByTagNameNS(namespace, AntBuildExtenderImplementation.ELEMENT_EXTENSION); |
203 |
for (int i=0; i <roots.getLength(); i++) { |
204 |
Element root = (Element) roots.item(i); |
205 |
String id = root.getAttribute(AntBuildExtenderImplementation.ATTR_ID); |
206 |
assert id.length() > 0 : "Illegal project.xml"; |
207 |
String value = root.getAttribute(AntBuildExtenderImplementation.ATTR_FILE); |
208 |
FileObject script = nbproj.getFileObject(value); |
209 |
assert script != null : "Missing file " + script; |
210 |
Extension ext = new Extension(id, script, value); |
211 |
extensions.put(id, ext); |
212 |
NodeList deps = root.getElementsByTagNameNS(namespace, AntBuildExtenderImplementation.ELEMENT_DEPENDENCY); |
213 |
for (int j = 0; j < deps.getLength(); j++) { |
214 |
Element dep = (Element)deps.item(j); |
215 |
String target = dep.getAttribute(AntBuildExtenderImplementation.ATTR_TARGET); |
216 |
String dependsOn = dep.getAttribute(AntBuildExtenderImplementation.ATTR_DEPENDSON); |
217 |
assert target != null; |
218 |
assert dependsOn != null; |
219 |
ext.loadDependency(target, dependsOn); |
220 |
} |
221 |
} |
222 |
} |
223 |
} |
224 |
|
225 |
/** |
226 |
* Describes and allows to manipulate the build script extension and it's links to the main build script |
227 |
* of the project. |
228 |
*/ |
229 |
public final class Extension { |
230 |
String id; |
231 |
FileObject file; |
232 |
String path; |
233 |
TreeMap<String, Collection<String>> dependencies; |
234 |
|
235 |
Extension(String id, FileObject script, String relPath) { |
236 |
this.id = id; |
237 |
file = script; |
238 |
path = relPath; |
239 |
dependencies = new TreeMap<String, Collection<String>>(); |
240 |
} |
241 |
|
242 |
String getPath() { |
243 |
return path; |
244 |
} |
245 |
|
246 |
/** |
247 |
* Add a dependency of a main build script target on the target in the extension's script. |
248 |
* @param mainBuildTarget name of target in the main build script (see {@link org.netbeans.api.project.ant.AntBuildExtender#getExtendableTargets}) |
249 |
* @param extensionTarget name of target in the extention script |
250 |
*/ |
251 |
public void addDependency(String mainBuildTarget, String extensionTarget) { |
252 |
assert implementation.getExtendableTargets().contains(mainBuildTarget) : |
253 |
"The target '" + mainBuildTarget + "' is not designated by the project type as extensible."; |
254 |
synchronized (AntBuildExtender.class) { |
255 |
loadDependency(mainBuildTarget, extensionTarget); |
256 |
updateProjectMetadata(); |
257 |
} |
258 |
} |
259 |
|
260 |
private void loadDependency(String mainBuildTarget, String extensionTarget) { |
261 |
synchronized (AntBuildExtender.class) { |
262 |
Collection<String> tars = dependencies.get(mainBuildTarget); |
263 |
if (tars == null) { |
264 |
tars = new ArrayList<String>(); |
265 |
dependencies.put(mainBuildTarget, tars); |
266 |
} |
267 |
if (!tars.contains(extensionTarget)) { |
268 |
tars.add(extensionTarget); |
269 |
} else { |
270 |
//log? |
271 |
} |
272 |
} |
273 |
} |
274 |
|
275 |
|
276 |
/** |
277 |
* Remove a dependency of a main build script target on the target in the extension's script. |
278 |
* |
279 |
* @param mainBuildTarget name of target in the main build script (see {@link org.netbeans.api.project.ant.AntBuildExtender#getExtendableTargets}) |
280 |
* @param extensionTarget name of target in the extention script |
281 |
*/ |
282 |
public void removeDependency(String mainBuildTarget, String extensionTarget) { |
283 |
Collection<String> str = dependencies.get(mainBuildTarget); |
284 |
if (str != null) { |
285 |
str.remove(extensionTarget); |
286 |
updateProjectMetadata(); |
287 |
} else { |
288 |
//oh well, just ignore, nothing to update anyway.. |
289 |
} |
290 |
} |
291 |
|
292 |
Map<String, Collection<String>> getDependencies() { |
293 |
TreeMap<String, Collection<String>> toRet = new TreeMap<String, Collection<String>>(); |
294 |
synchronized (AntBuildExtender.class) { |
295 |
for (String str : dependencies.keySet()) { |
296 |
ArrayList<String> col = new ArrayList<String>(); |
297 |
col.addAll(dependencies.get(str)); |
298 |
toRet.put(str, col); |
299 |
} |
300 |
} |
301 |
return toRet; |
302 |
} |
303 |
} |
304 |
} |