KickJava   Java API By Example, From Geeks To Geeks.

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


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.deployment.devmodules.api;
21
22 import java.awt.event.WindowAdapter JavaDoc;
23 import java.awt.event.WindowEvent JavaDoc;
24 import java.util.ArrayList JavaDoc;
25 import java.util.Collection JavaDoc;
26 import java.util.Iterator JavaDoc;
27 import java.util.List JavaDoc;
28 import java.util.Map JavaDoc;
29 import java.util.Set JavaDoc;
30 import java.util.WeakHashMap JavaDoc;
31 import org.netbeans.modules.j2ee.deployment.devmodules.spi.InstanceListener;
32 import org.netbeans.modules.j2ee.deployment.devmodules.spi.J2eeModuleProvider;
33 import org.netbeans.modules.j2ee.deployment.impl.*;
34 import org.netbeans.modules.j2ee.deployment.impl.projects.*;
35 import org.netbeans.modules.j2ee.deployment.impl.ui.*;
36 import org.netbeans.modules.j2ee.deployment.plugins.api.InstanceProperties;
37 import org.openide.ErrorManager;
38 import org.openide.util.NbBundle;
39
40 /**
41  *
42  * @author Pavel Buzek
43  */

44 public final class Deployment {
45
46     private static boolean alsoStartTargets = true; //TODO - make it a property? is it really needed?
47

48     private static Deployment instance = null;
49
50     public static synchronized Deployment getDefault () {
51         if (instance == null) {
52             instance = new Deployment ();
53         }
54         return instance;
55     }
56     
57     private Deployment () {
58     }
59     
60     /** Deploys a web J2EE module to server.
61      * @param clientModuleUrl URL of module within a J2EE Application that
62      * should be used as a client (can be null for standalone modules)
63      * <div class="nonnormative">
64      * <p>Note: if null for J2EE application the first web or client module will be used.</p>
65      * </div>
66      * @return complete URL to be displayed in browser (server part plus the client module and/or client part provided as a parameter)
67      */

68     public String JavaDoc deploy (J2eeModuleProvider jmp, boolean debugmode, String JavaDoc clientModuleUrl, String JavaDoc clientUrlPart, boolean forceRedeploy) throws DeploymentException {
69         return deploy(jmp, debugmode, clientModuleUrl, clientUrlPart, forceRedeploy, null);
70     }
71     
72     public String JavaDoc deploy (J2eeModuleProvider jmp, boolean debugmode, String JavaDoc clientModuleUrl, String JavaDoc clientUrlPart, boolean forceRedeploy, Logger logger) throws DeploymentException {
73         
74         DeploymentTargetImpl target = new DeploymentTargetImpl(jmp, clientModuleUrl);
75         TargetModule[] modules = null;
76         final J2eeModule module = target.getModule();
77
78         String JavaDoc title = NbBundle.getMessage(Deployment.class, "LBL_Deploying", jmp.getDeploymentName());
79         ProgressUI progress = new ProgressUI(title, false, logger);
80         
81         try {
82             progress.start();
83             
84             ServerString server = target.getServer(); //will throw exception if bad server id
85

86             if (module == null) {
87                 String JavaDoc msg = NbBundle.getMessage (Deployment.class, "MSG_NoJ2eeModule");
88                 throw new DeploymentException(msg);
89             }
90             if (server == null || server.getServerInstance() == null) {
91                 String JavaDoc msg = NbBundle.getMessage (Deployment.class, "MSG_NoTargetServer");
92                 throw new DeploymentException(msg);
93             }
94             
95             boolean serverReady = false;
96             TargetServer targetserver = new TargetServer(target);
97
98             if (alsoStartTargets || debugmode) {
99                 targetserver.startTargets(debugmode, progress);
100             } else { //PENDING: how do we know whether target does not need to start when deploy only
101
server.getServerInstance().start(progress);
102             }
103
104             jmp.deployDatasources();
105
106             modules = targetserver.deploy(progress, forceRedeploy);
107             // inform the plugin about the deploy action, even if there was
108
// really nothing needed to be deployed
109
targetserver.notifyIncrementalDeployment(modules);
110             
111             if (modules != null && modules.length > 0) {
112                 target.setTargetModules(modules);
113             } else {
114                 String JavaDoc msg = NbBundle.getMessage(Deployment.class, "MSG_ModuleNotDeployed");
115                 throw new DeploymentException (msg);
116             }
117             return target.getClientUrl(clientUrlPart);
118         } catch (Exception JavaDoc ex) {
119             String JavaDoc msg = NbBundle.getMessage (Deployment.class, "MSG_DeployFailed", ex.getLocalizedMessage ());
120             ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex);
121             throw new DeploymentException(msg, ex);
122         } finally {
123             if (progress != null) {
124                 progress.finish();
125             }
126         }
127     }
128     
129     public static final class DeploymentException extends Exception JavaDoc {
130         private DeploymentException (String JavaDoc msg) {
131             super (msg);
132         }
133         private DeploymentException (Throwable JavaDoc t) {
134             super (t);
135         }
136         private DeploymentException (String JavaDoc s, Throwable JavaDoc t) {
137             super (s, t);
138         }
139         /**
140          * Returns a short description of this DeploymentException.
141          * overwrite the one from Exception to avoid showing the class name that does nto provide any real value.
142          * @return a string representation of this DeploymentException.
143          */

144         public String JavaDoc toString() {
145             String JavaDoc s = getClass().getName();
146             String JavaDoc message = getLocalizedMessage();
147             return (message != null) ? (message) : s;
148         }
149     }
150     
151     public String JavaDoc [] getServerInstanceIDs () {
152         return InstanceProperties.getInstanceList ();
153     }
154     
155     /**
156      * Return ServerInstanceIDs of all registered server instances that support
157      * specified module types.
158      *
159      * @param moduleTypes list of module types that the server instance must support.
160      *
161      * @return ServerInstanceIDs of all registered server instances that meet
162      * the specified requirements.
163      * @since 1.6
164      */

