Search in sources :

Example 1 with Deployments

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

the class ConfigurationFactory method toConfigDeclaration.

public static Object toConfigDeclaration(final String id, final URI uri) throws OpenEJBException {
    final String serviceType;
    try {
        serviceType = uri.getHost();
        final Object object;
        try {
            object = JaxbOpenejb.create(serviceType);
        } catch (final Exception e) {
            throw new OpenEJBException("Invalid URI '" + uri + "'. " + e.getMessage());
        }
        final Map<String, String> map;
        try {
            map = URISupport.parseParamters(uri);
        } catch (final URISyntaxException e) {
            throw new OpenEJBException("Unable to parse URI parameters '" + uri + "'. URISyntaxException: " + e.getMessage());
        }
        if (object instanceof AbstractService) {
            final AbstractService service = (AbstractService) object;
            service.setId(id);
            service.setType(map.remove("type"));
            service.setProvider(map.remove("provider"));
            service.setClassName(map.remove("class-name"));
            service.setConstructor(map.remove("constructor"));
            service.setFactoryName(map.remove("factory-name"));
            service.setPropertiesProvider(map.remove("properties-provider"));
            service.setTemplate(map.remove("template"));
            final String cp = map.remove("classpath");
            if (null != cp) {
                service.setClasspath(cp);
            }
            service.setClasspathAPI(map.remove("classpath-api"));
            if (object instanceof Resource) {
                final Resource resource = Resource.class.cast(object);
                final String aliases = map.remove("aliases");
                if (aliases != null) {
                    resource.getAliases().addAll(Arrays.asList(aliases.split(",")));
                }
                final String depOn = map.remove("depends-on");
                if (depOn != null) {
                    resource.getDependsOn().addAll(Arrays.asList(depOn.split(",")));
                }
                resource.setPostConstruct(map.remove("post-construct"));
                resource.setPreDestroy(map.remove("pre-destroy"));
            }
            service.getProperties().putAll(map);
        } else if (object instanceof Deployments) {
            final Deployments deployments = (Deployments) object;
            deployments.setDir(map.remove("dir"));
            deployments.setFile(map.remove("jar"));
            final String cp = map.remove("classpath");
            if (cp != null) {
                final String[] paths = cp.split(File.pathSeparator);
                final List<URL> urls = new ArrayList<>();
                for (final String path : paths) {
                    final Set<String> values = ProvisioningUtil.realLocation(PropertyPlaceHolderHelper.value(path));
                    for (final String v : values) {
                        urls.add(new File(v).toURI().normalize().toURL());
                    }
                }
                deployments.setClasspath(new URLClassLoaderFirst(urls.toArray(new URL[urls.size()]), ParentClassLoaderFinder.Helper.get()));
            }
        } else if (SystemProperty.class.isInstance(object)) {
            final SystemProperty sp = SystemProperty.class.cast(object);
            sp.setName(map.remove("name"));
            sp.setValue(map.remove("value"));
        }
        return object;
    } catch (final Exception e) {
        throw new OpenEJBException("Error declaring service '" + id + "'. Unable to create Service definition from URI '" + uri.toString() + "'", e);
    }
}
Also used : OpenEJBException(org.apache.openejb.OpenEJBException) URLClassLoaderFirst(org.apache.openejb.util.classloader.URLClassLoaderFirst) Set(java.util.Set) HashSet(java.util.HashSet) Resource(org.apache.openejb.config.sys.Resource) AdditionalDeployments(org.apache.openejb.config.sys.AdditionalDeployments) Deployments(org.apache.openejb.config.sys.Deployments) URISyntaxException(java.net.URISyntaxException) AbstractService(org.apache.openejb.config.sys.AbstractService) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) OpenEJBException(org.apache.openejb.OpenEJBException) MalformedURLException(java.net.MalformedURLException) URL(java.net.URL) ArrayList(java.util.ArrayList) List(java.util.List) LinkedList(java.util.LinkedList) File(java.io.File)

Example 2 with Deployments

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

the class ConfigUtils method addDeploymentEntryToConfig.

public static boolean addDeploymentEntryToConfig(final String jarLocation, final Openejb config) {
    final File jar = new File(jarLocation);
    /* Check to see if the entry is already listed */
    for (final Deployments d : config.getDeployments()) {
        if (d.getFile() != null) {
            try {
                final File target = SystemInstance.get().getBase().getFile(d.getFile(), false);
                /* 
                     * If the jar entry is already there, no need 
                     * to add it to the config or go any futher.
                     */
                if (jar.equals(target)) {
                    return false;
                }
            } catch (final IOException e) {
            /* No handling needed.  If there is a problem
                     * resolving a config file path, it is better to 
                     * just add this jars path explicitly.
                     */
            }
        } else if (d.getDir() != null) {
            try {
                final File target = SystemInstance.get().getBase().getFile(d.getDir(), false);
                final File jarDir = jar.getAbsoluteFile().getParentFile();
                /* 
                     * If a dir entry is already there, the jar
                     * will be loaded automatically.  No need 
                     * to add it explicitly to the config or go
                     * any futher.
                     */
                if (jarDir != null && jarDir.equals(target)) {
                    return false;
                }
            } catch (final IOException e) {
            /* No handling needed.  If there is a problem
                     * resolving a config file path, it is better to 
                     * just add this jars path explicitly.
                     */
            }
        }
    }
    /* Create a new Deployments entry */
    final Deployments dep = JaxbOpenejb.createDeployments();
    dep.setFile(jarLocation);
    config.getDeployments().add(dep);
    return true;
}
Also used : Deployments(org.apache.openejb.config.sys.Deployments) IOException(java.io.IOException) File(java.io.File)

