001 // Copyright 2007 The Apache Software Foundation
002 //
003 // Licensed under the Apache License, Version 2.0 (the "License");
004 // you may not use this file except in compliance with the License.
005 // You may obtain a copy of the License at
006 //
007 // http://www.apache.org/licenses/LICENSE-2.0
008 //
009 // Unless required by applicable law or agreed to in writing, software
010 // distributed under the License is distributed on an "AS IS" BASIS,
011 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012 // See the License for the specific language governing permissions and
013 // limitations under the License.
014
015 package org.apache.tapestry5.ioc.internal.services;
016
017 import org.apache.tapestry5.ioc.Registry;
018 import org.apache.tapestry5.ioc.internal.util.OneShotLock;
019 import org.slf4j.Logger;
020
021 import java.util.List;
022
023 /**
024 * Startup service for Tapestry IoC: automatically invoked at {@linkplain Registry#performRegistryStartup() registry
025 * startup} to execute a series of operations, via its ordered configuration of Runnable objects.
026 */
027 public class RegistryStartup implements Runnable
028 {
029 private final Logger logger;
030
031 private final List<Runnable> configuration;
032
033 private final OneShotLock lock = new OneShotLock();
034
035 public RegistryStartup(Logger logger, final List<Runnable> configuration)
036 {
037 this.logger = logger;
038 this.configuration = configuration;
039 }
040
041 /**
042 * Invokes run() on each contributed object. If the object throws a runtime exception, it is logged but startup
043 * continues anyway. This method may only be {@linkplain OneShotLock invoked once}.
044 */
045 public void run()
046 {
047 lock.lock();
048
049 // Do we want extra exception catching here?
050
051 for (Runnable r : configuration)
052 {
053 try
054 {
055 r.run();
056 }
057 catch (RuntimeException ex)
058 {
059 logger.error(ServiceMessages.startupFailure(ex));
060 }
061 }
062
063 // We don't need them any more since this method can only be run once. It's a insignificant
064 // savings, but still a nice thing to do.
065
066 configuration.clear();
067 }
068
069 }