165     public String JavaDoc[] getServerInstanceIDs(Object JavaDoc[] moduleTypes) {
166         return getServerInstanceIDs(moduleTypes, null, null);
167     }
168
169     /**
170      * Return ServerInstanceIDs of all registered server instances that support
171      * specified module types and J2EE specification versions.
172      *
173      * @param moduleTypes list of module types that the server instance must support.
174      * @param specVersion lowest J2EE specification version that the server instance must support.
175      *
176      * @return ServerInstanceIDs of all registered server instances that meet
177      * the specified requirements.
178      * @since 1.6
179      */

180     public String JavaDoc[] getServerInstanceIDs(Object JavaDoc[] moduleTypes, String JavaDoc specVersion) {
181         return getServerInstanceIDs(moduleTypes, specVersion, null);
182     }
183     
184     /**
185      * Return ServerInstanceIDs of all registered server instances that support
186      * specified module types, J2EE specification version and tools.
187      *
188      * @param moduleTypes list of module types that the server instance must support.
189      * @param specVersion lowest J2EE specification version that the server instance must support.
190      * @param tools list of tools that the server instance must support.
191      *
192      * @return ServerInstanceIDs of all registered server instances that meet
193      * the specified requirements.
194      * @since 1.6
195      */

196     public String JavaDoc[] getServerInstanceIDs(Object JavaDoc[] moduleTypes, String JavaDoc specVersion, String JavaDoc[] tools) {
197         List JavaDoc result = new ArrayList JavaDoc();
198         String JavaDoc[] serverInstanceIDs = getServerInstanceIDs();
199         for (int i = 0; i < serverInstanceIDs.length; i++) {
200             J2eePlatform platform = getJ2eePlatform(serverInstanceIDs[i]);
201             if (platform != null) {
202                 boolean isOk = true;
203         if (moduleTypes != null) {
204                     Set JavaDoc platModuleTypes = platform.getSupportedModuleTypes();
205                     for (int j = 0; j < moduleTypes.length; j++) {
206                         if (!platModuleTypes.contains(moduleTypes[j])) {
207                             isOk = false;
208                         }
209                     }
210         }
211                 if (isOk && specVersion != null) {
212                     Set JavaDoc platSpecVers = platform.getSupportedSpecVersions();
213                     if (specVersion.equals(J2eeModule.J2EE_13)) {
214                         isOk = platSpecVers.contains(J2eeModule.J2EE_13)
215                                 || platSpecVers.contains(J2eeModule.J2EE_14);
216                     } else {
217                         isOk = platSpecVers.contains(specVersion);
218                     }
219                 }
220                 if (isOk && tools != null) {
221                     for (int j = 0; j < tools.length; j++) {
222                         if (!platform.isToolSupported(tools[j])) {
223                             isOk = false;
224                         }
225                     }
226                 }
227                 if (isOk) {
228                     result.add(serverInstanceIDs[i]);
229                 }
230             }
231         }
232         return (String JavaDoc[])result.toArray(new String JavaDoc[result.size()]);
233     }
234     
235     public String JavaDoc getServerInstanceDisplayName (String JavaDoc id) {
236         return ServerRegistry.getInstance ().getServerInstance (id).getDisplayName ();
237     }
238     
239     public String JavaDoc getServerID (String JavaDoc instanceId) {
240         ServerInstance si = ServerRegistry.getInstance().getServerInstance(instanceId);
241         if (si != null) {
242             return si.getServer().getShortName();
243         }
244         return null;
245     }
246     
247     public String JavaDoc getDefaultServerInstanceID () {
248         ServerString defInst = ServerRegistry.getInstance ().getDefaultInstance ();
249         if (defInst != null) {
250             ServerInstance si = defInst.getServerInstance();
251             if (si != null) {
252                 return si.getUrl ();
253             }
254         }
255         return null;
256     }
257     
258     public String JavaDoc [] getInstancesOfServer (String JavaDoc id) {
259         if (id != null) {
260             Server server = ServerRegistry.getInstance().getServer(id);
261             if (server != null) {
262                 ServerInstance sis [] = ServerRegistry.getInstance ().getServer (id).getInstances ();
263                 String JavaDoc ids [] = new String JavaDoc [sis.length];
264                 for (int i = 0; i < sis.length; i++) {
265                     ids [i] = sis [i].getUrl ();
266                 }
267                 return ids;
268             }
269         }
270         return new String JavaDoc[0];
271     }
272     
273     public String JavaDoc [] getServerIDs () {
274         Collection JavaDoc c = ServerRegistry.getInstance ().getServers ();
275         String JavaDoc ids [] = new String JavaDoc [c.size ()];
276         Iterator JavaDoc iter = c.iterator ();
277         for (int i = 0; i < c.size (); i++) {
278             Server s = (Server) iter.next ();
279             ids [i] = s.getShortName ();
280         }
281         return ids;
282     }
283     
284     /**
285      * Return server instance's <code>J2eePlatform</code>.
286      *
287      * @param serverInstanceID server instance ID.
288      * @return <code>J2eePlatform</code> for the given server instance, <code>null</code> if
289      * server instance of the specified ID does not exist.
290      * @since 1.5
291      */

292     public J2eePlatform getJ2eePlatform(String JavaDoc serverInstanceID) {
293         ServerInstance serInst = ServerRegistry.getInstance().getServerInstance(serverInstanceID);
294         if (serInst == null) return null;
295         return J2eePlatform.create(serInst);
296     }
297     
298     public String JavaDoc getServerDisplayName (String JavaDoc id) {
299         return ServerRegistry.getInstance ().getServer (id).getDisplayName();
300     }
301     
302     /**
303      * Register an instance listener that will listen to server instances changes.
304      *
305      * @l listener which should be added.
306      *
307      * @since 1.6
308      */

309     public final void addInstanceListener(InstanceListener l) {
310         ServerRegistry.getInstance ().addInstanceListener(l);
311     }
312
313     /**
314      * Remove an instance listener which has been registered previously.
315      *
316      * @l listener which should be removed.
317      *
318      * @since 1.6
319      */

320     public final void removeInstanceListener(InstanceListener l) {
321         ServerRegistry.getInstance ().removeInstanceListener(l);
322     }
323     
324     public static interface Logger {
325         public void log(String JavaDoc message);
326     }
327 }
328
Popular Tags