Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 8ff88ababacd2fea46b47f8419c7fb378fbb3957 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/*******************************************************************************
 * Copyright (c) 2008 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/

package org.eclipse.equinox.internal.transforms;

import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.URL;

/**
 * A proxy stream transformer is a transformer instance that relies on reflection to obtain the "getInputStream" method from an underlying object.  
 * This class is useful due to the restrictions in the builder that prevent transformer providers from directly implementing {@link StreamTransformer} due to visibility and builder issues related to referring to classes within fragments.
 */
public class ProxyStreamTransformer extends StreamTransformer {

	private Method method;
	private Object object;

	/**
	 * Create a new proxy transformer based on the given object.
	 * @param object the object to proxy
	 * @throws SecurityException thrown if there is an issue utilizing the reflection methods
	 * @throws NoSuchMethodException thrown if the provided object does not have a "getInputStream" method that takes an {@link InputStream} and an {@link URL}
	 */
	public ProxyStreamTransformer(Object object) throws SecurityException, NoSuchMethodException {
		this.object = object;
		method = object.getClass().getMethod("getInputStream", new Class[] {InputStream.class, URL.class}); //$NON-NLS-1$
		Class returnType = method.getReturnType();
		if (!returnType.equals(InputStream.class))
			throw new NoSuchMethodException();

	}

	public InputStream getInputStream(InputStream inputStream, URL transformerUrl) throws IOException {
		try {
			return (InputStream) method.invoke(object, new Object[] {inputStream, transformerUrl});
		} catch (IllegalArgumentException e) {
			throw new IOException(e.getMessage());
		} catch (IllegalAccessException e) {
			throw new IOException(e.getMessage());
		} catch (InvocationTargetException e) {
			if (e.getCause() instanceof IOException)
				throw (IOException) e.getCause();
		}
		return null;
	}

	/**
	 * Get the object that is being proxied.
	 * @return the object.  Never <code>null</code>.
	 */
	public Object getTransformer() {
		return object;
	}
}

Back to the top