Search in sources :

Example 1 with AdditionalDeployments

use of org.apache.openejb.config.sys.AdditionalDeployments in project tomee by apache.

the class DeployerEjb method saveDeployment.

private synchronized void saveDeployment(final File file, final boolean add) {
    final Deployments deps = new Deployments();
    if (file.isDirectory()) {
        deps.setDir(file.getAbsolutePath());
    } else {
        deps.setFile(file.getAbsolutePath());
    }
    File config;
    try {
        config = SystemInstance.get().getBase().getFile(ADDITIONAL_DEPLOYMENTS, false);
    } catch (final IOException e) {
        config = null;
    }
    if (config == null || !config.getParentFile().exists()) {
        LOGGER.info("Cannot save the added app because the conf folder does not exist, it will not be present on a restart");
        return;
    }
    // dump it
    OutputStream os = null;
    try {
        final AdditionalDeployments additionalDeployments;
        if (config.exists() && config.length() > 0) {
            final InputStream fis = IO.read(config);
            try {
                additionalDeployments = JaxbOpenejb.unmarshal(AdditionalDeployments.class, fis);
            } finally {
                IO.close(fis);
            }
        } else {
            additionalDeployments = new AdditionalDeployments();
        }
        if (add) {
            if (!additionalDeployments.getDeployments().contains(deps)) {
                additionalDeployments.getDeployments().add(deps);
            }
        } else {
            final Iterator<Deployments> it = additionalDeployments.getDeployments().iterator();
            while (it.hasNext()) {
                final Deployments current = it.next();
                if (deps.getDir() != null && deps.getDir().equals(current.getDir())) {
                    it.remove();
                    break;
                } else if (deps.getFile() != null && deps.getFile().equals(current.getFile())) {
                    it.remove();
                    break;
                } else {
                    // exploded dirs
                    final String jar = deps.getFile();
                    if (jar != null && jar.length() > 3) {
                        final String substring = jar.substring(0, jar.length() - 4);
                        if (substring.equals(current.getDir()) || substring.equals(current.getFile())) {
                            it.remove();
                            break;
                        }
                    } else {
                        final String jarC = current.getFile();
                        if (jarC != null && jarC.length() > 3) {
                            final String substring = jarC.substring(0, jarC.length() - 4);
                            if (substring.equals(deps.getDir()) || substring.equals(deps.getFile())) {
                                it.remove();
                                break;
                            }
                        }
                    }
                }
            }
        }
        os = IO.write(config);
        JaxbOpenejb.marshal(AdditionalDeployments.class, additionalDeployments, os);
    } catch (final Exception e) {
        LOGGER.error("cannot save the added app, will not be present next time you'll start", e);
    } finally {
        IO.close(os);
    }
}
Also used : InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) Deployments(org.apache.openejb.config.sys.Deployments) AdditionalDeployments(org.apache.openejb.config.sys.AdditionalDeployments) IOException(java.io.IOException) AdditionalDeployments(org.apache.openejb.config.sys.AdditionalDeployments) File(java.io.File) UndeployException(org.apache.openejb.UndeployException) OpenEJBException(org.apache.openejb.OpenEJBException) IOException(java.io.IOException) ValidationException(javax.validation.ValidationException) NoSuchApplicationException(org.apache.openejb.NoSuchApplicationException) OpenEJBRuntimeException(org.apache.openejb.OpenEJBRuntimeException)

Example 2 with AdditionalDeployments

use of org.apache.openejb.config.sys.AdditionalDeployments in project tomee by apache.

the class DeployerEjbTest method removeDeploymentsLogic.

@Test
public void removeDeploymentsLogic() throws Exception {
    final File dir1 = Files.mkdirs(new File("target/DeployerEjbTest/removeDeploymentsLogic/app1/"));
    final File file = SystemInstance.get().getBase().getFile(ADDITIONAL_DEPLOYMENTS, false);
    final Method save = DeployerEjb.class.getDeclaredMethod("saveDeployment", File.class, boolean.class);
    save.setAccessible(true);
    {
        final AdditionalDeployments deployments = new AdditionalDeployments();
        final Deployments d1 = new Deployments();
        d1.setDir(dir1.getCanonicalPath());
        deployments.getDeployments().add(d1);
        final Deployments d12 = new Deployments();
        d12.setDir(dir1.getCanonicalPath());
        deployments.getDeployments().add(d12);
        final Deployments d2 = new Deployments();
        d2.setFile(new File(File.listRoots()[0], "/foo/bar/app.war").getAbsolutePath());
        deployments.getDeployments().add(d2);
        try (final FileOutputStream fos = new FileOutputStream(file)) {
            JaxbOpenejb.marshal(AdditionalDeployments.class, deployments, fos);
        }
        assertDeployementsSize(file, 3);
    }
    {
        save.invoke(new DeployerEjb(), dir1, false);
        assertDeployementsSize(file, 2);
    }
    {
        save.invoke(new DeployerEjb(), new File(dir1.getParentFile(), dir1.getName() + ".war"), false);
        assertDeployementsSize(file, 1);
    }
    {
        save.invoke(new DeployerEjb(), new File(File.listRoots()[0], "/foo/bar/app.war"), false);
        assertDeployementsSize(file, 0);
    }
}
Also used : FileOutputStream(java.io.FileOutputStream) Deployments(org.apache.openejb.config.sys.Deployments) AdditionalDeployments(org.apache.openejb.config.sys.AdditionalDeployments) Method(java.lang.reflect.Method) AdditionalDeployments(org.apache.openejb.config.sys.AdditionalDeployments) File(java.io.File) Test(org.junit.Test)

