|
| 1 | +/******************************************************************************* |
| 2 | + * Copyright (c) 2024 Christoph Läubrich and others. |
| 3 | + * |
| 4 | + * This program and the accompanying materials |
| 5 | + * are made available under the terms of the Eclipse Public License 2.0 |
| 6 | + * which accompanies this distribution, and is available at |
| 7 | + * https://www.eclipse.org/legal/epl-2.0/ |
| 8 | + * |
| 9 | + * SPDX-License-Identifier: EPL-2.0 |
| 10 | + *******************************************************************************/ |
| 11 | +package org.eclipse.m2e.editor.lemminx.bnd; |
| 12 | + |
| 13 | +import java.io.File; |
| 14 | +import java.util.ArrayList; |
| 15 | +import java.util.LinkedHashSet; |
| 16 | +import java.util.List; |
| 17 | +import java.util.Set; |
| 18 | + |
| 19 | +import org.eclipse.core.runtime.FileLocator; |
| 20 | +import org.osgi.framework.Bundle; |
| 21 | +import org.osgi.framework.FrameworkUtil; |
| 22 | +import org.osgi.framework.wiring.BundleWire; |
| 23 | +import org.osgi.framework.wiring.BundleWiring; |
| 24 | + |
| 25 | +import aQute.bnd.help.Syntax; |
| 26 | + |
| 27 | +/** |
| 28 | + * register additional jars and the extension bundle |
| 29 | + */ |
| 30 | +@SuppressWarnings("restriction") |
| 31 | +public class BndClasspathExtensionProvider |
| 32 | + implements org.eclipse.wildwebdeveloper.xml.LemminxClasspathExtensionProvider { |
| 33 | + |
| 34 | + @Override |
| 35 | + public List<File> get() { |
| 36 | + List<File> list = new ArrayList<>(); |
| 37 | + Set<Bundle> bundleRequirements = new LinkedHashSet<>(); |
| 38 | + bundleRequirements.add((FrameworkUtil.getBundle(getClass()))); |
| 39 | + collectBundles(FrameworkUtil.getBundle(Syntax.class), bundleRequirements); |
| 40 | + for (Bundle bundle : bundleRequirements) { |
| 41 | + FileLocator.getBundleFileLocation(bundle).ifPresent(file -> { |
| 42 | + if (file.isDirectory()) { |
| 43 | + // For bundles from the workspace launch include the bin folder for classes |
| 44 | + File outputFolder = new File(file, "bin"); |
| 45 | + if (outputFolder.exists()) { |
| 46 | + list.add(outputFolder); |
| 47 | + } |
| 48 | + } |
| 49 | + list.add(file); |
| 50 | + }); |
| 51 | + } |
| 52 | + return list; |
| 53 | + } |
| 54 | + |
| 55 | + private void collectBundles(Bundle bundle, Set<Bundle> bundleRequirements) { |
| 56 | + if (isValid(bundle) && bundleRequirements.add(bundle)) { |
| 57 | + BundleWiring wiring = bundle.adapt(BundleWiring.class); |
| 58 | + List<BundleWire> wires = wiring.getRequiredWires("osgi.wiring.package"); |
| 59 | + for (BundleWire bundleWire : wires) { |
| 60 | + collectBundles(bundleWire.getProvider().getBundle(), bundleRequirements); |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + } |
| 65 | + |
| 66 | + private boolean isValid(Bundle bundle) { |
| 67 | + if (bundle == null) { |
| 68 | + return false; |
| 69 | + } |
| 70 | + String bsn = bundle.getSymbolicName(); |
| 71 | + if ("slf4j.api".equals(bsn)) { |
| 72 | + // slf4j is already provided |
| 73 | + return false; |
| 74 | + } |
| 75 | + return true; |
| 76 | + } |
| 77 | + |
| 78 | +} |
0 commit comments