KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > osgi > internal > resolver > ImportPackageSpecificationImpl


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

11 package org.eclipse.osgi.internal.resolver;
12
13 import java.util.*;
14 import org.eclipse.osgi.framework.internal.core.Constants;
15 import org.eclipse.osgi.service.resolver.*;
16
17 public class ImportPackageSpecificationImpl extends VersionConstraintImpl implements ImportPackageSpecification {
18     private String JavaDoc resolution = ImportPackageSpecification.RESOLUTION_STATIC; // the default is static
19
private String JavaDoc symbolicName;
20     private VersionRange bundleVersionRange;
21     private Map attributes;
22
23     public Map getDirectives() {
24         Map result = new HashMap(5);
25         if (resolution != null)
26             result.put(Constants.RESOLUTION_DIRECTIVE, resolution);
27         return result;
28     }
29
30     public Object JavaDoc getDirective(String JavaDoc key) {
31         if (key.equals(Constants.RESOLUTION_DIRECTIVE))
32             return resolution;
33         return null;
34     }
35
36     public Object JavaDoc setDirective(String JavaDoc key, Object JavaDoc value) {
37         if (key.equals(Constants.RESOLUTION_DIRECTIVE))
38             return resolution = (String JavaDoc) value;
39         return null;
40     }
41
42     public void setDirectives(Map directives) {
43         if (directives == null)
44             return;
45         resolution = (String JavaDoc) directives.get(Constants.RESOLUTION_DIRECTIVE);
46     }
47
48     public String JavaDoc getBundleSymbolicName() {
49         return symbolicName;
50     }
51
52     public VersionRange getBundleVersionRange() {
53         if (bundleVersionRange == null)
54             return VersionRange.emptyRange;
55         return bundleVersionRange;
56     }
57
58     public Map getAttributes() {
59         return attributes;
60     }
61
62     public boolean isSatisfiedBy(BaseDescription supplier) {
63         if (!(supplier instanceof ExportPackageDescription))
64             return false;
65         ExportPackageDescriptionImpl pkgDes = (ExportPackageDescriptionImpl) supplier;
66
67         // If we are in strict mode, check to see if the export specifies friends.
68
// If it does, are we one of the friends
69
String JavaDoc[] friends = (String JavaDoc[]) pkgDes.getDirective(Constants.FRIENDS_DIRECTIVE);
70         Boolean JavaDoc internal = (Boolean JavaDoc) pkgDes.getDirective(Constants.INTERNAL_DIRECTIVE);
71         if (internal.booleanValue() || friends != null) {
72             boolean strict = ((StateImpl) getBundle().getContainingState()).inStrictMode();
73             if (strict) {
74                 if (internal.booleanValue())
75                     return false;
76                 boolean found = false;
77                 if (friends != null && getBundle().getSymbolicName() != null)
78                     for (int i = 0; i < friends.length; i++)
79                         if (getBundle().getSymbolicName().equals(friends[i]))
80                             found = true;
81                 if (!found)
82                     return false;
83             }
84         }
85
86         if (symbolicName != null) {
87             BundleDescription exporter = pkgDes.getExporter();
88             if (!symbolicName.equals(exporter.getSymbolicName()))
89                 return false;
90             if (getBundleVersionRange() != null && !getBundleVersionRange().isIncluded(exporter.getVersion()))
91                 return false;
92         }
93
94         String JavaDoc name = getName();
95         // shortcut '*'
96
// NOTE: wildcards are supported only in cases where this is a dynamic import
97
if (!"*".equals(name) && !(name.endsWith(".*") && pkgDes.getName().startsWith(name.substring(0, name.length() - 1))) && !pkgDes.getName().equals(name)) //$NON-NLS-1$ //$NON-NLS-2$
98
return false;
99         if (getVersionRange() != null && !getVersionRange().isIncluded(pkgDes.getVersion()))
100             return false;
101
102         Map importAttrs = getAttributes();
103         if (importAttrs != null) {
104             Map exportAttrs = pkgDes.getAttributes();
105             if (exportAttrs == null)
106                 return false;
107             for (Iterator i = importAttrs.keySet().iterator(); i.hasNext();) {
108                 String JavaDoc importKey = (String JavaDoc) i.next();
109                 Object JavaDoc importValue = importAttrs.get(importKey);
110                 Object JavaDoc exportValue = exportAttrs.get(importKey);
111                 if (exportValue == null || !importValue.equals(exportValue))
112                     return false;
113             }
114         }
115         String JavaDoc[] mandatory = (String JavaDoc[]) pkgDes.getDirective(Constants.MANDATORY_DIRECTIVE);
116         if (mandatory != null) {
117             for (int i = 0; i < mandatory.length; i++) {
118                 if (Constants.BUNDLE_SYMBOLICNAME_ATTRIBUTE.equals(mandatory[i])) {
119                     if (symbolicName == null)
120                         return false;
121                 } else if (Constants.BUNDLE_VERSION_ATTRIBUTE.equals(mandatory[i])) {
122                     if (bundleVersionRange == null)
123                         return false;
124                 } else if (Constants.PACKAGE_SPECIFICATION_VERSION.equals(mandatory[i]) || Constants.VERSION_ATTRIBUTE.equals(mandatory[i])) {
125                     if (getVersionRange() == null)
126                         return false;
127                 } else { // arbitrary attribute
128
if (importAttrs == null)
129                         return false;
130                     if (importAttrs.get(mandatory[i]) == null)
131                         return false;
132                 }
133             }
134         }
135         // finally check the ee index
136
if (((BundleDescriptionImpl)getBundle()).getEquinoxEE() < 0)
137             return true;
138         int eeIndex = ((Integer JavaDoc) pkgDes.getDirective(ExportPackageDescriptionImpl.EQUINOX_EE)).intValue();
139         return eeIndex < 0 || eeIndex == ((BundleDescriptionImpl)getBundle()).getEquinoxEE();
140     }
141
142     protected void setBundleSymbolicName(String JavaDoc symbolicName) {
143         this.symbolicName = symbolicName;
144     }
145
146     protected void setBundleVersionRange(VersionRange bundleVersionRange) {
147         this.bundleVersionRange = bundleVersionRange;
148     }
149
150     protected void setAttributes(Map attributes) {
151         this.attributes = attributes;
152     }
153
154     public String JavaDoc toString() {
155         return "Import-Package: " + getName() + "; version=\"" + getVersionRange() + "\""; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
156
}
157 }
158
Popular Tags