KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > tomcat5 > wizard > AddInstanceIterator


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.tomcat5.wizard;
21
22 import java.io.File JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.util.Enumeration JavaDoc;
25 import java.util.HashSet JavaDoc;
26 import java.util.NoSuchElementException JavaDoc;
27 import java.util.Properties JavaDoc;
28 import java.util.Set JavaDoc;
29 import javax.swing.JComponent JavaDoc;
30 import org.netbeans.modules.j2ee.deployment.plugins.api.InstanceProperties;
31 import org.netbeans.modules.tomcat5.TomcatManager;
32 import org.netbeans.modules.tomcat5.TomcatManager.TomcatVersion;
33 import org.netbeans.modules.tomcat5.util.TomcatInstallUtil;
34 import org.openide.DialogDisplayer;
35 import org.openide.ErrorManager;
36 import org.openide.NotifyDescriptor;
37 import org.openide.WizardDescriptor;
38 import org.openide.filesystems.FileUtil;
39 import org.openide.util.NbBundle;
40 import org.openide.util.Utilities;
41 import org.netbeans.modules.tomcat5.ide.*;
42 import org.netbeans.modules.tomcat5.util.TomcatProperties;
43
44 /**
45  * Iterator for the add Tomcat server wizard.
46  *
47  * @author abadea
48  */

