Marcel Offermans | e14b342 | 2009-11-25 23:04:32 +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 | */ |
Pierre De Rop | f74a116 | 2009-12-04 22:40:38 +0000 | [diff] [blame] | 19 | package org.apache.felix.dm.impl; |
Marcel Offermans | e14b342 | 2009-11-25 23:04:32 +0000 | [diff] [blame] | 20 | |
| 21 | import java.util.List; |
| 22 | |
Pierre De Rop | f74a116 | 2009-12-04 22:40:38 +0000 | [diff] [blame] | 23 | import org.apache.felix.dm.dependencies.Dependency; |
Marcel Offermans | e14b342 | 2009-11-25 23:04:32 +0000 | [diff] [blame] | 24 | |
| 25 | /** |
| 26 | * Encapsulates the current state of the dependencies of a service. A state is |
| 27 | * basically an immutable value object. |
| 28 | * |
| 29 | * @author <a href="mailto:dev@felix.apache.org">Felix Project Team</a> |
| 30 | */ |
| 31 | public final class State { |
| 32 | private static final String[] STATES = { "?", "inactive", "waiting for required", "tracking optional", "bound", "waiting for required (instantiated)" }; |
| 33 | private static final int INACTIVE = 1; |
| 34 | private static final int WAITING_FOR_REQUIRED = 2; |
| 35 | private static final int TRACKING_OPTIONAL = 3; |
| 36 | private static final int BOUND = 4; |
| 37 | private static final int WAITING_FOR_REQUIRED_INSTANTIATED = 5; |
| 38 | private final List m_deps; |
| 39 | private final int m_state; |
| 40 | private String m_stringValue; |
| 41 | |
| 42 | /** |
| 43 | * Creates a new state instance. |
| 44 | * |
| 45 | * @param deps the dependencies that determine the state |
| 46 | * @param isActive <code>true</code> if the service is active (started) |
| 47 | */ |
| 48 | public State(List deps, boolean isActive, boolean isInstantiated, boolean isBound /* unused? */) { |
| 49 | m_deps = deps; |
| 50 | // only bother calculating dependencies if we're active |
| 51 | if (isActive) { |
| 52 | boolean allRequiredAvailable = true; |
| 53 | boolean keepInstanceAround = isInstantiated; |
| 54 | for (int i = 0; i < deps.size(); i++) { |
| 55 | Dependency dep = (Dependency) deps.get(i); |
| 56 | if (dep.isRequired()) { |
| 57 | if (!dep.isAvailable()) { |
| 58 | allRequiredAvailable = false; |
| 59 | if (!dep.isInstanceBound()) { |
| 60 | keepInstanceAround = false; |
| 61 | } |
| 62 | } |
| 63 | } |
| 64 | } |
| 65 | if (allRequiredAvailable) { |
| 66 | if (isInstantiated) { |
| 67 | m_state = BOUND; |
| 68 | } |
| 69 | else { |
| 70 | m_state = TRACKING_OPTIONAL; |
| 71 | } |
| 72 | } |
| 73 | else { |
| 74 | if (keepInstanceAround) { |
| 75 | m_state = WAITING_FOR_REQUIRED_INSTANTIATED; |
| 76 | } |
| 77 | else { |
| 78 | m_state = WAITING_FOR_REQUIRED; |
| 79 | } |
| 80 | } |
| 81 | } |
| 82 | else { |
| 83 | m_state = INACTIVE; |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | public boolean isInactive() { |
| 88 | return m_state == INACTIVE; |
| 89 | } |
| 90 | |
| 91 | public boolean isWaitingForRequired() { |
| 92 | return m_state == WAITING_FOR_REQUIRED; |
| 93 | } |
| 94 | |
| 95 | public boolean isTrackingOptional() { |
| 96 | return m_state == TRACKING_OPTIONAL; |
| 97 | } |
| 98 | |
| 99 | public boolean isBound() { |
| 100 | return m_state == BOUND; |
| 101 | } |
| 102 | |
| 103 | public boolean isAllRequiredAvailable() { |
| 104 | return isTrackingOptional() || isBound(); |
| 105 | } |
| 106 | |
| 107 | public boolean isWaitingForRequiredInstantiated() { |
| 108 | return m_state == WAITING_FOR_REQUIRED_INSTANTIATED; |
| 109 | } |
| 110 | |
| 111 | public List getDependencies() { |
| 112 | return m_deps; |
| 113 | } |
| 114 | |
| 115 | public synchronized String toString() { |
| 116 | if (m_stringValue == null) { |
| 117 | // we only need to determine this once, but we do it lazily |
| 118 | StringBuffer buf = new StringBuffer(); |
Marcel Offermans | 78e5dfc | 2009-12-10 13:52:49 +0000 | [diff] [blame^] | 119 | buf.append("State[" + STATES[m_state]); |
Marcel Offermans | e14b342 | 2009-11-25 23:04:32 +0000 | [diff] [blame] | 120 | List deps = m_deps; |
| 121 | for (int i = 0; i < deps.size(); i++) { |
Marcel Offermans | 78e5dfc | 2009-12-10 13:52:49 +0000 | [diff] [blame^] | 122 | if (i == 0) { |
| 123 | buf.append("|"); |
| 124 | } |
Marcel Offermans | e14b342 | 2009-11-25 23:04:32 +0000 | [diff] [blame] | 125 | Dependency dep = (Dependency) deps.get(i); |
Marcel Offermans | 78e5dfc | 2009-12-10 13:52:49 +0000 | [diff] [blame^] | 126 | buf.append("(" + (dep.isRequired() ? "Req " : " ") + (dep.isAvailable() ? "Avl " : " ") + (dep.isInstanceBound() ? "InB " : " ") + (dep.isPropagated() ? "Prp " : " ") + dep + ")"); |
Marcel Offermans | e14b342 | 2009-11-25 23:04:32 +0000 | [diff] [blame] | 127 | } |
Marcel Offermans | 78e5dfc | 2009-12-10 13:52:49 +0000 | [diff] [blame^] | 128 | buf.append("]"); |
Marcel Offermans | e14b342 | 2009-11-25 23:04:32 +0000 | [diff] [blame] | 129 | m_stringValue = buf.toString(); |
| 130 | } |
| 131 | return m_stringValue; |
| 132 | } |
| 133 | } |