KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > osgi > internal > module > ResolverConstraint


1 /*******************************************************************************
2  * Copyright (c) 2005, 2007 IBM Corporation and others. All rights reserved. This
3  * program and the accompanying materials are made available under the terms of
4  * the Eclipse Public License v1.0 which accompanies this distribution, and is
5  * available at http://www.eclipse.org/legal/epl-v10.html
6  *
7  * Contributors: IBM Corporation - initial API and implementation
8  ******************************************************************************/

9 package org.eclipse.osgi.internal.module;
10
11 import org.eclipse.osgi.service.resolver.BundleDescription;
12 import org.eclipse.osgi.service.resolver.VersionConstraint;
13
14 /*
15  * A companion to VersionConstraint from the state used while resolving
16  */

17 public abstract class ResolverConstraint {
18     final protected ResolverBundle bundle;
19     final protected VersionConstraint constraint;
20     private VersionSupplier[] possibleSuppliers;
21     private int selectedSupplierIndex = 0;
22
23     ResolverConstraint(ResolverBundle bundle, VersionConstraint constraint) {
24         this.bundle = bundle;
25         this.constraint = constraint;
26     }
27
28     // returns the Resolver bundle requiring the ResolverConstraint
29
ResolverBundle getBundle() {
30         return bundle;
31     }
32
33     // returns the BundleDescription requiring the ResolverConstraint
34
BundleDescription getBundleDescription() {
35         return bundle.getBundle();
36     }
37
38     // returns whether this constraint is from an attached fragment
39
boolean isFromFragment() {
40         return constraint.getBundle().getHost() != null;
41     }
42
43     // Same as VersionConstraint but does additinal permission checks
44
boolean isSatisfiedBy(VersionSupplier vs) {
45         if (!bundle.getResolver().getPermissionChecker().checkPermission(constraint, vs.getBaseDescription()))
46             return false;
47         return constraint.isSatisfiedBy(vs.getBaseDescription());
48     }
49
50     // returns the companion VersionConstraint object from the State
51
VersionConstraint getVersionConstraint() {
52         return constraint;
53     }
54
55     // returns the name of this constraint
56
public String JavaDoc getName() {
57         return constraint.getName();
58     }
59
60     public String JavaDoc toString() {
61         return constraint.toString();
62     }
63
64     // returns whether this constraint is optional
65
abstract boolean isOptional();
66
67     public void setPossibleSuppliers(VersionSupplier[] possibleSuppliers) {
68         this.possibleSuppliers = possibleSuppliers;
69     }
70
71     void addPossibleSupplier(VersionSupplier supplier) {
72         if (supplier == null)
73             return;
74         // we hope multiple suppliers are rare so do simple array expansion here.
75
if (possibleSuppliers == null) {
76             possibleSuppliers = new VersionSupplier[] {supplier};
77             return;
78         }
79         VersionSupplier[] newSuppliers = new VersionSupplier[possibleSuppliers.length + 1];
80         System.arraycopy(possibleSuppliers, 0, newSuppliers, 0, possibleSuppliers.length);
81         newSuppliers[possibleSuppliers.length] = supplier;
82         possibleSuppliers = newSuppliers;
83     }
84
85     public void removePossibleSupplier(VersionSupplier supplier) {
86         if (possibleSuppliers == null || supplier == null)
87             return;
88         int index = -1;
89         for (int i = 0; i < possibleSuppliers.length; i++) {
90             if (possibleSuppliers[i] == supplier) {
91                 index = i;
92                 break;
93             }
94         }
95         if (index >= 0) {
96             if (possibleSuppliers.length == 1) {
97                 possibleSuppliers = null;
98                 return;
99             }
100             VersionSupplier[] newSuppliers = new VersionSupplier[possibleSuppliers.length - 1];
101             System.arraycopy(possibleSuppliers, 0, newSuppliers, 0, index);
102             if (index < possibleSuppliers.length - 1)
103                 System.arraycopy(possibleSuppliers, index + 1, newSuppliers, index, possibleSuppliers.length - index - 1);
104             possibleSuppliers = newSuppliers;
105         }
106     }
107
108     int getNumPossibleSuppliers() {
109         if (possibleSuppliers == null)
110             return 0;
111         return possibleSuppliers.length;
112     }
113
114     boolean selectNextSupplier() {
115         if (possibleSuppliers == null || selectedSupplierIndex >= possibleSuppliers.length)
116             return false;
117         selectedSupplierIndex += 1;
118         return selectedSupplierIndex < possibleSuppliers.length;
119     }
120
121     VersionSupplier getSelectedSupplier() {
122         if (possibleSuppliers == null || selectedSupplierIndex >= possibleSuppliers.length)
123             return null;
124         return possibleSuppliers[selectedSupplierIndex];
125     }
126
127     void setSelectedSupplier(int selectedSupplier) {
128         this.selectedSupplierIndex = selectedSupplier;
129     }
130
131     int getSelectedSupplierIndex() {
132         return this.selectedSupplierIndex;
133     }
134
135     VersionSupplier[] getPossibleSuppliers() {
136         return possibleSuppliers;
137     }
138
139     void clearPossibleSuppliers() {
140         possibleSuppliers = null;
141         selectedSupplierIndex = 0;
142     }
143 }
144
Popular Tags