001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.tomcat5.wizard;
043:
044: import java.io.File;
045: import java.io.IOException;
046: import java.util.Enumeration;
047: import java.util.HashSet;
048: import java.util.NoSuchElementException;
049: import java.util.Properties;
050: import java.util.Set;
051: import java.util.logging.Level;
052: import java.util.logging.Logger;
053: import javax.enterprise.deploy.spi.exceptions.DeploymentManagerCreationException;
054: import javax.swing.JComponent;
055: import org.netbeans.modules.j2ee.deployment.plugins.api.InstanceProperties;
056: import org.netbeans.modules.tomcat5.TomcatFactory;
057: import org.netbeans.modules.tomcat5.TomcatManager;
058: import org.netbeans.modules.tomcat5.TomcatManager.TomcatVersion;
059: import org.netbeans.modules.tomcat5.util.TomcatInstallUtil;
060: import org.openide.DialogDisplayer;
061: import org.openide.NotifyDescriptor;
062: import org.openide.WizardDescriptor;
063: import org.openide.filesystems.FileUtil;
064: import org.openide.util.NbBundle;
065: import org.openide.util.Utilities;
066: import org.netbeans.modules.tomcat5.ide.*;
067: import org.netbeans.modules.tomcat5.util.TomcatProperties;
068: import org.netbeans.modules.tomcat5.util.TomcatUsers;
069:
070: /**
071: * Iterator for the add Tomcat server wizard.
072: *
073: * @author abadea
074: */
075: public class AddInstanceIterator implements
076: WizardDescriptor.InstantiatingIterator {
077:
078: public final static String PROP_ERROR_MESSAGE = "WizardPanel_errorMessage"; // NOI18N
079: private final static String PROP_CONTENT_DATA = "WizardPanel_contentData"; // NOI18N
080: private final static String PROP_CONTENT_SELECTED_INDEX = "WizardPanel_contentSelectedIndex"; // NOI18N
081: private final static String PROP_DISPLAY_NAME = "ServInstWizard_displayName"; // NOI18N
082:
083: private final static String[] CONTENT_DATA = new String[] { NbBundle
084: .getMessage(AddInstanceIterator.class,
085: "LBL_InstallationAndLoginDetails") };
086:
087: private WizardDescriptor wizard;
088: private InstallPanel panel;
089:
090: private final TomcatVersion tomcatVersion;
091:
092: public AddInstanceIterator(TomcatVersion tomcatVersion) {
093: this .tomcatVersion = tomcatVersion;
094:
095: }
096:
097: public void removeChangeListener(javax.swing.event.ChangeListener l) {
098: }
099:
100: public void addChangeListener(javax.swing.event.ChangeListener l) {
101: }
102:
103: public void uninitialize(WizardDescriptor wizard) {
104: }
105:
106: public void initialize(WizardDescriptor wizard) {
107: this .wizard = wizard;
108: }
109:
110: public void previousPanel() {
111: throw new NoSuchElementException();
112: }
113:
114: public void nextPanel() {
115: throw new NoSuchElementException();
116: }
117:
118: public String name() {
119: return null;
120: }
121:
122: public Set instantiate() throws java.io.IOException {
123: Set result = new HashSet();
124: String displayName = getDisplayName();
125: String url = panel.getVisual().getUrl();
126: String username = panel.getVisual().getUsername();
127: String password = panel.getVisual().getPassword();
128: try {
129: InstanceProperties ip = InstanceProperties
130: .createInstanceProperties(url, username, password,
131: displayName);
132: Properties prop = panel.getVisual().getProperties();
133: Enumeration en = prop.propertyNames();
134: while (en.hasMoreElements()) {
135: String key = (String) en.nextElement();
136: ip.setProperty(key, prop.getProperty(key));
137: }
138: ip
139: .setProperty(
140: TomcatProperties.PROP_RUNNING_CHECK_TIMEOUT,
141: Integer
142: .toString(TomcatProperties.DEF_VALUE_RUNNING_CHECK_TIMEOUT));
143:
144: result.add(ip);
145: checkStartupScript(panel.getVisual().getHomeDir());
146:
147: // TODO: refactor the ensureCatalinaBaseReady out of the TomcatManager class and let it throw exceptions
148: // when something goes wrong, so that we can provide reasonable feedback about failures to the user
149: TomcatManager manager = null;
150: try {
151: switch (panel.getVisual().getTomcatVersion()) {
152: case TOMCAT_50:
153: manager = (TomcatManager) TomcatFactory.create50()
154: .getDeploymentManager(url, username,
155: password);
156: break;
157: case TOMCAT_55:
158: manager = (TomcatManager) TomcatFactory.create55()
159: .getDeploymentManager(url, username,
160: password);
161: break;
162: case TOMCAT_60:
163: default:
164: manager = (TomcatManager) TomcatFactory.create60()
165: .getDeploymentManager(url, username,
166: password);
167: }
168: } catch (DeploymentManagerCreationException e) {
169: // this should never happen
170: Logger.getLogger(AddInstanceIterator.class.getName())
171: .log(Level.SEVERE, null, e);
172: return result;
173: }
174: manager.ensureCatalinaBaseReady();
175: if (panel.getVisual().createUserEnabled()) {
176: File tomcatUsersXml = new File(manager
177: .getTomcatProperties().getCatalinaDir(),
178: "conf/tomcat-users.xml"); // NOI18N
179: TomcatUsers.createUser(tomcatUsersXml, username,
180: password);
181: }
182: } catch (IOException e) {
183: // TODO: remove this catch as soon as the ensureCatalinaBaseReady method is refactored
184: Logger.getLogger(AddInstanceIterator.class.getName()).log(
185: Level.WARNING, null, e);
186: }
187: return result;
188: }
189:
190: public boolean hasPrevious() {
191: return false;
192: }
193:
194: public boolean hasNext() {
195: return false;
196: }
197:
198: public WizardDescriptor.Panel current() {
199: if (panel == null) {
200: panel = new InstallPanel(tomcatVersion);
201: }
202: setContentData((JComponent) panel.getComponent());
203: setContentSelectedIndex((JComponent) panel.getComponent());
204: return panel;
205: }
206:
207: private void setContentData(JComponent component) {
208: if (component.getClientProperty(PROP_CONTENT_DATA) == null) {
209: component
210: .putClientProperty(PROP_CONTENT_DATA, CONTENT_DATA);
211: }
212: }
213:
214: private void setContentSelectedIndex(JComponent component) {
215: if (component.getClientProperty(PROP_CONTENT_SELECTED_INDEX) == null) {
216: component.putClientProperty(PROP_CONTENT_SELECTED_INDEX,
217: Integer.valueOf(0));
218: }
219: }
220:
221: private String getDisplayName() {
222: return (String) wizard.getProperty(PROP_DISPLAY_NAME);
223: }
224:
225: /** check for missing startup script - workaround for Tomcat Windows installer distribution */
226: private void checkStartupScript(File homeDir) {
227: String CATALINA = Utilities.isWindows() ? StartTomcat.CATALINA_BAT
228: : StartTomcat.CATALINA_SH;
229: boolean catalinaOK = new File(homeDir, "bin/" + CATALINA)
230: .exists(); // NOI18N
231:
232: String SETCLASSPATH = Utilities.isWindows() ? StartTomcat.SETCLASSPATH_BAT
233: : StartTomcat.SETCLASSPATH_SH;
234: boolean setclasspathOK = new File(homeDir, "bin/"
235: + SETCLASSPATH).exists(); // NOI18N
236:
237: if (!catalinaOK || !setclasspathOK) {
238: File bundledHome = TomcatInstallUtil.getBundledHome();
239: // INFO: DO NOT FORGET TO CHANGE THE BUNDLED TOMCAT VERSION (AND THE VERSION STRING BELLOW) WHEN UPGRADING
240: // 5.5.17 - hopefully this string helps not to miss it;)
241: if (tomcatVersion != TomcatManager.TomcatVersion.TOMCAT_55
242: || bundledHome == null || !bundledHome.exists()) {
243: // If there is no bundled Tomcat or the being installed Tomcat is a different
244: // version, there is no place where to get the startup scripts from. Lets inform
245: // the user about the problem at least.
246: String msg;
247: if (!catalinaOK && !setclasspathOK) {
248: msg = NbBundle.getMessage(
249: AddInstanceIterator.class,
250: "MSG_no_startup_scripts_fix_by_hand",
251: CATALINA, SETCLASSPATH);
252: } else {
253: msg = NbBundle.getMessage(
254: AddInstanceIterator.class,
255: "MSG_no_startup_script_fix_by_hand",
256: !catalinaOK ? CATALINA : SETCLASSPATH);
257: }
258: NotifyDescriptor nd = new NotifyDescriptor.Message(msg,
259: NotifyDescriptor.WARNING_MESSAGE);
260: DialogDisplayer.getDefault().notify(nd);
261: } else {
262: // The IDE can copy the startup scripts for the user, lets ask
263: // him whether to do it or not.
264: String msg;
265: if (!catalinaOK && !setclasspathOK) {
266: msg = NbBundle.getMessage(
267: AddInstanceIterator.class,
268: "MSG_no_startup_scripts", CATALINA,
269: SETCLASSPATH);
270: } else {
271: msg = NbBundle.getMessage(
272: AddInstanceIterator.class,
273: "MSG_no_startup_script",
274: !catalinaOK ? CATALINA : SETCLASSPATH);
275: }
276: NotifyDescriptor nd = new NotifyDescriptor.Confirmation(
277: msg, NotifyDescriptor.YES_NO_OPTION);
278: if (DialogDisplayer.getDefault().notify(nd).equals(
279: NotifyDescriptor.YES_OPTION)) {
280: try {
281: if (bundledHome != null) {
282: if (!catalinaOK) {
283: FileUtil.copyFile(FileUtil
284: .toFileObject(new File(
285: bundledHome, "bin/"
286: + CATALINA)), // NOI18N
287: FileUtil.toFileObject(new File(
288: homeDir, "bin")), // NOI18N
289: CATALINA.substring(0, CATALINA
290: .indexOf(".")) // NOI18N
291: );
292: }
293: if (!setclasspathOK) {
294: FileUtil
295: .copyFile(
296: FileUtil
297: .toFileObject(new File(
298: bundledHome,
299: "bin/"
300: + SETCLASSPATH)), // NOI18N
301: FileUtil
302: .toFileObject(new File(
303: homeDir,
304: "bin")), // NOI18N
305: SETCLASSPATH
306: .substring(
307: 0,
308: SETCLASSPATH
309: .indexOf(".")) // NOI18N
310: );
311: }
312: }
313: } catch (IOException e) {
314: msg = NbBundle.getMessage(
315: AddInstanceIterator.class,
316: "MSG_startup_scripts_copy_failed");
317: nd = new NotifyDescriptor.Message(msg,
318: NotifyDescriptor.WARNING_MESSAGE);
319: DialogDisplayer.getDefault().notify(nd);
320: }
321: }
322: }
323: }
324: }
325: }
|