com.nec.core.container.ContextAwareContainer.java Source code

Java tutorial

Introduction

Here is the source code for com.nec.core.container.ContextAwareContainer.java

Source

/**
 * Copyright(C) 2014
 * NEC Corporation All rights reserved.
 * 
 * No permission to use, copy, modify and distribute this software
 * and its documentation for any purpose is granted.
 * This software is provided under applicable license agreement only.
 */
package com.nec.core.container;

import java.util.Collection;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.nec.core.context.RequestContext;
import com.nec.core.context.RootContext;
import com.nec.core.context.RootSecurityContext;
import com.nec.core.exception.ConfigurationNotFoundException;
import com.nec.core.exception.ObjectNotFoundException;
import com.nec.core.exception.TooManyObjectsException;

/**
 * This class will be used for getting objects from the object model and other
 * application information.
 * 
 * @author sondn
 * 
 */
public final class ContextAwareContainer {

    private static final String APPLICATION_CONFIGURATION = "applicationContext.xml";
    private static final String WEB_CONTEXT_LOADER_CLASS = "org.springframework.web.context.ContextLoader";
    private static final String SECURITY_CONTEXT_CLASS = "org.springframework.security.core.Authentication";

    private static ContextAwareContainer instance = new ContextAwareContainer();

    private ApplicationContext context;

    private boolean isSecurityAvailable;

    private ContextAwareContainer() {
        if (this.isWebApplication()) {
            this.context = this.getWebApplicationContext();
        } else {
            this.context = this.getApplicationContext();
        }
        this.isSecurityAvailable = this.isSecurityAvailable();
    }

    private boolean isWebApplication() {
        try {
            Class.forName(ContextAwareContainer.WEB_CONTEXT_LOADER_CLASS);
            return RootContext.isWebApplication();
        } catch (ClassNotFoundException ex) {
            return false;
        }
    }

    private boolean isSecurityAvailable() {
        try {
            Class.forName(ContextAwareContainer.SECURITY_CONTEXT_CLASS);
            return true;
        } catch (ClassNotFoundException ex) {
            return false;
        }
    }

    private ApplicationContext getWebApplicationContext() {
        return RootContext.getWebApplicationContext();
    }

    private ApplicationContext getApplicationContext() {
        ClassLoader classLoader = ContextAwareContainer.class.getClassLoader();
        if (classLoader.getResource(ContextAwareContainer.APPLICATION_CONFIGURATION) == null) {
            throw new ConfigurationNotFoundException(
                    "File " + ContextAwareContainer.APPLICATION_CONFIGURATION + " not found");
        }

        return new ClassPathXmlApplicationContext(new String[] { ContextAwareContainer.APPLICATION_CONFIGURATION });
    }

    /**
     * Retrieve an item from the Context
     * 
     * @param clazz
     *            A class want to get instance
     * @return A instance of class
     */
    @SuppressWarnings("unchecked")
    public <T> T getComponent(final Class<T> clazz) {
        Map<String, Object> map = (Map<String, Object>) this.context.getBeansOfType(clazz);
        if (map.size() == 0) {
            throw new ObjectNotFoundException("Object not found for type " + clazz.getCanonicalName());
        }
        if (map.size() > 1) {
            throw new TooManyObjectsException("To many objects found for type " + clazz.getCanonicalName());
        }

        return (T) map.values().iterator().next();
    }

    /**
     * Retrieve a list of items from the Context
     * 
     * @param clazz
     *            A class want to get instance
     * @return A set of instances of class
     */
    @SuppressWarnings("unchecked")
    public <T> Collection<T> getComponents(final Class<T> clazz) {
        Map<String, Object> map = (Map<String, Object>) this.context.getBeansOfType(clazz);
        return (Collection<T>) map.values();
    }

    /**
     * Get a instance of context aware container
     * 
     * @return A instance
     */
    public static ContextAwareContainer getInstance() {
        return ContextAwareContainer.instance;
    }

    /**
     * Retrieve principal from the Context
     * 
     * @return A Principal
     */
    public Object getPrincipal() {
        if (this.isSecurityAvailable) {
            return RootSecurityContext.getPrincipal();
        }
        return null;
    }

    /**
     * Exposes the native javax.servlet.http.HttpServletRequest that we're
     * wrapping
     * 
     * @return javax.servlet.http.HttpServletRequest
     */
    public HttpServletRequest getRequest() {
        return RequestContext.getRequest();
    }

}