KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > websphere6 > ui > wizard > WSInstantiatingIterator


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 package org.netbeans.modules.j2ee.websphere6.ui.wizard;
20
21 import java.util.*;
22 import java.io.*;
23 import java.net.*;
24 import javax.swing.*;
25 import javax.swing.event.*;
26
27 import org.openide.*;
28 import org.openide.util.*;
29 import org.netbeans.modules.j2ee.deployment.plugins.api.*;
30
31 import org.netbeans.modules.j2ee.websphere6.*;
32
33 /**
34  * The main class of the custom wizard for registering a new server instance.
35  * It performs all the orchestration of the panels and actually creates the
36  * instance.
37  *
38  * @author Kirill Sorokin
39  */

40 public class WSInstantiatingIterator
41         implements WizardDescriptor.InstantiatingIterator {
42     /**
43      * Since the WizardDescriptor does not expose the property name for the
44      * error message label, we have to keep it here also
45      */

46     private static final String JavaDoc PROP_DISPLAY_NAME =
47             "ServInstWizard_displayName"; // NOI18N
48

49     /**
50      * The default debugger port for the instance, it will be assigned to it
51      * at creation time and can be changed via the properties sheet
52      */

53     private static final String JavaDoc DEFAULT_DEBUGGER_PORT = "8787";
54     
55     /**
56      * The parent wizard descriptor
57      */

58     private WizardDescriptor wizardDescriptor;
59     
60     /**
61      * A misterious method whose purpose is obviously in freeing the resources
62      * obtained by the wizard during instance registration. We do not need such
63      * functionality, thus we do not implement it.
64      */

65     public void uninitialize(WizardDescriptor wizardDescriptor) {
66         // do nothing as we do not need to release any resources
67
}
68     
69     /**
70      * This method initializes the wizard. AS for us the only thing we should
71      * do is save the wizard descriptor handle.
72      *
73      * @param wizardDescriptor the parent wizard descriptor
74      */

75     public void initialize(WizardDescriptor wizardDescriptor) {
76         this.wizardDescriptor = wizardDescriptor;
77     }
78     
79     /**
80      * Returns the name for the wizard. I failed to find a place where it
81      * could be used, so we do not return anything sensible
82      *
83      * @return the wizard name
84      */

85     public String JavaDoc name() {
86         return ""; // NOI18N
87
}
88     
89     /**
90      * This methos actually creates the instance. It fetches all the required
91      * parameters, builds the URL and calls
92      * InstanceProperties.createInstamceProperties(), which registers the
93      * instance.
94      *
95      * @return a set of created instance properties
96      */

97     public Set instantiate() throws IOException {
98         // initialize the resulting set
99
Set result = new HashSet();
100         
101         // build the URL
102
String JavaDoc url = /*"["+domainRoot+"]"+*/WSURIManager.WSURI + this.host + ":" + // NOI18N
103
this.port;
104         
105         // build the display name
106
String JavaDoc displayName = getDisplayName() + " [" + this.host + // NOI18N
107
":" + this.port +"]"; // NOI18N
108

109         // if all the data is normally validated - create the instance and
110
// attach the additional properties
111
if (validate()) {
112             InstanceProperties ip = InstanceProperties.
113                     createInstanceProperties(url, username, password,
114                     displayName);
115             ip.setProperty(WSDeploymentFactory.SERVER_ROOT_ATTR, serverRoot);
116             ip.setProperty(WSDeploymentFactory.DOMAIN_ROOT_ATTR, domainRoot);
117             ip.setProperty(WSDeploymentFactory.IS_LOCAL_ATTR, isLocal);
118             ip.setProperty(WSDeploymentFactory.SERVER_NAME_ATTR, serverName);
119             ip.setProperty(WSDeploymentFactory.DEBUGGER_PORT_ATTR,
120                     DEFAULT_DEBUGGER_PORT);
121             ip.setProperty(WSDeploymentFactory.CONFIG_XML_PATH, configXmlPath);
122             ip.setProperty(WSDeploymentFactory.ADMIN_PORT_ATTR, adminPort);
123         ip.setProperty(WSDeploymentFactory.DEFAULT_HOST_PORT_ATTR, defaultHostPort);
124             
125             // add the created instance properties to the result set
126
result.add(ip);
127             
128             // return the result
129
return result;
130         } else {
131             return null;
132         }
133     }
134     
135     /**
136      * Validates the input data, by attempting to open a socket to the
137      * specified host and port, expecting to get an UnknownHostException
138      *
139      * @return true, if the validation goes smoothly, false otherwise
140      */

141     private boolean validate() {
142         // try to open a socket to the specified host and port, if we get an
143
// UnknownHostException this means that the entered host is invalid, all
144
// other exception may merely mean that the server is not started
145
try {
146             new Socket(getHost(), new Integer JavaDoc(getPort()).intValue());
147         } catch (UnknownHostException e) {
148             JOptionPane.showMessageDialog(serverPropertiesPanel,
149                     NbBundle.getMessage(WSInstantiatingIterator.class,
150                     "MSG_unknownHost", getHost()),
151                     NbBundle.getMessage(WSInstantiatingIterator.class,
152                     "MSG_instanceCreationFailed"), JOptionPane.ERROR_MESSAGE);
153             return false;
154         } catch (IOException e) {
155             // in this case we do nothing because it could simply mean that the
156
// server is not started
157
}
158         
159         // the validation passed normally
160
return true;
161     }
162     
163     /**
164      * Gets the wizards display name
165      *
166      * @return the display name
167      */

168     private String JavaDoc getDisplayName() {
169         return (String JavaDoc) wizardDescriptor.getProperty(PROP_DISPLAY_NAME);
170     }
171     
172     // the main and additional instance properties
173
private String JavaDoc serverRoot;
174     private String JavaDoc domainRoot;
175     private String JavaDoc isLocal;
176     private String JavaDoc host;
177     private String JavaDoc port;
178     private String JavaDoc username;
179     private String JavaDoc password;
180     private String JavaDoc serverName;
181     private String JavaDoc configXmlPath;
182     private String JavaDoc adminPort;
183     private String JavaDoc defaultHostPort;
184     /**
185      * Setter for the server installation directory
186      *
187      * @param serverRoot the new server installation directory path
188      */

189     public void setServerRoot(String JavaDoc serverRoot) {
190         this.serverRoot = serverRoot;
191         
192         // reinit the instances list
193
serverPropertiesPanel.getWizardServerProperties().
194                 updateInstancesList(serverRoot);
195     }
196     
197     /**
198      * Getter for the server installation directory
199      *
200      * @return the server installation directory path
201      */

202     public String JavaDoc getServerRoot() {
203         return this.serverRoot;
204     }
205     
206     /**
207      * Setter for the profile root directory
208      *
209      * @param domainRoot the new profile root directory path
210      */

211     public void setDomainRoot(String JavaDoc domainRoot) {
212         this.domainRoot = domainRoot;
213     }
214
215     /**
216      * Getter for the profile root directory
217      *
218      * @return the profile root directory path
219      */

220     public String JavaDoc getDomainRoot() {
221         return domainRoot;
222     }
223
224     /**
225      * Getter for the server host
226      *
227      * @return the server host
228      */

229     public String JavaDoc getHost() {
230         return host;
231     }
232
233     /**
234      * Setter for the server host
235      *
236      * @param host the new server host
237      */

238     public void setHost(String JavaDoc host) {
239         this.host = host;
240     }
241
242     /**
243      * Getter for the server port
244      *
245      * @return the server port
246      */

247     public String JavaDoc getPort() {
248         return port;
249     }
250
251     /**
252      * Setter for the server port
253      *
254      * @param port the new server port
255      */

256     public void setPort(String JavaDoc port) {
257         this.port = port;
258     }
259
260     /**
261      * Getter for the username
262      *
263      * @return the username
264      */

265     public String JavaDoc getUsername() {
266         return username;
267     }
268
269     /**
270      * Setter for the username
271      *
272      * @param username the new username
273      */

274     public void setUsername(String JavaDoc username) {
275         this.username = username;
276     }
277
278     /**
279      * Getter for the password
280      *
281      * @return the password
282      */

283     public String JavaDoc getPassword() {
284         return password;
285     }
286
287     /**
288      * Setter for the password
289      *
290      * @param password the new password
291      */

292     public void setPassword(String JavaDoc password) {
293         this.password = password;
294     }
295     
296     /**
297      * Getter for the isLocal property
298      *
299      * @return "true" if the server is local, "false" otherwise
300      */

301     public String JavaDoc getIsLocal() {
302         return this.isLocal;
303     }
304     
305     /**
306      * Setter for the isLocal property
307      *
308      * @param isLocal "true" if the server is local, "false" otherwise
309      */

310     public void setIsLocal(String JavaDoc isLocal) {
311         this.isLocal = isLocal;
312     }
313     
314     /**
315      * Getter for the server name
316      *
317      * @return the server name
318      */

319     public String JavaDoc getServerName() {
320         return this.serverName;
321     }
322     
323     /**
324      * Setter for the server name
325      *
326      * @param serverName the new server name
327      */

328     public void setServerName(String JavaDoc serverName) {
329         this.serverName = serverName;
330     }
331     
332     /**
333      * Getter for the server's config xml file path
334      *
335      * @return the server's config xml file path
336      */

337     public String JavaDoc getConfigXmlPath() {
338         return this.configXmlPath;
339     }
340     
341     /**
342      * Setter for the server's config xml file path
343      *
344      * @param serverName the new server's config xml file path
345      */

346     public void setConfigXmlPath(String JavaDoc configXmlPath) {
347         this.configXmlPath = configXmlPath;
348     }
349     
350     public String JavaDoc getAdminPort() {
351         return adminPort;
352     }
353     
354     public void setAdminPort(String JavaDoc adminPort) {
355         this.adminPort = adminPort;
356     }
357
358     public String JavaDoc getDefaultHostPort() {
359         return defaultHostPort;
360     }
361     
362     public void setDefaultHostPort(String JavaDoc defaultHostPort) {
363         this.defaultHostPort = defaultHostPort;
364     }
365     
366     ////////////////////////////////////////////////////////////////////////////
367
// Panels section
368
////////////////////////////////////////////////////////////////////////////
369
/**
370      * The steps names for the wizard: Server Location & Instance properties
371      */

372     private Vector steps = new Vector();
373     {
374         steps.add(NbBundle.getMessage(ServerPropertiesPanel.class,
375                 "SERVER_LOCATION_STEP")); // NOI18N
376
steps.add(NbBundle.getMessage(ServerPropertiesPanel.class,
377                 "SERVER_PROPERTIES_STEP")); // NOI18N
378
}
379     
380     /**
381      * The wizard's panels
382      */

383     private Vector panels = new Vector();
384     private ServerLocationPanel serverLocationPanel =
385             new ServerLocationPanel(
386                     (String JavaDoc[]) steps.toArray(new String JavaDoc[steps.size()]),
387                     0,
388                     new IteratorListener(), this);
389     private ServerPropertiesPanel serverPropertiesPanel =
390             new ServerPropertiesPanel(
391                     (String JavaDoc[]) steps.toArray(new String JavaDoc[steps.size()]),
392                     1,
393                     new IteratorListener(), this);
394     {
395         panels.add(serverLocationPanel);
396         panels.add(serverPropertiesPanel);
397     }
398     
399     /**
400      * Index of the currently shown panel
401      */

402     private int index;
403     
404     /**
405      * Tells whether the wizard has previous panels. Basically controls the
406      * Back button
407      */

408     public boolean hasPrevious() {
409         return index > 0;
410     }
411     
412     /**
413      * Reverts the wizard to the previous panel if available.
414      * If the previous panel is not available a NoSuchElementException will be
415      * thrown.
416      */

417     public void previousPanel() {
418         
419         if (!hasPrevious()) {
420             throw new NoSuchElementException();
421         }
422         index--;
423     }
424     
425     /**
426      * Tells whether the wizard has next panels. Basically controls the
427      * Next button
428      */

429     public boolean hasNext() {
430         return index < panels.size() - 1;
431     }
432
433     /**
434      * Proceeds the wizard to the next panel if available.
435      * If the next panel is not available a NoSuchElementException will be
436      * thrown.
437      */

438     public void nextPanel() {
439         if (!hasNext()) {
440             throw new NoSuchElementException();
441         }
442         index++;
443     }
444     
445     /**
446      * Returns the current panel of the wizard
447      *
448      * @return current panel of the wizard
449      */

450     public WizardDescriptor.Panel current() {
451         return (WizardDescriptor.Panel) panels.get(index);
452     }
453     
454     ////////////////////////////////////////////////////////////////////////////
455
// Listeners section
456
////////////////////////////////////////////////////////////////////////////
457
/**
458      * The registered listeners
459      */

460     private Vector listeners = new Vector();
461     
462     /**
463      * Removes an already registered listener in a synchronized manner
464      *
465      * @param listener a listener to be removed
466      */

467     public void removeChangeListener(ChangeListener listener) {
468         if (listeners != null) {
469             synchronized (listeners) {
470                 listeners.remove(listener);
471             }
472         }
473     }
474
475     /**
476      * Registers a new listener in a synchronized manner
477      *
478      * @param listener a listener to be registered
479      */

480     public void addChangeListener(ChangeListener listener) {
481         synchronized (listeners) {
482             listeners.add(listener);
483         }
484     }
485     
486     /**
487      * Notifies all the listeners of the supplied event
488      *
489      * @param event the event to be passed to the listeners
490      */

491     private void fireChangeEvent(ChangeEvent event) {
492         // copy the registered listeners, to avoid conflicts if the listeners'
493
// list changes
494
Vector targetListeners;
495         synchronized (listeners) {
496             targetListeners = (Vector) listeners.clone();
497         }
498         
499         // notify each listener of the event
500
for (int i = 0; i < targetListeners.size(); i++) {
501             ChangeListener listener =
502                     (ChangeListener) targetListeners.elementAt(i);
503             listener.stateChanged(event);
504         }
505     }
506     
507     /**
508      * A simple listener that only notifies the parent iterator of all the
509      * events that come to it
510      *
511      * @author Kirill Sorokin
512      */

513     private class IteratorListener implements ChangeListener {
514         /**
515          * Notifies the parent iterator of the supplied event
516          *
517          * @param event the event to be passed to the parent iterator
518          */

519         public void stateChanged(ChangeEvent event) {
520             fireChangeEvent(event);
521         }
522     }
523 }
524
Popular Tags