Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[LOGMGR-261] Add a simple API to allow jboss-modules to implement a m…
…odule loader for cases when the log manager is on the boot class path.
- Loading branch information
Showing
5 changed files
with
208 additions
and
31 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
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,67 @@ | ||
/* | ||
* JBoss, Home of Professional Open Source. | ||
* | ||
* Copyright 2019 Red Hat, Inc., and individual contributors | ||
* as indicated by the @author tags. | ||
* | ||
* 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 org.jboss.logmanager; | ||
|
||
/** | ||
* An interface which can be used to locate information about a module. | ||
* | ||
* <p> | ||
* Note that if JBoss Modules is found on the class path for the log manager the {@link #getModuleNameOf(Class)} and | ||
* {@link #getModuleVersionOf(Class)} will not be invoked on the resolver and the name and version will be resolved from | ||
* JBoss Modules itself. | ||
* </p> | ||
* | ||
* @author <a href="mailto:jperkins@redhat.com">James R. Perkins</a> | ||
*/ | ||
public interface ModuleResolver { | ||
|
||
/** | ||
* Retrieves the name of the module for the specified class. | ||
* | ||
* @param clazz the class to resolve the module for | ||
* | ||
* @return the name of the module for the class or {@code null} if it could not be determined | ||
*/ | ||
default String getModuleNameOf(Class<?> clazz) { | ||
return JDKSpecific.getModuleNameOf(clazz); | ||
} | ||
|
||
/** | ||
* Retrieves the version of the module for the specified class. | ||
* | ||
* @param clazz the class to resolve the module for | ||
* | ||
* @return the version of the module for the class or {@code null} if it could not be determined | ||
*/ | ||
default String getModuleVersionOf(Class<?> clazz) { | ||
return JDKSpecific.getModuleVersionOf(clazz); | ||
} | ||
|
||
/** | ||
* Determines the class loader for the module. | ||
* | ||
* @param name the name of the module | ||
* | ||
* @return the class loader for the module | ||
* | ||
* @throws IllegalArgumentException if an error occurs determining the class loader | ||
*/ | ||
ClassLoader getModuleClassLoader(String name) throws IllegalArgumentException; | ||
} |
80 changes: 80 additions & 0 deletions
80
src/main/java/org/jboss/logmanager/ModuleResolverFactory.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,80 @@ | ||
/* | ||
* JBoss, Home of Professional Open Source. | ||
* | ||
* Copyright 2019 Red Hat, Inc., and individual contributors | ||
* as indicated by the @author tags. | ||
* | ||
* 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 org.jboss.logmanager; | ||
|
||
import org.jboss.modules.Module; | ||
import org.jboss.modules.ModuleLoadException; | ||
import org.jboss.modules.ModuleLoader; | ||
|
||
/** | ||
* The factory used to get an instance of the {@linkplain ModuleResolver module resolver}. | ||
* | ||
* @author <a href="mailto:jperkins@redhat.com">James R. Perkins</a> | ||
*/ | ||
public class ModuleResolverFactory { | ||
private static volatile ModuleResolver INSTANCE; | ||
|
||
/** | ||
* Returns the current instance of the {@link ModuleResolver}. | ||
* | ||
* @return the module resolver used to load modules | ||
*/ | ||
@SuppressWarnings("Convert2Lambda") | ||
public static ModuleResolver getInstance() { | ||
ModuleResolver result = INSTANCE; | ||
if (INSTANCE == null) { | ||
synchronized (ModuleResolverFactory.class) { | ||
if (INSTANCE == null) { | ||
result = INSTANCE = new ModuleResolver() { | ||
@Override | ||
public ClassLoader getModuleClassLoader(final String name) throws IllegalArgumentException { | ||
// If we're in a module use that loader, otherwise use the boot module loader | ||
ModuleLoader moduleLoader = ModuleLoader.forClass(ModuleResolver.class); | ||
if (moduleLoader == null) { | ||
moduleLoader = Module.getBootModuleLoader(); | ||
} | ||
try { | ||
return moduleLoader.loadModule(name).getClassLoader(); | ||
} catch (ModuleLoadException e) { | ||
throw new IllegalArgumentException("Could load module " + name, e); | ||
} | ||
} | ||
}; | ||
} | ||
} | ||
} | ||
return result; | ||
} | ||
|
||
/** | ||
* Sets the module resolver to use for loading modules. If set to {@code null} a default module resolver will be | ||
* used based on JBoss Modules. | ||
* | ||
* @param resolver the module resolver to use or {@code null} to use a default module resolver | ||
*/ | ||
@SuppressWarnings("unused") | ||
public static void setModuleResolver(final ModuleResolver resolver) { | ||
final SecurityManager sm = System.getSecurityManager(); | ||
if (sm != null) { | ||
sm.checkPermission(LogContext.CONTROL_PERMISSION); | ||
} | ||
INSTANCE = resolver; | ||
} | ||
} |
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