Added
Link Here
|
1 |
/* |
2 |
* Sun Public License Notice |
3 |
* |
4 |
* The contents of this file are subject to the Sun Public License |
5 |
* Version 1.0 (the "License"). You may not use this file except in |
6 |
* compliance with the License. A copy of the License is available at |
7 |
* http://www.sun.com/ |
8 |
* |
9 |
* The Original Code is NetBeans. The Initial Developer of the Original |
10 |
* Code is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun |
11 |
* Microsystems, Inc. All Rights Reserved. |
12 |
*/ |
13 |
|
14 |
|
15 |
package org.netbeans.modules.j2ee.deployment.devmodules.api; |
16 |
|
17 |
import java.io.File; |
18 |
import java.io.IOException; |
19 |
import java.io.InputStream; |
20 |
import java.io.OutputStream; |
21 |
import javax.enterprise.deploy.shared.ModuleType; |
22 |
import org.netbeans.modules.j2ee.deployment.impl.ServerInstance; |
23 |
import org.netbeans.modules.j2ee.deployment.impl.ServerRegistry; |
24 |
import org.netbeans.modules.j2ee.deployment.plugins.api.AntDeploymentProvider; |
25 |
import org.openide.filesystems.FileLock; |
26 |
import org.openide.filesystems.FileObject; |
27 |
import org.openide.filesystems.FileUtil; |
28 |
|
29 |
/** |
30 |
* Helps to generate Ant deployment build scripts. |
31 |
* |
32 |
* @author sherold |
33 |
* |
34 |
* @since 1.17 |
35 |
*/ |
36 |
public final class AntDeploymentHelper { |
37 |
|
38 |
/** |
39 |
* Generates the Ant deployment build script for the given module type for |
40 |
* the specified server instance to the specified file. If the specified |
41 |
* serverInstanceID is null or no server instance of the specified ID exists |
42 |
* a default deployment build script will be generated. |
43 |
* <p> |
44 |
* The Ant deployment build script requires the following properties to be |
45 |
* defined. |
46 |
* <ul> |
47 |
* <li><code>deploy.ant.properties.file</code> - Path to the server instance |
48 |
* specific deployment properties file, see {@link #getDeploymentPropertiesFile}. |
49 |
* <li><code>deploy.ant.archive</code> - The deployable archive. |
50 |
* <li><code>deploy.ant.resource.dir</code> - The server resources directory. |
51 |
* <li><code>deploy.ant.enabled</code> - The Ant deployment targets should be |
52 |
* executed only if this property has been set. |
53 |
* </ul> |
54 |
* <p> |
55 |
* The Ant deployment build script is bound to provide the following targets. |
56 |
* <ul> |
57 |
* <li><code>-deploy-ant</code> - Deploys the deployable archive defined by the |
58 |
* <code>deploy.ant.archive</code> property. If the deployable archive is a web |
59 |
* module or an enterprise application with a web module the |
60 |
* <code>deploy.ant.client.url</code> property is set by this target. |
61 |
* <li><code>-undeploy-ant</code> - Undeploys the deployable archive defined |
62 |
* by the <code>deploy.ant.archive</code> property. |
63 |
* </ul> |
64 |
* |
65 |
* @param file the file to which the deployment build script will be generated. |
66 |
* If the file does not exist, it will be created. |
67 |
* @param moduleType the module type the build script should handle. Use the |
68 |
* constants defined in the {@link org.netbeans.modules.j2ee.deployment.devmodules.api.J2eeModule}. |
69 |
* @param serverInstanceID the server instance for which the build script will |
70 |
* be generated. |
71 |
* @throws IOException if a problem during generating the build script occurs. |
72 |
*/ |
73 |
public static void writeDeploymentScript(File file, Object moduleType, String serverInstanceID) |
74 |
throws IOException { |
75 |
AntDeploymentProvider provider = null; |
76 |
if (serverInstanceID != null) { |
77 |
ServerInstance si = ServerRegistry.getInstance().getServerInstance(serverInstanceID); |
78 |
if (si != null) { |
79 |
provider = si.getAntDeploymentProvider(); |
80 |
} |
81 |
} |
82 |
file.createNewFile(); |
83 |
FileObject fo = FileUtil.toFileObject(file); |
84 |
FileLock lock = fo.lock(); |
85 |
try { |
86 |
OutputStream os = fo.getOutputStream(lock); |
87 |
try { |
88 |
if (provider == null) { |
89 |
InputStream is = ServerInstance.class.getResourceAsStream("resources/default-ant-deploy.xml"); // NOI18N |
90 |
try { |
91 |
FileUtil.copy(is, os); |
92 |
} finally { |
93 |
is.close(); |
94 |
} |
95 |
} else { |
96 |
provider.writeDeploymentScript(os, moduleType); |
97 |
} |
98 |
} finally { |
99 |
os.close(); |
100 |
} |
101 |
} finally { |
102 |
lock.releaseLock(); |
103 |
} |
104 |
} |
105 |
|
106 |
/** |
107 |
* Returns the server instance specific deployment properties file used by |
108 |
* the deployment build script generated by the {@link #writeDeploymentScript(File,Object,String)}. |
109 |
* |
110 |
* @param serverInstanceID specifies the server instance. |
111 |
* |
112 |
* @return the deployment properties file for the specified server instance, |
113 |
* if such instance exists and supports Ant deployment, null otherwise. |
114 |
* |
115 |
* @throws NullPointerException if the specified serverInstanceID is null. |
116 |
*/ |
117 |
public static File getDeploymentPropertiesFile(String serverInstanceID) { |
118 |
if (serverInstanceID == null) { |
119 |
throw new NullPointerException("The serverInstanceID must not be null"); // NOI18N |
120 |
} |
121 |
ServerInstance si = ServerRegistry.getInstance().getServerInstance(serverInstanceID); |
122 |
if (si == null) { |
123 |
return null; |
124 |
} |
125 |
AntDeploymentProvider sup = si.getAntDeploymentProvider(); |
126 |
return sup == null ? null : sup.getDeploymentPropertiesFile(); |
127 |
} |
128 |
} |