KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > deployment > devmodules > api > AntDeploymentHelper


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
21 package org.netbeans.modules.j2ee.deployment.devmodules.api;
22
23 import java.io.File JavaDoc;
24 import java.io.IOException JavaDoc;
25 import java.io.InputStream JavaDoc;
26 import java.io.OutputStream JavaDoc;
27 import javax.enterprise.deploy.shared.ModuleType JavaDoc;
28 import org.netbeans.modules.j2ee.deployment.impl.ServerInstance;
29 import org.netbeans.modules.j2ee.deployment.impl.ServerRegistry;
30 import org.netbeans.modules.j2ee.deployment.plugins.api.AntDeploymentProvider;
31 import org.openide.filesystems.FileLock;
32 import org.openide.filesystems.FileObject;
33 import org.openide.filesystems.FileUtil;
34
35 /**
36  * Helps to generate Ant deployment build scripts.
37  *
38  * @author sherold
39  *
40  * @since 1.18
41  */

42 public final class AntDeploymentHelper {
43     
44     /**
45      * Generates the Ant deployment build script for the given module type for
46      * the specified server instance to the specified file. If the specified
47      * serverInstanceID is null or no server instance of the specified ID exists
48      * a default deployment build script will be generated.
49      * <p>
50      * The Ant deployment build script requires the following properties to be
51      * defined.
52      * <ul>
53      * <li><code>deploy.ant.properties.file</code> - Path to the server instance
54      * specific deployment properties file, see {@link #getDeploymentPropertiesFile}.
55      * <li><code>deploy.ant.archive</code> - The deployable archive.
56      * <li><code>deploy.ant.resource.dir</code> - The server resources directory.
57      * <li><code>deploy.ant.enabled</code> - The Ant deployment targets should be
58      * executed only if this property has been set.
59      * </ul>
60      * <p>
61      * The Ant deployment build script is bound to provide the following targets.
62      * <ul>
63      * <li><code>-deploy-ant</code> - Deploys the deployable archive defined by the
64      * <code>deploy.ant.archive</code> property. If the deployable archive is a web
65      * module or an enterprise application with a web module the
66      * <code>deploy.ant.client.url</code> property is set by this target.
67      * <li><code>-undeploy-ant</code> - Undeploys the deployable archive defined
68      * by the <code>deploy.ant.archive</code> property.
69      * </ul>
70      *
71      * @param file the file to which the deployment build script will be generated.
72      * If the file does not exist, it will be created.
73      * @param moduleType the module type the build script should handle. Use the
74      * constants defined in the {@link org.netbeans.modules.j2ee.deployment.devmodules.api.J2eeModule}.
75      * @param serverInstanceID the server instance for which the build script will
76      * be generated.
77      * @throws IOException if a problem during generating the build script occurs.
78      */

79     public static void writeDeploymentScript(File JavaDoc file, Object JavaDoc moduleType, String JavaDoc serverInstanceID)
80     throws IOException JavaDoc {
81         AntDeploymentProvider provider = null;
82         if (serverInstanceID != null) {
83             ServerInstance si = ServerRegistry.getInstance().getServerInstance(serverInstanceID);
84             if (si != null) {
85                 provider = si.getAntDeploymentProvider();
86             }
87         }
88         file.createNewFile();
89         FileObject fo = FileUtil.toFileObject(FileUtil.normalizeFile(file));
90         FileLock lock = fo.lock();
91         try {
92             OutputStream JavaDoc os = fo.getOutputStream(lock);
93             try {
94                 if (provider == null) {
95                     InputStream JavaDoc is = ServerInstance.class.getResourceAsStream("resources/default-ant-deploy.xml"); // NOI18N
96
try {
97                         FileUtil.copy(is, os);
98                     } finally {
99                         is.close();
100                     }
101                 } else {
102                     provider.writeDeploymentScript(os, moduleType);
103                 }
104             } finally {
105                 os.close();
106             }
107         } finally {
108             lock.releaseLock();
109         }
110     }
111     
112     /**
113      * Returns the server instance specific deployment properties file used by
114      * the deployment build script generated by the {@link #writeDeploymentScript(File,Object,String)}.
115      *
116      * @param serverInstanceID specifies the server instance.
117      *
118      * @return the deployment properties file for the specified server instance,
119      * if such instance exists and supports Ant deployment, null otherwise.
120      *
121      * @throws NullPointerException if the specified serverInstanceID is null.
122      */

123     public static File JavaDoc getDeploymentPropertiesFile(String JavaDoc serverInstanceID) {
124         if (serverInstanceID == null) {
125             throw new NullPointerException JavaDoc("The serverInstanceID must not be null"); // NOI18N
126
}
127         ServerInstance si = ServerRegistry.getInstance().getServerInstance(serverInstanceID);
128         if (si == null) {
129             return null;
130         }
131         AntDeploymentProvider sup = si.getAntDeploymentProvider();
132         return sup == null ? null : sup.getDeploymentPropertiesFile();
133     }
134 }
135
Popular Tags