49 public class AddInstanceIterator implements WizardDescriptor.InstantiatingIterator {
50     
51     public final static String JavaDoc PROP_ERROR_MESSAGE = "WizardPanel_errorMessage"; // NOI18N
52
private final static String JavaDoc PROP_CONTENT_DATA = "WizardPanel_contentData"; // NOI18N
53
private final static String JavaDoc PROP_CONTENT_SELECTED_INDEX = "WizardPanel_contentSelectedIndex"; // NOI18N
54
private final static String JavaDoc PROP_DISPLAY_NAME = "ServInstWizard_displayName"; // NOI18N
55

56     private final static String JavaDoc[] CONTENT_DATA = new String JavaDoc[] {
57         NbBundle.getMessage(AddInstanceIterator.class, "LBL_InstanceProperties") };
58
59     private WizardDescriptor wizard;
60     private InstallPanel panel;
61     
62     private final TomcatVersion tomcatVersion;
63     
64     public AddInstanceIterator(TomcatVersion tomcatVersion) {
65         this.tomcatVersion = tomcatVersion;
66         
67     }
68
69     public void removeChangeListener(javax.swing.event.ChangeListener JavaDoc l) {
70     }
71
72     public void addChangeListener(javax.swing.event.ChangeListener JavaDoc l) {
73     }
74
75     public void uninitialize(WizardDescriptor wizard) {
76     }
77
78     public void initialize(WizardDescriptor wizard) {
79         this.wizard = wizard;
80     }
81
82     public void previousPanel() {
83         throw new NoSuchElementException JavaDoc();
84     }
85
86     public void nextPanel() {
87         throw new NoSuchElementException JavaDoc();
88     }
89
90     public String JavaDoc name() {
91         return null;
92     }
93
94     public Set JavaDoc instantiate() throws java.io.IOException JavaDoc {
95         Set JavaDoc result = new HashSet JavaDoc();
96         String JavaDoc displayName = getDisplayName();
97         String JavaDoc url = panel.getVisual().getUrl();
98         String JavaDoc username = panel.getVisual().getUsername();
99         String JavaDoc password = panel.getVisual().getPassword();
100         try {
101             InstanceProperties ip = InstanceProperties.createInstanceProperties(
102                     url, username, password, displayName);
103             Properties JavaDoc prop = panel.getVisual().getProperties ();
104             Enumeration JavaDoc en = prop.propertyNames ();
105             while (en.hasMoreElements ()) {
106                 String JavaDoc key = (String JavaDoc) en.nextElement ();
107                 ip.setProperty (key, prop.getProperty (key));
108             }
109             ip.setProperty(TomcatProperties.PROP_RUNNING_CHECK_TIMEOUT,
110                     Integer.toString(TomcatProperties.DEF_VALUE_RUNNING_CHECK_TIMEOUT));
111             
112             result.add(ip);
113             checkStartupScript(panel.getVisual().getHomeDir());
114         } catch (Exception JavaDoc ex) {
115             ErrorManager.getDefault().log(ErrorManager.EXCEPTION, ex.getMessage());
116         }
117         return result;
118     }
119
120     public boolean hasPrevious() {
121         return false;
122     }
123
124     public boolean hasNext() {
125         return false;
126     }
127
128     public WizardDescriptor.Panel current() {
129         if (panel == null) {
130             panel = new InstallPanel(tomcatVersion);
131         }
132         setContentData((JComponent JavaDoc)panel.getComponent());
133         setContentSelectedIndex((JComponent JavaDoc)panel.getComponent());
134         return panel;
135     }
136
137     private void setContentData(JComponent JavaDoc component) {
138         if (component.getClientProperty(PROP_CONTENT_DATA) == null) {
139             component.putClientProperty(PROP_CONTENT_DATA, CONTENT_DATA);
140         }
141     }
142
143     private void setContentSelectedIndex(JComponent JavaDoc component) {
144         if (component.getClientProperty(PROP_CONTENT_SELECTED_INDEX) == null) {
145             component.putClientProperty(PROP_CONTENT_SELECTED_INDEX, new Integer JavaDoc(0));
146         }
147     }
148
149     private String JavaDoc getDisplayName() {
150         return (String JavaDoc)wizard.getProperty(PROP_DISPLAY_NAME);
151     }
152     
153     /** check for missing startup script - workaround for Tomcat Windows installer distribution */
154     private void checkStartupScript(File JavaDoc homeDir) {
155         String JavaDoc CATALINA = Utilities.isWindows() ? StartTomcat.CATALINA_BAT
156                                                 : StartTomcat.CATALINA_SH;
157         boolean catalinaOK = new File JavaDoc(homeDir, "bin/" + CATALINA).exists(); // NOI18N
158

159         String JavaDoc SETCLASSPATH = Utilities.isWindows() ? StartTomcat.SETCLASSPATH_BAT
160                                                     : StartTomcat.SETCLASSPATH_SH;
161         boolean setclasspathOK = new File JavaDoc(homeDir, "bin/" + SETCLASSPATH).exists(); // NOI18N
162

163         if (!catalinaOK || !setclasspathOK) {
164             File JavaDoc bundledHome = TomcatInstallUtil.getBundledHome();
165             // INFO: DO NOT FORGET TO CHANGE THE BUNDLED TOMCAT VERSION (AND THE VERSION STRING BELLOW) WHEN UPGRADING
166
// 5.5.17 - hopefully this string helps not to miss it;)
167
if (tomcatVersion != TomcatManager.TomcatVersion.TOMCAT_55
168                     || bundledHome == null || !bundledHome.exists()) {
169                 // If there is no bundled Tomcat or the being installed Tomcat is a different
170
// version, there is no place where to get the startup scripts from. Lets inform
171
// the user about the problem at least.
172
String JavaDoc msg;
173                 if (!catalinaOK && !setclasspathOK) {
174                     msg = NbBundle.getMessage(AddInstanceIterator.class, "MSG_no_startup_scripts_fix_by_hand", CATALINA, SETCLASSPATH);
175                 } else {
176                     msg = NbBundle.getMessage(AddInstanceIterator.class, "MSG_no_startup_script_fix_by_hand", !catalinaOK ? CATALINA : SETCLASSPATH);
177                 }
178                 NotifyDescriptor nd = new NotifyDescriptor.Message(msg, NotifyDescriptor.WARNING_MESSAGE);
179                 DialogDisplayer.getDefault().notify(nd);
180             } else {
181                 // The IDE can copy the startup scripts for the user, lets ask
182
// him whether to do it or not.
183
String JavaDoc msg;
184                 if (!catalinaOK && !setclasspathOK) {
185                     msg = NbBundle.getMessage(AddInstanceIterator.class, "MSG_no_startup_scripts", CATALINA, SETCLASSPATH);
186                 } else {
187                     msg = NbBundle.getMessage(AddInstanceIterator.class, "MSG_no_startup_script", !catalinaOK ? CATALINA : SETCLASSPATH);
188                 }
189                 NotifyDescriptor nd =
190                         new NotifyDescriptor.Confirmation(msg, NotifyDescriptor.YES_NO_OPTION);
191                 if (DialogDisplayer.getDefault().notify(nd).equals(NotifyDescriptor.YES_OPTION)) {
192                     try {
193                         if (bundledHome != null) {
194                             if (!catalinaOK) {
195                                 FileUtil.copyFile(
196                                     FileUtil.toFileObject(new File JavaDoc(bundledHome, "bin/" + CATALINA)), // NOI18N
197
FileUtil.toFileObject(new File JavaDoc(homeDir, "bin")), // NOI18N
198
CATALINA.substring(0, CATALINA.indexOf(".")) // NOI18N
199
);
200                             }
201                             if (!setclasspathOK) {
202                                 FileUtil.copyFile(
203                                     FileUtil.toFileObject(new File JavaDoc(bundledHome, "bin/" + SETCLASSPATH)), // NOI18N
204
FileUtil.toFileObject(new File JavaDoc(homeDir, "bin")), // NOI18N
205
SETCLASSPATH.substring(0, SETCLASSPATH.indexOf(".")) // NOI18N
206
);
207                             }
208                         }
209                     } catch (IOException JavaDoc e) {
210                         msg = NbBundle.getMessage(AddInstanceIterator.class, "MSG_startup_scripts_copy_failed");
211                         nd = new NotifyDescriptor.Message(msg, NotifyDescriptor.WARNING_MESSAGE);
212                         DialogDisplayer.getDefault().notify(nd);
213                     }
214                 }
215             }
216         }
217     }
218 }
219
Popular Tags