All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.openejb.config.rules.CheckDescriptorLocation Maven / Gradle / Ivy

There is a newer version: 4.7.5
Show newest version
/**
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.openejb.config.rules;

import org.apache.openejb.config.AppModule;
import org.apache.openejb.config.DeploymentModule;
import org.apache.openejb.config.EjbModule;
import org.apache.openejb.config.WebModule;
import org.apache.openejb.util.LogCategory;
import org.apache.openejb.util.Logger;
import org.apache.xbean.finder.ResourceFinder;

import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.*;

import static org.apache.openejb.util.CollectionsUtil.safe;


public class CheckDescriptorLocation extends ValidationBase {

    @Override
    public void validate(AppModule appModule){

        List validated = new ArrayList();

        for(WebModule webModule: safe(appModule.getWebModules()))
        {
            validated.add(webModule.getModuleId());
            validateWebModule(webModule);
        }

        for(EjbModule ejbModule: safe(appModule.getEjbModules()))
        {
            //without this check, CheckDescriptorLocationTest#testWarWithDescriptorInRoot() would fail
            if(!validated.contains(ejbModule.getModuleId()))
            {
                validateEjbModule(ejbModule);
            }
        }

    }

    private void validateWebModule(DeploymentModule webModule) {
        URL baseUrl = null;
        this.module= webModule;
        List descriptorsToSearch = Arrays.asList("beans.xml","ejb-jar.xml","faces-config.xml");
        File file = webModule.getFile();
        if (file != null) {
            try {
                URL rootOfArchive=file.toURI().toURL();
                URL metaInf=new URL(rootOfArchive.toExternalForm()+"META-INF/");
                Map incorrectlyLocatedDescriptors
                        = retrieveDescriptors(file, descriptorsToSearch, rootOfArchive, metaInf);
                warnIncorrectLocationOfDescriptors(incorrectlyLocatedDescriptors,"WEB-INF");
            } catch (MalformedURLException ignored) {
                    //ignored
            }
        }

    }

    public void validateEjbModule(DeploymentModule deploymentModule) {
        URL baseUrl = null;
        this.module= deploymentModule;
        List descriptorsToSearch = Arrays.asList("beans.xml","ejb-jar.xml","openejb-jar.xml","env-entries.properties");
        File file = deploymentModule.getFile();
        if (file != null) {
            try {
                URL rootOfArchive=file.toURI().toURL();
                Map incorrectlyLocatedDescriptors
                        = retrieveDescriptors(file, descriptorsToSearch, rootOfArchive);
                warnIncorrectLocationOfDescriptors(incorrectlyLocatedDescriptors,"META-INF");
            } catch (MalformedURLException ignored) {
                  //ignored
            }
        }

    }

    private static Map retrieveDescriptors(File file, List descriptorsToSearch, URL... locationsToSearch ){
      final Map  descriptorAndWrongLocation = new HashMap();


            ResourceFinder finder = new ResourceFinder(locationsToSearch);
            for(String descriptor:descriptorsToSearch)
            {
                URL resource = finder.getResource(descriptor);
                if(resource!=null)
                {
                   descriptorAndWrongLocation.put(descriptor,resource);
                }
            }

        return descriptorAndWrongLocation;
    }

    private void warnIncorrectLocationOfDescriptors(Map descriptorsPlacedInWrongLocation, String expectedLocation) {
        for (Map.Entry map : descriptorsPlacedInWrongLocation.entrySet()) {

            warn(this.module.toString(), "descriptor.incorrectLocation", map.getValue().toExternalForm(), expectedLocation);
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy