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.nodes;
031:
032: import com.caucho.netbeans.ResinDeploymentManager;
033: import com.caucho.netbeans.ide.ResinTargetModuleID;
034:
035: import org.openide.DialogDisplayer;
036: import org.openide.NotifyDescriptor;
037: import org.openide.awt.StatusDisplayer;
038: import org.openide.nodes.Children;
039: import org.openide.nodes.Node;
040: import org.openide.util.RequestProcessor;
041:
042: import javax.enterprise.deploy.shared.CommandType;
043: import javax.enterprise.deploy.shared.StateType;
044: import javax.enterprise.deploy.spi.TargetModuleID;
045: import javax.enterprise.deploy.spi.status.DeploymentStatus;
046: import javax.enterprise.deploy.spi.status.ProgressEvent;
047: import javax.enterprise.deploy.spi.status.ProgressListener;
048: import javax.enterprise.deploy.spi.status.ProgressObject;
049:
050: /**
051: * This class represents one J2EE module, UI representation is ResinModuleNode
052: */
053: public final class ResinModule implements Node.Cookie, Comparable,
054: ProgressListener {
055:
056: private final ResinDeploymentManager manager;
057: private final ResinTargetModuleID tmID;
058: private Node node;
059: private final String displayName;
060:
061: /**
062: * Creates a new instance of ResinModule
063: */
064: public ResinModule(ResinDeploymentManager manager,
065: ResinTargetModuleID tmID) {
066: this .manager = manager;
067: this .tmID = tmID;
068: displayName = computeDisplayName();
069: }
070:
071: public String getDisplayName() {
072: return displayName;
073: }
074:
075: public String getShortDescription() {
076: return tmID.getModuleID();
077: }
078:
079: public void setRepresentedNode(Node node) {
080: this .node = node;
081: }
082:
083: public String getWebURL() {
084: return tmID.getWebURL();
085: }
086:
087: public void undeploy() {
088: RequestProcessor.getDefault().post(new Runnable() {
089: public void run() {
090: // TODO: Use the Progress API to track the undeploy progress
091: ProgressObject po = manager
092: .undeploy(new TargetModuleID[] { tmID });
093: po.addProgressListener(ResinModule.this );
094: DeploymentStatus deploymentStatus = po
095: .getDeploymentStatus();
096: // TODO make sure handleDeploymentStatus won't be called twice
097: if (deploymentStatus.isCompleted()
098: || deploymentStatus.isFailed()) {
099: handleDeploymentStatus(deploymentStatus);
100: }
101: }
102: });
103: }
104:
105: private String computeDisplayName() {
106: String result = tmID.getModuleID();
107: int slashIdx = result.lastIndexOf('/');
108: if (slashIdx == result.length() - 1) {
109: // module is deployed as directory, get the second rightmost slash
110: result = result.substring(0, result.length() - 1);
111: slashIdx = result.lastIndexOf('/');
112: }
113: // cut the extension if deployed as archive
114: int dotIdx = result.lastIndexOf('.');
115: int endIdx = (dotIdx > -1 && dotIdx > slashIdx) ? dotIdx
116: : result.length();
117: return result.substring(slashIdx + 1, endIdx);
118: }
119:
120: public void handleProgressEvent(ProgressEvent progressEvent) {
121: handleDeploymentStatus(progressEvent.getDeploymentStatus());
122: }
123:
124: private void handleDeploymentStatus(DeploymentStatus deployStatus) {
125: if (deployStatus.getState() == StateType.COMPLETED) {
126: CommandType command = deployStatus.getCommand();
127: if (command == CommandType.START
128: || command == CommandType.STOP) {
129: StatusDisplayer.getDefault().setStatusText(
130: deployStatus.getMessage());
131: } else if (command == CommandType.UNDEPLOY) {
132: Node parent = node.getParentNode();
133: if (parent != null) {
134: Children children = parent.getChildren();
135: if (children instanceof ResinModuleChildren) {
136: ((ResinModuleChildren) children).updateKeys();
137: StatusDisplayer.getDefault().setStatusText(
138: deployStatus.getMessage());
139: }
140: }
141: }
142: } else if (deployStatus.getState() == StateType.FAILED) {
143: NotifyDescriptor notDesc = new NotifyDescriptor.Message(
144: deployStatus.getMessage(),
145: NotifyDescriptor.ERROR_MESSAGE);
146: DialogDisplayer.getDefault().notify(notDesc);
147: StatusDisplayer.getDefault().setStatusText(
148: deployStatus.getMessage());
149: }
150: }
151:
152: public int compareTo(Object o) {
153: ResinModule other = (ResinModule) o;
154: int res = getDisplayName().compareToIgnoreCase(
155: other.getDisplayName());
156: if (res == 0) {
157: res = getShortDescription().compareToIgnoreCase(
158: other.getShortDescription());
159: }
160: return res;
161: }
162: }
|