Skip to content

Commit

Permalink
Add "replace" as a default available transformer
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
laeubi committed Dec 21, 2024
1 parent 5810f68 commit abd6756
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*******************************************************************************
* 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 replaces a matching resource with
* another.
*/
class ReplaceTransformer {

static final String TYPE = "replace"; //$NON-NLS-1$
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, String.format("Can't replace resource with %s", 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, TYPE)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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$
Expand Down

0 comments on commit abd6756

Please sign in to comment.