Exposing Active Profiles as a JNDI value in Tomcat
here's a quick one for exposing Active Profiles in JNDI for Tomcat. the use case is when you don't want to set an Active Profile in web.xml (or webappcontextinitializer) but you need them for Tomcat. (say you're developing against a Tomcat container, but deploying against something else)
so, here's the class
package de.incompleteco.spring.tomcat; import java.util.Enumeration; import java.util.Hashtable; import javax.naming.Context; import javax.naming.Name; import javax.naming.RefAddr; import javax.naming.Reference; import javax.naming.spi.ObjectFactory; public class ActiveProfilesObjectFactory implements ObjectFactory { static final String VALUE = "value"; static final String DELIMITER = ","; @Override public Object getObjectInstance(Object obj, Name name, Context nameCtx, Hashtable < ?, ? > environment) throws Exception { //init String values = null; //retrieve Reference reference = (Reference) obj; Enumeration < RefAddr > addresses = reference.getAll(); while (addresses.hasMoreElements()) { RefAddr address = addresses.nextElement(); String attributeName = address.getType(); String attributeValue = address.getContent().toString(); if (attributeName.equalsIgnoreCase(VALUE)) { values = attributeValue; break; }//end if }//end while if (values != null) { return values.split(DELIMITER); }//end if return null;//default --> no active profiles } }
and here's how you would access it in Tomcat. (remember, the location is [your app]/META-INF/context.xml)
No comments:
Post a Comment