Example usage for java.lang Package getClass

List of usage examples for java.lang Package getClass

Introduction

In this page you can find the example usage for java.lang Package getClass.

Prototype

@HotSpotIntrinsicCandidate
public final native Class<?> getClass();

Source Link

Document

Returns the runtime class of this Object .

Usage

From source file:net.chaosserver.timelord.swingui.Timelord.java

/**
 * Shows the about dialog that tells about the application.
 *///w  w w  .  ja  va 2s  .com
public void showAboutDialog() {
    Package packageInfo = Package.getPackage("net.chaosserver.timelord.swingui");

    if (log.isTraceEnabled()) {
        if (packageInfo != null) {
            StringBuffer sb = new StringBuffer();
            sb.append(packageInfo.getClass().getName());
            sb.append(" [name=");
            sb.append(packageInfo.getName());
            sb.append(", specificationTitle=");
            sb.append(packageInfo.getSpecificationTitle());
            sb.append(", specificationVersion=");
            sb.append(packageInfo.getSpecificationVersion());
            sb.append(", specificationVendor=");
            sb.append(packageInfo.getSpecificationVendor());
            sb.append(", implementationTitle=");
            sb.append(packageInfo.getImplementationTitle());
            sb.append(", implementationVersion=");
            sb.append(packageInfo.getImplementationVersion());
            sb.append(", implementationVendor=");
            sb.append(packageInfo.getImplementationVendor());
            sb.append("]");
            log.trace(sb.toString());
        }
    }

    StringBuffer sb = new StringBuffer();
    sb.append("Timelord");

    if ((packageInfo != null) && (packageInfo.getImplementationVersion() != null)) {
        sb.append(" [");
        sb.append(packageInfo.getImplementationVersion());
        sb.append("]");
    } else {
        Properties appProperties = getAppProperties();
        if (appProperties != null) {
            sb.append(" ");
            sb.append(appProperties.getProperty("implementation.version", "[Unknown Version]"));
        } else {
            sb.append(" [Unknown Version]");
        }
    }

    sb.append("\n");
    sb.append(resourceBundle.getString(RROOT + ".about"));

    JOptionPane.showMessageDialog(applicationFrame, sb.toString(), "About Timelord",
            JOptionPane.INFORMATION_MESSAGE, applicationIcon);
}

From source file:com.android.sdklib.internal.repository.Archive.java

/**
 * Install the given archive in the given folder.
 *//*from w  ww.j a  v  a2s. co  m*/
