Author: adamw Date: 2006-02-15 12:22:02 -0500 (Wed, 15 Feb 2006) New Revision: 2459 Modified: trunk/forge/portal-extensions/shotoku/shotoku-base/shotoku-base.iml trunk/forge/portal-extensions/shotoku/shotoku-base/src/java/org/jboss/shotoku/tools/Tools.java trunk/forge/portal-extensions/shotoku/shotoku-jcr/src/java/org/jboss/shotoku/jcr/JcrContentManager.java trunk/forge/portal-extensions/shotoku/shotoku-jcr/src/java/org/jboss/shotoku/jcr/JcrNode.java trunk/forge/portal-extensions/shotoku/shotoku-jcr/src/java/org/jboss/shotoku/jcr/JcrNodeContent.java trunk/forge/portal-extensions/shotoku/shotoku-jcr/src/java/org/jboss/shotoku/jcr/JcrResource.java trunk/forge/portal-extensions/shotoku/shotoku-test/src/java/org/jboss/shotoku/test/embedded/JcrTest.java Log: http://jira.jboss.com/jira/browse/JBSHOTOKU-51 Modified: trunk/forge/portal-extensions/shotoku/shotoku-base/shotoku-base.iml =================================================================== --- trunk/forge/portal-extensions/shotoku/shotoku-base/shotoku-base.iml 2006-02-15 16:19:47 UTC (rev 2458) +++ trunk/forge/portal-extensions/shotoku/shotoku-base/shotoku-base.iml 2006-02-15 17:22:02 UTC (rev 2459) @@ -5,6 +5,7 @@ <output url="file://$MODULE_DIR$/target" /> <exclude-output /> <content url="file://$MODULE_DIR$"> + <sourceFolder url="file://$MODULE_DIR$/src/etc" isTestSource="false" /> <sourceFolder url="file://$MODULE_DIR$/src/java" isTestSource="false" /> </content> <orderEntry type="inheritedJdk" /> Modified: trunk/forge/portal-extensions/shotoku/shotoku-base/src/java/org/jboss/shotoku/tools/Tools.java =================================================================== --- trunk/forge/portal-extensions/shotoku/shotoku-base/src/java/org/jboss/shotoku/tools/Tools.java 2006-02-15 16:19:47 UTC (rev 2458) +++ trunk/forge/portal-extensions/shotoku/shotoku-base/src/java/org/jboss/shotoku/tools/Tools.java 2006-02-15 17:22:02 UTC (rev 2459) @@ -93,21 +93,26 @@ * @return <code>toNormalize</code> with unnecessary / removed. */ public static String normalizeSlashes(String toNormalize) { - while (toNormalize.contains("//")) + while (toNormalize.contains("//")) { toNormalize = toNormalize.replace("//", "/"); + } - if (!toNormalize.startsWith("/")) + if (!toNormalize.startsWith("/")) { toNormalize = "/" + toNormalize; + } - if ((toNormalize.endsWith("/")) && (toNormalize.length() > 1)) + if ((toNormalize.endsWith("/")) && (toNormalize.length() > 1)) { toNormalize = toNormalize.substring(0, toNormalize.length() - 1); + } return toNormalize; } /** * Concatenates two parts of a path into 1: returns a string - * <code>path1/path2</code>. + * <code>path1/path2</code>, only with the necessary slashes + * (so running normalizeSlashes(String) won't have any effect + * on the string). * * @param path1 * First path to concatenate. @@ -116,6 +121,10 @@ * @return <code>path1/path2</code> */ public static String concatenatePaths(String path1, String path2) { + if ("".equals(path1) || "".equals(path2)) { + return path1 + path2; + } + boolean p1e = path1.endsWith("/"); boolean p2s = path2.startsWith("/"); Modified: trunk/forge/portal-extensions/shotoku/shotoku-jcr/src/java/org/jboss/shotoku/jcr/JcrContentManager.java =================================================================== --- trunk/forge/portal-extensions/shotoku/shotoku-jcr/src/java/org/jboss/shotoku/jcr/JcrContentManager.java 2006-02-15 16:19:47 UTC (rev 2458) +++ trunk/forge/portal-extensions/shotoku/shotoku-jcr/src/java/org/jboss/shotoku/jcr/JcrContentManager.java 2006-02-15 17:22:02 UTC (rev 2459) @@ -9,6 +9,10 @@ import org.jboss.shotoku.exceptions.RepositoryException; import javax.jcr.*; +import javax.jcr.version.VersionException; +import javax.jcr.nodetype.ConstraintViolationException; +import javax.jcr.nodetype.NoSuchNodeTypeException; +import javax.jcr.lock.LockException; import java.util.Collection; /** @@ -91,18 +95,16 @@ } n = getNodeForPath(session, path); - } catch (Exception e) { - throw new RepositoryException(e); - } - try { if (!n.getPrimaryNodeType().isNodeType(requiredType)) { throw new ResourceDoesNotExist("Resource exist, but of a different type."); } - } catch (ItemNotFoundException e) { + } catch (PathNotFoundException e) { throw new ResourceDoesNotExist(path); } catch (javax.jcr.RepositoryException e) { throw new RepositoryException(e); + } catch (Exception e) { + throw new RepositoryException(e); } } finally { JcrTools.safeSessionLogout(session); @@ -146,6 +148,24 @@ } session.save(); + } catch (SaveException e) { + throw e; + } catch (LockException e) { + throw new SaveException(e); + } catch (AccessDeniedException e) { + throw new SaveException(e); + } catch (ConstraintViolationException e) { + throw new SaveException(e); + } catch (ItemExistsException e) { + throw new SaveException(e); + } catch (NoSuchNodeTypeException e) { + throw new SaveException(e); + } catch (InvalidItemStateException e) { + throw new SaveException(e); + } catch (ValueFormatException e) { + throw new SaveException(e); + } catch (VersionException e) { + throw new SaveException(e); } catch (Exception e) { throw new RepositoryException(e); } finally { @@ -154,6 +174,8 @@ } public void delete(Collection<Resource> resources) throws DeleteException, RepositoryException { - + for (Resource resource : resources) { + resource.delete(); + } } } Modified: trunk/forge/portal-extensions/shotoku/shotoku-jcr/src/java/org/jboss/shotoku/jcr/JcrNode.java =================================================================== --- trunk/forge/portal-extensions/shotoku/shotoku-jcr/src/java/org/jboss/shotoku/jcr/JcrNode.java 2006-02-15 16:19:47 UTC (rev 2458) +++ trunk/forge/portal-extensions/shotoku/shotoku-jcr/src/java/org/jboss/shotoku/jcr/JcrNode.java 2006-02-15 17:22:02 UTC (rev 2459) @@ -48,6 +48,10 @@ } } + boolean isChanged() { + return content.getChanged() || super.isChanged(); + } + /* * Node implementation */ Modified: trunk/forge/portal-extensions/shotoku/shotoku-jcr/src/java/org/jboss/shotoku/jcr/JcrNodeContent.java =================================================================== --- trunk/forge/portal-extensions/shotoku/shotoku-jcr/src/java/org/jboss/shotoku/jcr/JcrNodeContent.java 2006-02-15 16:19:47 UTC (rev 2458) +++ trunk/forge/portal-extensions/shotoku/shotoku-jcr/src/java/org/jboss/shotoku/jcr/JcrNodeContent.java 2006-02-15 17:22:02 UTC (rev 2459) @@ -22,12 +22,14 @@ package org.jboss.shotoku.jcr; import java.io.InputStream; +import java.io.ByteArrayInputStream; import org.jboss.shotoku.exceptions.RepositoryException; import org.jboss.shotoku.common.content.NodeContent; import javax.jcr.Repository; import javax.jcr.Session; +import javax.jcr.Node; /** * A subclass of NodeContent, which reads content from a given JCR node, @@ -58,8 +60,13 @@ try { session = connector.getSession(repository); - return jcrCm.getNodeForPath(session, fullPath).getProperty( - connector.getDataPropertyName()).getString().getBytes(); + Node node = jcrCm.getNodeForPath(session, fullPath); + String propertyName = connector.getDataPropertyName(); + if (node.hasProperty(propertyName)) { + return node.getProperty(propertyName).getString().getBytes(); + } else { + return new byte[0]; + } } catch (Exception e) { throw new RepositoryException(e); } finally { @@ -77,8 +84,13 @@ try { session = connector.getSession(repository); - return jcrCm.getNodeForPath(session, fullPath).getProperty( - connector.getDataPropertyName()).getStream(); + Node node = jcrCm.getNodeForPath(session, fullPath); + String propertyName = connector.getDataPropertyName(); + if (node.hasProperty(propertyName)) { + return node.getProperty(propertyName).getStream(); + } else { + return new ByteArrayInputStream(new byte[0]); + } } catch (Exception e) { throw new RepositoryException(e); } finally { @@ -96,8 +108,13 @@ try { session = connector.getSession(repository); - return jcrCm.getNodeForPath(session, fullPath).getProperty( - connector.getDataPropertyName()).getString(); + Node node = jcrCm.getNodeForPath(session, fullPath); + String propertyName = connector.getDataPropertyName(); + if (node.hasProperty(propertyName)) { + return node.getProperty(propertyName).getString(); + } else { + return ""; + } } catch (Exception e) { throw new RepositoryException(e); } finally { @@ -115,8 +132,13 @@ try { session = connector.getSession(repository); - return jcrCm.getNodeForPath(session, fullPath).getProperty( - connector.getDataPropertyName()).getLength(); + Node node = jcrCm.getNodeForPath(session, fullPath); + String propertyName = connector.getDataPropertyName(); + if (node.hasProperty(propertyName)) { + return node.getProperty(propertyName).getLength(); + } else { + return 0; + } } catch (Exception e) { throw new RepositoryException(e); } finally { Modified: trunk/forge/portal-extensions/shotoku/shotoku-jcr/src/java/org/jboss/shotoku/jcr/JcrResource.java =================================================================== --- trunk/forge/portal-extensions/shotoku/shotoku-jcr/src/java/org/jboss/shotoku/jcr/JcrResource.java 2006-02-15 16:19:47 UTC (rev 2458) +++ trunk/forge/portal-extensions/shotoku/shotoku-jcr/src/java/org/jboss/shotoku/jcr/JcrResource.java 2006-02-15 17:22:02 UTC (rev 2459) @@ -7,6 +7,10 @@ import org.jboss.shotoku.exceptions.RepositoryException; import javax.jcr.*; +import javax.jcr.version.VersionException; +import javax.jcr.nodetype.ConstraintViolationException; +import javax.jcr.nodetype.NoSuchNodeTypeException; +import javax.jcr.lock.LockException; import java.util.*; /** @@ -87,82 +91,90 @@ * a directory or file node, depending on this resource type. * @return Last node created. */ - Node createNodes(Session session) throws SaveException { - String[] tokens = fullPath.split("[/]"); + Node createNodes(Session session) throws SaveException, javax.jcr.RepositoryException { + String[] tokens = getFullPath().split("[/]"); - try { - Node currNode = session.getRootNode(); + Node currNode = session.getRootNode(); - for (int i=1; i<tokens.length-1; i++) { - try { - currNode = currNode.getNode(tokens[i]); - } catch (PathNotFoundException pe) { - // Creating a new directory. - currNode = currNode.addNode(tokens[i], - getConnector().getDirectoryNodeType()); - } + for (int i=1; i<tokens.length-1; i++) { + try { + currNode = currNode.getNode(tokens[i]); + } catch (PathNotFoundException pe) { + // Creating a new directory. + currNode = currNode.addNode(tokens[i], + getConnector().getDirectoryNodeType()); } + } - // Creating the last node. - return createNode(currNode); - } catch (javax.jcr.RepositoryException e) { - throw new SaveException(e); - } catch (Exception e) { - throw new RepositoryException(e); - } + // Creating the last node. + return createNode(currNode); } - void internalSave(Session session, String logMessage) throws SaveException { - try { - Node n; - long now = Calendar.getInstance().getTimeInMillis(); - if (isNew) { - // Creating all the necessary nodes. - n = createNodes(session); + /** + * Checks if this resource has been changed in any way (or just used + * read-only). + */ + boolean isChanged() { + return modifiedProperties.size() > 0 || deletedProperties.size() > 0 || isNew(); + } - // Setting the "created" time. - n.setProperty(getConnector().createInternalPropertyName( - JcrTools.JCR_PROP_CREATED), now); + void internalSave(Session session, String logMessage) + throws SaveException, javax.jcr.RepositoryException { + // Checking if there is anything to save. + if (!isChanged()) return; - isNew = false; - } else { - n = session.getRootNode().getNode(fullPath); - } + Node n; + long now = Calendar.getInstance().getTimeInMillis(); + if (isNew()) { + // Creating all the necessary nodes. + n = createNodes(session); - // Setting the "modified" time. + // Setting the "created" time. n.setProperty(getConnector().createInternalPropertyName( - JcrTools.JCR_PROP_LAST_MOD), now); + JcrTools.JCR_PROP_CREATED), now); - // Setting the log property - n.setProperty(getConnector().createInternalPropertyName(JcrTools.JCR_PROP_LOG), - logMessage); + isNew = false; + } else { + n = jcrCm.getNodeForPath(session, getFullPath()); + } - // Saving properties. - for (String propName : modifiedProperties.keySet()) { - n.setProperty(getConnector().createUserPropertyName(propName), - modifiedProperties.get(propName)); - } + // Setting the "modified" time. + n.setProperty(getConnector().createInternalPropertyName( + JcrTools.JCR_PROP_LAST_MOD), now); - for (String propName : deletedProperties) { - n.setProperty(getConnector().createUserPropertyName(propName), - (String) null); - } + // Setting the log property + n.setProperty(getConnector().createInternalPropertyName(JcrTools.JCR_PROP_LOG), + logMessage); - // Executing resource-specific actions. - notifySave(n); - } catch (javax.jcr.RepositoryException e) { - throw new RepositoryException(e); + // Saving properties. + for (String propName : modifiedProperties.keySet()) { + n.setProperty(getConnector().createUserPropertyName(propName), + modifiedProperties.get(propName)); } + + for (String propName : deletedProperties) { + n.setProperty(getConnector().createUserPropertyName(propName), + (String) null); + } + + // Executing resource-specific actions. + notifySave(n); } + /** + * @param jcrPropertyName Name of the property to get (should already have + * all necessary namespace information). + * @return Value of the given property as written in a jcr node corresponding + * to this resource or null if this property is not set. + */ String getJcrProperty(String jcrPropertyName) { Session session = null; try { - session = connector.getSession(repository); + session = getConnector().getSession(getRepository()); - Node n = jcrCm.getNodeForPath(session, fullPath); + Node n = getJcrCm().getNodeForPath(session, getFullPath()); if (n.hasProperty(jcrPropertyName)) { - return jcrCm.getNodeForPath(session, fullPath).getProperty( + return getJcrCm().getNodeForPath(session, getFullPath()).getProperty( jcrPropertyName).getString(); } else { return null; @@ -183,13 +195,13 @@ */ public void copyTo(Directory dir, String newName, String logMessage) throws CopyException { - if (isNew) { + if (isNew()) { throw new CopyException("Resource not yet saved!"); } } public void moveTo(Directory dir, String logMessage) throws MoveException { - if (isNew) { + if (isNew()) { throw new MoveException("Resource not yet saved!"); } } @@ -201,9 +213,9 @@ Map<String, String> ret = new HashMap<String, String>(); - if (!isNew) { + if (!isNew()) { // Only getting properties from jcr on saved nodes. - Node n = jcrCm.getNodeForPath(session, fullPath); + Node n = getJcrCm().getNodeForPath(session, getFullPath()); PropertyIterator pi = n.getProperties( getConnector().createUserPropertyName("*")); @@ -236,7 +248,7 @@ } // If this is a new node, no further possibilities. - if (isNew) return null; + if (isNew()) return null; return getJcrProperty(getConnector().createUserPropertyName(propertyName)); } @@ -252,17 +264,17 @@ } public Directory getParent() throws RepositoryException { - if (jcrCm.isRoot(fullName)) { + if (getJcrCm().isRoot(getFullName())) { // We cannot go higher. return null; } - int lastSlash = fullName.lastIndexOf('/'); + int lastSlash = getFullName().lastIndexOf('/'); if (lastSlash == -1) { - return jcrCm.getRootDirectory(); + return getJcrCm().getRootDirectory(); } else { try { - return jcrCm.getDirectory(fullName.substring(0, lastSlash)); + return getJcrCm().getDirectory(getFullName().substring(0, lastSlash)); } catch (ResourceDoesNotExist e) { // Parent directory must exist, but who knows ... throw new RepositoryException(e); @@ -275,8 +287,24 @@ } public void delete() throws DeleteException, RepositoryException { - if (!isNew) { + if (!isNew()) { + Session session = null; + try { + session = getConnector().getSession(getRepository()); + getJcrCm().getNodeForPath(session, getFullPath()).remove(); + session.save(); + } catch (LockException e) { + throw new DeleteException(e); + } catch (ConstraintViolationException e) { + throw new DeleteException(e); + } catch (VersionException e) { + throw new DeleteException(e); + } catch (Exception e) { + throw new RepositoryException(e); + } finally { + JcrTools.safeSessionLogout(session); + } } } Modified: trunk/forge/portal-extensions/shotoku/shotoku-test/src/java/org/jboss/shotoku/test/embedded/JcrTest.java =================================================================== --- trunk/forge/portal-extensions/shotoku/shotoku-test/src/java/org/jboss/shotoku/test/embedded/JcrTest.java 2006-02-15 16:19:47 UTC (rev 2458) +++ trunk/forge/portal-extensions/shotoku/shotoku-test/src/java/org/jboss/shotoku/test/embedded/JcrTest.java 2006-02-15 17:22:02 UTC (rev 2459) @@ -7,6 +7,7 @@ import org.jboss.shotoku.exceptions.SaveException; import org.jboss.shotoku.exceptions.ResourceAlreadyExists; import org.jboss.shotoku.exceptions.ResourceDoesNotExist; +import org.jboss.shotoku.exceptions.DeleteException; import java.util.Map; @@ -23,7 +24,7 @@ System.out.println("-------------------"); } - public static void main(String[] args) throws SaveException, ResourceAlreadyExists, ResourceDoesNotExist { + public static void main(String[] args) throws SaveException, ResourceAlreadyExists, ResourceDoesNotExist, DeleteException, InterruptedException { ContentManager.setup(); ContentManager cm = ContentManager.getContentManager("jcr", ""); Directory d = cm.getRootDirectory(); @@ -38,21 +39,47 @@ System.out.println("Name: " + n + "; " + props.get(n)); } */ - /*Directory d2 = d.newDirectory("aaa/bbb"); + /*Directory d2 = d.newDirectory("uuu/bbb"); d2.save("x"); - d2 = d.newDirectory("aaa/ccc"); - d2.save("y"); */ + d2 = d.newDirectory("uuu/ccc"); + d2.save("y");*/ + // Checking if the test directory exists, and if not, creating it. + ContentManager cmTemp = ContentManager.getContentManager("jcr", ""); + try { + cmTemp.getDirectory("rrrttt"); + } catch (ResourceDoesNotExist e) { + try { + cmTemp.getRootDirectory().newDirectory("rrrttt").save("z"); + } catch (Exception e2) { + throw new RuntimeException("Exception while intializing the test directory", e2); + } + } + System.out.println("--------------"); - for (Directory dd : d.getDirectory("aaa").getDirectories()) { + for (Directory dd : d.getDirectories()) { System.out.println(dd.getFullName()); } System.out.println("--------------"); + d = cmTemp.getRootDirectory(); System.out.println("--------------"); - for (Node nn : d.getDirectory("aaa").getNodes()) { - System.out.println(nn.getFullName()); + for (Directory dd : d.getDirectories()) { + System.out.println(dd.getFullName()); } System.out.println("--------------"); + + cmTemp.getDirectory("rrrttt").delete(); + + //d.newNode("zzz/node").save("x"); + + /*Node n = d.getNode("zzz/node"); + n.setContent("zzz"); + n.save("x"); + System.out.println("--------------"); + System.out.println(d.getDirectory("zzz").getNode("node").getContent()); + System.out.println("--------------");*/ + + } } |