001    /**
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.apache.xbean.kernel.standard;
018    
019    import java.util.concurrent.Executor;
020    import org.apache.xbean.kernel.ServiceEvent;
021    import org.apache.xbean.kernel.ServiceMonitor;
022    
023    /**
024     * The AsyncServiceMonitor delivers service events to a delegate ServiceMonitor asynchronously using an executor.
025     *
026     * @author Dain Sundstrom
027     * @version $Id$
028     * @since 2.0
029     */
030    public class AsyncServiceMonitor implements ServiceMonitor {
031        private final ServiceMonitor delegate;
032        private final Executor executor;
033    
034        /**
035         * Creates a AsyncServiceMonitor which asynchronously delivers service events to specified delegate
036         * ServiceMonitor using the specified executor.
037         *
038         * @param delegate the service monitor that should recieve the asynchronous events
039         * @param executor the executor used to asynchronously deliver the events
040         */
041        public AsyncServiceMonitor(ServiceMonitor delegate, Executor executor) {
042            this.delegate = delegate;
043            this.executor = executor;
044        }
045    
046        /**
047         * {@inheritDoc}
048         */
049        public void serviceRegistered(final ServiceEvent serviceEvent) {
050            executor.execute(new Runnable() {
051                public void run() {
052                    delegate.serviceRegistered(serviceEvent);
053                }
054            });
055        }
056    
057        /**
058         * {@inheritDoc}
059         */
060        public void serviceStarting(final ServiceEvent serviceEvent) {
061            executor.execute(new Runnable() {
062                public void run() {
063                    delegate.serviceStarting(serviceEvent);
064                }
065            });
066        }
067    
068        /**
069         * {@inheritDoc}
070         */
071        public void serviceWaitingToStart(final ServiceEvent serviceEvent) {
072            executor.execute(new Runnable() {
073                public void run() {
074                    delegate.serviceWaitingToStart(serviceEvent);
075                }
076            });
077        }
078    
079        /**
080         * {@inheritDoc}
081         */
082        public void serviceStartError(final ServiceEvent serviceEvent) {
083            executor.execute(new Runnable() {
084                public void run() {
085                    delegate.serviceStartError(serviceEvent);
086                }
087            });
088        }
089    
090        /**
091         * {@inheritDoc}
092         */
093        public void serviceRunning(final ServiceEvent serviceEvent) {
094            executor.execute(new Runnable() {
095                public void run() {
096                    delegate.serviceRunning(serviceEvent);
097                }
098            });
099        }
100    
101        /**
102         * {@inheritDoc}
103         */
104        public void serviceStopping(final ServiceEvent serviceEvent) {
105            executor.execute(new Runnable() {
106                public void run() {
107                    delegate.serviceStopping(serviceEvent);
108                }
109            });
110        }
111    
112        /**
113         * {@inheritDoc}
114         */
115        public void serviceWaitingToStop(final ServiceEvent serviceEvent) {
116            executor.execute(new Runnable() {
117                public void run() {
118                    delegate.serviceWaitingToStop(serviceEvent);
119                }
120            });
121        }
122    
123        /**
124         * {@inheritDoc}
125         */
126        public void serviceStopError(final ServiceEvent serviceEvent) {
127            executor.execute(new Runnable() {
128                public void run() {
129                    delegate.serviceStopError(serviceEvent);
130                }
131            });
132        }
133    
134        /**
135         * {@inheritDoc}
136         */
137        public void serviceStopped(final ServiceEvent serviceEvent) {
138            executor.execute(new Runnable() {
139                public void run() {
140                    delegate.serviceStopped(serviceEvent);
141                }
142            });
143        }
144    
145        /**
146         * {@inheritDoc}
147         */
148        public void serviceUnregistered(final ServiceEvent serviceEvent) {
149            executor.execute(new Runnable() {
150                public void run() {
151                    delegate.serviceUnregistered(serviceEvent);
152                }
153            });
154        }
155    }