private boolean unarchive(String osSdkRoot, File archiveFile, SdkManager sdkManager, ITaskMonitor monitor) {
    boolean success = false;
    Package pkg = getParentPackage();
    String pkgName = pkg.getShortDescription();
    String pkgDesc = String.format("Installing %1$s", pkgName);
    monitor.setDescription(pkgDesc);
    monitor.setResult(pkgDesc);

    // We always unzip in a temp folder which name depends on the package type
    // (e.g. addon, tools, etc.) and then move the folder to the destination folder.
    // If the destination folder exists, it will be renamed and deleted at the very
    // end if everything succeeded.

    String pkgKind = pkg.getClass().getSimpleName();

    File destFolder = null;
    File unzipDestFolder = null;
    File oldDestFolder = null;

    try {
        // Find a new temp folder that doesn't exist yet
        unzipDestFolder = createTempFolder(osSdkRoot, pkgKind, "new"); //$NON-NLS-1$

        if (unzipDestFolder == null) {
            // this should not seriously happen.
            monitor.setResult("Failed to find a temp directory in %1$s.", osSdkRoot);
            return false;
        }

        if (!unzipDestFolder.mkdirs()) {
            monitor.setResult("Failed to create directory %1$s", unzipDestFolder.getPath());
            return false;
        }

        String[] zipRootFolder = new String[] { null };
        if (!unzipFolder(archiveFile, getSize(), unzipDestFolder, pkgDesc, zipRootFolder, monitor)) {
            return false;
        }

        if (!generateSourceProperties(unzipDestFolder)) {
            return false;
        }

        // Compute destination directory
        destFolder = pkg.getInstallFolder(osSdkRoot, zipRootFolder[0], sdkManager);

        if (destFolder == null) {
            // this should not seriously happen.
            monitor.setResult("Failed to compute installation directory for %1$s.", pkgName);
            return false;
        }

        if (!pkg.preInstallHook(this, monitor, osSdkRoot, destFolder)) {
            monitor.setResult("Skipping archive: %1$s", pkgName);
            return false;
        }

        // Swap the old folder by the new one.
        // We have 2 "folder rename" (aka moves) to do.
        // They must both succeed in the right order.
        boolean move1done = false;
        boolean move2done = false;
        while (!move1done || !move2done) {
            File renameFailedForDir = null;

            // Case where the dest dir already exists
            if (!move1done) {
                if (destFolder.isDirectory()) {
                    // Create a new temp/old dir
                    if (oldDestFolder == null) {
                        oldDestFolder = createTempFolder(osSdkRoot, pkgKind, "old"); //$NON-NLS-1$
                    }
                    if (oldDestFolder == null) {
                        // this should not seriously happen.
                        monitor.setResult("Failed to find a temp directory in %1$s.", osSdkRoot);
                        return false;
                    }

                    // try to move the current dest dir to the temp/old one
                    if (!destFolder.renameTo(oldDestFolder)) {
                        monitor.setResult("Failed to rename directory %1$s to %2$s.", destFolder.getPath(),
                                oldDestFolder.getPath());
                        renameFailedForDir = destFolder;
                    }
                }

                move1done = (renameFailedForDir == null);
            }

            // Case where there's no dest dir or we successfully moved it to temp/old
            // We now try to move the temp/unzip to the dest dir
            if (move1done && !move2done) {
                if (renameFailedForDir == null && !unzipDestFolder.renameTo(destFolder)) {
                    monitor.setResult("Failed to rename directory %1$s to %2$s", unzipDestFolder.getPath(),
                            destFolder.getPath());
                    renameFailedForDir = unzipDestFolder;
                }

                move2done = (renameFailedForDir == null);
            }

            if (renameFailedForDir != null) {
                if (SdkConstants.CURRENT_PLATFORM == SdkConstants.PLATFORM_WINDOWS) {

                    String msg = String.format(
                            "-= Warning ! =-\n" + "A folder failed to be renamed or moved. On Windows this "
                                    + "typically means that a program is using that folder (for example "
                                    + "Windows Explorer or your anti-virus software.)\n"
                                    + "Please momentarily deactivate your anti-virus software.\n"
                                    + "Please also close any running programs that may be accessing "
                                    + "the directory '%1$s'.\n" + "When ready, press YES to try again.",
                            renameFailedForDir.getPath());

                    if (monitor.displayPrompt("SDK Manager: failed to install", msg)) {
                        // loop, trying to rename the temp dir into the destination
                        continue;
                    }

                }
                return false;
            }
            break;
        }

        unzipDestFolder = null;
        success = true;
        pkg.postInstallHook(this, monitor, destFolder);
        return true;

    } finally {
        // Cleanup if the unzip folder is still set.
        deleteFileOrFolder(oldDestFolder);
        deleteFileOrFolder(unzipDestFolder);

        // In case of failure, we call the postInstallHool with a null directory
        if (!success) {
            pkg.postInstallHook(this, monitor, null /*installDir*/);
        }
    }
}

From source file:com.android.sdklib.internal.repository.ArchiveInstaller.java

/**
 * Install the given archive in the given folder.
 *///w ww .  j  a v a  2 s .  c om
