Sunday Nov 18

Maven Variable Resolver source

PDF Print E-mail
Wednesday, 29 June 2011 08:57
AddThis Social Bookmark Button

Writing our Maven Variable Resolver

ELResolver implementation source

/**
 * 
 */
package com.ubiteck.jsf.util;

import java.beans.FeatureDescriptor;
import java.io.IOException;
import java.io.InputStream;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;
import java.util.logging.Logger;

import javax.el.ELContext;
import javax.el.ELResolver;
import javax.el.PropertyNotFoundException;
import javax.el.PropertyNotWritableException;
import javax.faces.context.FacesContext;
import javax.servlet.ServletContext;

import com.sun.faces.util.MessageUtils;
import com.sun.faces.util.Util;


/**
 * Maven properties pom resolver
 * 
 * @author sursini
 */
public class MavenVariableResolver extends ELResolver {
	private static final Logger logger = Logger.getLogger(MavenVariableResolver.class.getName());

	private final String 	POM_FILE 	= "/META-INF/maven/com.ubiteck.bacula/BaculaWeb/pom.properties";
	private final String 	MAVEN 		= "maven";
	private final String 	POM 		= "pom";
	private final boolean 	RESOLVABLE_DESIGN_TIME = Boolean.FALSE;
	
	private Map<String,String> pomMap = null; 

	@Override
	public Object getValue(ELContext context, Object base, Object property) {
		logger.info("get Value property : "+ property + " base " + base);
		 if (MAVEN.equalsIgnoreCase(property.toString())){
			 logger.info("\tReturn maven object : " + new Maven());
			 context.setPropertyResolved(true);
			 return new Maven();
		 }
		 if ("pom".equalsIgnoreCase(property.toString())){
			 logger.info("\treturn pom result : " + getMavemPomProperties());
			 context.setPropertyResolved(true);
			 return getMavemPomProperties();
		}
		return null;
	}
	
	@Override
	public Class<?> getType(ELContext context, Object base, Object property) {
		logger.info("Get type for"+ "property" + property+"  base " + base);
		if (base != null) {
			if (base instanceof Maven && property == null){
				context.setPropertyResolved(true);
				logger.info("\return Maven class type...");
				return Maven.class;
			}
		}
        if (property == null) {
            String message = MessageUtils.getExceptionMessageString
                (MessageUtils.NULL_PARAMETERS_ERROR_MESSAGE_ID, "property");
            throw new PropertyNotFoundException(message);
        }
        if (MAVEN.equalsIgnoreCase(property.toString())) {
            context.setPropertyResolved(true);
            logger.info("\treturn type :  Maven.class"  );
            return Maven.class;
        }
        if (POM.equalsIgnoreCase(property.toString())) {
            context.setPropertyResolved(true);
            logger.info("\treturn type :  Map.class"  );
            return Map.class;
        }
        return null;
	}

	@Override
	public void setValue(ELContext context, Object base, Object property,
			Object value) {
		logger.info("Setting value...");
	    if (base != null) return;
        if (property == null) {
            String message = MessageUtils.getExceptionMessageString
                (MessageUtils.NULL_PARAMETERS_ERROR_MESSAGE_ID, "property");
            throw new PropertyNotFoundException(message);
        }
        if (MAVEN.equals(property) || POM.equals(property)) {
            throw new PropertyNotWritableException((String) property);
        }
	}

	@Override
	public boolean isReadOnly(ELContext context, Object base, Object property) {
		logger.info("isReadOnly..."  );
        if (MAVEN.equals(property) || POM.equals(property)) {
        	logger.info("\t read only : true"  );
            return true;
            
        }
		return true;
	}

	@Override
	public Iterator<FeatureDescriptor> getFeatureDescriptors(ELContext context,
			Object base) {
		logger.info("Registering features... (base : " + base + ")");
        if (base != null) return null;
        ArrayList<FeatureDescriptor> list = new ArrayList<FeatureDescriptor>(14);
        list.add(Util.getFeatureDescriptor(
        			"maven", 
        			"maven",
        			"maven",
        			false,//EXPERT, 
        			false,//HIDDEN,
        			true,//PREFERRED, 
        			Object.class, 
        			RESOLVABLE_DESIGN_TIME)
        );
        list.add(Util.getFeatureDescriptor(
    			"pom", 
    			"pom",
    			"pom",
    			false,//EXPERT, 
    			false,//HIDDEN, 
    			true,//PREFERRED, 
    			Map.class, 
    			RESOLVABLE_DESIGN_TIME)
        );
        return list.iterator();
	}

	@Override
	public Class<?> getCommonPropertyType(ELContext context, Object base) {
		logger.info("getCommonProperty base : " + base  );
		if (base != null) return null;
        return Object.class;
	}

	private Map<String,String> getMavemPomProperties(){
		if (pomMap == null){
			logger.info("Read maven POM properties...");
			pomMap = new HashMap<String,String>();
			try {
				ServletContext servletContext = (ServletContext) FacesContext.getCurrentInstance().getExternalContext().getContext();
				InputStream pom_properties = servletContext.getResourceAsStream(POM_FILE);
				Properties properties = new Properties();
				properties.load(pom_properties);
				for (String key : properties.stringPropertyNames()) {
					pomMap.put(key, properties.getProperty(key));
				}
			}
			catch(IOException ex) {
				logger.severe("Error while reading pom properties : " + ex.getMessage());
			}
		}
		return pomMap;
	}
	
	public class Maven implements Serializable {
		private static final long serialVersionUID = 1L;
		
	}

}

Tags: maven , class , public , object , variable , null; , return , resolver , void , context , @override , base , property) , base)

Add comment


Security code
Refresh

Java Tutorial on Facebook