001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: *
023: * Free Software Foundation, Inc.
024: * 59 Temple Place, Suite 330
025: * Boston, MA 02111-1307 USA
026: *
027: * @author Sam
028: */
029:
030: package com.caucho.netbeans.ide;
031:
032: import com.caucho.netbeans.ide.ui.AddServerLocationPanel;
033: import java.io.File;
034: import java.io.IOException;
035: import java.util.ArrayList;
036: import java.util.Arrays;
037: import java.util.HashSet;
038: import java.util.Set;
039: import java.util.logging.Level;
040: import java.util.logging.Logger;
041: import javax.swing.JComponent;
042: import javax.swing.event.ChangeEvent;
043: import javax.swing.event.ChangeListener;
044: import org.netbeans.modules.j2ee.deployment.plugins.api.InstanceCreationException;
045: import org.netbeans.modules.j2ee.deployment.plugins.api.InstanceProperties;
046: import org.openide.WizardDescriptor;
047: import org.openide.WizardDescriptor.InstantiatingIterator;
048: import org.openide.WizardDescriptor.Panel;
049:
050: public final class AddInstanceIterator implements
051: InstantiatingIterator, ChangeListener {
052: private static final Logger log = Logger
053: .getLogger(AddInstanceIterator.class.getName());
054:
055: private static final String[] RESIN_REQUIRED_JARS;
056: private WizardDescriptor _wizard;
057: private ArrayList<ChangeListener> _listeners = new ArrayList<ChangeListener>();
058:
059: private int _index;
060: private Panel[] _panels;
061:
062: private String _userName;
063: private String _password;
064:
065: private String _resinHome;
066:
067: private String _host = "localhost";
068: private int _port = 8081;
069:
070: public AddInstanceIterator() {
071: _resinHome = findResinHome();
072: }
073:
074: public String getResinHome() {
075: return _resinHome;
076: }
077:
078: public void setResinHome(String resinHome) {
079: _resinHome = resinHome;
080: }
081:
082: public int getPort() {
083: return _port;
084: }
085:
086: public void setPort(int port) {
087: _port = port;
088: }
089:
090: /**
091: * Tries to find a Resin home
092: */
093: private String findResinHome() {
094: String resinHome;
095:
096: resinHome = findResinHome(System.getProperty("user.home"));
097: if (resinHome != null)
098: return resinHome;
099:
100: resinHome = findResinHome(System.getProperty("user.home")
101: + "/ws");
102: if (resinHome != null)
103: return resinHome;
104:
105: resinHome = findResinHome("/usr/local/share");
106: if (resinHome != null)
107: return resinHome;
108:
109: resinHome = findResinHome("/usr/local");
110: if (resinHome != null)
111: return resinHome;
112:
113: resinHome = findResinHome("/opt");
114: if (resinHome != null)
115: return resinHome;
116:
117: return null;
118: }
119:
120: private String findResinHome(String path) {
121: File dir = new File(path);
122:
123: File resin = new File(dir, "resin");
124: if (isResinHomeValid(resin))
125: return resin.getAbsolutePath();
126:
127: String[] list = dir.list();
128: Arrays.sort(list);
129:
130: for (String name : list) {
131: resin = new File(dir, name);
132:
133: if (isResinHomeValid(resin))
134: return resin.getAbsolutePath();
135: }
136:
137: return null;
138: }
139:
140: /**
141: * Checks if the resin-home is valid by checking for expected jar files
142: */
143: public boolean isResinHomeValid() {
144: return isResinHomeValid(new File(_resinHome));
145: }
146:
147: /**
148: * Checks if the resin-home is valid by checking for expected jar files
149: */
150: public boolean isResinHomeValid(File resinHome) {
151: if (!resinHome.isDirectory())
152: return false;
153:
154: File lib = new File(resinHome, "lib");
155:
156: if (!lib.isDirectory())
157: return false;
158:
159: for (String jar : RESIN_REQUIRED_JARS) {
160: File file = new File(lib, jar);
161:
162: if (!file.canRead()) {
163: return false;
164: }
165: }
166:
167: return true;
168: }
169:
170: /**
171: * Checks if the server is valid.
172: */
173: public boolean isValid() {
174: return isResinHomeValid();
175: }
176:
177: /**
178: * Creates a new server instance
179: *
180: * @return a set containing the instance
181: * @throws java.io.IOException
182: */
183: public Set instantiate() throws IOException {
184: HashSet<InstanceProperties> set = new HashSet<InstanceProperties>();
185:
186: if (!isValid())
187: return set;
188:
189: String url = "resin:" + _host + ":" + _port;
190: String displayName = "Resin";
191: try {
192: InstanceProperties ip;
193: ip = InstanceProperties.createInstanceProperties(url,
194: _userName, _password, displayName);
195: ip.setProperty("resin.home", getResinHome());
196: ip.setProperty("resin.host", _host);
197: ip.setProperty("resin.port", String.valueOf(_port));
198: set.add(ip);
199: } catch (InstanceCreationException e) {
200: // XXX: should show
201:
202: log.log(Level.SEVERE, e.getMessage(), e);
203: }
204:
205: return set;
206: }
207:
208: public void initialize(WizardDescriptor wizard) {
209: _wizard = wizard;
210: _panels = new Panel[] { new AddServerLocationPanel(this ), };
211: _index = 0;
212: }
213:
214: public void uninitialize(WizardDescriptor wizard) {
215: }
216:
217: public Panel current() {
218: Panel panel = _panels[_index];
219:
220: String[] steps = new String[] { "Resin Home Location" };
221: JComponent comp = (JComponent) panel.getComponent();
222: comp.putClientProperty("WizardPanel_contentData", steps);
223: comp.putClientProperty("WizardPanel_contentSelectedIndex",
224: _index);
225:
226: return panel;
227: }
228:
229: public String name() {
230: return "Resin 3.1";
231: }
232:
233: public boolean hasNext() {
234: return _index + 1 < _panels.length;
235: }
236:
237: public boolean hasPrevious() {
238: return _index > 0;
239: }
240:
241: public void nextPanel() {
242: if (_index + 1 < _panels.length)
243: _index++;
244: }
245:
246: public void previousPanel() {
247: if (_index > 0)
248: _index--;
249: }
250:
251: public void addChangeListener(ChangeListener listener) {
252: synchronized (_listeners) {
253: _listeners.add(listener);
254: }
255: }
256:
257: public void removeChangeListener(ChangeListener listener) {
258: synchronized (_listeners) {
259: _listeners.remove(listener);
260: }
261: }
262:
263: public void stateChanged(ChangeEvent event) {
264: HashSet<ChangeListener> listeners;
265:
266: synchronized (_listeners) {
267: listeners = new HashSet<ChangeListener>(_listeners);
268: }
269:
270: for (ChangeListener listener : listeners)
271: listener.stateChanged(event);
272: }
273:
274: static {
275: RESIN_REQUIRED_JARS = new String[] { "resin.jar",
276: "resin-util.jar", };
277: }
278: }
|