/* * CDDL HEADER START * * The contents of this file are subject to the terms of the * Common Development and Distribution License, Version 1.0 only * (the "License"). You may not use this file except in compliance * with the License. * * You can obtain a copy of the license at legal-notices/CDDLv1_0.txt * or http://forgerock.org/license/CDDLv1.0.html. * See the License for the specific language governing permissions * and limitations under the License. * * When distributing Covered Code, include this CDDL HEADER in each * file and include the License file at legal-notices/CDDLv1_0.txt. * If applicable, add the following below this CDDL HEADER, with the * fields enclosed by brackets "[]" replaced with your own identifying * information: * Portions Copyright [yyyy] [name of copyright owner] * * CDDL HEADER END * * * Copyright 2008-2009 Sun Microsystems, Inc. * Portions Copyright 2012-2015 ForgeRock AS. */ package org.opends.server.admin; import static org.opends.messages.AdminMessages.*; import static org.opends.messages.ExtensionMessages.*; import static org.opends.server.util.StaticUtils.*; import static org.opends.server.util.ServerConstants.EOL; import java.io.ByteArrayOutputStream; import java.io.BufferedReader; import java.io.File; import java.io.FileFilter; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.PrintStream; import java.lang.reflect.Method; import java.net.MalformedURLException; import java.net.URL; import java.net.URLClassLoader; import java.util.*; import java.util.jar.Attributes; import java.util.jar.JarEntry; import java.util.jar.JarFile; import java.util.jar.Manifest; import org.forgerock.i18n.LocalizableMessage; import org.opends.server.admin.std.meta.RootCfgDefn; import org.opends.server.core.DirectoryServer; import org.forgerock.i18n.slf4j.LocalizedLogger; import org.opends.server.types.InitializationException; /** * Manages the class loader which should be used for loading configuration definition classes and associated extensions. *
* For extensions which define their own extended configuration definitions, the class loader will make sure * that the configuration definition classes are loaded and initialized. *
* Initially the class loader provider is disabled, and calls to the {@link #getClassLoader()} will return * the system default class loader. *
* Applications MUST NOT maintain persistent references to the class loader as it can change at run-time.
*/
public final class ClassLoaderProvider {
private static final LocalizedLogger logger = LocalizedLogger.getLoggerForThisClass();
/**
* Private URLClassLoader implementation.
* This is only required so that we can provide access to the addURL method.
*/
private static final class MyURLClassLoader extends URLClassLoader {
/** Create a class loader with the default parent class loader. */
public MyURLClassLoader() {
super(new URL[0]);
}
/**
* Create a class loader with the provided parent class loader.
*
* @param parent
* The parent class loader.
*/
public MyURLClassLoader(ClassLoader parent) {
super(new URL[0], parent);
}
/**
* Add a Jar file to this class loader.
*
* @param jarFile
* The name of the Jar file.
* @throws MalformedURLException
* If a protocol handler for the URL could not be found, or if some other error occurred
* while constructing the URL.
* @throws SecurityException
* If a required system property value cannot be accessed.
*/
public void addJarFile(File jarFile) throws SecurityException, MalformedURLException {
addURL(jarFile.toURI().toURL());
}
}
/** The name of the manifest file listing the core configuration definition classes. */
private static final String CORE_MANIFEST = "core.manifest";
/** The name of the manifest file listing a extension's configuration definition classes. */
private static final String EXTENSION_MANIFEST = "extension.manifest";
/** The name of the lib directory. */
private static final String LIB_DIR = "lib";
/** The name of the extensions directory. */
private static final String EXTENSIONS_DIR = "extensions";
/** The singleton instance. */
private static final ClassLoaderProvider INSTANCE = new ClassLoaderProvider();
/** Attribute name in jar's MANIFEST corresponding to the revision number. */
private static final String REVISION_NUMBER = "Revision-Number";
/** The attribute names for build information is name, version and revision number. */
private static final String[] BUILD_INFORMATION_ATTRIBUTE_NAMES =
new String[]{Attributes.Name.EXTENSION_NAME.toString(),
Attributes.Name.IMPLEMENTATION_VERSION.toString(),
REVISION_NUMBER};
/**
* Get the single application wide class loader provider instance.
*
* @return Returns the single application wide class loader provider instance.
*/
public static ClassLoaderProvider getInstance() {
return INSTANCE;
}
/** Set of registered Jar files. */
private Set
* Applications MUST NOT maintain persistent references to the class loader as it can change at run-time.
*
* @return Returns the class loader which should be used for loading classes and resources.
*/
public synchronized ClassLoader getClassLoader() {
if (loader != null) {
return loader;
} else {
return ClassLoader.getSystemClassLoader();
}
}
/**
* Indicates whether this class loader provider is enabled.
*
* @return Returns
* We contain a reference to the URLClassLoader rather than sub-class it so that it is possible to replace the
* loader at run-time. For example, when removing or replacing extension Jar files (the URLClassLoader
* only supports adding new URLs, not removal).
*/
private MyURLClassLoader loader;
/** Private constructor. */
private ClassLoaderProvider() {
// No implementation required.
}
/**
* Disable this class loader provider and removed any registered extensions.
*
* @throws IllegalStateException
* If this class loader provider is already disabled.
*/
public synchronized void disable()
throws IllegalStateException {
if (loader == null) {
throw new IllegalStateException(
"Class loader provider already disabled.");
}
loader = null;
jarFiles = new HashSet<>();
}
/**
* Enable this class loader provider using the application's class loader as the parent class loader.
*
* @throws InitializationException
* If the class loader provider could not initialize successfully.
* @throws IllegalStateException
* If this class loader provider is already enabled.
*/
public synchronized void enable()
throws InitializationException, IllegalStateException {
enable(RootCfgDefn.class.getClassLoader());
}
/**
* Enable this class loader provider using the provided parent class loader.
*
* @param parent
* The parent class loader.
* @throws InitializationException
* If the class loader provider could not initialize successfully.
* @throws IllegalStateException
* If this class loader provider is already enabled.
*/
public synchronized void enable(ClassLoader parent)
throws InitializationException, IllegalStateException {
if (loader != null) {
throw new IllegalStateException("Class loader provider already enabled.");
}
if (parent != null) {
loader = new MyURLClassLoader(parent);
} else {
loader = new MyURLClassLoader();
}
// Forcefully load all configuration definition classes in OpenDJ.jar.
initializeCoreComponents();
// Put extensions jars into the class loader and load all configuration definition classes in that they contain.
// First load the extension from the install directory, then from the instance directory.
File installExtensionsPath = buildExtensionPath(DirectoryServer.getServerRoot());
File instanceExtensionsPath = buildExtensionPath(DirectoryServer.getInstanceRoot());
initializeAllExtensions(installExtensionsPath);
if (! installExtensionsPath.getAbsolutePath().equals(instanceExtensionsPath.getAbsolutePath())) {
initializeAllExtensions(instanceExtensionsPath);
}
}
private File buildExtensionPath(String directory) {
File libDir = new File(directory, LIB_DIR);
try {
return new File(libDir, EXTENSIONS_DIR).getCanonicalFile();
} catch (Exception e) {
return new File(libDir, EXTENSIONS_DIR);
}
}
/**
* Gets the class loader which should be used for loading classes and resources. When this class loader provider
* is disabled, the system default class loader will be returned by default.
* true if this class loader provider is enabled.
*/
public synchronized boolean isEnabled() {
return loader != null;
}
/**
* Add the named extensions to this class loader.
*
* @param extensions
* A List of the names of the extensions to be loaded.
* @throws InitializationException
* If one of the extensions could not be loaded and initialized.
*/
private synchronized void addExtension(Listnull if there is no information available.
*/
public String printExtensionInformation() {
File extensionsPath = buildExtensionPath(DirectoryServer.getServerRoot());
List
index 0: the name of the extension.
*
index 1: the build number of the extension.
*
index 2: the revision number of the extension.
*
* @param extension the jar file of the extension
* @return a String array containing the name, the build number and the revision number
* of the extension given in argument
* @throws java.io.IOException thrown if the jar file has been closed.
*/
private String[] getBuildInformation(JarFile extension)
throws IOException {
String[] result = new String[3];
// retrieve MANIFEST entry and display name, version and revision
Manifest manifest = extension.getManifest();
if ( manifest != null ) {
Attributes attributes = manifest.getMainAttributes();
int index = 0;
for(String name : BUILD_INFORMATION_ATTRIBUTE_NAMES) {
String value = attributes.getValue(name);
if ( value == null ) {
value = "