From 5c9f6f604cce3b9795faa814f4f56743d232b156 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christoph=20L=C3=A4ubrich?= Date: Sat, 21 Dec 2024 08:20:31 +0100 Subject: [PATCH] Add "replace" as a default available transformer A while back in the incubation phase there was a dedicated bundle that provided a "replace" transformer. As this is the very basic transformation and do not require any special dependency it is now added back (in a slightly enhanced implementation) as being available by default. --- .../META-INF/MANIFEST.MF | 2 +- .../transforms/ReplaceTransformer.java | 49 +++++++++++++++++++ .../internal/transforms/TransformerHook.java | 1 + 3 files changed, 51 insertions(+), 1 deletion(-) create mode 100644 bundles/org.eclipse.equinox.transforms.hook/src/org/eclipse/equinox/internal/transforms/ReplaceTransformer.java diff --git a/bundles/org.eclipse.equinox.transforms.hook/META-INF/MANIFEST.MF b/bundles/org.eclipse.equinox.transforms.hook/META-INF/MANIFEST.MF index 0439ba08288..2069b071de1 100644 --- a/bundles/org.eclipse.equinox.transforms.hook/META-INF/MANIFEST.MF +++ b/bundles/org.eclipse.equinox.transforms.hook/META-INF/MANIFEST.MF @@ -3,7 +3,7 @@ Bundle-ManifestVersion: 2 Bundle-Name: %bundleName Bundle-Vendor: %providerName Bundle-SymbolicName: org.eclipse.equinox.transforms.hook -Bundle-Version: 1.4.100.qualifier +Bundle-Version: 1.4.200.qualifier Fragment-Host: org.eclipse.osgi;bundle-version="[3.10.0,4.0.0)" Bundle-RequiredExecutionEnvironment: JavaSE-17 Bundle-Localization: transformsHook diff --git a/bundles/org.eclipse.equinox.transforms.hook/src/org/eclipse/equinox/internal/transforms/ReplaceTransformer.java b/bundles/org.eclipse.equinox.transforms.hook/src/org/eclipse/equinox/internal/transforms/ReplaceTransformer.java new file mode 100644 index 00000000000..cff8017f593 --- /dev/null +++ b/bundles/org.eclipse.equinox.transforms.hook/src/org/eclipse/equinox/internal/transforms/ReplaceTransformer.java @@ -0,0 +1,49 @@ +/******************************************************************************* + * Copyright (c) 2024 Christoph Läubrich and others. + * + * This program and the accompanying materials + * are made available under the terms of the Eclipse Public License 2.0 + * which accompanies this distribution, and is available at + * https://www.eclipse.org/legal/epl-2.0/ + * + * SPDX-License-Identifier: EPL-2.0 + * + * Contributors: + * Christoph Läubrich - initial API and implementation + *******************************************************************************/ +package org.eclipse.equinox.internal.transforms; + +import java.io.IOException; +import java.io.InputStream; +import java.net.URL; +import java.util.Map; +import org.eclipse.osgi.framework.log.FrameworkLogEntry; +import org.osgi.framework.BundleContext; +import org.osgi.framework.FrameworkUtil; + +/** + * Basic default transformer that simply replace a matching resource with + * another + */ +class ReplaceTransformer { + + private TransformerHook hook; + + ReplaceTransformer(TransformerHook hook) { + this.hook = hook; + } + + public InputStream getInputStream(InputStream inputStream, final URL transformerUrl) { + try { + return transformerUrl.openStream(); + } catch (IOException e) { + hook.log(FrameworkLogEntry.WARNING, "can't replace resource with " + transformerUrl, e); //$NON-NLS-1$ + return null; + } + } + + public static void register(BundleContext context, TransformerHook transformerHook) { + context.registerService(Object.class, new ReplaceTransformer(transformerHook), + FrameworkUtil.asDictionary(Map.of(TransformTuple.TRANSFORMER_TYPE, "replace"))); //$NON-NLS-1$ + } +} diff --git a/bundles/org.eclipse.equinox.transforms.hook/src/org/eclipse/equinox/internal/transforms/TransformerHook.java b/bundles/org.eclipse.equinox.transforms.hook/src/org/eclipse/equinox/internal/transforms/TransformerHook.java index 1eecc150be6..1f0bcea632f 100644 --- a/bundles/org.eclipse.equinox.transforms.hook/src/org/eclipse/equinox/internal/transforms/TransformerHook.java +++ b/bundles/org.eclipse.equinox.transforms.hook/src/org/eclipse/equinox/internal/transforms/TransformerHook.java @@ -46,6 +46,7 @@ public void addHooks(HookRegistry hookRegistry) { public void start(BundleContext context) throws BundleException { try { + ReplaceTransformer.register(context, this); this.transformers = new TransformerList(context, logServices); } catch (InvalidSyntaxException e) { throw new BundleException("Problem registering service tracker: transformers", e); //$NON-NLS-1$