private boolean unarchive(Archive archive, String osSdkRoot, File archiveFile, SdkManager sdkManager,
        ITaskMonitor monitor) {
    boolean success = false;
    Package pkg = archive.getParentPackage();
    String pkgName = pkg.getShortDescription();
    String pkgDesc = String.format("Installing %1$s", pkgName);
    monitor.setDescription(pkgDesc);
    monitor.setResult(pkgDesc);

    // Ideally we want to always unzip in a temp folder which name depends on the package
    // type (e.g. addon, tools, etc.) and then move the folder to the destination folder.
    // If the destination folder exists, it will be renamed and deleted at the very
    // end if everything succeeded. This provides a nice atomic swap and should leave the
    // original folder untouched in case something wrong (e.g. program crash) in the
    // middle of the unzip operation.
    //
    // However that doesn't work on Windows, we always end up not being able to move the
    // new folder. There are actually 2 cases:
    // A- A process such as a the explorer is locking the *old* folder or a file inside
    //    (e.g. adb.exe)
    //    In this case we really shouldn't be tried to work around it and we need to let
    //    the user know and let it close apps that access that folder.
    // B- A process is locking the *new* folder. Very often this turns to be a file indexer
    //    or an anti-virus that is busy scanning the new folder that we just unzipped.
    //
    // So we're going to change the strategy:
    // 1- Try to move the old folder to a temp/old folder. This might fail in case of issue A.
    //    Note: for platform-tools, we can try killing adb first.
    //    If it still fails, we do nothing and ask the user to terminate apps that can be
    //    locking that folder.
    // 2- Once the old folder is out of the way, we unzip the archive directly into the
    //    optimal new location. We no longer unzip it in a temp folder and move it since we
    //    know that's what fails in most of the cases.
    // 3- If the unzip fails, remove everything and try to restore the old folder by doing
    //    a *copy* in place and not a folder move (which will likely fail too).

    String pkgKind = pkg.getClass().getSimpleName();

    File destFolder = null;
    File oldDestFolder = null;

    try {
        // -0- Compute destination directory and check install pre-conditions

        destFolder = pkg.getInstallFolder(osSdkRoot, sdkManager);

        if (destFolder == null) {
            // this should not seriously happen.
            monitor.setResult("Failed to compute installation directory for %1$s.", pkgName);
            return false;
        }

        if (!pkg.preInstallHook(archive, monitor, osSdkRoot, destFolder)) {
            monitor.setResult("Skipping archive: %1$s", pkgName);
            return false;
        }

        // -1- move old folder.

        if (destFolder.exists()) {
            // Create a new temp/old dir
            if (oldDestFolder == null) {
                oldDestFolder = getNewTempFolder(osSdkRoot, pkgKind, "old"); //$NON-NLS-1$
            }
            if (oldDestFolder == null) {
                // this should not seriously happen.
                monitor.setResult("Failed to find a temp directory in %1$s.", osSdkRoot);
                return false;
            }

            // Try to move the current dest dir to the temp/old one. Tell the user if it failed.
            while (true) {
                if (!moveFolder(destFolder, oldDestFolder)) {
                    monitor.setResult("Failed to rename directory %1$s to %2$s.", destFolder.getPath(),
                            oldDestFolder.getPath());

                    if (SdkConstants.CURRENT_PLATFORM == SdkConstants.PLATFORM_WINDOWS) {
                        String msg = String.format(
                                "-= Warning ! =-\n" + "A folder failed to be moved. On Windows this "
                                        + "typically means that a program is using that folder (for "
                                        + "example Windows Explorer or your anti-virus software.)\n"
                                        + "Please momentarily deactivate your anti-virus software or "
                                        + "close any running programs that may be accessing the "
                                        + "directory '%1$s'.\n" + "When ready, press YES to try again.",
                                destFolder.getPath());

                        if (monitor.displayPrompt("SDK Manager: failed to install", msg)) {
                            // loop, trying to rename the temp dir into the destination
                            continue;
                        } else {
                            return false;
                        }
                    }
                }
                break;
            }
        }

        assert !destFolder.exists();

        // -2- Unzip new content directly in place.

        if (!destFolder.mkdirs()) {
            monitor.setResult("Failed to create directory %1$s", destFolder.getPath());
            return false;
        }

        if (!unzipFolder(archiveFile, archive.getSize(), destFolder, pkgDesc, monitor)) {
            return false;
        }

        if (!generateSourceProperties(archive, destFolder)) {
            monitor.setResult("Failed to generate source.properties in directory %1$s", destFolder.getPath());
            return false;
        }

        success = true;
        pkg.postInstallHook(archive, monitor, destFolder);
        return true;

    } finally {
        if (!success) {
            // In case of failure, we try to restore the old folder content.
            if (oldDestFolder != null) {
                restoreFolder(oldDestFolder, destFolder);
            }

            // We also call the postInstallHool with a null directory to give a chance
            // to the archive to cleanup after preInstallHook.
            pkg.postInstallHook(archive, monitor, null /*installDir*/);
        }

        // Cleanup if the unzip folder is still set.
        OsHelper.deleteFileOrFolder(oldDestFolder);
    }
}