Example 3 with AdditionalDeployments

use of org.apache.openejb.config.sys.AdditionalDeployments in project tomee by apache.

the class ConfigurationFactory method getDeclaredApps.

private List<File> getDeclaredApps() {
    // make a copy of the list because we update it
    final List<Deployments> deployments = new ArrayList<>();
    if (openejb != null) {
        deployments.addAll(openejb.getDeployments());
    }
    Collection<Deployments> additionalDeploymentsList = Collections.emptyList();
    try {
        final File additionalDeploymentFile = SystemInstance.get().getBase().getFile(ADDITIONAL_DEPLOYMENTS, false);
        if (additionalDeploymentFile.exists()) {
            InputStream fis = null;
            try {
                fis = IO.read(additionalDeploymentFile);
                final AdditionalDeployments additionalDeployments = JaxbOpenejb.unmarshal(AdditionalDeployments.class, fis);
                additionalDeploymentsList = additionalDeployments.getDeployments();
            } catch (final Exception e) {
                logger.error("can't read " + ADDITIONAL_DEPLOYMENTS, e);
            } finally {
                IO.close(fis);
            }
        }
    } catch (final Exception e) {
        logger.info("No additional deployments found: " + e);
    }
    // resolve jar locations //////////////////////////////////////  BEGIN  ///////
    final FileUtils base = SystemInstance.get().getBase();
    final List<Deployments> autoDeploy = new ArrayList<>();
    final List<File> declaredAppsUrls = new ArrayList<>();
    for (final Deployments deployment : deployments) {
        try {
            DeploymentsResolver.loadFrom(deployment, base, declaredAppsUrls);
            if (deployment.isAutoDeploy()) {
                autoDeploy.add(deployment);
            }
        } catch (final SecurityException se) {
            logger.warning("Security check failed on deployment: " + deployment.getFile(), se);
        }
    }
    for (final Deployments additionalDep : additionalDeploymentsList) {
        if (additionalDep.getFile() != null) {
            declaredAppsUrls.add(Files.path(base.getDirectory().getAbsoluteFile(), additionalDep.getFile()));
        } else if (additionalDep.getDir() != null) {
            declaredAppsUrls.add(Files.path(base.getDirectory().getAbsoluteFile(), additionalDep.getDir()));
        }
        if (additionalDep.isAutoDeploy()) {
            autoDeploy.add(additionalDep);
        }
    }
    if (autoDeploy.size() > 0) {
        final AutoDeployer autoDeployer = new AutoDeployer(this, autoDeploy);
        SystemInstance.get().setComponent(AutoDeployer.class, autoDeployer);
        SystemInstance.get().addObserver(autoDeployer);
    }
    return declaredAppsUrls;
}
Also used : FileUtils(org.apache.openejb.loader.FileUtils) InputStream(java.io.InputStream) AdditionalDeployments(org.apache.openejb.config.sys.AdditionalDeployments) Deployments(org.apache.openejb.config.sys.Deployments) ArrayList(java.util.ArrayList) AdditionalDeployments(org.apache.openejb.config.sys.AdditionalDeployments) File(java.io.File) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) OpenEJBException(org.apache.openejb.OpenEJBException) MalformedURLException(java.net.MalformedURLException)

Aggregations

File (java.io.File)3 AdditionalDeployments (org.apache.openejb.config.sys.AdditionalDeployments)3 Deployments (org.apache.openejb.config.sys.Deployments)3 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 OpenEJBException (org.apache.openejb.OpenEJBException)2 FileOutputStream (java.io.FileOutputStream)1 OutputStream (java.io.OutputStream)1 Method (java.lang.reflect.Method)1 MalformedURLException (java.net.MalformedURLException)1 URISyntaxException (java.net.URISyntaxException)1 ArrayList (java.util.ArrayList)1 ValidationException (javax.validation.ValidationException)1 NoSuchApplicationException (org.apache.openejb.NoSuchApplicationException)1 OpenEJBRuntimeException (org.apache.openejb.OpenEJBRuntimeException)1 UndeployException (org.apache.openejb.UndeployException)1 FileUtils (org.apache.openejb.loader.FileUtils)1 Test (org.junit.Test)1