|
| 1 | +/** |
| 2 | + * Copyright (c) 2025-2025 IILS mbH and others. |
| 3 | + * All rights reserved. This program and the accompanying materials |
| 4 | + * are made available under the terms of the Eclipse Public License v2.0 |
| 5 | + * which accompanies this distribution, and is available at |
| 6 | + * http://www.eclipse.org/legal/epl-v20.html |
| 7 | + * |
| 8 | + * Contributors: |
| 9 | + * Hannes Wellmann (IILS mbH) - Initial API and implementation |
| 10 | + */ |
| 11 | +package org.eclipse.emf.ecore.plugin; |
| 12 | + |
| 13 | + |
| 14 | +import java.io.IOException; |
| 15 | +import java.io.InputStream; |
| 16 | +import java.util.ArrayList; |
| 17 | +import java.util.Collections; |
| 18 | +import java.util.List; |
| 19 | +import java.util.Map; |
| 20 | +import java.util.function.Function; |
| 21 | + |
| 22 | +import org.eclipse.emf.common.util.URI; |
| 23 | +import org.eclipse.emf.common.util.WrappedException; |
| 24 | +import org.eclipse.emf.ecore.EFactory; |
| 25 | +import org.eclipse.emf.ecore.EPackage; |
| 26 | +import org.eclipse.emf.ecore.plugin.ManifestEPackageTracker.ManifestEPackageDescriptor; |
| 27 | +import org.eclipse.emf.ecore.resource.URIConverter; |
| 28 | +import org.eclipse.osgi.util.ManifestElement; |
| 29 | +import org.osgi.framework.Bundle; |
| 30 | +import org.osgi.framework.BundleContext; |
| 31 | +import org.osgi.framework.BundleEvent; |
| 32 | +import org.osgi.framework.BundleException; |
| 33 | +import org.osgi.framework.Constants; |
| 34 | +import org.osgi.framework.wiring.BundleRevision; |
| 35 | +import org.osgi.resource.Capability; |
| 36 | +import org.osgi.resource.Resource; |
| 37 | +import org.osgi.util.tracker.BundleTracker; |
| 38 | + |
| 39 | + |
| 40 | +/** |
| 41 | + * An {@link BundleTracker OSGi-BundleTracker} that reads and registers the EPackages generated into {@code META-INF/MANIFEST.MF} files as provided OSGi capabilities: |
| 42 | + * |
| 43 | + * <pre> |
| 44 | + * Provide-Capability: |
| 45 | + * org.eclipse.emf.ecore.generated_package; |
| 46 | + * uri=http://foo.bar/packA; |
| 47 | + * class=packA.PackAPackage; |
| 48 | + * genModel="model/packA.genmodel" |
| 49 | + * </pre> |
| 50 | + */ |
| 51 | +class ManifestEPackageTracker extends BundleTracker<List<ManifestEPackageDescriptor>> |
| 52 | +{ |
| 53 | + public static final String GENERATED_PACKAGE_NAMESPACE = "org.eclipse.emf.ecore.generated_package"; |
| 54 | + |
| 55 | + public static final String ATTRIBUTE_URI = "uri"; |
| 56 | + |
| 57 | + public static final String ATTRIBUTE_CLASS = "class"; |
| 58 | + |
| 59 | + public static final String ATTRIBUTE_GEN_MODEL = "genModel"; |
| 60 | + |
| 61 | + static void registerManifestPackagesIfAbsent(URI manifest, ClassLoader classLoader) throws IOException, BundleException |
| 62 | + { |
| 63 | + try (InputStream content = URIConverter.INSTANCE.createInputStream(manifest)) |
| 64 | + { |
| 65 | + List<ManifestEPackageDescriptor> descriptors = ManifestEPackageDescriptor.parse(content, classLoader); |
| 66 | + for (ManifestEPackageDescriptor descriptor : descriptors) |
| 67 | + { |
| 68 | + registerIfAbsent(descriptor, genModel -> manifest.trimSegments(2).appendFileExtension(genModel)); |
| 69 | + } |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + ManifestEPackageTracker(BundleContext context) |
| 74 | + { |
| 75 | + // TODO: check if stopping and (re-)starting creates a new Classloader?! But re-wiring does?! |
| 76 | + super(context, Bundle.RESOLVED | Bundle.STARTING | Bundle.ACTIVE | Bundle.STOPPING, null); |
| 77 | + } |
| 78 | + |
| 79 | + @Override |
| 80 | + public List<ManifestEPackageDescriptor> addingBundle(Bundle bundle, BundleEvent event) |
| 81 | + { |
| 82 | + Resource resource = bundle.adapt(BundleRevision.class); |
| 83 | + List<ManifestEPackageDescriptor> descriptors = new ArrayList<>(); |
| 84 | + for (Capability capability : resource.getCapabilities(GENERATED_PACKAGE_NAMESPACE)) |
| 85 | + { |
| 86 | + ManifestEPackageDescriptor descriptor = ManifestEPackageDescriptor.parse(bundle, capability); |
| 87 | + if (registerIfAbsent(descriptor, genModel -> { |
| 88 | + String path = bundle.getSymbolicName() + "/" + genModel; |
| 89 | + return URI.createPlatformPluginURI(path, true); |
| 90 | + })) |
| 91 | + { |
| 92 | + descriptors.add(descriptor); |
| 93 | + } |
| 94 | + } |
| 95 | + return !descriptors.isEmpty() ? Collections.unmodifiableList(descriptors) : null; |
| 96 | + } |
| 97 | + |
| 98 | + private static boolean registerIfAbsent(ManifestEPackageDescriptor descriptor, Function<String, URI> absoluteGenModelURIFactory) |
| 99 | + { |
| 100 | + String packageURI = descriptor.packageURI; |
| 101 | + if (EPackage.Registry.INSTANCE.putIfAbsent(packageURI, descriptor) == null) |
| 102 | + { |
| 103 | + // Only add if absent to grant precedence for registrations through a plugin.xml |
| 104 | + |
| 105 | + String genModel = descriptor.genModel; |
| 106 | + URI genModelURI = URI.createURI(genModel); |
| 107 | + if (genModelURI.isRelative()) |
| 108 | + { |
| 109 | + genModelURI = absoluteGenModelURIFactory.apply(genModel); |
| 110 | + } |
| 111 | + EcorePlugin.getEPackageNsURIToGenModelLocationMap(false).put(packageURI, genModelURI); |
| 112 | + return true; |
| 113 | + } |
| 114 | + return false; |
| 115 | + } |
| 116 | + |
| 117 | + @Override |
| 118 | + public void removedBundle(Bundle bundle, BundleEvent event, List<ManifestEPackageDescriptor> packages) |
| 119 | + { |
| 120 | + for (ManifestEPackageDescriptor descriptor : packages) |
| 121 | + { |
| 122 | + String uri = descriptor.packageURI; |
| 123 | + EPackage.Registry.INSTANCE.remove(uri); |
| 124 | + EcorePlugin.getEPackageNsURIToGenModelLocationMap(false).remove(uri); |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + private static interface ClassLoaderWrapper |
| 129 | + { |
| 130 | + Class<?> loadClass(String name) throws ClassNotFoundException; |
| 131 | + } |
| 132 | + |
| 133 | + static class ManifestEPackageDescriptor implements EPackage.Descriptor |
| 134 | + { |
| 135 | + |
| 136 | + static ManifestEPackageDescriptor parse(Bundle bundle, Capability capability) |
| 137 | + { |
| 138 | + Map<String, Object> attributes = capability.getAttributes(); |
| 139 | + return new ManifestEPackageDescriptor( |
| 140 | + bundle::loadClass, |
| 141 | + (String)attributes.get(ATTRIBUTE_URI), |
| 142 | + (String)attributes.get(ATTRIBUTE_CLASS), |
| 143 | + (String)attributes.get(ATTRIBUTE_GEN_MODEL)); |
| 144 | + } |
| 145 | + |
| 146 | + static List<ManifestEPackageDescriptor> parse(InputStream manifestContent, ClassLoader classLoader) throws IOException, BundleException |
| 147 | + { |
| 148 | + Map<String, String> bundleManifest = ManifestElement.parseBundleManifest(manifestContent, null); |
| 149 | + String value = bundleManifest.get(Constants.PROVIDE_CAPABILITY); |
| 150 | + if (value == null || value.trim().isEmpty()) |
| 151 | + { |
| 152 | + return Collections.emptyList(); |
| 153 | + } |
| 154 | + ManifestElement[] elements = ManifestElement.parseHeader(Constants.PROVIDE_CAPABILITY, value.trim()); |
| 155 | + if (elements == null) |
| 156 | + { |
| 157 | + return Collections.emptyList(); |
| 158 | + } |
| 159 | + List<ManifestEPackageDescriptor> descriptors = new ArrayList<>(); |
| 160 | + for (ManifestElement element : elements) |
| 161 | + { |
| 162 | + if (GENERATED_PACKAGE_NAMESPACE.equals(element.getValue())) |
| 163 | + { |
| 164 | + ManifestEPackageDescriptor descriptor = new ManifestEPackageDescriptor( |
| 165 | + classLoader::loadClass, |
| 166 | + element.getAttribute(ATTRIBUTE_URI), |
| 167 | + element.getAttribute(ATTRIBUTE_CLASS), |
| 168 | + element.getAttribute(ATTRIBUTE_GEN_MODEL)); |
| 169 | + descriptors.add(descriptor); |
| 170 | + } |
| 171 | + } |
| 172 | + return descriptors; |
| 173 | + } |
| 174 | + |
| 175 | + final ClassLoaderWrapper classLoader; |
| 176 | + |
| 177 | + final String packageURI; |
| 178 | + |
| 179 | + final String packageClassName; |
| 180 | + |
| 181 | + final String genModel; |
| 182 | + |
| 183 | + ManifestEPackageDescriptor(Bundle bundle, String packageURI, String packageClassName, String genModel) |
| 184 | + { // Don't use current Wiring ClassLoader directly to allow changes until the first EPackage is eventually created |
| 185 | + this(bundle::loadClass, packageURI, packageClassName, genModel); |
| 186 | + } |
| 187 | + |
| 188 | + ManifestEPackageDescriptor(ClassLoader classLoader, String packageURI, String packageClassName, String genModel) |
| 189 | + { |
| 190 | + this(classLoader::loadClass, packageURI, packageClassName, genModel); |
| 191 | + } |
| 192 | + |
| 193 | + private ManifestEPackageDescriptor(ClassLoaderWrapper classLoader, String packageURI, String packageClassName, String genModel) |
| 194 | + { |
| 195 | + this.classLoader = classLoader; |
| 196 | + this.packageURI = packageURI; |
| 197 | + this.packageClassName = packageClassName; |
| 198 | + this.genModel = genModel; |
| 199 | + } |
| 200 | + |
| 201 | + @Override |
| 202 | + public EPackage getEPackage() |
| 203 | + { |
| 204 | + try |
| 205 | + { |
| 206 | + Class<?> packageClass = classLoader.loadClass(packageClassName); |
| 207 | + return (EPackage)packageClass.getField("eINSTANCE").get(null); |
| 208 | + } |
| 209 | + catch (ReflectiveOperationException e) |
| 210 | + { |
| 211 | + throw new WrappedException(e); |
| 212 | + } |
| 213 | + } |
| 214 | + |
| 215 | + @Override |
| 216 | + public EFactory getEFactory() |
| 217 | + { |
| 218 | + EPackage ePackage = getEPackage(); |
| 219 | + return ePackage != null ? ePackage.getEFactoryInstance() : null; |
| 220 | + } |
| 221 | + } |
| 222 | + |
| 223 | +} |
0 commit comments