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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
|
/*******************************************************************************
* Copyright (c) 2015, 2016 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.osgi.tests.wrapper.hooks.a;
import java.io.*;
import java.net.*;
import org.eclipse.osgi.container.Module;
import org.eclipse.osgi.internal.hookregistry.*;
import org.eclipse.osgi.storage.BundleInfo.Generation;
import org.eclipse.osgi.storage.bundlefile.*;
public class TestHookConfigurator implements HookConfigurator {
public void addHooks(HookRegistry hookRegistry) {
BundleFileWrapperFactoryHook modifyContent = new BundleFileWrapperFactoryHook() {
@Override
public BundleFileWrapper wrapBundleFile(BundleFile bundleFile, Generation generation, boolean base) {
return new BundleFileWrapper(bundleFile) {
@Override
public BundleEntry getEntry(String path) {
final BundleEntry original = super.getEntry(path);
final byte[] content = "CUSTOM_CONTENT".getBytes();
if ("data/resource1".equals(path)) {
return new BundleEntry() {
@Override
public long getTime() {
return original.getTime();
}
@Override
public long getSize() {
return content.length;
}
@Override
public String getName() {
return original.getName();
}
@Override
public URL getLocalURL() {
return original.getLocalURL();
}
/**
* @throws IOException
*/
@Override
public InputStream getInputStream() throws IOException {
return new ByteArrayInputStream(content);
}
@Override
public URL getFileURL() {
return original.getFileURL();
}
};
}
return original;
}
};
}
};
BundleFileWrapperFactoryHook noop = new BundleFileWrapperFactoryHook() {
@Override
public BundleFileWrapper wrapBundleFile(BundleFile bundleFile, Generation generation, boolean base) {
return new BundleFileWrapper(bundleFile) {
// Do nothing to test multiple wrappers
};
}
};
// add no-op before the getResourceURL override
hookRegistry.addBundleFileWrapperFactoryHook(noop);
// add a hook that modifies content
hookRegistry.addBundleFileWrapperFactoryHook(modifyContent);
hookRegistry.addBundleFileWrapperFactoryHook(new BundleFileWrapperFactoryHook() {
@Override
public BundleFileWrapper wrapBundleFile(BundleFile bundleFile, Generation generation, boolean base) {
return new BundleFileWrapper(bundleFile) {
@Override
public URL getResourceURL(String path, Module hostModule, int index) {
// just making sure the wrapper getResourceURL is never called
throw new RuntimeException("Should not be called");
}
@Override
protected URL createResourceURL(BundleEntry bundleEntry, Module hostModule, int index, String path) {
final URL url = super.createResourceURL(bundleEntry, hostModule, index, path);
if (url == null) {
return null;
}
try {
return new URL("custom", "custom", 0, path, new URLStreamHandler() {
@Override
protected URLConnection openConnection(URL u) throws IOException {
// TODO Auto-generated method stub
return url.openConnection();
}
});
} catch (MalformedURLException e) {
throw new RuntimeException(e);
}
}
};
}
});
// And add no-op after
hookRegistry.addBundleFileWrapperFactoryHook(noop);
}
}
|