use of com.unboundid.util.InternalUseOnly in project ldapsdk by pingidentity.
the class SanityCheck method validateSDKJarFile.
/**
* Validates the contents of the unboundid-ldapsdk.jar file to
* ensure that it doesn't contain anything that isn't supposed to be there.
*
* @param jarFile The unboundid-ldapsdk.jar file to be examined.
*
* @throws BuildException If a problem is found with the content of the jar
* file.
*/
private void validateSDKJarFile(final File jarFile) throws BuildException {
JarFile jar = null;
try {
jar = new JarFile(jarFile);
final HashSet<String> packageNames = new HashSet<>(50);
// Look at the files contained in the jar to make sure they are correct.
final Enumeration<? extends JarEntry> entries = jar.entries();
while (entries.hasMoreElements()) {
final JarEntry jarEntry = entries.nextElement();
if ((!jarEntry.isDirectory()) && (!jarEntry.getName().startsWith("META-INF"))) {
final String name = jarEntry.getName().replace('\\', '/');
final int lastSlashPos = name.lastIndexOf('/');
if (lastSlashPos > 0) {
// Try to load the class. If we can't do it, then it shouldn't be
// considered an error, but we want to be able to load at least one
// class from each package so that the class loader knows about all
// of the packages so we can see what annotations might be defined
// for them.
final int classPos = name.lastIndexOf(".class");
if (classPos > 0) {
final String className = name.substring(0, classPos).replace('/', '.');
try {
Class.forName(className);
} catch (final Exception e) {
}
}
final String packageName = name.substring(0, lastSlashPos).replace('/', '.');
boolean acceptablePackage = false;
for (final String pkg : PACKAGES) {
if (packageName.equals(pkg)) {
acceptablePackage = true;
break;
}
}
if (!acceptablePackage) {
throw new BuildException("ERROR: Unexpected class file " + jarEntry.getName() + " found in package " + packageName + " of the " + jarFile.getAbsolutePath() + " jar file -- " + "this class is not in any of the defined packages.");
}
packageNames.add(packageName);
}
}
}
// Look at the manifest to ensure that the list of exported packages is
// correct.
final Manifest manifest = jar.getManifest();
if (manifest == null) {
throw new BuildException("Unable to read the manifest from jar file " + jarFile.getAbsolutePath());
}
final Attributes attributes = manifest.getMainAttributes();
if (attributes == null) {
throw new BuildException("Could not find any main attributes in the " + jarFile.getAbsolutePath() + " manifest");
}
final String exportPackageStr = attributes.getValue("Export-Package");
if (exportPackageStr == null) {
throw new BuildException("Could not find an Export-Package attribute " + "in the " + jarFile.getAbsolutePath() + " manifest");
}
final String versionStr = ";version=\"" + MAJOR_VERSION + '.' + MINOR_VERSION + '.' + POINT_VERSION + '"';
StringTokenizer tokenizer = new StringTokenizer(exportPackageStr, ", ");
while (tokenizer.hasMoreTokens()) {
final String exportToken = tokenizer.nextToken();
if (!exportToken.endsWith(versionStr)) {
throw new BuildException("Export-Package value " + exportToken + " does not end with expected version component " + versionStr);
}
final String packageName = exportToken.substring(0, exportToken.length() - versionStr.length());
if (!packageNames.remove(packageName)) {
throw new BuildException("Unexpected package " + packageName + " found in the Export-Package attribute of the " + jarFile.getAbsolutePath() + " manifest");
}
}
final String importPackageStr = attributes.getValue("Import-Package");
if (importPackageStr == null) {
throw new BuildException("Could not find an Import-Package attribute " + "in the " + jarFile.getAbsolutePath() + " manifest");
}
tokenizer = new StringTokenizer(importPackageStr, ", ");
while (tokenizer.hasMoreTokens()) {
final String importToken = tokenizer.nextToken();
if (osgiImportedPackages.contains(importToken)) {
throw new BuildException("Duplicate Import-Package value " + importToken + " found in the" + jarFile.getAbsolutePath() + " manifest");
} else {
osgiImportedPackages.add(importToken);
}
}
// package or be marked with an @InternalUseOnly annotation.
for (final String packageName : packageNames) {
if (packageName.endsWith(".examples")) {
continue;
}
final Package p = Package.getPackage(packageName);
if (p == null) {
throw new BuildException("Unable to find any information about " + "package " + packageName + " contained in the " + jarFile.getAbsolutePath() + " jar file but not included in " + "the Export-Package manifest attribute");
}
if (!p.isAnnotationPresent(InternalUseOnly.class)) {
throw new BuildException("Package " + packageName + " contained in " + "jar file " + jarFile.getAbsolutePath() + " is not included " + "in the Export-Package manifest attribute and is not marked " + "@InternalUseOnly");
}
}
} catch (final IOException ioe) {
throw new BuildException("ERROR: I/O error encountered while reading " + "jar file " + jarFile.getAbsolutePath() + ": " + ioe, ioe);
} finally {
try {
if (jar != null) {
jar.close();
}
} catch (final Exception e) {
}
}
}
use of com.unboundid.util.InternalUseOnly in project ldapsdk by pingidentity.
the class InternalSDKHelper method cancel.
/**
* Sends an LDAP cancel extended request to the server over the provided
* connection without waiting for the response. This is intended for use when
* it is necessary to send a cancel request over a connection operating in
* synchronous mode.
*
* @param connection The connection over which to send the cancel
* request.
* @param targetMessageID The message ID of the request to cancel.
* @param controls The set of controls to include in the request.
*
* @throws LDAPException If a problem occurs while sending the cancel
* request.
*/
@InternalUseOnly()
public static void cancel(@NotNull final LDAPConnection connection, final int targetMessageID, @Nullable final Control... controls) throws LDAPException {
final int messageID = connection.nextMessageID();
final CancelExtendedRequest cancelRequest = new CancelExtendedRequest(targetMessageID);
Debug.debugLDAPRequest(Level.INFO, cancelRequest, messageID, connection);
final LDAPConnectionLogger logger = connection.getConnectionOptions().getConnectionLogger();
if (logger != null) {
logger.logExtendedRequest(connection, messageID, cancelRequest);
}
connection.sendMessage(new LDAPMessage(messageID, new ExtendedRequest(cancelRequest), controls), connection.getConnectionOptions().getExtendedOperationResponseTimeoutMillis(CancelExtendedRequest.CANCEL_REQUEST_OID));
}
Aggregations