-
Notifications
You must be signed in to change notification settings - Fork 283
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a new property
turms.plugin.java.duplicate-class-load-strategy
…
…to control how to handle duplicate classes defined by both the Turms server and the plugin when loading classes by the plugin classloader
- Loading branch information
1 parent
973fddb
commit 98afa7d
Showing
9 changed files
with
204 additions
and
30 deletions.
There are no files selected for viewing
66 changes: 66 additions & 0 deletions
66
turms-server-common/src/main/java/im/turms/server/common/infra/archive/ZipUtils.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,66 @@ | ||
/* | ||
* Copyright (C) 2019 The Turms Project | ||
* https://github.com/turms-im/turms | ||
* | ||
* 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 im.turms.server.common.infra.archive; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.util.zip.ZipEntry; | ||
import java.util.zip.ZipFile; | ||
|
||
/** | ||
* @author James Chen | ||
*/ | ||
public final class ZipUtils { | ||
|
||
private static final byte[] BYTES_EMPTY = new byte[0]; | ||
|
||
private ZipUtils() { | ||
} | ||
|
||
public static byte[] readEntry(ZipFile zipFile, ZipEntry entry) throws IOException { | ||
long size = entry.getSize(); | ||
if (size == 0) { | ||
return BYTES_EMPTY; | ||
} | ||
if (size < 0) { | ||
try (InputStream inputStream = zipFile.getInputStream(entry)) { | ||
return inputStream.readAllBytes(); | ||
} | ||
} | ||
if (size > Integer.MAX_VALUE) { | ||
throw new IOException( | ||
"The size of the entry must be less than or equal to " | ||
+ Integer.MAX_VALUE | ||
+ ", but got:" | ||
+ size); | ||
} | ||
int sizeInt = (int) size; | ||
byte[] bytes = new byte[sizeInt]; | ||
try (InputStream inputStream = zipFile.getInputStream(entry)) { | ||
int bytesRead = inputStream.readNBytes(bytes, 0, sizeInt); | ||
if (bytesRead != sizeInt) { | ||
throw new IOException( | ||
"The size of the entry must be " | ||
+ sizeInt | ||
+ ", but got: " | ||
+ bytesRead); | ||
} | ||
} | ||
return bytes; | ||
} | ||
} |
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
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
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
54 changes: 54 additions & 0 deletions
54
.../main/java/im/turms/server/common/infra/property/constant/DuplicateClassLoadStrategy.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,54 @@ | ||
/* | ||
* Copyright (C) 2019 The Turms Project | ||
* https://github.com/turms-im/turms | ||
* | ||
* 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 im.turms.server.common.infra.property.constant; | ||
|
||
/** | ||
* We don't allow child-first visibility as it is more error-prone: For example, if the plugin | ||
* contains the {@link reactor.core.publisher.Mono} class, and pass its instance to the Turms | ||
* server, which will cause the JVM to throw an error as the "same" class are loaded by different | ||
* class loaders: the application classloader and the plugin classloader. | ||
* | ||
* @author James Chen | ||
*/ | ||
public enum DuplicateClassLoadStrategy { | ||
/** | ||
* Consider this strategy as the "strict" mode. | ||
* <p> | ||
* Throw an exception if the class is defined by both the Turms server and the plugin. | ||
* <p> | ||
* 1. Pros: There are won't be any duplicate class, and the classes in the plugin should always | ||
* work as expected. | ||
* <p> | ||
* 2. Cons: The plugin developers need to reallocate the classes in the plugin, which is | ||
* troublesome and error-prone as there are usually many transitive dependencies. | ||
*/ | ||
THROW_EXCEPTION, | ||
/** | ||
* Consider this strategy as the "lenient" mode. | ||
* <p> | ||
* Parent-first visibility: | ||
* <p> | ||
* 1. Pros: The plugin developers don't need to reallocate the classes in the plugin, which is | ||
* troublesome and error-prone as there are usually many transitive dependencies. | ||
* <p> | ||
* 2. Cons: The plugin developer may be confused as the classes in their plugin may not work as | ||
* expected. Though the plugin developers can always fix the issues by introducing Turms server | ||
* as the provided dependency to use the same classes used by the Turms server. | ||
*/ | ||
PARENT_FIRST, | ||
} |
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
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
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
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