Search in sources :

Example 6 with FileLocker

use of org.eclipse.tycho.locking.facade.FileLocker in project tycho by eclipse.

the class DefaultBundleReader method getEntry.

@Override
public File getEntry(File bundleLocation, String path) {
    if (path.startsWith("external:")) {
        getLogger().warn("Ignoring Bundle-ClassPath entry '" + path + "' of bundle " + bundleLocation);
        return null;
    }
    final File result;
    if (bundleLocation.isDirectory()) {
        result = new File(bundleLocation, path);
    } else {
        try {
            File outputDirectory = new File(cacheDir, bundleLocation.getName());
            result = new File(outputDirectory, path);
            String resultPath = result.getCanonicalPath();
            if (extractedFiles.contains(resultPath) && result.exists()) {
                return result;
            } else {
                FileLocker locker = fileLockService.getFileLocker(outputDirectory);
                locker.lock(5 * 60 * 1000L);
                try {
                    extractZipEntries(bundleLocation, path, outputDirectory);
                } finally {
                    locker.release();
                }
                extractedFiles.add(resultPath);
            }
        } catch (IOException e) {
            throw new RuntimeException("IOException while extracting '" + path + "' from " + bundleLocation, e);
        }
    }
    if (result.exists()) {
        return result;
    } else {
        getLogger().debug("Bundle-ClassPath entry " + path + " does not exist in " + bundleLocation);
        return null;
    }
}
Also used : FileLocker(org.eclipse.tycho.locking.facade.FileLocker) IOException(java.io.IOException) JarFile(java.util.jar.JarFile) File(java.io.File) ZipFile(java.util.zip.ZipFile)

Example 7 with FileLocker

use of org.eclipse.tycho.locking.facade.FileLocker in project tycho by eclipse.

the class FeatureXmlTransformer method getInstallSize.

protected long getInstallSize(File location) {
    long installSize = 0;
    FileLocker locker = fileLockService.getFileLocker(location);
    locker.lock();
    try {
        try {
            JarFile jar = new JarFile(location);
            try {
                Enumeration<JarEntry> entries = jar.entries();
                while (entries.hasMoreElements()) {
                    JarEntry entry = entries.nextElement();
                    long entrySize = entry.getSize();
                    if (entrySize > 0) {
                        installSize += entrySize;
                    }
                }
            } finally {
                jar.close();
            }
        } catch (IOException e) {
            throw new RuntimeException("Could not determine installation size of file " + location, e);
        }
    } finally {
        locker.release();
    }
    return installSize;
}
Also used : FileLocker(org.eclipse.tycho.locking.facade.FileLocker) IOException(java.io.IOException) JarFile(java.util.jar.JarFile) JarEntry(java.util.jar.JarEntry)

Example 8 with FileLocker

use of org.eclipse.tycho.locking.facade.FileLocker in project tycho by eclipse.

the class UpdateSiteAssembler method unpackJar.

private void unpackJar(File location, File outputJar) {
    ZipUnArchiver unzip;
    FileLockService fileLockService;
    try {
        unzip = (ZipUnArchiver) session.lookup(ZipUnArchiver.ROLE, "zip");
        fileLockService = (FileLockService) session.lookup(FileLockService.class.getName());
    } catch (ComponentLookupException e) {
        throw new RuntimeException("Could not lookup required component", e);
    }
    outputJar.mkdirs();
    if (!outputJar.isDirectory()) {
        throw new RuntimeException("Could not create output directory " + outputJar.getAbsolutePath());
    }
    unzip.setSourceFile(location);
    unzip.setDestDirectory(outputJar);
    FileLocker locker = fileLockService.getFileLocker(location);
    locker.lock();
    try {
        unzip.extract();
    } catch (ArchiverException e) {
        throw new RuntimeException("Could not unpack jar", e);
    } finally {
        locker.release();
    }
}
Also used : FileLocker(org.eclipse.tycho.locking.facade.FileLocker) ArchiverException(org.codehaus.plexus.archiver.ArchiverException) ZipUnArchiver(org.codehaus.plexus.archiver.zip.ZipUnArchiver) ComponentLookupException(org.codehaus.plexus.component.repository.exception.ComponentLookupException) FileLockService(org.eclipse.tycho.locking.facade.FileLockService)

Example 9 with FileLocker

