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

com.ibm.commons.runtime.impl.AbstractApplication Maven / Gradle / Ivy

/*
 * © Copyright IBM Corp. 2012
 * 
 * Licensed 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 com.ibm.commons.runtime.impl;

import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import com.ibm.commons.extension.ExtensionManager;
import com.ibm.commons.runtime.Application;
import com.ibm.commons.util.QuickSort;



/**
 * SBT Application abstract implementation for J2EE servers.
 * 
 * This class implements the managed beans and properties factories. 
 *  
 * @author Philippe Riand
 */
public abstract class AbstractApplication extends Application {

    private static final String MANAGEDBEAN_FACTORY = "com.ibm.commons.runtime.managedbeansfactory";
    private static final String PROPERTIES_FACTORY = "com.ibm.commons.runtime.propertiesfactory";
	
	private int references;
	private Object applicationContext;
	private Properties properties;
	private HashMap scope = new HashMap(); 

	private ManagedBeanFactory[] managedBeanFactories;
	private PropertiesFactory[] propertiesFactories;
	
	private Map> services;
	
	protected AbstractApplication(Object applicationContext) {
		this.applicationContext = applicationContext;
		this.properties = new Properties();
	}

	public int _references() {
		return references;
	}

	public void _incReferences() {
		references++;
	}

	public void _decReferences() {
		references--;
	}

	@Override
	public ClassLoader getClassLoader() {
		return Thread.currentThread().getContextClassLoader();
	}
	
	@Override
	public Object getApplicationContext() {
		return applicationContext;
	}

	@Override
	public Map getScope() {
		return scope;
	}

	@Override
	public String getProperty(String name) {
		String s = properties.getProperty(name);
		if(s!=null) {
			return s;
		}
		PropertiesFactory[] f = getPropertiesFactories();
		for(int i=0; i l = AccessController.doPrivileged(new PrivilegedAction>() {
		                @Override
						public List run() {
		                    List l = (List)ExtensionManager.findApplicationServices(getClassLoader(),MANAGEDBEAN_FACTORY); 
		                    return l;
		                }
		            });
		            managedBeanFactories = l.toArray(new ManagedBeanFactory[l.size()]);
				}
			}
		}
		return managedBeanFactories;
	}
	
	public PropertiesFactory[] getPropertiesFactories() {
		if(propertiesFactories==null) {
			synchronized(this) {
				if(propertiesFactories==null) {
		            List l = AccessController.doPrivileged(new PrivilegedAction>() {
		                @Override
						public List run() {
		                    List l = (List)ExtensionManager.findApplicationServices(getClassLoader(),PROPERTIES_FACTORY); 
		                    return l;
		                }
		            });
		            propertiesFactories = (PropertiesFactory[])l.toArray(new PropertiesFactory[l.size()]);
		            // Make sure they are accessed in order
		            (new QuickSort.ObjectArray(propertiesFactories) {
						@Override
						public int compare(Object o1, Object o2) {
							PropertiesFactory p1 = (PropertiesFactory)o1;
							PropertiesFactory p2 = (PropertiesFactory)o2;
							return p1.getPriority()-p2.getPriority();
						}
		            	
		            }).sort();
				}
			}
		}
		return propertiesFactories;
	}
		
	@Override
	public List findServices(String serviceName) {
		if(services==null) {
			services = new HashMap>();
		}
		return ExtensionManager.findApplicationServices(services, getClassLoader(), serviceName);
	}	
}