Example 3 with Deployments

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

the class ConfigurationFactoryTest method testToConfigDeclaration.

@Test
public void testToConfigDeclaration() throws Exception {
    final String path = ".";
    final ConfigurationFactory factory = new ConfigurationFactory();
    final Deployments deployments = (Deployments) factory.toConfigDeclaration("", new URI("new://Deployments?classpath=" + path));
    final URLClassLoader cl = (URLClassLoader) deployments.getClasspath();
    final URL[] urls = cl.getURLs();
    assertEquals(urls[0], new File(path).toURI().normalize().toURL());
}
Also used : URLClassLoader(java.net.URLClassLoader) Deployments(org.apache.openejb.config.sys.Deployments) URI(java.net.URI) File(java.io.File) URL(java.net.URL) Test(org.junit.Test)

Example 4 with Deployments

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

the class AbstractTomEEMojo method ensureAppsFolderExistAndIsConfiguredByDefault.

private void ensureAppsFolderExistAndIsConfiguredByDefault(final File file) throws IOException {
    if ("openejb".equals(container.toLowerCase(Locale.ENGLISH)) || (file.exists() && ((apps != null && !apps.isEmpty()) || (!"pom".equals(packaging) && !"war".equals(packaging))))) {
        // webapps doesn't need apps folder in tomee
        final String rootTag = container.toLowerCase(Locale.ENGLISH);
        if (file.isFile()) {
            // can be not existing since we dont always deploy tomee but shouldn't since then apps/ is not guaranteed to work
            try {
                final Openejb jaxb = JaxbOpenejb.readConfig(file.getAbsolutePath());
                boolean needAdd = true;
                for (final Deployments d : jaxb.getDeployments()) {
                    if ("apps".equals(d.getDir())) {
                        needAdd = false;
                        break;
                    }
                }
                if (needAdd) {
                    final String content = IO.slurp(file);
                    final FileWriter writer = new FileWriter(file);
                    final String end = "</" + rootTag + ">";
                    writer.write(content.replace(end, "  <Deployments dir=\"apps\" />\n" + end));
                    writer.close();
                }
            } catch (final OpenEJBException e) {
                throw new IllegalStateException("illegal tomee.xml:\n" + IO.slurp(file), e);
            }
        } else {
            final FileWriter writer = new FileWriter(file);
            writer.write("<?xml version=\"1.0\"?>\n" + "<" + rootTag + ">\n" + "  <Deployments dir=\"apps\" />\n" + "</" + rootTag + ">\n");
            writer.close();
        }
        final File appsFolder = new File(catalinaBase, "apps");
        if (!appsFolder.exists() && !appsFolder.mkdirs()) {
            throw new RuntimeException("Failed to create: " + appsFolder);
        }
    }
}
Also used : OpenEJBException(org.apache.openejb.OpenEJBException) OpenEJBRuntimeException(org.apache.openejb.OpenEJBRuntimeException) FileWriter(java.io.FileWriter) Deployments(org.apache.openejb.config.sys.Deployments) ZipFile(java.util.zip.ZipFile) File(java.io.File) Openejb(org.apache.openejb.config.sys.Openejb) JaxbOpenejb(org.apache.openejb.config.sys.JaxbOpenejb)

Example 5 with Deployments

use of org.apache.openejb.config.sys.Deployments 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<Deployments>();
    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<Deployments>();
    final List<File> declaredAppsUrls = new ArrayList<File>();
    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) ArrayList(java.util.ArrayList) AdditionalDeployments(org.apache.openejb.config.sys.AdditionalDeployments) Deployments(org.apache.openejb.config.sys.Deployments) 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)8 Deployments (org.apache.openejb.config.sys.Deployments)8 OpenEJBException (org.apache.openejb.OpenEJBException)5 AdditionalDeployments (org.apache.openejb.config.sys.AdditionalDeployments)5 IOException (java.io.IOException)4 ArrayList (java.util.ArrayList)3 InputStream (java.io.InputStream)2 MalformedURLException (java.net.MalformedURLException)2 URISyntaxException (java.net.URISyntaxException)2 URL (java.net.URL)2 OpenEJBRuntimeException (org.apache.openejb.OpenEJBRuntimeException)2 AbstractService (org.apache.openejb.config.sys.AbstractService)2 FileOutputStream (java.io.FileOutputStream)1 FileWriter (java.io.FileWriter)1 OutputStream (java.io.OutputStream)1 Method (java.lang.reflect.Method)1 URI (java.net.URI)1 URLClassLoader (java.net.URLClassLoader)1 HashSet (java.util.HashSet)1 LinkedList (java.util.LinkedList)1