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: * Free SoftwareFoundation, Inc.
023: * 59 Temple Place, Suite 330
024: * Boston, MA 02111-1307 USA
025: *
026: * @author Sam
027: */
028:
029: package com.caucho.server.admin;
030:
031: import com.caucho.util.L10N;
032:
033: import javax.enterprise.deploy.shared.StateType;
034: import javax.enterprise.deploy.spi.TargetModuleID;
035: import javax.enterprise.deploy.spi.exceptions.OperationUnsupportedException;
036: import javax.enterprise.deploy.spi.status.ClientConfiguration;
037: import javax.enterprise.deploy.spi.status.DeploymentStatus;
038: import javax.enterprise.deploy.spi.status.ProgressEvent;
039: import javax.enterprise.deploy.spi.status.ProgressListener;
040: import javax.enterprise.deploy.spi.status.ProgressObject;
041: import java.util.Iterator;
042: import java.util.LinkedList;
043: import java.util.List;
044: import java.util.logging.Level;
045: import java.util.logging.Logger;
046:
047: public class ProgressObjectImpl implements ProgressObject {
048: private static final Logger log = Logger
049: .getLogger(ProgressObjectImpl.class.getName());
050: private static final L10N L = new L10N(ProgressObjectImpl.class);
051:
052: private final TargetModuleID[] _targetModuleIDs;
053: private final DeploymentStatusImpl _status = new DeploymentStatusImpl();
054: private final List<ProgressListener> _listeners = new LinkedList<ProgressListener>();
055:
056: ProgressObjectImpl() {
057: _targetModuleIDs = null;
058: }
059:
060: public ProgressObjectImpl(TargetModuleID[] targetModuleIDs) {
061: _targetModuleIDs = targetModuleIDs;
062: }
063:
064: public DeploymentStatus getDeploymentStatus() {
065: if (_status != null)
066: return _status;
067: else
068: return new DeploymentStatusImpl();
069: }
070:
071: public TargetModuleID[] getResultTargetModuleIDs() {
072: return _targetModuleIDs;
073: }
074:
075: public ClientConfiguration getClientConfiguration(TargetModuleID id) {
076: throw new UnsupportedOperationException();
077: }
078:
079: public boolean isCancelSupported() {
080: return false;
081: }
082:
083: public void cancel() throws OperationUnsupportedException {
084: throw new OperationUnsupportedException(L
085: .l("'cancel' is not supported"));
086: }
087:
088: public boolean isStopSupported() {
089: return false;
090: }
091:
092: public void stop() throws OperationUnsupportedException {
093: throw new OperationUnsupportedException(L
094: .l("'stop' is not supported"));
095: }
096:
097: public void completed(String message) {
098: boolean isChanged = _status.getState() != StateType.COMPLETED;
099:
100: _status.setState(StateType.COMPLETED);
101: _status.setMessage(message);
102:
103: if (isChanged)
104: fireProgressEvent();
105: }
106:
107: public void failed(String message) {
108: if (log.isLoggable(Level.FINEST))
109: log.log(Level.FINEST, "failed " + message);
110:
111: boolean isChanged = _status.getState() != StateType.FAILED;
112:
113: _status.setState(StateType.FAILED);
114: _status.setMessage(message);
115:
116: if (isChanged)
117: fireProgressEvent();
118: }
119:
120: public void addProgressListener(ProgressListener listener) {
121: synchronized (_listeners) {
122: _listeners.add(listener);
123: }
124: }
125:
126: public void removeProgressListener(ProgressListener listener) {
127: synchronized (_listeners) {
128: Iterator<ProgressListener> iter = _listeners.iterator();
129:
130: while (iter.hasNext()) {
131: if (iter.next() == listener)
132: iter.remove();
133: }
134: }
135: }
136:
137: private void fireProgressEvent() {
138: if (_targetModuleIDs == null || _targetModuleIDs.length == 0)
139: return;
140:
141: ProgressListener[] listeners;
142:
143: synchronized (_listeners) {
144: if (_listeners.isEmpty())
145: return;
146:
147: listeners = _listeners
148: .toArray(new ProgressListener[_listeners.size()]);
149: }
150:
151: ProgressEvent[] events = new ProgressEvent[_targetModuleIDs.length];
152:
153: for (int i = 0; i < _targetModuleIDs.length; i++)
154: events[i] = new ProgressEvent(this , _targetModuleIDs[i],
155: _status);
156:
157: for (ProgressListener listener : listeners) {
158: for (ProgressEvent event : events)
159: listener.handleProgressEvent(event);
160: }
161: }
162:
163: public String toString() {
164: return "ProgressObjectImpl[" + _status + "]";
165: }
166: }
|