X-Git-Url: http://git.argeo.org/?a=blobdiff_plain;f=server%2Fruntime%2Forg.argeo.server.jcr%2Fsrc%2Fmain%2Fjava%2Forg%2Fargeo%2Fjcr%2FJcrResourceAdapter.java;h=583995057ad614d3160c8344739aca0fe4c8d1e1;hb=019e0f2af17286be08ab17c1c9e1d8ba871ec9b2;hp=3c69e0b1efc043f5a6cef71a75b9f86183769564;hpb=0e11f046c7af8c6b95aa7372bd912455b40955d5;p=lgpl%2Fargeo-commons.git diff --git a/server/runtime/org.argeo.server.jcr/src/main/java/org/argeo/jcr/JcrResourceAdapter.java b/server/runtime/org.argeo.server.jcr/src/main/java/org/argeo/jcr/JcrResourceAdapter.java index 3c69e0b1e..583995057 100644 --- a/server/runtime/org.argeo.server.jcr/src/main/java/org/argeo/jcr/JcrResourceAdapter.java +++ b/server/runtime/org.argeo.server.jcr/src/main/java/org/argeo/jcr/JcrResourceAdapter.java @@ -1,3 +1,19 @@ +/* + * Copyright (C) 2010 Mathieu Baudier + * + * 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.argeo.jcr; import java.io.IOException; @@ -6,7 +22,6 @@ import java.util.ArrayList; import java.util.Calendar; import java.util.List; -import javax.activation.MimetypesFileTypeMap; import javax.jcr.Node; import javax.jcr.Property; import javax.jcr.RepositoryException; @@ -15,12 +30,15 @@ import javax.jcr.version.Version; import javax.jcr.version.VersionHistory; import javax.jcr.version.VersionIterator; -import org.apache.commons.io.FilenameUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.argeo.ArgeoException; import org.springframework.core.io.Resource; +/** + * Bridge Spring resources and JCR folder / files semantics (nt:folder / + * nt:file), supporting versioning as well. + */ public class JcrResourceAdapter { private final static Log log = LogFactory.getLog(JcrResourceAdapter.class); @@ -31,8 +49,15 @@ public class JcrResourceAdapter { // private String restoreBase = "/.restore"; + public JcrResourceAdapter() { + } + + public JcrResourceAdapter(Session session) { + this.session = session; + } + public void mkdirs(String path) { - JcrUtils.mkdirs(session(), path, "nt:folder", versioning); + JcrUtils.mkdirs(session(), path, "nt:folder", "nt:folder", versioning); } public void create(String path, Resource file, String mimeType) { @@ -99,22 +124,27 @@ public class JcrResourceAdapter { try { if (!session().itemExists(path)) { - String type = new MimetypesFileTypeMap() - .getContentType(FilenameUtils.getName(path)); + String type = null; + // FIXME: using javax.activation leads to conflict between Java + // 1.5 and 1.6 (since javax.activation was included in Java 1.6) + // String type = new MimetypesFileTypeMap() + // .getContentType(FilenameUtils.getName(path)); create(path, in, type); return; } Node fileNode = (Node) session().getItem(path); Node contentNode = fileNode.getNode("jcr:content"); - fileNode.checkout(); + if (versioning) + fileNode.checkout(); contentNode.setProperty("jcr:data", in); Calendar lastModified = Calendar.getInstance(); // lastModified.setTimeInMillis(file.lastModified()); contentNode.setProperty("jcr:lastModified", lastModified); session().save(); - fileNode.checkin(); + if (versioning) + fileNode.checkin(); if (log.isDebugEnabled()) log.debug("Updated " + path); @@ -187,8 +217,8 @@ public class JcrResourceAdapter { protected InputStream fromVersion(Version version) throws RepositoryException { Node frozenNode = version.getNode("jcr:frozenNode"); - InputStream in = frozenNode.getNode("jcr:content").getProperty( - "jcr:data").getStream(); + InputStream in = frozenNode.getNode("jcr:content") + .getProperty("jcr:data").getStream(); return in; } @@ -216,6 +246,5 @@ public class JcrResourceAdapter { public void setSession(Session session) { this.session = session; } - - + }