use of org.eclipse.tycho.locking.facade.FileLocker in project tycho by eclipse.

the class PublishProductMojo method getExpandedLauncherBinaries.

private File getExpandedLauncherBinaries() throws MojoExecutionException, MojoFailureException {
    // TODO 364134 take the executable feature from the target platform instead
    DependencyArtifacts dependencyArtifacts = TychoProjectUtils.getDependencyArtifacts(getProject());
    ArtifactDescriptor artifact = dependencyArtifacts.getArtifact(ArtifactType.TYPE_ECLIPSE_FEATURE, "org.eclipse.equinox.executable", null);
    if (artifact == null) {
        throw new MojoExecutionException("Unable to locate feature 'org.eclipse.equinox.executable'. This feature is required for native product launchers.");
    }
    checkMacOSLauncherCompatibility(artifact);
    File equinoxExecFeature = artifact.getLocation();
    if (equinoxExecFeature.isDirectory()) {
        return equinoxExecFeature.getAbsoluteFile();
    } else {
        File unzipped = new File(getProject().getBuild().getDirectory(), artifact.getKey().getId() + "-" + artifact.getKey().getVersion());
        if (unzipped.exists()) {
            return unzipped.getAbsoluteFile();
        }
        try {
            FileLocker locker = fileLockService.getFileLocker(equinoxExecFeature);
            locker.lock();
            try {
                // unzip now then:
                unzipped.mkdirs();
                deflater.setSourceFile(equinoxExecFeature);
                deflater.setDestDirectory(unzipped);
                deflater.extract();
                return unzipped.getAbsoluteFile();
            } finally {
                locker.release();
            }
        } catch (ArchiverException e) {
            throw new MojoFailureException("Unable to unzip the eqiuinox executable feature", e);
        }
    }
}
Also used : DependencyArtifacts(org.eclipse.tycho.artifacts.DependencyArtifacts) FileLocker(org.eclipse.tycho.locking.facade.FileLocker) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) ArtifactDescriptor(org.eclipse.tycho.ArtifactDescriptor) ArchiverException(org.codehaus.plexus.archiver.ArchiverException) MojoFailureException(org.apache.maven.plugin.MojoFailureException) File(java.io.File)

Example 10 with FileLocker

use of org.eclipse.tycho.locking.facade.FileLocker in project tycho by eclipse.

the class FileLockServiceTest method testLockReentranceSameLocker.

@Test
public void testLockReentranceSameLocker() throws IOException {
    FileLocker fileLocker = subject.getFileLocker(newTestFile());
    fileLocker.lock();
    try {
        // locks are not re-entrant
        fileLocker.lock(0L);
        fail("lock already held by same VM but could be acquired a second time");
    } catch (LockTimeoutException e) {
    // expected
    } finally {
        fileLocker.release();
    }
}
Also used : FileLocker(org.eclipse.tycho.locking.facade.FileLocker) LockTimeoutException(org.eclipse.tycho.locking.facade.LockTimeoutException) Test(org.junit.Test)

Aggregations

FileLocker (org.eclipse.tycho.locking.facade.FileLocker)17 File (java.io.File)9 Test (org.junit.Test)9 IOException (java.io.IOException)3 ArchiverException (org.codehaus.plexus.archiver.ArchiverException)3 LockTimeoutException (org.eclipse.tycho.locking.facade.LockTimeoutException)3 JarFile (java.util.jar.JarFile)2 ZipFile (java.util.zip.ZipFile)2 Random (java.util.Random)1 JarEntry (java.util.jar.JarEntry)1 ZipEntry (java.util.zip.ZipEntry)1 MavenExecutionException (org.apache.maven.MavenExecutionException)1 Artifact (org.apache.maven.artifact.Artifact)1 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)1 MojoFailureException (org.apache.maven.plugin.MojoFailureException)1 ZipUnArchiver (org.codehaus.plexus.archiver.zip.ZipUnArchiver)1 ComponentLookupException (org.codehaus.plexus.component.repository.exception.ComponentLookupException)1 RawInputStreamFacade (org.codehaus.plexus.util.io.RawInputStreamFacade)1 ArtifactDescriptor (org.eclipse.tycho.ArtifactDescriptor)1 DependencyArtifacts (org.eclipse.tycho.artifacts.DependencyArtifacts)1