You are viewing a plain text version of this content. The canonical link for it is here.
Posted to slide-dev@jakarta.apache.org by st...@apache.org on 2004/09/06 19:10:58 UTC
cvs commit: jakarta-slide/proposals/jcrri/src/org/apache/slide/jcr/core/state PersistenceManager.java
stefan 2004/09/06 10:10:58
Modified: proposals/jcrri/src/org/apache/slide/jcr/core/version
GenericVersionSelector.java
proposals/jcrri/src/org/apache/slide/jcr/core/state/xml
XMLPersistenceManager.java
proposals/jcrri/src/org/apache/slide/jcr/core/state
PersistenceManager.java
Log:
jcrri: implementing REFRERENCES type & minor fixes
Revision Changes Path
1.3 +3 -3 jakarta-slide/proposals/jcrri/src/org/apache/slide/jcr/core/version/GenericVersionSelector.java
Index: GenericVersionSelector.java
===================================================================
RCS file: /home/cvs/jakarta-slide/proposals/jcrri/src/org/apache/slide/jcr/core/version/GenericVersionSelector.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- GenericVersionSelector.java 24 Aug 2004 09:51:54 -0000 1.2
+++ GenericVersionSelector.java 6 Sep 2004 17:10:58 -0000 1.3
@@ -112,13 +112,13 @@
public static Version selectByDate(VersionHistory history, Calendar date)
throws RepositoryException {
- long time = date==null ? Long.MAX_VALUE : date.getTime().getTime();
+ long time = date==null ? Long.MAX_VALUE : date.getTimeInMillis();
long latestDate=Long.MIN_VALUE;
Version latestVersion=null;
VersionIterator iter = history.getAllVersions();
while (iter.hasNext()) {
Version v = iter.nextVersion();
- long c = v.getCreated().getTime().getTime();
+ long c = v.getCreated().getTimeInMillis();
if (c>latestDate && c<=time) {
latestDate = c;
latestVersion = v;
1.13 +220 -16 jakarta-slide/proposals/jcrri/src/org/apache/slide/jcr/core/state/xml/XMLPersistenceManager.java
Index: XMLPersistenceManager.java
===================================================================
RCS file: /home/cvs/jakarta-slide/proposals/jcrri/src/org/apache/slide/jcr/core/state/xml/XMLPersistenceManager.java,v
retrieving revision 1.12
retrieving revision 1.13
diff -u -r1.12 -r1.13
--- XMLPersistenceManager.java 2 Aug 2004 16:22:23 -0000 1.12
+++ XMLPersistenceManager.java 6 Sep 2004 17:10:58 -0000 1.13
@@ -16,10 +16,7 @@
package org.apache.slide.jcr.core.state.xml;
import org.apache.log4j.Logger;
-import org.apache.slide.jcr.core.BLOBFileValue;
-import org.apache.slide.jcr.core.InternalValue;
-import org.apache.slide.jcr.core.QName;
-import org.apache.slide.jcr.core.WorkspaceDef;
+import org.apache.slide.jcr.core.*;
import org.apache.slide.jcr.core.nodetype.NodeDefId;
import org.apache.slide.jcr.core.nodetype.PropDefId;
import org.apache.slide.jcr.core.state.*;
@@ -86,6 +83,8 @@
private static final String NODEFILENAME = ".node.xml";
+ private static final String NODEREFSFILENAME = ".references";
+
private boolean initialized;
// file system where the item state is stored
@@ -150,6 +149,10 @@
return buildNodeFolderPath(uuid) + "/" + NODEFILENAME;
}
+ private String buildNodeReferencesFilePath(String uuid) {
+ return buildNodeFolderPath(uuid) + "/" + NODEREFSFILENAME;
+ }
+
private void readState(Element nodeElement, PersistentNodeState state)
throws ItemStateException {
// first do some paranoid sanity checks
@@ -182,9 +185,7 @@
// definition id
String definitionId = nodeElement.getAttributeValue(DEFINITIONID_ATTRIBUTE);
- if (definitionId != null && definitionId.length() > 0) {
- state.setDefinitionId(NodeDefId.valueOf(definitionId));
- }
+ state.setDefinitionId(NodeDefId.valueOf(definitionId));
// parent uuid's
Iterator iter = nodeElement.getChild(PARENTS_ELEMENT).getChildren(PARENT_ELEMENT).iterator();
@@ -267,9 +268,7 @@
// definition id
String definitionId = propElement.getAttributeValue(DEFINITIONID_ATTRIBUTE);
- if (definitionId != null && definitionId.length() > 0) {
- state.setDefinitionId(PropDefId.valueOf(definitionId));
- }
+ state.setDefinitionId(PropDefId.valueOf(definitionId));
// values
Iterator iter = propElement.getChild(VALUES_ELEMENT).getChildren(VALUE_ELEMENT).iterator();
@@ -599,7 +598,7 @@
writer.write("<" + NODE_ELEMENT + " " +
UUID_ATTRIBUTE + "=\"" + state.getUUID() + "\" " +
PARENTUUID_ATTRIBUTE + "=\"" + (state.getParentUUID() == null ? "" : state.getParentUUID()) + "\" " +
- DEFINITIONID_ATTRIBUTE + "=\"" + (state.getDefinitionId() == null ? "" : state.getDefinitionId().toString()) + "\" " +
+ DEFINITIONID_ATTRIBUTE + "=\"" + state.getDefinitionId().toString() + "\" " +
NODETYPE_ATTRIBUTE + "=\"" + state.getNodeTypeName() + "\">\n");
// parents
writer.write("\t<" + PARENTS_ELEMENT + ">\n");
@@ -693,7 +692,7 @@
writer.write("<" + PROPERTY_ELEMENT + " " +
NAME_ATTRIBUTE + "=\"" + state.getName() + "\" " +
PARENTUUID_ATTRIBUTE + "=\"" + state.getParentUUID() + "\" " +
- DEFINITIONID_ATTRIBUTE + "=\"" + (state.getDefinitionId() == null ? "" : state.getDefinitionId().toString()) + "\" " +
+ DEFINITIONID_ATTRIBUTE + "=\"" + state.getDefinitionId().toString() + "\" " +
TYPE_ATTRIBUTE + "=\"" + typeName + "\">\n");
// values
writer.write("\t<" + VALUES_ELEMENT + ">\n");
@@ -775,9 +774,7 @@
props.setProperty(TYPE_ATTRIBUTE, typeName);
// definition id
- if (state.getDefinitionId() != null) {
- props.setProperty(DEFINITIONID_ATTRIBUTE, state.getDefinitionId().toString());
- }
+ props.setProperty(DEFINITIONID_ATTRIBUTE, state.getDefinitionId().toString());
InternalValue[] values = state.getValues();
@@ -921,5 +918,212 @@
*/
public PersistentPropertyState createPropertyStateInstance(QName name, String parentUUID) {
return new XMLPropertyState(name, parentUUID, this);
+ }
+
+ /**
+ * @see PersistenceManager#createNodeReferencesInstance(String)
+ */
+ public NodeReferences createNodeReferencesInstance(String uuid) {
+ return new NodeReferences(new NodeId(uuid));
+ }
+
+ /**
+ * @see PersistenceManager#loadNodeReferences(String uuid)
+ */
+ public NodeReferences loadNodeReferences(String uuid) throws NoSuchItemStateException, ItemStateException {
+ if (!initialized) {
+ throw new IllegalStateException("not initialized");
+ }
+
+ Exception e = null;
+ String refsFilePath = buildNodeReferencesFilePath(uuid);
+ try {
+ if (!itemStateStore.isFile(refsFilePath)) {
+ throw new NoSuchItemStateException(uuid);
+ }
+ NodeReferences refs = createNodeReferencesInstance(uuid);
+
+ InputStream in = itemStateStore.getInputStream(refsFilePath);
+ Reader reader = null;
+ try {
+ String encoding = DEFAULT_ENCODING;
+ try {
+ reader = new BufferedReader(new InputStreamReader(in, encoding));
+ } catch (UnsupportedEncodingException uee) {
+ // should never get here!
+ InputStreamReader isw = new InputStreamReader(in);
+ encoding = isw.getEncoding();
+ reader = new BufferedReader(isw);
+ }
+ // read references (i.e. the id's of the REFERENCE properties)
+ StringBuffer tmp = new StringBuffer();
+ int c;
+ while ((c = reader.read()) != -1) {
+ if (c != '\n') {
+ tmp.append((char) c);
+ continue;
+ }
+ String s = tmp.toString();
+ if (s.length() > 0) {
+ PropertyId propId = PropertyId.valueOf(s);
+ refs.addReference(propId);
+ tmp.setLength(0);
+ }
+ }
+ String s = tmp.toString();
+ if (s.length() > 0) {
+ PropertyId propId = PropertyId.valueOf(s);
+ refs.addReference(propId);
+ }
+ } finally {
+ reader.close();
+ }
+
+ return refs;
+ } catch (IOException ioe) {
+ e = ioe;
+ // fall through
+ } catch (FileSystemException fse) {
+ e = fse;
+ // fall through
+ }
+ String msg = "failed to load references: " + uuid;
+ log.error(msg, e);
+ throw new ItemStateException(msg, e);
+ }
+
+ /**
+ * @see PersistenceManager#reload(NodeReferences)
+ */
+ public void reload(NodeReferences refs) throws ItemStateException {
+ if (!initialized) {
+ throw new IllegalStateException("not initialized");
+ }
+
+ Exception e = null;
+ String uuid = refs.getTargetId().getUUID();
+ String refsFilePath = buildNodeReferencesFilePath(uuid);
+ try {
+ if (!itemStateStore.isFile(refsFilePath)) {
+ throw new NoSuchItemStateException(uuid);
+ }
+
+ refs.clearAllReferences();
+
+ InputStream in = itemStateStore.getInputStream(refsFilePath);
+ Reader reader = null;
+ try {
+ String encoding = DEFAULT_ENCODING;
+ try {
+ reader = new BufferedReader(new InputStreamReader(in, encoding));
+ } catch (UnsupportedEncodingException uee) {
+ // should never get here!
+ InputStreamReader isw = new InputStreamReader(in);
+ encoding = isw.getEncoding();
+ reader = new BufferedReader(isw);
+ }
+ // read references (i.e. the id's of the REFERENCE properties)
+ StringBuffer tmp = new StringBuffer();
+ int c;
+ while ((c = reader.read()) != -1) {
+ if (c != '\n') {
+ tmp.append((char) c);
+ continue;
+ }
+ String s = tmp.toString();
+ if (s.length() > 0) {
+ PropertyId propId = PropertyId.valueOf(s);
+ refs.addReference(propId);
+ tmp.setLength(0);
+ }
+ }
+ String s = tmp.toString();
+ if (s.length() > 0) {
+ PropertyId propId = PropertyId.valueOf(s);
+ refs.addReference(propId);
+ }
+ } finally {
+ reader.close();
+ }
+
+ return;
+ } catch (IOException ioe) {
+ e = ioe;
+ // fall through
+ } catch (FileSystemException fse) {
+ e = fse;
+ // fall through
+ }
+ String msg = "failed to load references: " + uuid;
+ log.error(msg, e);
+ throw new ItemStateException(msg, e);
+ }
+
+ /**
+ * @see PersistenceManager#store(NodeReferences)
+ */
+ public void store(NodeReferences refs) throws ItemStateException {
+ if (!initialized) {
+ throw new IllegalStateException("not initialized");
+ }
+
+ String uuid = refs.getTargetId().getUUID();
+ String refsFilePath = buildNodeReferencesFilePath(uuid);
+ FileSystemResource refsFile = new FileSystemResource(itemStateStore, refsFilePath);
+ try {
+ refsFile.makeParentDirs();
+ OutputStream os = refsFile.getOutputStream();
+ Writer writer = null;
+ try {
+ String encoding = DEFAULT_ENCODING;
+ try {
+ writer = new BufferedWriter(new OutputStreamWriter(os, encoding));
+ } catch (UnsupportedEncodingException e) {
+ // should never get here!
+ OutputStreamWriter osw = new OutputStreamWriter(os);
+ encoding = osw.getEncoding();
+ writer = new BufferedWriter(osw);
+ }
+ // write references (i.e. the id's of the REFERENCE properties)
+ Iterator iter = refs.getReferences().iterator();
+ while (iter.hasNext()) {
+ PropertyId propId = (PropertyId) iter.next();
+ writer.write(propId.toString() + '\n');
+ }
+ } finally {
+ writer.close();
+ }
+ } catch (Exception e) {
+ String msg = "failed to store references: " + uuid;
+ log.error(msg, e);
+ throw new ItemStateException(msg, e);
+ }
+ }
+
+ /**
+ * @see PersistenceManager#destroy(NodeReferences)
+ */
+ public void destroy(NodeReferences refs) throws ItemStateException {
+ if (!initialized) {
+ throw new IllegalStateException("not initialized");
+ }
+
+ String uuid = refs.getTargetId().getUUID();
+ String refsFilePath = buildNodeReferencesFilePath(uuid);
+ FileSystemResource refsFile = new FileSystemResource(itemStateStore, refsFilePath);
+ try {
+ refsFile.delete();
+ // prune empty folders
+ String parentDir = FileSystemPathUtil.getParentDir(refsFilePath);
+ while (!parentDir.equals(FileSystem.SEPARATOR) &&
+ !itemStateStore.hasChildren(parentDir)) {
+ itemStateStore.deleteFolder(parentDir);
+ parentDir = FileSystemPathUtil.getParentDir(parentDir);
+ }
+ } catch (FileSystemException fse) {
+ String msg = "failed to delete references: " + uuid;
+ log.error(msg, fse);
+ throw new ItemStateException(msg, fse);
+ }
}
}
1.9 +34 -1 jakarta-slide/proposals/jcrri/src/org/apache/slide/jcr/core/state/PersistenceManager.java
Index: PersistenceManager.java
===================================================================
RCS file: /home/cvs/jakarta-slide/proposals/jcrri/src/org/apache/slide/jcr/core/state/PersistenceManager.java,v
retrieving revision 1.8
retrieving revision 1.9
diff -u -r1.8 -r1.9
--- PersistenceManager.java 2 Aug 2004 16:22:22 -0000 1.8
+++ PersistenceManager.java 6 Sep 2004 17:10:58 -0000 1.9
@@ -64,6 +64,21 @@
public void reload(PersistentPropertyState state) throws ItemStateException;
/**
+ * @param uuid
+ * @return
+ * @throws NoSuchItemStateException
+ * @throws ItemStateException
+ */
+ public NodeReferences loadNodeReferences(String uuid)
+ throws NoSuchItemStateException, ItemStateException;
+
+ /**
+ * @param refs
+ * @throws ItemStateException
+ */
+ public void reload(NodeReferences refs) throws ItemStateException;
+
+ /**
* @param state
* @throws ItemStateException
*/
@@ -76,6 +91,12 @@
public void store(PersistentPropertyState state) throws ItemStateException;
/**
+ * @param refs
+ * @throws ItemStateException
+ */
+ public void store(NodeReferences refs) throws ItemStateException;
+
+ /**
* @param state
* @throws ItemStateException
*/
@@ -87,6 +108,12 @@
*/
public void destroy(PersistentPropertyState state) throws ItemStateException;
+ /**
+ * @param refs
+ * @throws ItemStateException
+ */
+ public void destroy(NodeReferences refs) throws ItemStateException;
+
//------------------------------------------------------< factory methods >
/**
* @param uuid
@@ -101,4 +128,10 @@
* @return
*/
public PersistentPropertyState createPropertyStateInstance(QName name, String parentUUID);
+
+ /**
+ * @param uuid
+ * @return
+ */
+ public NodeReferences createNodeReferencesInstance(String uuid);
}
---------------------------------------------------------------------
To unsubscribe, e-mail: slide-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: slide-dev-help@jakarta.apache.org