~ubuntu-branches/debian/sid/felix-resolver/sid

« back to all changes in this revision

Viewing changes to src/main/java/org/osgi/service/resolver/ResolveContext.java

  • Committer: Package Import Robot
  • Author(s): Kai-Chung Yan (殷啟聰)
  • Date: 2018-03-07 23:19:56 UTC
  • Revision ID: package-import@ubuntu.com-20180307231956-e9v0qt2bjiysqlho
Tags: upstream-1.14.0
Import upstream version 1.14.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (c) OSGi Alliance (2011, 2012). All Rights Reserved.
 
3
 *
 
4
 * Licensed under the Apache License, Version 2.0 (the "License");
 
5
 * you may not use this file except in compliance with the License.
 
6
 * You may obtain a copy of the License at
 
7
 *
 
8
 *      http://www.apache.org/licenses/LICENSE-2.0
 
9
 *
 
10
 * Unless required by applicable law or agreed to in writing, software
 
11
 * distributed under the License is distributed on an "AS IS" BASIS,
 
12
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
13
 * See the License for the specific language governing permissions and
 
14
 * limitations under the License.
 
15
 */
 
16
 
 
17
package org.osgi.service.resolver;
 
18
 
 
19
import java.util.Collection;
 
20
import java.util.Collections;
 
21
import java.util.List;
 
22
import java.util.Map;
 
23
import org.osgi.resource.Capability;
 
24
import org.osgi.resource.Requirement;
 
25
import org.osgi.resource.Resource;
 
26
import org.osgi.resource.Wiring;
 
27
 
 
28
/**
 
29
 * A resolve context provides resources, options and constraints to the
 
30
 * potential solution of a {@link Resolver#resolve(ResolveContext) resolve}
 
31
 * operation.
 
32
 * 
 
33
 * <p>
 
34
 * Resolve Contexts:
 
35
 * <ul>
 
36
 * <li>Specify the mandatory and optional resources to resolve. The mandatory
 
37
 * and optional resources must be consistent and correct. For example, they must
 
38
 * not violate the singleton policy of the implementer.</li>
 
39
 * <li>Provide {@link Capability capabilities} that the Resolver can use to
 
40
 * satisfy {@link Requirement requirements} via the
 
41
 * {@link #findProviders(Requirement)} method</li>
 
42
 * <li>Constrain solutions via the {@link #getWirings()} method. A wiring
 
43
 * consists of a map of existing {@link Resource resources} to {@link Wiring
 
44
 * wiring}.</li>
 
45
 * <li>Filter requirements that are part of a resolve operation via the
 
46
 * {@link #isEffective(Requirement)}.</li>
 
47
 * </ul>
 
48
 * 
 
49
 * <p>
 
50
 * A resolver may call the methods on the resolve context any number of times
 
51
 * during a resolve operation using any thread. Implementors should ensure that
 
52
 * this class is properly thread safe.
 
53
 * 
 
54
 * <p>
 
55
 * Except for {@link #insertHostedCapability(List, HostedCapability)}, the
 
56
 * resolve context methods must be <i>idempotent</i>. This means that resources
 
57
 * must have constant capabilities and requirements and the resolve context must
 
58
 * return a consistent set of capabilities, wires and effective requirements.
 
59
 * 
 
60
 * @ThreadSafe
 
61
 * @version $Id: f92eae32ab6fadb25e13d226458d6af50e8dcbba $
 
62
 */
 
