]> git.argeo.org Git - gpl/argeo-slc.git/blob - org.argeo.slc.core/src/org/argeo/slc/jcr/JcrMetadataWriter.java
Disable trace logging
[gpl/argeo-slc.git] / org.argeo.slc.core / src / org / argeo / slc / jcr / JcrMetadataWriter.java
1 /*
2 * Copyright (C) 2007-2012 Argeo GmbH
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package org.argeo.slc.jcr;
17
18 import java.util.HashMap;
19 import java.util.Map;
20
21 import javax.jcr.Node;
22 import javax.jcr.RepositoryException;
23
24 import org.apache.commons.logging.Log;
25 import org.apache.commons.logging.LogFactory;
26 import org.argeo.jcr.JcrUtils;
27 import org.argeo.slc.SlcException;
28
29 /**
30 * Writes arbitrary metadata into a child node of a given node (or the node
31 * itself if metadata node name is set to null)
32 */
33 public class JcrMetadataWriter implements Runnable {
34 private final static Log log = LogFactory.getLog(JcrMetadataWriter.class);
35
36 private Node baseNode;
37 private String metadataNodeName = SlcNames.SLC_METADATA;
38
39 private Map<String, String> metadata = new HashMap<String, String>();
40
41 public void run() {
42 try {
43 Node metadataNode;
44 if (metadataNodeName != null)
45 metadataNode = baseNode.hasNode(metadataNodeName) ? baseNode
46 .getNode(metadataNodeName) : baseNode
47 .addNode(metadataNodeName);
48 else
49 metadataNode = baseNode;
50
51 for (String key : metadata.keySet())
52 metadataNode.setProperty(key, metadata.get(key));
53
54 baseNode.getSession().save();
55
56 if (log.isDebugEnabled())
57 log.debug("Wrote " + metadata.size() + " metadata entries to "
58 + metadataNode);
59 } catch (RepositoryException e) {
60 throw new SlcException("Cannot write metadata to " + baseNode, e);
61 } finally {
62 JcrUtils.discardUnderlyingSessionQuietly(baseNode);
63 }
64
65 }
66
67 public void setBaseNode(Node baseNode) {
68 this.baseNode = baseNode;
69 }
70
71 public void setMetadataNodeName(String metadataNodeName) {
72 this.metadataNodeName = metadataNodeName;
73 }
74
75 public void setMetadata(Map<String, String> metadata) {
76 this.metadata = metadata;
77 }
78
79 }