uk.co.unclealex.process.spring.PackageCheckingPostProcessor.java Source code

Java tutorial

Introduction

Here is the source code for uk.co.unclealex.process.spring.PackageCheckingPostProcessor.java

Source

/**
 * Copyright 2012 Alex Jones
 *
 * 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 uk.co.unclealex.process.spring;

import java.util.SortedSet;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.beans.factory.support.BeanDefinitionValidationException;

import uk.co.unclealex.process.packages.PackageChecker;
import uk.co.unclealex.process.packages.PackagesRequired;
import uk.co.unclealex.process.packages.RequiresPackages;

import com.google.common.base.Joiner;

/**
 * A Spring {@link BeanPostProcessor} that fails if a bean requires uninstalled
 * packages.
 * 
 * @author alex
 * @see RequiresPackages
 * @see PackagesRequired
 */
public class PackageCheckingPostProcessor implements BeanPostProcessor {

    /** The package checker. */
    private final PackageChecker packageChecker;

    /**
     * Instantiates a new package checking post processor.
     * 
     * @param packageChecker
     *          the package checker
     */
    public PackageCheckingPostProcessor(PackageChecker packageChecker) {
        super();
        this.packageChecker = packageChecker;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        SortedSet<String> missingPackages = getPackageChecker().listMissingPackages(bean);
        if (!missingPackages.isEmpty()) {
            throw new BeanDefinitionValidationException(
                    String.format("Bean %s requires the following missing packages: %s", beanName,
                            Joiner.on(", ").join(missingPackages)));
        }
        return bean;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        return bean;
    }

    /**
     * Gets the package checker.
     * 
     * @return the package checker
     */
    public PackageChecker getPackageChecker() {
        return packageChecker;
    }
}