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    
021    package org.apache.geronimo.kernel.config;
022    
023    import java.util.ArrayList;
024    import java.util.Collections;
025    import java.util.List;
026    
027    import org.apache.geronimo.kernel.repository.Artifact;
028    
029    /**
030     * @version $Rev: 706640 $ $Date: 2008-10-21 14:44:05 +0000 (Tue, 21 Oct 2008) $
031     */
032    public class RecordingLifecycleMonitor implements LifecycleMonitor{
033        private final List<Event> events = new ArrayList<Event>();
034    
035        public void addConfiguration(Artifact configurationId) {
036            events.add(new Event(Action.ADD, configurationId));
037        }
038    
039        public void resolving(Artifact configurationId) {
040            events.add(new Event(Action.RESOLVING, configurationId));
041        }
042    
043        public void reading(Artifact configurationId) {
044            events.add(new Event(Action.READING, configurationId));
045        }
046    
047        public void loading(Artifact configurationId) {
048            events.add(new Event(Action.LOADING, configurationId));
049        }
050    
051        public void starting(Artifact configurationId) {
052            events.add(new Event(Action.STARTING, configurationId));
053        }
054    
055        public void stopping(Artifact configurationId) {
056            events.add(new Event(Action.STOPPING, configurationId));
057        }
058    
059        public void unloading(Artifact configurationId) {
060            events.add(new Event(Action.UNLOADING, configurationId));
061        }
062    
063        public void succeeded(Artifact configurationId) {
064            events.add(new Event(Action.SUCCEEDED, configurationId));
065        }
066    
067        public void failed(Artifact configurationId, Throwable cause) {
068            events.add(new FailedEvent(configurationId, cause));
069        }
070    
071        public void finished() {
072            events.add(new Event(Action.FINISHED, null));
073        }
074    
075        public List<Event> getEvents() {
076            return Collections.unmodifiableList(events);
077        }
078    
079        @Override
080        public String toString() {
081            StringBuffer buf = new StringBuffer();
082            for (Event event: events) {
083                buf.append(event.toString()).append("\n");
084            }
085            return buf.toString();
086        }
087    
088        private static enum Action {
089    
090            ADD,
091            RESOLVING,
092            READING,
093            LOADING,
094            STARTING,
095            STOPPING,
096            UNLOADING,
097            SUCCEEDED,
098            FAILED,
099            FINISHED
100        }
101    
102        private static class Event {
103            private final Action action;
104            private final Artifact artifact;
105    
106            private Event(Action action, Artifact artifact) {
107                this.action = action;
108                this.artifact = artifact;
109            }
110    
111            public Action getAction() {
112                return action;
113            }
114    
115            public Artifact getArtifact() {
116                return artifact;
117            }
118    
119            @Override
120            public String toString() {
121                if (artifact == null) {
122                    return action.toString();
123                }
124                return artifact.toString() + ": " + action.toString();
125            }
126        }
127    
128        private static class FailedEvent extends Event {
129            private final Throwable cause;
130    
131            private FailedEvent(Artifact artifact, Throwable cause) {
132                super(Action.FAILED, artifact);
133                this.cause = cause;
134            }
135    
136            public Throwable getCause() {
137                return cause;
138            }
139    
140            @Override
141            public String toString() {
142                return super.toString() + ": " + cause.getMessage();
143            }
144        }
145    }