| Method from org.apache.struts2.views.velocity.VelocityManager Detail: |
public Context createContext(ValueStack stack,
HttpServletRequest req,
HttpServletResponse res) {
VelocityContext[] chainedContexts = prepareChainedContexts(req, res, stack.getContext());
StrutsVelocityContext context = new StrutsVelocityContext(chainedContexts, stack);
Map standardMap = ContextUtil.getStandardContext(stack, req, res);
for (Iterator iterator = standardMap.entrySet().iterator(); iterator.hasNext();) {
Map.Entry entry = (Map.Entry) iterator.next();
context.put((String) entry.getKey(), entry.getValue());
}
context.put(STRUTS, new VelocityStrutsUtil(velocityEngine, context, stack, req, res));
ServletContext ctx = null;
try {
ctx = ServletActionContext.getServletContext();
} catch (NullPointerException npe) {
// in case this was used outside the lifecycle of struts servlet
log.debug("internal toolbox context ignored");
}
if (toolboxManager != null && ctx != null) {
ChainedContext chained = new ChainedContext(context, req, res, ctx);
chained.setToolbox(toolboxManager.getToolboxContext(chained));
return chained;
} else {
return context;
}
}
This method is responsible for creating the standard VelocityContext used by all WW2 velocity views. The
following context parameters are defined:
|
public VelocityEngine getVelocityEngine() {
return velocityEngine;
}
|
public Properties getVelocityProperties() {
return velocityProperties;
}
|
public synchronized void init(ServletContext context) {
if (velocityEngine == null) {
velocityEngine = newVelocityEngine(context);
}
this.initToolbox(context);
}
initializes the VelocityManager. this should be called during the initialization process, say by
ServletDispatcher. this may be called multiple times safely although calls beyond the first won't do anything |
protected void initToolbox(ServletContext context) {
/* if we have a toolbox, get a manager for it */
if (toolBoxLocation != null) {
toolboxManager = ServletToolboxManager.getInstance(context, toolBoxLocation);
} else {
Velocity.info("VelocityViewServlet: No toolbox entry in configuration.");
}
}
Initializes the ServletToolboxManager for this servlet's
toolbox (if any). |
public Properties loadConfiguration(ServletContext context) {
if (context == null) {
String gripe = "Error attempting to create a loadConfiguration from a null ServletContext!";
log.error(gripe);
throw new IllegalArgumentException(gripe);
}
Properties properties = new Properties();
// now apply our systemic defaults, then allow user to override
applyDefaultConfiguration(context, properties);
String defaultUserDirective = properties.getProperty("userdirective");
/**
* if the user has specified an external velocity configuration file, we'll want to search for it in the
* following order
*
* 1. relative to the context path
* 2. relative to /WEB-INF
* 3. in the class path
*/
String configfile;
if (customConfigFile != null) {
configfile = customConfigFile;
} else {
configfile = "velocity.properties";
}
configfile = configfile.trim();
InputStream in = null;
String resourceLocation = null;
try {
if (context.getRealPath(configfile) != null) {
// 1. relative to context path, i.e. /velocity.properties
String filename = context.getRealPath(configfile);
if (filename != null) {
File file = new File(filename);
if (file.isFile()) {
resourceLocation = file.getCanonicalPath() + " from file system";
in = new FileInputStream(file);
}
// 2. if nothing was found relative to the context path, search relative to the WEB-INF directory
if (in == null) {
file = new File(context.getRealPath("/WEB-INF/" + configfile));
if (file.isFile()) {
resourceLocation = file.getCanonicalPath() + " from file system";
in = new FileInputStream(file);
}
}
}
}
// 3. finally, if there's no physical file, how about something in our classpath
if (in == null) {
in = VelocityManager.class.getClassLoader().getResourceAsStream(configfile);
if (in != null) {
resourceLocation = configfile + " from classloader";
}
}
// if we've got something, load 'er up
if (in != null) {
log.info("Initializing velocity using " + resourceLocation);
properties.load(in);
}
} catch (IOException e) {
log.warn("Unable to load velocity configuration " + resourceLocation, e);
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
}
}
}
// overide with programmatically set properties
if (this.velocityProperties != null) {
Iterator keys = this.velocityProperties.keySet().iterator();
while (keys.hasNext()) {
String key = (String) keys.next();
properties.setProperty(key, this.velocityProperties.getProperty(key));
}
}
String userdirective = properties.getProperty("userdirective");
if ((userdirective == null) || userdirective.trim().equals("")) {
userdirective = defaultUserDirective;
} else {
userdirective = userdirective.trim() + "," + defaultUserDirective;
}
properties.setProperty("userdirective", userdirective);
// for debugging purposes, allows users to dump out the properties that have been configured
if (log.isDebugEnabled()) {
log.debug("Initializing Velocity with the following properties ...");
for (Iterator iter = properties.keySet().iterator();
iter.hasNext();) {
String key = (String) iter.next();
String value = properties.getProperty(key);
if (log.isDebugEnabled()) {
log.debug(" '" + key + "' = '" + value + "'");
}
}
}
return properties;
}
load optional velocity properties using the following loading strategy
- relative to the servlet context path
- relative to the WEB-INF directory
- on the classpath
|
protected VelocityEngine newVelocityEngine(ServletContext context) {
if (context == null) {
String gripe = "Error attempting to create a new VelocityEngine from a null ServletContext!";
log.error(gripe);
throw new IllegalArgumentException(gripe);
}
Properties p = loadConfiguration(context);
VelocityEngine velocityEngine = new VelocityEngine();
// Set the velocity attribute for the servlet context
// if this is not set the webapp loader WILL NOT WORK
velocityEngine.setApplicationAttribute(ServletContext.class.getName(),
context);
try {
velocityEngine.init(p);
} catch (Exception e) {
String gripe = "Unable to instantiate VelocityEngine!";
throw new StrutsException(gripe, e);
}
return velocityEngine;
}
Instantiates a new VelocityEngine.
The following is the default Velocity configuration
resource.loader = file, class
file.resource.loader.path = real path of webapp
class.resource.loader.description = Velocity Classpath Resource Loader
class.resource.loader.class = org.apache.struts2.views.velocity.StrutsResourceLoader
this default configuration can be overridden by specifying a struts.velocity.configfile property in the
struts.properties file. the specified config file will be searched for in the following order:
- relative to the servlet context path
- relative to the WEB-INF directory
- on the classpath
|
protected VelocityContext[] prepareChainedContexts(HttpServletRequest servletRequest,
HttpServletResponse servletResponse,
Map extraContext) {
if (this.chainedContextNames == null) {
return null;
}
List contextList = new ArrayList();
for (int i = 0; i < chainedContextNames.length; i++) {
String className = chainedContextNames[i];
try {
VelocityContext velocityContext = (VelocityContext) objectFactory.buildBean(className, null);
contextList.add(velocityContext);
} catch (Exception e) {
log.warn("Warning. " + e.getClass().getName() + " caught while attempting to instantiate a chained VelocityContext, " + className + " -- skipping");
}
}
if (contextList.size() > 0) {
VelocityContext[] extraContexts = new VelocityContext[contextList.size()];
contextList.toArray(extraContexts);
return extraContexts;
} else {
return null;
}
}
constructs contexts for chaining on this request. This method does not
perform any initialization of the contexts. All that must be done in the
context itself. |
public void setChainedContexts(String contexts) {
// we expect contexts to be a comma separated list of classnames
StringTokenizer st = new StringTokenizer(contexts, ",");
List contextList = new ArrayList();
while (st.hasMoreTokens()) {
String classname = st.nextToken();
contextList.add(classname);
}
if (contextList.size() > 0) {
String[] chainedContexts = new String[contextList.size()];
contextList.toArray(chainedContexts);
this.chainedContextNames = chainedContexts;
}
}
allow users to specify via the struts.properties file a set of additional VelocityContexts to chain to the
the StrutsVelocityContext. The intent is to allow these contexts to store helper objects that the ui
developer may want access to. Examples of reasonable VelocityContexts would be an IoCVelocityContext, a
SpringReferenceVelocityContext, and a ToolboxVelocityContext |
public void setContainer(Container container) {
List< TagLibrary > list = new ArrayList< TagLibrary >();
Set< String > prefixes = container.getInstanceNames(TagLibrary.class);
for (String prefix : prefixes) {
list.add(container.getInstance(TagLibrary.class, prefix));
}
this.tagLibraries = Collections.unmodifiableList(list);
}
|
public void setCustomConfigFile(String val) {
this.customConfigFile = val;
}
|
public void setObjectFactory(ObjectFactory fac) {
this.objectFactory = fac;
}
|
public void setToolBoxLocation(String toolboxLocation) {
this.toolBoxLocation = toolboxLocation;
}
|
public void setVelocityProperties(Properties velocityProperties) {
this.velocityProperties = velocityProperties;
}
|