> Or, you can post it to the wiki page.
> http://wiki.apache.org/jakarta-velocity/
> http://wiki.apache.org/jakarta-velocity/VelocityTools
>
> ## I like the wiki page because it's easier to modify the code afterwards. :)
>
I tried to post it but I get "You are not allowed to edit this page.". So, I attach the patch. If anyone want to give it a try apply it on org.apache.velocity.tools.
In order to work the engine runtime property "standalone.toolbox.path" must return the path to the file with toolbox.xml format. The toolbox.xml format is slightly modified to accept singleton lifecycle for tools.
Usage: // Velocity Singleton model VelocityContext ctx = new StandaloneContext() ;
// Velocity separate instance VelocityEngine engine = new VelocityEngine(); engine.addProperty(StandaloneContext.PATH_KEY, "path"); engine.init(); StandaloneContext ctx = new StandaloneContext() ; ctx.setToolbox(StandaloneToolboxManager.getToolbox(engine));
I also made a simple test case but I guess the subscribers won't be very happy if I send many attachments.
Thanks Edgar
Shinobu Kawai wrote:
Hi Edgar,
I made a StandaloneContext which contains a set of tools defined in the toolbox.xml format and I extended a couple of ViewTool classes for adding a property called singleton. The usage is: VelocityContext ctx = new StandaloneContext().
Uploading a patch to bugzilla is the proper way to propose contributions?
Or, you can post it to the wiki page. http://wiki.apache.org/jakarta-velocity/ http://wiki.apache.org/jakarta-velocity/VelocityTools
## I like the wiki page because it's easier to modify the code afterwards. :)
Best regards, -- Shinobu Kawai
-- Shinobu Kawai <[EMAIL PROTECTED]>
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Index: standalone/StandaloneToolInfo.java =================================================================== --- standalone/StandaloneToolInfo.java (revision 0) +++ standalone/StandaloneToolInfo.java (revision 0) @@ -0,0 +1,26 @@ +/* + * Created on 09/12/2004 + */ +package org.apache.velocity.tools.standalone; + +import org.apache.velocity.tools.view.ViewToolInfo; + +/** + * TODO Documentation is important + * + * @author <a href="mailto:[EMAIL PROTECTED]">Edgar Poce</a> + */ +public class StandaloneToolInfo extends ViewToolInfo +{ + private boolean singleton = true ; + + public boolean isSingleton() + { + return singleton; + } + + public void setSingleton(boolean singleton) + { + this.singleton = singleton; + } +} Index: standalone/Toolbox.java =================================================================== --- standalone/Toolbox.java (revision 0) +++ standalone/Toolbox.java (revision 0) @@ -0,0 +1,127 @@ +/* + * Copyright 2003-2004 The Apache Software Foundation. + * + * 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 org.apache.velocity.tools.standalone; + +import java.util.Collection; +import java.util.HashMap; +import java.util.Iterator; +import java.util.Map; + +import org.apache.velocity.app.Velocity; +import org.apache.velocity.tools.view.ToolInfo; + +/** + * <p> + * Tool container. + * <p> + * + * @author <a href="mailto:[EMAIL PROTECTED]">Edgar Poce </a> + */ +public class Toolbox +{ + /** Map of tools */ + private Map tools = new HashMap(); + /** singleton */ + private boolean singleton = true; + + /** + * @return the Collection of tools + */ + public Collection getTools() + { + return tools.values(); + } + + /** + * Add a tool to the Toolbox + * @param tool + */ + public void addTool(Tool tool) + { + + // Check duplicated keys + if (tools.get(tool.getKey()) != null) + Velocity.error("The key " + + tool.getKey() + + " is already in use in the toolbox. Check the config file."); + + // Singleton validation + if (!tool.isSingleton()) + this.singleton = false; + + //Add tool + this.tools.put(tool.getKey(), tool); + } + + /** + * <p> + * Add a tool to the Toolbox + * </p> + * + * @param info + */ + public void addTool(ToolInfo info) + { + Tool tool = new Tool(); + tool.setKey(info.getKey()); + tool.setObject(info.getInstance(null)); + if (info instanceof StandaloneToolInfo) + tool.setSingleton(((StandaloneToolInfo) info).isSingleton()); + tool.setToolInfo(info); + this.addTool(tool); + + Velocity.info("Added " + info.getKey() + " (" + info.getClassname() + + ")."); + } + + public Tool getTool(String key) + { + return (Tool) this.tools.get(key); + } + + /** + * A Toolbox is a singleton + * only when every Tool it contains + * is a singleton too. + * + * @return singleton field + */ + public boolean isSingleton() + { + return this.singleton; + } + + /** + * Clone the Toolbox. + * It doesn't clone the Tools with the + * singleton field set to true + */ + protected Object clone() + { + Toolbox box = new Toolbox(); + Iterator iter = this.tools.values().iterator(); + while (iter.hasNext()) + { + Tool t = (Tool) iter.next(); + if (t.isSingleton()) + box.addTool(t); + else + box.addTool((Tool) t.clone()); + } + return box; + } +} \ No newline at end of file Index: standalone/StandaloneToolboxManager.java =================================================================== --- standalone/StandaloneToolboxManager.java (revision 0) +++ standalone/StandaloneToolboxManager.java (revision 0) @@ -0,0 +1,205 @@ +/* + * Copyright 2003-2004 The Apache Software Foundation. + * + * 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 org.apache.velocity.tools.standalone; + +import java.io.File; +import java.io.FileInputStream; +import java.io.InputStream; +import java.io.PrintWriter; +import java.io.StringWriter; +import java.util.HashMap; +import java.util.Map; + +import org.apache.commons.digester.Digester; +import org.apache.velocity.app.Velocity; +import org.apache.velocity.app.VelocityEngine; + +/** + * <p> + * Toolbox Manager. It loads the configuration preferences and creates the + * Toolbox prototype. + * </p> + * <p> + * Configuration example + * + * <pre> + * + * + * + * <?xml version="1.0"?> + * + * <toolbox> + * <tool> + * <key>date</key> + * <singleton>true</singleton> + * <class>org.apache.velocity.tools.generic.DateTool</class> + * </tool> + * <tool> + * <key>math</key> + * <singleton>true</singleton> + * <class>org.apache.velocity.tools.generic.MathTool</class> + * </tool> + * <data type="number"> + * <key>luckynumber</key> + * <value>1.37</value> + * </data> + * <data type="string"> + * <key>greeting</key> + * <value>Hello World!</value> + * </data> + * <xhtml>true</xhtml> + * </toolbox> + * + * + * + * </pre> + * + * </p> + * + * + * @author <a href="mailto:[EMAIL PROTECTED]">Edgar Poce </a> + */ +public class StandaloneToolboxManager +{ + /** Singleton */ + private static StandaloneToolboxManager builder = new StandaloneToolboxManager(); + + /** + * <p> + * Toolbox cache <br> + * key = path to toolbox configuration file <br> + * value = toolbox instance <br> + * </p> + */ + private Map toolboxCache = new HashMap(); + + /** + * returns the singleton builder + * + * @return + */ + private static StandaloneToolboxManager getInstance() + { + return builder; + } + + /** + * <p> + * Returns the toolbox. If every Tool is a singleton then return the same + * toolbox instance. If not return a clone. + * </p> + * + * @return the toolbox + */ + public static Toolbox getToolbox() + { + return getInstance() + .getToolbox((String) Velocity.getProperty(StandaloneContext.PATH_KEY)); + } + + /** + * + * @param path + * @return a toolbox + */ + private Toolbox getToolbox(String path) + { + Toolbox toolbox = null; + + // Check cache + if (this.toolboxCache.containsKey(path)) + { + toolbox = (Toolbox) this.toolboxCache.get(path); + return (Toolbox) toolbox.clone(); + } + + // Create a new toolbox instance for the given path + toolbox = new Toolbox(); + + // Load the toolbox with the configuration file + this.loadToolbox(path, toolbox); + + // Cache toolbox + this.toolboxCache.put(path, toolbox); + + return (Toolbox) toolbox.clone(); + } + + /** + * <p> + * Returns the Toolbox for the given engine. + * </p> + * <p> + * If every Tool is a singleton then return the same toolbox instance. If + * not return a clone. + * </p> + * + * @return the toolbox + */ + public static Toolbox getToolbox(VelocityEngine engine) + { + return getInstance().getToolbox((String) engine.getProperty(StandaloneContext.PATH_KEY)); + } + + /** + * <p> + * Parse toolbox configuration file + * </p> + */ + private void loadToolbox(String path, Toolbox toolbox) + { + if (path == null) + { + Velocity + .warn(Velocity.WARN_PREFIX + + "Unkown path to toolbox configuration file. Please set the " + + StandaloneContext.PATH_KEY + " property."); + return; + } + + // Locate file + File file = new File(path); + if (!file.exists()) + { + Velocity.error(Velocity.ERROR_PREFIX + + "The toolbox configuration file is not properly set. " + + " No such file: " + path + "."); + return ; + } + + // Configure Digester + Digester digester = new Digester(); + digester.setValidating(false); + digester.setUseContextClassLoader(true); + digester.push(toolbox); + digester.addRuleSet(new StandaloneRuleSet()); + try + { // Parse xml + InputStream input = new FileInputStream(file); + digester.parse(input); + } catch (Exception e) + { + Velocity.error(Velocity.ERROR_PREFIX + + "Unable to parse tools configuration file " + path); + StringWriter w = new StringWriter(); + PrintWriter out = new PrintWriter(w); + e.printStackTrace(out); + Velocity.error(Velocity.ERROR_PREFIX + w.toString()); + } + } + +} \ No newline at end of file Index: standalone/StandaloneRuleSet.java =================================================================== --- standalone/StandaloneRuleSet.java (revision 0) +++ standalone/StandaloneRuleSet.java (revision 0) @@ -0,0 +1,47 @@ +/* + * Copyright 2003-2004 The Apache Software Foundation. + * + * 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 org.apache.velocity.tools.standalone; + +import org.apache.commons.digester.Digester; +import org.apache.velocity.tools.view.ToolboxRuleSet; + +/** + * <p> + * RuleSet + * </p> + * + * @author <a href="mailto:[EMAIL PROTECTED]">Edgar Poce</a> + */ +public class StandaloneRuleSet extends ToolboxRuleSet +{ + + /** + * Overrides [EMAIL PROTECTED] ToolboxRuleSet} to add Standalone settings. + */ + protected void addToolRules(Digester digester) + { + super.addToolRules(digester); + digester.addBeanPropertySetter("toolbox/tool/singleton", "singleton"); + } + + /** + * Overrides [EMAIL PROTECTED] ToolboxRuleSet} to use StandaloneToolInfo class. + */ + protected Class getToolInfoClass() + { + return StandaloneToolInfo.class; + } +} Index: standalone/StandaloneContext.java =================================================================== --- standalone/StandaloneContext.java (revision 0) +++ standalone/StandaloneContext.java (revision 0) @@ -0,0 +1,156 @@ +/* + * Copyright 2003-2004 The Apache Software Foundation. + * + * 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 org.apache.velocity.tools.standalone; + +import java.util.Map; + +import org.apache.velocity.VelocityContext; +import org.apache.velocity.app.Velocity; +import org.apache.velocity.context.Context; + +/** + * <p> + * Context with a Toolbox. <br> + * Superclass mappings overrides the Toolbox. + * </p> + * <p> + * Requisites: <br> + * 1 - Configure a file with toolbox.xml format <br> + * 2 - Set the property standalone.toolbox.path in the VelocityEngine.<br> + * </p> + * <p> + * <b>Usage with Velocity Singleton model: </b><br> + * VelocityContext ctx = new StandaloneContext() ;<br> + * </p> + * <p> + * <b>Usage with Velocity separate instance model</b><br> + * VelocityEngine engine = new VelocityEngine();<br> + * engine.addProperty(StandaloneContext.PATH_KEY, [path to file]); <br> + * engine.init(); <br> + * StandaloneContext ctx = new StandaloneContext() ;<br> + * ctx.setToolbox(StandaloneToolboxManager.getToolbox(engine));<br> + * </p> + * + * @author <a href="mailto:[EMAIL PROTECTED]">Edgar Poce </a> + */ +public class StandaloneContext extends VelocityContext +{ + + /** velocity property key for path to toolbox config file */ + public static String PATH_KEY = "standalone.toolbox.path"; + + /** Toolbox */ + private Toolbox toolbox; + + /** + * Constructor + */ + public StandaloneContext() + { + super(); + this.initToolbox(); + } + + /** + * Constructor + */ + public StandaloneContext(Map arg0) + { + super(arg0); + this.initToolbox(); + } + + /** + * Constructor + */ + public StandaloneContext(Map arg0, Context arg1) + { + super(arg0, arg1); + this.initToolbox(); + } + + /** + * Constructor + */ + public StandaloneContext(Context arg0) + { + super(arg0); + this.initToolbox(); + } + + /** + * Get a Toolbox instance from the Manager + */ + private void initToolbox() + { + this.toolbox = StandaloneToolboxManager.getToolbox(); + } + + /** + * Constructor + */ + public Object internalGet(String key) + { + + // search the local HashTable + Object o = super.internalGet(key); + + // search the toolbox + if (o == null && toolbox.getTool(key) != null) + { + o = toolbox.getTool(key).getObject(); + } + + return o; + } + + /** + * returns the Toolbox + */ + public Toolbox getToolbox() + { + return toolbox; + } + + /** + * check key overridde for logging purposes + */ + public Object internalPut(String key, Object o) + { + if (this.toolbox != null && this.toolbox.getTool(key) != null) + { + Velocity.warn(Velocity.WARN_PREFIX + + "The Tool with id " + key + + " is being overridden"); + } + + return super.internalPut(key, o); + } + + /** + * <p> + * Sets a custom toolbox. <br> + * Use this method to override the Velocity singleton toolbox + * </p> + * + * @param toolbox + */ + public void setToolbox(Toolbox toolbox) + { + this.toolbox = toolbox; + } +} \ No newline at end of file Index: standalone/Tool.java =================================================================== --- standalone/Tool.java (revision 0) +++ standalone/Tool.java (revision 0) @@ -0,0 +1,105 @@ +/* + * Copyright 2003-2004 The Apache Software Foundation. + * + * 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 org.apache.velocity.tools.standalone; + +import org.apache.velocity.app.Velocity; +import org.apache.velocity.tools.view.ToolInfo; + +/** + * <p> + * Tool wrapper. It contains the ToolInfo for constructing a new instance if + * necessary. + * </p> + * + * @author <a href="mailto:[EMAIL PROTECTED]">Edgar Poce </a> + */ +public class Tool implements Cloneable +{ + /** Tool key */ + private String key; + + /** Tool */ + private Object object; + + /** Is singleton */ + private boolean singleton = true; + + /** Info for this tool */ + private ToolInfo toolInfo; + + public String getKey() + { + return key; + } + + public void setKey(String key) + { + this.key = key; + } + + public Object getObject() + { + return object; + } + + public void setObject(Object object) + { + this.object = object; + } + + public boolean isSingleton() + { + return singleton; + } + + public void setSingleton(boolean singleton) + { + this.singleton = singleton; + } + + /** + * <p> + * Clone the Tool. + * The ToolInfo creates the new instance. + * </p> + */ + protected Object clone() + { + Tool tool = new Tool(); + tool.key = this.key; + tool.singleton = this.singleton; + try + { + tool.object = toolInfo.getInstance(null); + } catch (Exception e) + { + Velocity.error("Unable to instantiate tool (id=" + this.key + ") " + + e.getMessage()); + } + tool.toolInfo = this.toolInfo; + return tool; + } + + public ToolInfo getToolInfo() + { + return toolInfo; + } + + public void setToolInfo(ToolInfo toolInfo) + { + this.toolInfo = toolInfo; + } +} \ No newline at end of file
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
