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;
018    
019    import java.util.Set;
020    
021    /**
022     * This class contains the built-in common start startegies.
023     *
024     * @author Dain Sundstrom
025     * @version $Id$
026     * @since 2.0
027     */
028    public final class StartStrategies {
029        private StartStrategies() {
030        }
031    
032        /**
033         * This strategy attempts to immedately start the service.  When there are unsatisfied conditions, this strategy
034         * will leave the service in the STARTING state, and throw an UnsatisfiedConditionsException
035         * to the caller.  When there is a start error, the service will be destroyed and the exception will be rethrown to
036         * the caller.
037         */
038        public static final StartStrategy SYNCHRONOUS = new Synchronous();
039    
040        private static class Synchronous implements StartStrategy {
041            public boolean waitForUnsatisfiedConditions(ServiceName serviceName, Set conditions) throws UnsatisfiedConditionsException {
042                throw new UnsatisfiedConditionsException("Unsatisfied start conditions", serviceName, conditions);
043            }
044    
045            public void startError(ServiceName serviceName, Throwable startError) throws Exception {
046                if (startError instanceof Exception) {
047                    throw (Exception) startError;
048                } else if (startError instanceof Error) {
049                    throw (Error) startError;
050                } else {
051                    throw new AssertionError(startError);
052                }
053            }
054        }
055    
056        /**
057         * This strategy attempts to start the service asynchronously.  When there are unsatisfied conditions, this strategy
058         * will leave the service in the STARTING state, and caller will not recieve any exceptions.
059         * When there is a start error the service will be destroyed adn the exception will be sent to the service montior.
060         * The caller will not recieve any start exception.
061         */
062        public static final StartStrategy ASYNCHRONOUS = new Asynchronous();
063    
064        private static class Asynchronous implements StartStrategy {
065            public boolean waitForUnsatisfiedConditions(ServiceName serviceName, Set conditions) {
066                return false;
067            }
068    
069            public void startError(ServiceName serviceName, Throwable startError) {
070            }
071        }
072    
073        /**
074         * This strategy wait until the service start.  This strategy blocks until all unsatisfied conditons
075         * are satisfied.  When there is a start error, the service will be destroyed and the exception will be rethrown to
076         * the caller.
077         */
078        public static final StartStrategy BLOCK = new Block();
079    
080        private static class Block implements StartStrategy {
081            public boolean waitForUnsatisfiedConditions(ServiceName serviceName, Set conditions) {
082                return true;
083            }
084    
085            public void startError(ServiceName serviceName, Throwable startError) throws Exception {
086                if (startError instanceof Exception) {
087                    throw (Exception) startError;
088                } else if (startError instanceof Error) {
089                    throw (Error) startError;
090                } else {
091                    throw new AssertionError(startError);
092                }
093            }
094        }
095    
096        /**
097         * This strategy attempts to start the service immedately.  When there are unsatisfied conditions or a start error
098         * the dervice will be destroyed and unregistered.  In this case an UnsatisfiedConditionsException or
099         * the start error will be thrown to the caller.
100         */
101        public static final StartStrategy UNREGISTER = new Unregister();
102    
103        private static class Unregister implements StartStrategy {
104            public boolean waitForUnsatisfiedConditions(ServiceName serviceName, Set conditions) throws UnregisterServiceException {
105                UnsatisfiedConditionsException userException = new UnsatisfiedConditionsException("Unsatisfied start conditions", serviceName, conditions);
106                throw new UnregisterServiceException(serviceName, userException);
107            }
108    
109            public void startError(ServiceName serviceName, Throwable startError) throws UnregisterServiceException {
110                throw new UnregisterServiceException(serviceName, startError);
111            }
112        }
113    }