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