Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add "replace" as a default available transformer #731

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading