Search in sources :

Example 26 with PatchException

use of io.fabric8.api.PatchException in project fabric8 by jboss-fuse.

the class ServiceImpl method download.

@Override
public Iterable<Patch> download(URL url) {
    if ("file".equals(url.getProtocol())) {
        // ENTESB-4992: prevent adding non existing files or directories
        try {
            if (!new File(url.toURI()).isFile()) {
                throw new PatchException("Path " + url.getPath() + " doesn't exist or is not a file");
            }
        } catch (URISyntaxException e) {
            throw new PatchException(e.getMessage(), e);
        }
    }
    try {
        List<PatchData> patchesData = patchManagement.fetchPatches(url);
        List<Patch> patches = new ArrayList<>(patchesData.size());
        for (PatchData patchData : patchesData) {
            Patch patch = patchManagement.trackPatch(patchData);
            patches.add(patch);
        }
        return patches;
    } catch (PatchException e) {
        throw e;
    } catch (Exception e) {
        throw new PatchException("Unable to download patch from url " + url, e);
    }
}
Also used : PatchData(io.fabric8.patch.management.PatchData) ArrayList(java.util.ArrayList) PatchException(io.fabric8.patch.management.PatchException) URISyntaxException(java.net.URISyntaxException) File(java.io.File) Patch(io.fabric8.patch.management.Patch) URISyntaxException(java.net.URISyntaxException) PatchException(io.fabric8.patch.management.PatchException) BundleException(org.osgi.framework.BundleException) IOException(java.io.IOException)

Example 27 with PatchException

use of io.fabric8.api.PatchException in project fabric8 by jboss-fuse.

the class ServiceImpl method checkStandaloneChild.

/**
 * Check if this is installation in @{link {@link io.fabric8.patch.management.EnvType#STANDALONE_CHILD}}
 * - in this case the patch has to be installed in root first
 * @param patches
 */
private void checkStandaloneChild(Collection<Patch> patches) {
    if (patchManagement.isStandaloneChild()) {
        for (Patch patch : patches) {
            if (patch.getResult() == null) {
                throw new PatchException(String.format("Patch '%s' should be installed in parent container first", patch.getPatchData().getId()));
            } else {
                List<String> bases = patch.getResult().getKarafBases();
                boolean isInstalledInRoot = false;
                for (String base : bases) {
                    String[] coords = base.split("\\s*\\|\\s*");
                    if (coords.length == 2 && coords[1].trim().equals(System.getProperty("karaf.home"))) {
                        isInstalledInRoot = true;
                    }
                }
                if (!isInstalledInRoot) {
                    throw new PatchException(String.format("Patch '%s' should be installed in parent container first", patch.getPatchData().getId()));
                }
            }
        }
    }
}
Also used : PatchException(io.fabric8.patch.management.PatchException) Patch(io.fabric8.patch.management.Patch)

Example 28 with PatchException

use of io.fabric8.api.PatchException in project fabric8 by jboss-fuse.

the class ServiceImpl method applyChanges.

private void applyChanges(Map<Bundle, String> toUpdate) throws BundleException, IOException {
    List<Bundle> toStop = new ArrayList<Bundle>();
    Map<Bundle, String> lessToUpdate = new HashMap<>();
    for (Bundle b : toUpdate.keySet()) {
        if (b.getState() != Bundle.UNINSTALLED) {
            toStop.add(b);
            lessToUpdate.put(b, toUpdate.get(b));
        }
    }
    while (!toStop.isEmpty()) {
        List<Bundle> bs = getBundlesToDestroy(toStop);
        for (Bundle bundle : bs) {
            String hostHeader = bundle.getHeaders().get(Constants.FRAGMENT_HOST);
            if (hostHeader == null && (bundle.getState() == Bundle.ACTIVE || bundle.getState() == Bundle.STARTING)) {
                if (!"org.ops4j.pax.url.mvn".equals(bundle.getSymbolicName())) {
                    bundle.stop();
                }
            }
            toStop.remove(bundle);
        }
    }
    // eagerly load some classes
    try {
        getClass().getClassLoader().loadClass(Parser.class.getName());
        getClass().getClassLoader().loadClass(Clause.class.getName());
        getClass().getClassLoader().loadClass(Attribute.class.getName());
        getClass().getClassLoader().loadClass(Directive.class.getName());
        getClass().getClassLoader().loadClass(RefreshListener.class.getName());
    } catch (Exception ignored) {
    }
    Set<Bundle> toRefresh = new HashSet<Bundle>();
    Set<Bundle> toStart = new HashSet<Bundle>();
    for (Map.Entry<Bundle, String> e : lessToUpdate.entrySet()) {
        Bundle bundle = e.getKey();
        if (!"org.ops4j.pax.url.mvn".equals(bundle.getSymbolicName())) {
            System.out.println("updating: " + bundle.getSymbolicName());
            try {
                BundleUtils.update(bundle, new URL(e.getValue()));
            } catch (BundleException ex) {
                System.err.println("Failed to update: " + bundle.getSymbolicName() + ", due to: " + e);
            }
            toStart.add(bundle);
            toRefresh.add(bundle);
        }
    }
    findBundlesWithOptionalPackagesToRefresh(toRefresh);
    findBundlesWithFragmentsToRefresh(toRefresh);
    if (!toRefresh.isEmpty()) {
        final CountDownLatch l = new CountDownLatch(1);
        FrameworkListener listener = new RefreshListener(l);
        FrameworkWiring wiring = bundleContext.getBundle(0).adapt(FrameworkWiring.class);
        wiring.refreshBundles(toRefresh, listener);
        try {
            l.await();
        } catch (InterruptedException e) {
            throw new PatchException("Bundle refresh interrupted", e);
        }
    }
    for (Bundle bundle : toStart) {
        String hostHeader = bundle.getHeaders().get(Constants.FRAGMENT_HOST);
        if (hostHeader == null) {
            try {
                bundle.start();
            } catch (BundleException e) {
                System.err.println("Failed to start: " + bundle.getSymbolicName() + ", due to: " + e);
            }
        }
    }
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Attribute(org.apache.felix.utils.manifest.Attribute) Bundle(org.osgi.framework.Bundle) ArrayList(java.util.ArrayList) FrameworkWiring(org.osgi.framework.wiring.FrameworkWiring) CountDownLatch(java.util.concurrent.CountDownLatch) URISyntaxException(java.net.URISyntaxException) PatchException(io.fabric8.patch.management.PatchException) BundleException(org.osgi.framework.BundleException) IOException(java.io.IOException) URL(java.net.URL) Parser(org.apache.felix.utils.manifest.Parser) Clause(org.apache.felix.utils.manifest.Clause) BundleException(org.osgi.framework.BundleException) PatchException(io.fabric8.patch.management.PatchException) FrameworkListener(org.osgi.framework.FrameworkListener) Directive(org.apache.felix.utils.manifest.Directive) Map(java.util.Map) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Example 29 with PatchException

use of io.fabric8.api.PatchException in project fabric8 by jboss-fuse.

the class ServiceImplTest method testCheckPrerequisitesMultiplePatches.

@Test
public void testCheckPrerequisitesMultiplePatches() throws IOException {
    ServiceImpl service = createMockServiceImpl(getDirectoryForResource("prereq/patch1.patch"));
    Collection<Patch> patches = new LinkedList<Patch>();
    patches.add(service.getPatch("patch3"));
    // this should not throw a PatchException
    service.checkPrerequisites(patches);
    patches.add(service.getPatch("patch2"));
    try {
        service.checkPrerequisites(patches);
        fail("Should not pass check if one of the patches is missing a requirement");
    } catch (PatchException e) {
    // graciously do nothing, this is OK
    }
}
Also used : GitPatchManagementServiceImpl(io.fabric8.patch.management.impl.GitPatchManagementServiceImpl) PatchException(io.fabric8.patch.management.PatchException) Patch(io.fabric8.patch.management.Patch) LinkedList(java.util.LinkedList) Test(org.junit.Test)

Example 30 with PatchException

use of io.fabric8.api.PatchException in project fabric8 by jboss-fuse.

the class ServiceImplTest method testCheckPrerequisitesSatisfied.

@Test
public void testCheckPrerequisitesSatisfied() throws IOException {
    ServiceImpl service = createMockServiceImpl(getDirectoryForResource("prereq/patch3.patch"));
    Patch patch = service.getPatch("patch3");
    assertNotNull(patch);
    // this should not throw a PatchException
    service.checkPrerequisites(patch);
}
Also used : GitPatchManagementServiceImpl(io.fabric8.patch.management.impl.GitPatchManagementServiceImpl) Patch(io.fabric8.patch.management.Patch) Test(org.junit.Test)

Aggregations

PatchException (io.fabric8.patch.management.PatchException)30 IOException (java.io.IOException)19 File (java.io.File)15 Patch (io.fabric8.patch.management.Patch)14 GitAPIException (org.eclipse.jgit.api.errors.GitAPIException)11 ZipFile (org.apache.commons.compress.archivers.zip.ZipFile)10 Git (org.eclipse.jgit.api.Git)10 LinkedList (java.util.LinkedList)9 Test (org.junit.Test)9 ArrayList (java.util.ArrayList)7 GitPatchManagementServiceImpl (io.fabric8.patch.management.impl.GitPatchManagementServiceImpl)6 URISyntaxException (java.net.URISyntaxException)6 HashMap (java.util.HashMap)6 RevCommit (org.eclipse.jgit.revwalk.RevCommit)6 PatchResult (io.fabric8.patch.management.PatchResult)5 FileInputStream (java.io.FileInputStream)5 FileNotFoundException (java.io.FileNotFoundException)5 BundleException (org.osgi.framework.BundleException)5 ManagedPatch (io.fabric8.patch.management.ManagedPatch)4 URL (java.net.URL)4