001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */
019:
020: package org.apache.geronimo.kernel.config;
021:
022: import java.util.ArrayList;
023: import java.util.Collections;
024: import java.util.List;
025:
026: import org.apache.geronimo.kernel.repository.Artifact;
027:
028: /**
029: * @version $Rev: 607674 $ $Date: 2007-12-30 21:16:26 -0800 (Sun, 30 Dec 2007) $
030: */
031: public class RecordingLifecycleMonitor implements LifecycleMonitor {
032: private final List<Event> events = new ArrayList<Event>();
033:
034: public void addConfiguration(Artifact configurationId) {
035: events.add(new Event(Action.ADD, configurationId));
036: }
037:
038: public void resolving(Artifact configurationId) {
039: events.add(new Event(Action.RESOLVING, configurationId));
040: }
041:
042: public void reading(Artifact configurationId) {
043: events.add(new Event(Action.READING, configurationId));
044: }
045:
046: public void loading(Artifact configurationId) {
047: events.add(new Event(Action.LOADING, configurationId));
048: }
049:
050: public void starting(Artifact configurationId) {
051: events.add(new Event(Action.STARTING, configurationId));
052: }
053:
054: public void stopping(Artifact configurationId) {
055: events.add(new Event(Action.STOPPING, configurationId));
056: }
057:
058: public void unloading(Artifact configurationId) {
059: events.add(new Event(Action.UNLOADING, configurationId));
060: }
061:
062: public void succeeded(Artifact configurationId) {
063: events.add(new Event(Action.SUCCEEDED, configurationId));
064: }
065:
066: public void failed(Artifact configurationId, Throwable cause) {
067: events.add(new FailedEvent(configurationId, cause));
068: }
069:
070: public void finished() {
071: events.add(new Event(Action.FINISHED, null));
072: }
073:
074: public List<Event> getEvents() {
075: return Collections.unmodifiableList(events);
076: }
077:
078: @Override
079: public String toString() {
080: StringBuffer buf = new StringBuffer();
081: for (Event event : events) {
082: buf.append(event.toString()).append("\n");
083: }
084: return buf.toString();
085: }
086:
087: private static enum Action {
088:
089: ADD, RESOLVING, READING, LOADING, STARTING, STOPPING, UNLOADING, SUCCEEDED, FAILED, FINISHED
090: }
091:
092: private static class Event {
093: private final Action action;
094: private final Artifact artifact;
095:
096: private Event(Action action, Artifact artifact) {
097: this .action = action;
098: this .artifact = artifact;
099: }
100:
101: public Action getAction() {
102: return action;
103: }
104:
105: public Artifact getArtifact() {
106: return artifact;
107: }
108:
109: @Override
110: public String toString() {
111: if (artifact == null) {
112: return action.toString();
113: }
114: return artifact.toString() + ": " + action.toString();
115: }
116: }
117:
118: private static class FailedEvent extends Event {
119: private final Throwable cause;
120:
121: private FailedEvent(Artifact artifact, Throwable cause) {
122: super (Action.FAILED, artifact);
123: this .cause = cause;
124: }
125:
126: public Throwable getCause() {
127: return cause;
128: }
129:
130: @Override
131: public String toString() {
132: return super .toString() + ": " + cause.getMessage();
133: }
134: }
135: }
|