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;
031:
032: import org.netbeans.modules.j2ee.deployment.plugins.api.ServerDebugInfo;
033: import org.netbeans.modules.j2ee.deployment.plugins.spi.StartServer;
034: import org.openide.util.RequestProcessor;
035:
036: import javax.enterprise.deploy.shared.CommandType;
037: import javax.enterprise.deploy.shared.StateType;
038: import javax.enterprise.deploy.spi.Target;
039: import javax.enterprise.deploy.spi.TargetModuleID;
040: import javax.enterprise.deploy.spi.exceptions.OperationUnsupportedException;
041: import javax.enterprise.deploy.spi.status.ClientConfiguration;
042: import javax.enterprise.deploy.spi.status.DeploymentStatus;
043: import javax.enterprise.deploy.spi.status.ProgressListener;
044: import javax.enterprise.deploy.spi.status.ProgressObject;
045: import java.util.logging.Level;
046: import java.util.logging.Logger;
047:
048: public final class ResinStartServer extends StartServer {
049: private static final PluginL10N L = new PluginL10N(
050: ResinStartServer.class);
051: private static final Logger log = Logger
052: .getLogger(ResinStartServer.class.getName());
053:
054: private enum Mode {
055: RUN, DEBUG
056: }
057:
058: private final ResinDeploymentManager _manager;
059:
060: public ResinStartServer(ResinDeploymentManager manager) {
061: _manager = manager;
062: }
063:
064: public boolean isAlsoTargetServer(Target target) {
065: return true;
066: }
067:
068: public boolean supportsStartDeploymentManager() {
069: return true;
070: }
071:
072: public boolean supportsStartDebugging(Target target) {
073: return true;
074: }
075:
076: public ProgressObject startDeploymentManager() {
077: StartRunnable start = new StartRunnable(Mode.RUN,
078: CommandType.START);
079: RequestProcessor.getDefault().post(start);
080: return start;
081: }
082:
083: public ProgressObject stopDeploymentManager() {
084: StartRunnable start = new StartRunnable(Mode.RUN,
085: CommandType.STOP);
086: RequestProcessor.getDefault().post(start);
087: return start;
088: }
089:
090: public boolean needsStartForConfigure() {
091: return false;
092: }
093:
094: public boolean needsStartForTargetList() {
095: return true;
096: }
097:
098: public boolean needsStartForAdminConfig() {
099: return false;
100: }
101:
102: public boolean isRunning() {
103: return _manager.getResinProcess().isActive()
104: && _manager.getResinProcess().isResponding();
105: }
106:
107: public boolean isDebuggable(Target target) {
108: return _manager.getResinProcess().isActive()
109: && _manager.getResinProcess().isDebug();
110: }
111:
112: public ProgressObject startDebugging(Target target) {
113: StartRunnable start = new StartRunnable(Mode.DEBUG,
114: CommandType.START);
115: RequestProcessor.getDefault().post(start);
116:
117: return start;
118: }
119:
120: public ServerDebugInfo getDebugInfo(Target target) {
121: ResinConfiguration resinConfiguration = _manager
122: .getResinConfiguration();
123:
124: String address = resinConfiguration.getServerAddress();
125: int debugPort = resinConfiguration.getDebugPort();
126:
127: return new ServerDebugInfo(address, debugPort);
128: }
129:
130: private class StartRunnable implements Runnable, ProgressObject {
131: private final Mode _mode;
132: private final CommandType _command;
133: private final ProgressEventSupport _eventSupport;
134: private Process _process;
135:
136: public StartRunnable(Mode mode, CommandType command) {
137: _mode = mode;
138: _command = command;
139: _eventSupport = new ProgressEventSupport(this );
140:
141: // must be in constructor to stop netbeans nullpointer
142: if (_command == CommandType.START)
143: fireProgressEvent(StateType.RUNNING, L
144: .l("Waiting for Resin to start..."));
145: else if (_command == CommandType.STOP)
146: fireProgressEvent(StateType.RUNNING, L
147: .l("Waiting for Resin to stop..."));
148: else
149: throw new AssertionError(_command.toString());
150: }
151:
152: private void fireProgressEvent(StateType state, String msg) {
153: log.log(state == StateType.FAILED ? Level.WARNING
154: : Level.INFO, msg);
155:
156: DeploymentStatusImpl deploymentStatusImpl = new DeploymentStatusImpl(
157: _command, msg, state);
158:
159: _eventSupport.fireProgressEvent(null, deploymentStatusImpl);
160: }
161:
162: public void run() {
163: if (_command == CommandType.START) {
164: ResinProcess resinProcess = _manager.getResinProcess();
165:
166: try {
167: if (_mode == Mode.DEBUG)
168: resinProcess.startDebug();
169: else
170: resinProcess.start();
171:
172: fireProgressEvent(StateType.COMPLETED, L
173: .l("Resin start completed"));
174: } catch (IllegalStateException ex) {
175: fireProgressEvent(StateType.FAILED, ex
176: .getLocalizedMessage());
177: log.log(Level.FINE, ex.toString(), ex);
178: } catch (Exception ex) {
179: fireProgressEvent(StateType.FAILED, ex.toString());
180: log.log(Level.WARNING, ex.toString(), ex);
181: }
182: } else if (_command == CommandType.STOP) {
183: ResinProcess resinProcess = _manager.getResinProcess();
184:
185: if (!resinProcess.isActive()) {
186: fireProgressEvent(
187: StateType.COMPLETED,
188: L
189: .l("Resin stop completed, but there was no process to stop."));
190: } else {
191: try {
192: resinProcess.stop();
193: fireProgressEvent(StateType.COMPLETED, L
194: .l("Resin stop completed."));
195: } catch (Exception ex) {
196: log.log(Level.WARNING, ex.toString(), ex);
197:
198: fireProgressEvent(StateType.COMPLETED, L.l(
199: "Error stopping Resin: {0}", ex
200: .toString()));
201: }
202: }
203: } else
204: throw new AssertionError(_command.toString());
205: }
206:
207: public DeploymentStatus getDeploymentStatus() {
208: return _eventSupport.getDeploymentStatus();
209: }
210:
211: public TargetModuleID[] getResultTargetModuleIDs() {
212: return null;
213: }
214:
215: public ClientConfiguration getClientConfiguration(
216: TargetModuleID targetModuleID) {
217: return null;
218: }
219:
220: public boolean isCancelSupported() {
221: return false;
222: }
223:
224: public void cancel() throws OperationUnsupportedException {
225: throw new OperationUnsupportedException(
226: "Cancel is not supported");
227: }
228:
229: public boolean isStopSupported() {
230: return false;
231: }
232:
233: public void stop() throws OperationUnsupportedException {
234: throw new OperationUnsupportedException(
235: "Stop is not supported");
236: }
237:
238: public void addProgressListener(
239: ProgressListener progressListener) {
240: _eventSupport.addProgressListener(progressListener);
241: }
242:
243: public void removeProgressListener(
244: ProgressListener progressListener) {
245: _eventSupport.removeProgressListener(progressListener);
246: }
247: }
248:
249: }
|