Platform.java revision 7ccaa46b4a749896b2daabda390d8ddd3ae7743f
/*
* 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
* 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 2009-2010 Sun Microsystems, Inc.
* Portions Copyright 2013-2015 ForgeRock AS.
*/
/**
* Provides a wrapper class that collects all of the JVM vendor and JDK version
* specific code in a single place.
*/
public final class Platform
{
/** Prefix that determines which security package to use. */
/** The two security package prefixes (IBM and SUN). */
private static final PlatformIMPL IMPL;
static
{
{
}
else
{
}
IMPL = new DefaultPlatformIMPL();
}
/**
* Platform base class. Performs all of the certificate management functions.
*/
private static abstract class PlatformIMPL
{
/** Key size, key algorithm and signature algorithms used. */
private static final int KEY_SIZE = 1024;
/** Time values used in validity calculations. */
/** Methods pulled from the classes. */
private static final String GET_SELFSIGNED_CERT_METHOD =
"getSelfCertificate";
/** Classes needed to manage certificates. */
/** Constructors for each of the above classes. */
/** Filesystem APIs */
private static Method FILESYSTEMS_GETSTORES;
private static Method FILESYSTEMS_PATHSGET;
static
{
try
{
// FileSystem and FileStores APIs were introduced in JDK 7.
FILESYSTEMS_PATHSGET = Class.forName("java.nio.file.Paths").getMethod("get", String.class, String[].class);
}
catch (Exception e)
{
}
{
}
else
{ // Java 8 moved the CertAndKeyGen class to sun.security.tools.keytool
}
try
{
String.class);
}
catch (ClassNotFoundException e)
{
}
catch (SecurityException e)
{
}
catch (NoSuchMethodException e)
{
}
}
protected PlatformIMPL()
{
}
char[] pwd) throws KeyStoreException
{
try
{
{
}
}
catch (Exception e)
{
}
}
{
try
{
{
}
// Do not support certificate replies.
{
}
|| ks
else
{
}
}
catch (Exception e)
{
}
}
int validity) throws KeyStoreException
{
try
{
{
}
{
}
GENERATE_METHOD, int.class);
GET_SELFSIGNED_CERT_METHOD, X500NameClass, long.class);
}
catch (Exception e)
{
.getMessage());
}
return ks;
}
/**
* Generate a x509 certificate from the input stream. Verification is done
* only if it is self-signed.
*/
{
try
{
{
}
}
catch (Exception e)
{
}
}
/**
* Check that the issuer and subject DNs match.
*/
{
}
/**
* Normalize the data in the specified buffer.
*
* @param buffer
* The buffer to normalize.
*/
private long getUsableMemoryForCaching()
{
long youngGenSize = 0;
long oldGenSize = 0;
{
{
{
// Parallel.
}
{
// Parallel.
}
{
// CMS.
}
{
// CMS.
}
}
}
{
// We can calculate available memory based on GC info.
return oldGenSize - youngGenSize;
}
else if (oldGenSize > 0)
{
// Small old gen. It is going to be difficult to avoid full GCs if the
// young gen is bigger.
}
else
{
// Unknown GC (G1, JRocket, etc).
}
}
{
if (FILESYSTEMS_GETSTORES != null)
{
try
{
/*
* Since there is no concept of mount point in the APIs, iterate on all parents of
* the given directory until the FileSystem Store changes (hint of a different
* device, hence a mount point) or we get to root, which works too.
*/
{
{
return directory;
}
}
}
catch (Exception e)
{
throw new IOException(e);
}
}
return directory;
}
}
/** Prevent instantiation. */
private Platform()
{
}
/**
* Add the certificate in the specified path to the provided keystore;
* creating the keystore with the provided type and path if it doesn't exist.
*
* @param ks
* The keystore to add the certificate to, may be null if it doesn't
* exist.
* @param ksType
* The type to use if the keystore is created.
* @param ksPath
* The path to the keystore if it is created.
* @param alias
* The alias to store the certificate under.
* @param pwd
* The password to use in saving the certificate.
* @param certPath
* The path to the file containing the certificate.
* @throws KeyStoreException
* If an error occurred adding the certificate to the keystore.
*/
{
}
/**
* Delete the specified alias from the provided keystore.
*
* @param ks
* The keystore to delete the alias from.
* @param ksPath
* The path to the keystore.
* @param alias
* The alias to use in the request generation.
* @param pwd
* The keystore password to use.
* @throws KeyStoreException
* If an error occurred deleting the alias.
*/
char[] pwd) throws KeyStoreException
{
}
/**
* Generate a self-signed certificate using the specified alias, dn string and
* validity period. If the keystore does not exist, it will be created using
* the specified keystore type and path.
*
* @param ks
* The keystore to save the certificate in. May be null if it does
* not exist.
* @param ksType
* The keystore type to use if the keystore is created.
* @param ksPath
* The path to the keystore if the keystore is created.
* @param alias
* The alias to store the certificate under.
* @param pwd
* The password to us in saving the certificate.
* @param dn
* The dn string used as the certificate subject.
* @param validity
* The validity of the certificate in days.
* @throws KeyStoreException
* If the self-signed certificate cannot be generated.
*/
throws KeyStoreException
{
validity);
}
/**
* Default platform class.
*/
private static class DefaultPlatformIMPL extends PlatformIMPL
{
/** Normalize method. */
/** Normalized form method. */
static
{
try
{
}
{
// Do not use Normalizer. The values are already set to null.
}
}
{
try
{
}
{
// Don't do anything. buffer should be used.
}
}
}
/**
* Normalize the specified buffer.
*
* @param buffer
* The buffer to normalize.
*/
{
}
/**
* Test if a platform java vendor property starts with the specified vendor
* string.
*
* @param vendor
* The vendor to check for.
* @return {@code true} if the java vendor starts with the specified vendor
* string.
*/
{
}
/**
* Calculates the usable memory which could potentially be used by the
* application for caching objects. This method <b>does not</b> look at the
* amount of free memory, but instead tries to query the JVM's GC settings in
* order to determine the amount of usable memory in the old generation (or
* equivalent). More specifically, applications may also need to take into
* account the amount of memory already in use, for example by performing the
* following:
*
* <pre>
* Runtime runTime = Runtime.getRuntime();
* runTime.gc();
* runTime.gc();
* long freeCommittedMemory = runTime.freeMemory();
* long uncommittedMemory = runTime.maxMemory() - runTime.totalMemory();
* long freeMemory = freeCommittedMemory + uncommittedMemory;
* </pre>
*
* @return The usable memory which could potentially be used by the
* application for caching objects.
*/
public static long getUsableMemoryForCaching()
{
return IMPL.getUsableMemoryForCaching();
}
/**
* Returns the filesystem on which the given directory resides by its mountpoint.
*
* @param directory the directory whose filesystem is required
* @return the filesystem on which the given directory resides
* @throws IOException The exception in case information on filesystem/storage cannot be found
*/
{
}
}