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.Collections;
020 import java.util.Iterator;
021 import java.util.Set;
022
023 import java.util.concurrent.locks.Lock;
024 import org.apache.xbean.kernel.Kernel;
025 import org.apache.xbean.kernel.ServiceCondition;
026 import org.apache.xbean.kernel.ServiceFactory;
027 import org.apache.xbean.kernel.ServiceName;
028
029 /**
030 * A special sub-class of AggregateCondition used to manage the stop conditions of a non-restartable service. This class
031 * will update stop conditions to reflect the stop conditions currently registered with the service factory, when the
032 * initialized or getUnsatisfied methods are called.
033 *
034 * @author Dain Sundstrom
035 * @version $Id$
036 * @since 2.0
037 */
038 public class NonRestartableStopCondition extends AggregateCondition {
039 private final ServiceFactory serviceFactory;
040
041 /**
042 * Creates a NonRestartableStopCondition.
043 *
044 * @param kernel the kernel in which the service is registered
045 * @param serviceName the name of the service
046 * @param classLoader the class loader for the service
047 * @param lock the lock for the service manager
048 * @param serviceFactory the service factory for the service
049 */
050 public NonRestartableStopCondition(Kernel kernel, ServiceName serviceName, ClassLoader classLoader, Lock lock, ServiceFactory serviceFactory) {
051 super(kernel, serviceName, classLoader, lock, Collections.EMPTY_SET);
052 this.serviceFactory = serviceFactory;
053 }
054
055 /**
056 * Throws UnsupportedOperationException. Initialize is not a valid operation for a NonRestartableStopCondition
057 *
058 * @throws UnsupportedOperationException always
059 */
060 public synchronized void initialize() throws UnsupportedOperationException {
061 throw new UnsupportedOperationException("initialize should never be called on a NonRestartableStopCondition");
062 }
063
064 /**
065 * {@inheritDoc}
066 */
067 public synchronized Set getUnsatisfied() {
068 updateConditions();
069 return super.getUnsatisfied();
070 }
071
072 private void updateConditions() {
073 if (isDestroyed()) throw new IllegalStateException("destroyed");
074
075 Set conditions = getConditions();
076
077 // add the new conditions
078 Set stopConditions = serviceFactory.getStopConditions();
079 for (Iterator iterator = stopConditions.iterator(); iterator.hasNext();) {
080 ServiceCondition condition = (ServiceCondition) iterator.next();
081 if (!conditions.contains(condition)) {
082 addCondition(condition);
083 }
084 }
085
086 // remove the conditions that were dropped
087 for (Iterator iterator = conditions.iterator(); iterator.hasNext();) {
088 ServiceCondition serviceCondition = (ServiceCondition) iterator.next();
089 if (!stopConditions.contains(serviceCondition)) {
090 removeCondition(serviceCondition);
091 }
092 }
093 }
094
095 }