View Javadoc
1   /*-
2    * #%L
3    * io.earcam.instrumental.archive.osgi
4    * %%
5    * Copyright (C) 2018 earcam
6    * %%
7    * SPDX-License-Identifier: (BSD-3-Clause OR EPL-1.0 OR Apache-2.0 OR MIT)
8    * 
9    * You <b>must</b> choose to accept, in full - any individual or combination of 
10   * the following licenses:
11   * <ul>
12   * 	<li><a href="https://opensource.org/licenses/BSD-3-Clause">BSD-3-Clause</a></li>
13   * 	<li><a href="https://www.eclipse.org/legal/epl-v10.html">EPL-1.0</a></li>
14   * 	<li><a href="https://www.apache.org/licenses/LICENSE-2.0">Apache-2.0</a></li>
15   * 	<li><a href="https://opensource.org/licenses/MIT">MIT</a></li>
16   * </ul>
17   * #L%
18   */
19  package io.earcam.instrumental.archive.osgi.auto;
20  
21  import java.util.ArrayList;
22  import java.util.Iterator;
23  import java.util.List;
24  import java.util.stream.Collectors;
25  
26  import io.earcam.instrumental.archive.osgi.PackageBundleMapper;
27  import io.earcam.instrumental.module.osgi.BundleInfo;
28  import io.earcam.instrumental.module.osgi.Clause;
29  import io.earcam.instrumental.module.osgi.ClauseParameters.ClauseParameter;
30  
31  /**
32   * <p>
33   * Abstract AbstractPackageBundleMapper class.
34   * </p>
35   *
36   */
37  public abstract class AbstractPackageBundleMapper implements PackageBundleMapper {
38  
39  	/**
40  	 * <p>
41  	 * bundles.
42  	 * </p>
43  	 *
44  	 * @return a {@link java.util.List} object.
45  	 */
46  	protected abstract List<BundleInfo> bundles();
47  
48  
49  	/**
50  	 * <p>
51  	 * allAvailableExports.
52  	 * </p>
53  	 *
54  	 * @return a {@link java.util.List} object.
55  	 */
56  	protected List<Clause> allAvailableExports()
57  	{
58  		return bundles().stream()
59  				.map(BundleInfo::exportPackage)
60  				.flatMap(List::stream)
61  				.collect(Collectors.toList());
62  	}
63  
64  
65  	@Override
66  	public List<Clause> importsFor(Iterator<String> requiredPackages)
67  	{
68  		List<Clause> availableExports = allAvailableExports();
69  		List<Clause> matchedExports = new ArrayList<>();
70  
71  		while(requiredPackages.hasNext()) {
72  			String requiredPackage = requiredPackages.next();
73  
74  			if(requiredPackage.startsWith("java.")) {   // OSGi spec 7.0.0 § 2.4.1 "Implied Permissions"
75  				requiredPackages.remove();
76  				continue;
77  			}
78  
79  			for(Clause export : availableExports) {
80  
81  				if(export.uniqueNames().contains(requiredPackage)) {
82  					// TODO version-range... optionally
83  					String version = export.parameters().attributeOrDefault("version", "0.0.0");
84  					matchedExports.add(new Clause(requiredPackage, ClauseParameter.attribute("version", version)));
85  					requiredPackages.remove();
86  					break;
87  				}
88  			}
89  		}
90  		return matchedExports;
91  	}
92  }