From: <jbo...@li...> - 2005-11-03 21:37:38
|
Author: adamw Date: 2005-11-03 16:37:25 -0500 (Thu, 03 Nov 2005) New Revision: 1498 Added: trunk/forge/portal-extensions/shotoku/shotoku-base/src/etc/velocity.properties trunk/forge/portal-extensions/shotoku/shotoku-base/src/java/org/jboss/shotoku/velocity/ trunk/forge/portal-extensions/shotoku/shotoku-base/src/java/org/jboss/shotoku/velocity/ShotokuResourceLoader.java Modified: trunk/forge/portal-extensions/shotoku/shotoku-base/project.xml trunk/forge/portal-extensions/shotoku/shotoku-base/src/java/org/jboss/shotoku/ContentManager.java trunk/forge/portal-extensions/shotoku/shotoku-base/src/java/org/jboss/shotoku/NodeList.java trunk/forge/portal-extensions/shotoku/shotoku-svn-service/maven.xml trunk/forge/portal-extensions/shotoku/shotoku-svn-service/project.xml trunk/forge/portal-extensions/shotoku/shotoku-svn/src/java/org/jboss/shotoku/svn/SvnContentManager.java trunk/forge/portal-extensions/shotoku/shotoku-test/project.xml trunk/forge/portal-extensions/shotoku/shotoku-test/src/java/org/jboss/shotoku/test/servlet/ShotokuServlet.java Log: http://jira.jboss.com/jira/browse/JBSHOTOKU-15 : a velocit resource loader reading from shotoku, velocity engine access functions Modified: trunk/forge/portal-extensions/shotoku/shotoku-base/project.xml =================================================================== --- trunk/forge/portal-extensions/shotoku/shotoku-base/project.xml 2005-11-03 20:53:19 UTC (rev 1497) +++ trunk/forge/portal-extensions/shotoku/shotoku-base/project.xml 2005-11-03 21:37:25 UTC (rev 1498) @@ -17,6 +17,21 @@ <description></description> <dependencies> + <dependency> + <groupId>velocity</groupId> + <artifactId>velocity</artifactId> + <version>1.4</version> + </dependency> + <dependency> + <groupId>velocity</groupId> + <artifactId>velocity-dep</artifactId> + <version>1.4</version> + </dependency> + <dependency> + <groupId>velocity</groupId> + <artifactId>velocity</artifactId> + <version>1.4-dev</version> + </dependency> </dependencies> <build> Added: trunk/forge/portal-extensions/shotoku/shotoku-base/src/etc/velocity.properties =================================================================== --- trunk/forge/portal-extensions/shotoku/shotoku-base/src/etc/velocity.properties 2005-11-03 20:53:19 UTC (rev 1497) +++ trunk/forge/portal-extensions/shotoku/shotoku-base/src/etc/velocity.properties 2005-11-03 21:37:25 UTC (rev 1498) @@ -0,0 +1,5 @@ +resource.loader=shotoku +shotoku.resource.loader.description=Shotoku Resource Loader +shotoku.resource.loader.class=org.jboss.shotoku.velocity.ShotokuResourceLoader +shotoku.resource.loader.cache=true +shotoku.resource.loader.modificationCheckInterval=2 Modified: trunk/forge/portal-extensions/shotoku/shotoku-base/src/java/org/jboss/shotoku/ContentManager.java =================================================================== --- trunk/forge/portal-extensions/shotoku/shotoku-base/src/java/org/jboss/shotoku/ContentManager.java 2005-11-03 20:53:19 UTC (rev 1497) +++ trunk/forge/portal-extensions/shotoku/shotoku-base/src/java/org/jboss/shotoku/ContentManager.java 2005-11-03 21:37:25 UTC (rev 1498) @@ -24,8 +24,10 @@ import java.lang.reflect.Constructor; import java.util.HashMap; import java.util.Map; +import java.util.Properties; import org.apache.log4j.Logger; +import org.apache.velocity.app.VelocityEngine; import org.apache.xerces.parsers.DOMParser; import org.jboss.shotoku.exceptions.ResourceDoesNotExist; import org.jboss.shotoku.exceptions.RepositoryException; @@ -39,6 +41,15 @@ */ public abstract class ContentManager { /** + * <code>prefix</code> - prefix of this content manager. + */ + protected String prefix; + /** + * <code>id</code> - id of a corresponding repository. + */ + protected String id; + + /** * Gets a root directory that is represented by this content manager. * * @return A directory that is the root directory of this content manager. @@ -80,10 +91,73 @@ */ public abstract NodeList search(SearchParameters parameters) throws RepositoryException; + + /** + * Gets a velocity engine, initialized with default properties, as defined + * in velocity.properties in shotoku-base jar. Additionaly, the shotoku + * resource loader will be set to read templates from this content manager + * (if shotoku resource loader is defined to be used). + * + * @return A velocity engine initialized with default properties. + * @throws Exception + * In case of a velocity exception/ properties reading + * exception. + */ + public VelocityEngine getVelocityEngine() throws Exception { + return getVelocityEngine(true); + } - protected ContentManager(String id, String prefix) { + /** + * Gets a velocity engine, initialized with default properties, as defined + * in velocity.properties in shotoku-base jar. + * + * @param addIdPrefix + * true iff id/ prefix properties should be added to the + * properties. + * @return A velocity engine initialized with default properties. + * @throws Exception + * In case of a velocity exception/ properties reading + * exception. + */ + public VelocityEngine getVelocityEngine(boolean addIdPrefix) + throws Exception { + return getVelocityEngine(new HashMap<Object, Object>(), addIdPrefix); + } + /** + * Gets a velocity engine, initialized with default properties, possibly + * overwritten by the given propeties. + * + * @param overProps + * Properties which will be added to velocity configuration. + * @return An initialized velocity engine. + * @throws Exception + * In case of a velocity exception/ properties reading + * exception. + */ + public VelocityEngine getVelocityEngine( + Map<? extends Object, ? extends Object> overProps, + boolean addIdPrefix) throws Exception { + VelocityEngine ve = new VelocityEngine(); + Properties props = new Properties(); + props.load(ContentManager.class + .getResourceAsStream("/velocity.properties")); + props.putAll(overProps); + + if (addIdPrefix) { + props.put("shotoku.resource.loader." + "prefix", prefix); + props.put("shotoku.resource.loader." + "id", id); + } + + ve.init(props); + + return ve; } + + protected ContentManager(String id, String prefix) { + this.id = id; + this.prefix = prefix; + } /* * Content manager loading. @@ -183,6 +257,9 @@ * content manager with the given id registers. */ public static ContentManager getContentManager(String id, String prefix) { + if (id == null) id = defaultId; + if (prefix == null) prefix = ""; + Constructor constructor = contentManagers.get(id); if (constructor == null) Modified: trunk/forge/portal-extensions/shotoku/shotoku-base/src/java/org/jboss/shotoku/NodeList.java =================================================================== --- trunk/forge/portal-extensions/shotoku/shotoku-base/src/java/org/jboss/shotoku/NodeList.java 2005-11-03 20:53:19 UTC (rev 1497) +++ trunk/forge/portal-extensions/shotoku/shotoku-base/src/java/org/jboss/shotoku/NodeList.java 2005-11-03 21:37:25 UTC (rev 1498) @@ -59,8 +59,4 @@ public List<Node> toList() { return nodeList; } - - /*public InputStream getFeed(String velocityTemplate) { - throw new RuntimeException("Operation not yet implemented"); - }*/ } Added: trunk/forge/portal-extensions/shotoku/shotoku-base/src/java/org/jboss/shotoku/velocity/ShotokuResourceLoader.java =================================================================== --- trunk/forge/portal-extensions/shotoku/shotoku-base/src/java/org/jboss/shotoku/velocity/ShotokuResourceLoader.java 2005-11-03 20:53:19 UTC (rev 1497) +++ trunk/forge/portal-extensions/shotoku/shotoku-base/src/java/org/jboss/shotoku/velocity/ShotokuResourceLoader.java 2005-11-03 21:37:25 UTC (rev 1498) @@ -0,0 +1,48 @@ +package org.jboss.shotoku.velocity; + +import java.io.InputStream; + +import org.apache.commons.collections.ExtendedProperties; +import org.apache.velocity.exception.ResourceNotFoundException; +import org.apache.velocity.runtime.resource.Resource; +import org.apache.velocity.runtime.resource.loader.ResourceLoader; +import org.jboss.shotoku.ContentManager; + +public class ShotokuResourceLoader extends ResourceLoader { + private ContentManager cm; + + private boolean isStringEmpty(String s) { + return (s == null) || ("".equals(s)); + } + + @Override + public void init(ExtendedProperties properties) { + String prefix = properties.getString("prefix"); + String id = properties.getString("id"); + + if ((isStringEmpty(prefix)) && (isStringEmpty(id))) { + cm = ContentManager.getContentManager(); + } else if (isStringEmpty(id)) { + cm = ContentManager.getContentManager(prefix); + } else { + cm = ContentManager.getContentManager(id, prefix); + } + } + + @Override + public InputStream getResourceStream(String resource) throws ResourceNotFoundException { + return cm.getNode(resource).getContentInputStream(); + } + + @Override + public boolean isSourceModified(Resource resource) { + return resource.getLastModified() != cm.getNode(resource.getName()).getLastModfication(); + } + + @Override + public long getLastModified(Resource resource) { + return cm.getNode(resource.getName()).getLastModfication(); + } + +} + Modified: trunk/forge/portal-extensions/shotoku/shotoku-svn/src/java/org/jboss/shotoku/svn/SvnContentManager.java =================================================================== --- trunk/forge/portal-extensions/shotoku/shotoku-svn/src/java/org/jboss/shotoku/svn/SvnContentManager.java 2005-11-03 20:53:19 UTC (rev 1497) +++ trunk/forge/portal-extensions/shotoku/shotoku-svn/src/java/org/jboss/shotoku/svn/SvnContentManager.java 2005-11-03 21:37:25 UTC (rev 1498) @@ -37,23 +37,12 @@ */ public class SvnContentManager extends ContentManager { /** - * <code>prefix</code> - prefix of this content manager. - */ - private String prefix; - /** - * <code>id</code> - id of a corresponding repository. - */ - private String id; - /** * <code>service</code> - service interface binding. */ private SvnService service; public SvnContentManager(String id, String prefix) { super(id, prefix); - - this.prefix = prefix; - this.id = id; service = Tools.getService(); } Modified: trunk/forge/portal-extensions/shotoku/shotoku-svn-service/maven.xml =================================================================== --- trunk/forge/portal-extensions/shotoku/shotoku-svn-service/maven.xml 2005-11-03 20:53:19 UTC (rev 1497) +++ trunk/forge/portal-extensions/shotoku/shotoku-svn-service/maven.xml 2005-11-03 21:37:25 UTC (rev 1498) @@ -29,6 +29,12 @@ <ant:filename name="*.jar" /> </ant:fileset> </ant:copy> + <ant:copy + todir="target/${shotoku.ear.dir}" overwrite="true"> + <ant:fileset dir="${maven.repo.local}/velocity/jars"> + <ant:filename name="velocity*-1.4*.jar" /> + </ant:fileset> + </ant:copy> <ant:copy todir="target/${shotoku.ear.dir}"> <ant:fileset dir="src/app" flatten="true" overwrite="true"> <ant:filename name="**" /> Modified: trunk/forge/portal-extensions/shotoku/shotoku-svn-service/project.xml =================================================================== --- trunk/forge/portal-extensions/shotoku/shotoku-svn-service/project.xml 2005-11-03 20:53:19 UTC (rev 1497) +++ trunk/forge/portal-extensions/shotoku/shotoku-svn-service/project.xml 2005-11-03 21:37:25 UTC (rev 1498) @@ -55,6 +55,32 @@ </properties> </dependency> + <dependency> + <groupId>velocity</groupId> + <artifactId>velocity</artifactId> + <version>1.4</version> + <properties> + <ejb.manifest.classpath>true</ejb.manifest.classpath> + </properties> + </dependency> + <dependency> + <groupId>velocity</groupId> + <artifactId>velocity-dep</artifactId> + <version>1.4</version> + <properties> + <ejb.manifest.classpath>true</ejb.manifest.classpath> + </properties> + </dependency> + <dependency> + <groupId>velocity</groupId> + <artifactId>velocity</artifactId> + <version>1.4-dev</version> + <properties> + <ejb.manifest.classpath>true</ejb.manifest.classpath> + </properties> + </dependency> + + <dependency> <groupId>jboss</groupId> <artifactId>jboss-ejb3</artifactId> Modified: trunk/forge/portal-extensions/shotoku/shotoku-test/project.xml =================================================================== --- trunk/forge/portal-extensions/shotoku/shotoku-test/project.xml 2005-11-03 20:53:19 UTC (rev 1497) +++ trunk/forge/portal-extensions/shotoku/shotoku-test/project.xml 2005-11-03 21:37:25 UTC (rev 1498) @@ -41,7 +41,13 @@ <version>1.0</version> <jar>javax.servlet.jar</jar> </dependency> - + + <dependency> + <groupId>velocity</groupId> + <artifactId>velocity</artifactId> + <version>1.4</version> + </dependency> + <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> Modified: trunk/forge/portal-extensions/shotoku/shotoku-test/src/java/org/jboss/shotoku/test/servlet/ShotokuServlet.java =================================================================== --- trunk/forge/portal-extensions/shotoku/shotoku-test/src/java/org/jboss/shotoku/test/servlet/ShotokuServlet.java 2005-11-03 20:53:19 UTC (rev 1497) +++ trunk/forge/portal-extensions/shotoku/shotoku-test/src/java/org/jboss/shotoku/test/servlet/ShotokuServlet.java 2005-11-03 21:37:25 UTC (rev 1498) @@ -9,20 +9,12 @@ import javax.servlet.http.HttpServletResponse; import org.jboss.shotoku.ContentManager; -import org.jboss.shotoku.Node; import org.jboss.shotoku.aop.Inject; -import org.jboss.shotoku.aop.NodeInject; public class ShotokuServlet extends HttpServlet { @Inject(prefix="shotoku-test") ContentManager test; - - @NodeInject("shotoku-test/test") - Node injectedNode; - - @NodeInject("shotoku-test/test") - String injectedString; - + @Override protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { @@ -30,15 +22,12 @@ PrintWriter pw = response.getWriter(); pw.write("Depends test: " + test + "<br />"); pw.write("<br />"); + + pw.write("Velocity test:"); + pw.write("<br />"); - Node newNode = test.getRootDirectory().newNode("test"); - - newNode.setContent("zzz"); - newNode.save("Z"); - - pw.write("Content 1:" + newNode.getContent() + "<br />"); - pw.write("Content 2:" + injectedNode.getContent() + "<br />"); - pw.write("Content 3:" + injectedString + "<br />"); + //NodeList nl = new NodeList(); + //pw.write(nl.getFeed("default/feeds/templates/hello-world.vm", null, new HashMap<String, Object>()).toString()); } } |