63
public abstract class ResolveContext {
 
64
        /**
 
65
         * Return the resources that must be resolved for this resolve context.
 
66
         * 
 
67
         * <p>
 
68
         * The default implementation returns an empty collection.
 
69
         * 
 
70
         * @return The resources that must be resolved for this resolve context. May
 
71
         *         be empty if there are no mandatory resources.
 
72
         */
 
73
        public Collection<Resource> getMandatoryResources() {
 
74
                return emptyCollection();
 
75
        }
 
76
 
 
77
        /**
 
78
         * Return the resources that the resolver should attempt to resolve for this
 
79
         * resolve context. Inability to resolve one of the specified resources will
 
80
         * not result in a resolution exception.
 
81
         * 
 
82
         * <p>
 
83
         * The default implementation returns an empty collection.
 
84
         * 
 
85
         * @return The resources that the resolver should attempt to resolve for
 
86
         *         this resolve context. May be empty if there are no mandatory
 
87
         *         resources.
 
88
         */
 
89
        public Collection<Resource> getOptionalResources() {
 
90
                return emptyCollection();
 
91
        }
 
92
 
 
93
        private static <T> Collection<T> emptyCollection() {
 
94
                return Collections.EMPTY_LIST;
 
95
        }
 
96
 
 
97
        /**
 
98
         * Find Capabilities that match the given Requirement.
 
99
         * <p>
 
100
         * The returned list contains {@link Capability} objects where the Resource
 
101
         * must be the declared Resource of the Capability. The Resolver can then
 
102
         * add additional {@link HostedCapability} objects with the
 
103
         * {@link #insertHostedCapability(List, HostedCapability)} method when it,
 
104
         * for example, attaches fragments. Those {@link HostedCapability} objects
 
105
         * will then use the host's Resource which likely differs from the declared
 
106
         * Resource of the corresponding Capability.
 
107
         * 
 
108
         * <p>
 
109
         * The returned list is in priority order such that the Capabilities with a
 
110
         * lower index have a preference over those with a higher index. The
 
111
         * resolver must use the
 
112
         * {@link #insertHostedCapability(List, HostedCapability)} method to add
 
113
         * additional Capabilities to maintain priority order. In general, this is
 
114
         * necessary when the Resolver uses Capabilities declared in a Resource but
 
115
         * that must originate from an attached host.
 
116
         * 
 
117
         * <p>
 
118
         * Each returned Capability must match the given Requirement. This implies
 
119
         * that the filter in the Requirement must match as well as any namespace
 
120
         * specific directives. For example, the mandatory attributes for the
 
121
         * {@code osgi.wiring.package} namespace.
 
122
         * 
 
123
         * @param requirement The requirement that a resolver is attempting to
 
124
         *        satisfy. Must not be {@code null}.
 
125
         * @return A list of {@link Capability} objects that match the specified
 
126
         *         requirement.
 
127
         */
 
128
        public abstract List<Capability> findProviders(Requirement requirement);
 
129
 
 
130
        /**
 
131
         * Add a {@link HostedCapability} to the list of capabilities returned from
 
132
         * {@link #findProviders(Requirement)}.
 
133
         * 
 
134
         * <p>
 
135
         * This method is used by the {@link Resolver} to add Capabilities that are
 
136
         * hosted by another Resource to the list of Capabilities returned from
 
137
         * {@link #findProviders(Requirement)}. This function is necessary to allow
 
138
         * fragments to attach to hosts, thereby changing the origin of a
 
139
         * Capability. This method must insert the specified HostedCapability in a
 
140
         * place that makes the list maintain the preference order. It must return
 
141
         * the index in the list of the inserted {@link HostedCapability}.
 
142
         * 
 
143
         * @param capabilities The list returned from
 
144
         *        {@link #findProviders(Requirement)}. Must not be {@code null}.
 
145
         * @param hostedCapability The HostedCapability to insert in the specified
 
146
         *        list. Must not be {@code null}.
 
147
         * @return The index in the list of the inserted HostedCapability.
 
148
         * 
 
149
         */
 
150
        public abstract int insertHostedCapability(List<Capability> capabilities, HostedCapability hostedCapability);
 
151
 
 
152
        /**
 
153
         * Test if a given requirement should be wired in the resolve operation. If
 
154
         * this method returns {@code false}, then the resolver should ignore this
 
155
         * requirement during the resolve operation.
 
156
         * 
 
157
         * <p>
 
158
         * The primary use case for this is to test the {@code effective} directive
 
159
         * on the requirement, though implementations are free to use any effective
 
160
         * test.
 
161
         * 
 
162
         * @param requirement The Requirement to test. Must not be {@code null}.
 
163
         * @return {@code true} if the requirement should be considered as part of
 
164
         *         the resolve operation.
 
165
         */
 
166
        public abstract boolean isEffective(Requirement requirement);
 
167
 
 
168
        /**
 
169
         * Returns the wirings for existing resolved resources.
 
170
         * 
 
171
         * <p>
 
172
         * For example, if this resolve context is for an OSGi framework, then the
 
173
         * result would contain all the currently resolved bundles with each
 
174
         * bundle's current wiring.
 
175
         * 
 
176
         * <p>
 
177
         * Multiple calls to this method for this resolve context must return the
 
178
         * same result.
 
179
         * 
 
180
         * @return The wirings for existing resolved resources. The returned map is
 
181
         *         unmodifiable.
 
182
         */
 
183
        public abstract Map<Resource, Wiring> getWirings();
 
184
}