Marcel Offermans | a962bc9 | 2009-11-21 17:59:33 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | * or more contributor license agreements. See the NOTICE file |
| 4 | * distributed with this work for additional information |
| 5 | * regarding copyright ownership. The ASF licenses this file |
| 6 | * to you under the Apache License, Version 2.0 (the |
| 7 | * "License"); you may not use this file except in compliance |
| 8 | * with the License. You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, |
| 13 | * software distributed under the License is distributed on an |
| 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | * KIND, either express or implied. See the License for the |
| 16 | * specific language governing permissions and limitations |
| 17 | * under the License. |
| 18 | */ |
| 19 | package org.apache.felix.dependencymanager; |
| 20 | |
| 21 | import java.util.List; |
| 22 | |
| 23 | /** |
| 24 | * Encapsulates the current state of the dependencies of a service. A state is |
| 25 | * basically an immutable value object. |
| 26 | * |
| 27 | * @author <a href="mailto:dev@felix.apache.org">Felix Project Team</a> |
| 28 | */ |
| 29 | public final class State { |
| 30 | private static final String[] STATES = { "?", "inactive", "waiting for required", "tracking optional" }; |
| 31 | private static final int INACTIVE = 1; |
| 32 | private static final int WAITING_FOR_REQUIRED = 2; |
| 33 | private static final int TRACKING_OPTIONAL = 3; |
| 34 | private final List m_deps; |
| 35 | private final int m_state; |
| 36 | private String m_stringValue; |
| 37 | |
| 38 | /** |
| 39 | * Creates a new state instance. |
| 40 | * |
| 41 | * @param deps the dependencies that determine the state |
| 42 | * @param isActive <code>true</code> if the service is active (started) |
| 43 | */ |
| 44 | public State(List deps, boolean isActive) { |
| 45 | m_deps = deps; |
| 46 | // only bother calculating dependencies if we're active |
| 47 | if (isActive) { |
| 48 | boolean allRequiredAvailable = true; |
| 49 | for (int i = 0; i < deps.size(); i++) { |
| 50 | Dependency dep = (Dependency) deps.get(i); |
| 51 | if (dep.isRequired()) { |
| 52 | if (!dep.isAvailable()) { |
| 53 | allRequiredAvailable = false; |
| 54 | } |
| 55 | } |
| 56 | } |
| 57 | if (allRequiredAvailable) { |
| 58 | m_state = TRACKING_OPTIONAL; |
| 59 | } |
| 60 | else { |
| 61 | m_state = WAITING_FOR_REQUIRED; |
| 62 | } |
| 63 | } |
| 64 | else { |
| 65 | m_state = INACTIVE; |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | public boolean isInactive() { |
| 70 | return m_state == INACTIVE; |
| 71 | } |
| 72 | |
| 73 | public boolean isWaitingForRequired() { |
| 74 | return m_state == WAITING_FOR_REQUIRED; |
| 75 | } |
| 76 | |
| 77 | public boolean isTrackingOptional() { |
| 78 | return m_state == TRACKING_OPTIONAL; |
| 79 | } |
| 80 | |
| 81 | public List getDependencies() { |
| 82 | return m_deps; |
| 83 | } |
| 84 | |
| 85 | public synchronized String toString() { |
| 86 | if (m_stringValue == null) { |
| 87 | // we only need to determine this once, but we do it lazily |
| 88 | StringBuffer buf = new StringBuffer(); |
| 89 | buf.append("State[" + STATES[m_state] + "|"); |
| 90 | List deps = m_deps; |
| 91 | for (int i = 0; i < deps.size(); i++) { |
| 92 | Dependency dep = (Dependency) deps.get(i); |
| 93 | buf.append("(" + dep + (dep.isRequired() ? " R" : " O") + (dep.isAvailable() ? " +" : " -") + ")"); |
| 94 | } |
| 95 | m_stringValue = buf.toString(); |
| 96 | } |
| 97 | return m_stringValue; |
| 98 | } |
| 99 | } |