forked from pinpoint-apm/pinpoint
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[pinpoint-apm#8890] Merge all lib jars in custom filesystem
- Loading branch information
youngjin.kim2
committed
Jun 16, 2022
1 parent
1dd05c8
commit 3a3afa9
Showing
6 changed files
with
638 additions
and
105 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
85 changes: 85 additions & 0 deletions
85
...ava/com/navercorp/pinpoint/bootstrap/java9/module/merger/InMemorySeekableByteChannel.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
/* | ||
* Copyright 2022 NAVER Corp. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.navercorp.pinpoint.bootstrap.java9.module.merger; | ||
|
||
import java.nio.ByteBuffer; | ||
import java.nio.channels.SeekableByteChannel; | ||
|
||
/** | ||
* @author youngjin.kim2 | ||
*/ | ||
public class InMemorySeekableByteChannel implements SeekableByteChannel { | ||
|
||
private final byte[] bytes; | ||
private long position = 0; | ||
|
||
public InMemorySeekableByteChannel(byte[] bytes, long position) { | ||
this(bytes); | ||
this.position = Math.min(position, bytes.length); | ||
} | ||
|
||
public InMemorySeekableByteChannel(byte[] bytes) { | ||
this.bytes = bytes; | ||
} | ||
|
||
@Override | ||
public int read(ByteBuffer dst) { | ||
if (position >= bytes.length) { | ||
return -1; | ||
} | ||
|
||
long remain = size() - position; | ||
int sz = (int) Math.min(remain, dst.capacity()); | ||
|
||
dst.put(bytes, (int) position, sz); | ||
position += sz; | ||
return sz; | ||
} | ||
|
||
@Override | ||
public int write(ByteBuffer src) { | ||
bytes[(int) position++] = src.get(); | ||
return 1; | ||
} | ||
|
||
@Override | ||
public long position() { | ||
return position; | ||
} | ||
|
||
@Override | ||
public SeekableByteChannel position(long newPosition) { | ||
return new InMemorySeekableByteChannel(bytes, newPosition); | ||
} | ||
|
||
@Override | ||
public long size() { | ||
return bytes.length; | ||
} | ||
|
||
@Override | ||
public SeekableByteChannel truncate(long size) { | ||
throw new RuntimeException("Illegal truncate attempt"); | ||
} | ||
|
||
@Override | ||
public boolean isOpen() { | ||
return true; | ||
} | ||
|
||
@Override | ||
public void close() {} | ||
} |
116 changes: 116 additions & 0 deletions
116
...p-java9/src/main/java/com/navercorp/pinpoint/bootstrap/java9/module/merger/JarMerger.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
/* | ||
* Copyright 2022 NAVER Corp. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.navercorp.pinpoint.bootstrap.java9.module.merger; | ||
|
||
import java.io.ByteArrayOutputStream; | ||
import java.io.File; | ||
import java.io.IOException; | ||
import java.lang.module.ModuleFinder; | ||
import java.net.URI; | ||
import java.util.*; | ||
import java.util.jar.JarEntry; | ||
import java.util.jar.JarFile; | ||
import java.util.jar.JarOutputStream; | ||
|
||
/** | ||
* @author youngjin.kim2 | ||
*/ | ||
public class JarMerger { | ||
|
||
private final Set<String> entries = new HashSet<>(); | ||
private final Map<String, String> providesMap = new HashMap<>(); | ||
|
||
private final JarOutputStream jarStream; | ||
private final ByteArrayOutputStream byteStream; | ||
private final String moduleName; | ||
|
||
private JarMerger(String moduleName, List<URI> uris) throws IOException { | ||
this.moduleName = moduleName; | ||
this.byteStream = new ByteArrayOutputStream(); | ||
this.jarStream = new JarOutputStream(byteStream); | ||
|
||
entries.add("META-INF/MANIFEST.MF"); | ||
entries.add("module-info.class"); | ||
|
||
for (final URI uri: uris) { | ||
add(uri); | ||
} | ||
|
||
addManifest(); | ||
addProvides(); | ||
this.jarStream.close(); | ||
} | ||
|
||
private void add(URI uri) throws IOException { | ||
if (!uri.toString().endsWith(".jar")) { | ||
return; | ||
} | ||
|
||
final File file = new File(uri); | ||
try (final JarFile jarFile = new JarFile(file)) { | ||
final Enumeration<JarEntry> newEntries = jarFile.entries(); | ||
while (newEntries.hasMoreElements()) { | ||
final JarEntry newEntry = newEntries.nextElement(); | ||
final String name = newEntry.getName(); | ||
|
||
if (name.startsWith("META-INF/services/")) { | ||
final String provides = new String(jarFile.getInputStream(newEntry).readAllBytes()); | ||
provides(name, provides); | ||
} | ||
|
||
if (entries.contains(name)) { | ||
continue; | ||
} | ||
entries.add(newEntry.getName()); | ||
|
||
addEntry(name, jarFile.getInputStream(newEntry).readAllBytes()); | ||
} | ||
} | ||
} | ||
|
||
private void provides(String name, String provides) { | ||
providesMap.put(name, providesMap.getOrDefault(name, "") + provides); | ||
} | ||
|
||
public static ModuleFinder build(String moduleName, List<URI> uris) throws IOException { | ||
return (new JarMerger(moduleName, uris)).getModuleFinder(); | ||
} | ||
|
||
private ModuleFinder getModuleFinder() { | ||
return ModuleFinder.of(SingleFileSystem.byteArrayToPath("combined.jar", byteStream.toByteArray())); | ||
} | ||
|
||
private void addManifest() throws IOException { | ||
addEntry("META-INF/MANIFEST.MF", getManifestContent().getBytes()); | ||
} | ||
|
||
private void addProvides() throws IOException { | ||
for (Map.Entry<String, String> e: providesMap.entrySet()) { | ||
addEntry("META-INF/services/" + e.getKey(), e.getValue().getBytes()); | ||
} | ||
} | ||
|
||
private void addEntry(String name, byte[] bytes) throws IOException { | ||
jarStream.putNextEntry(new JarEntry(name)); | ||
jarStream.write(bytes); | ||
jarStream.closeEntry(); | ||
} | ||
|
||
private String getManifestContent() { | ||
return "Manifest-Version: 1.0\r\nAutomatic-Module-Name: " + moduleName + "\r\n\r\n"; | ||
} | ||
|
||
} |
Oops, something went wrong.