KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > exoplatform > services > jcr > impl > storage > filesystem > nodedata > FileNodeContainer


1 /***************************************************************************
2  * Copyright 2001-2003 The eXo Platform SARL All rights reserved. *
3  * Please look at license.txt in info directory for more license detail. *
4  **************************************************************************/

5
6 package org.exoplatform.services.jcr.impl.storage.filesystem.nodedata;
7
8
9 import javax.jcr.*;
10 import java.io.File JavaDoc;
11 import java.io.FileInputStream JavaDoc;
12 import java.io.FileOutputStream JavaDoc;
13 import java.util.ArrayList JavaDoc;
14 import java.util.List JavaDoc;
15 import org.apache.commons.codec.binary.Base64;
16 import org.exoplatform.services.jcr.core.NodeChange;
17 import org.exoplatform.services.jcr.core.NodeData;
18 import org.exoplatform.services.jcr.impl.core.NodeImpl;
19 import org.exoplatform.services.jcr.impl.core.PropertyImpl;
20 import org.exoplatform.services.jcr.storage.Container;
21
22
23 /**
24  * Created by The eXo Platform SARL
25  *
26  * @author <a HREF="mailto:geaz@users.sourceforge.net">Gennady Azarenkov</a>
27  * @version $Id: FileNodeContainer.java,v 1.14 2004/11/02 18:34:16 geaz Exp $
28  */

29
30 public final class FileNodeContainer extends BaseNodeContainer {
31
32   FileNodeContainer(String JavaDoc jcrPath, File JavaDoc storage) {
33     super(jcrPath, "nt:file", storage);
34   }
35
36   public NodeData getNodeByPath(String JavaDoc relPath) {
37     if (relPath.length() == 0)
38       return getRootNode();
39     else
40       return getContentNode(relPath);
41
42   }
43
44   public List JavaDoc getChildren(String JavaDoc relPath) {
45     ArrayList JavaDoc list = new ArrayList JavaDoc();
46     if (relPath.length() == 0)
47        list.add(getJcrPath("/jcr:content"));
48 // list.add(getContentNode("/jcr:content"));
49
return list;
50   }
51
52   public void add(Node node) throws ItemExistsException, RepositoryException {
53     try {
54       String JavaDoc relPath = parseRelPath(node.getPath());
55       // file is already created by resolver.create
56
// System.out.println("EXO:CONTENT ---- "+node);
57
if (relPath.equals("/jcr:content")) {
58         updateContent((NodeData)node);
59       }
60     } catch (Exception JavaDoc e) {
61       throw new RepositoryException("Adding node failed. Reason:" + e.getMessage());
62     }
63   }
64
65   public final void update(Node node) throws RepositoryException {
66     String JavaDoc relPath;
67     try {
68       relPath = parseRelPath(node.getPath());
69     } catch (Exception JavaDoc e) {
70       return;
71     }
72
73     if (relPath.equals("/jcr:content")) // ; ?????
74
updateContent(node);
75   }
76
77   public final void delete(String JavaDoc absPath) {
78     try {
79       String JavaDoc relPath = parseRelPath(absPath);
80       log.debug("FileNode Container delete node:" + absPath + "at "+relPath);
81       if (relPath.length() == 0)
82         storage.delete();
83       else if (relPath.equals("/jcr:content"))
84         cleanContent();
85     } catch (Exception JavaDoc e) {
86       return;
87     }
88   }
89
90
91 ///////////////////////////////////////////////////////
92

93   protected NodeImpl getContentNode(String JavaDoc relPath) {
94     if (!relPath.equals("/jcr:content"))
95       return null;
96
97     NodeImpl node;
98     try {
99       node = new NodeImpl(getJcrPath(relPath), getContentProps());
100     } catch (Exception JavaDoc e) {
101       return null;
102     }
103
104     return node;
105   }
106
107   protected List JavaDoc getContentProps() {
108
109     ArrayList JavaDoc props = new ArrayList JavaDoc();
110     props.add(getContentProperty("/jcr:content/jcr:primaryType"));
111     PropertyImpl contentProp = getContentProperty("/jcr:content/exo:content");
112     if(contentProp != null)
113        props.add(contentProp);
114     props.add(getContentProperty("/jcr:content/jcr:uuid"));
115
116     return props;
117
118   }
119
120
121   protected PropertyImpl getContentProperty(String JavaDoc relPath) {
122
123     if (!relPath.startsWith("/jcr:content"))
124       return null;
125
126     try {
127       if (relPath.equals("/jcr:content/jcr:primaryType"))
128         return new PropertyImpl(getJcrPath("/jcr:content/jcr:primaryType"),
129             new StringValue("nt:content"), PropertyType.STRING);
130
131       if (relPath.equals("/jcr:content/jcr:uuid"))
132         return new PropertyImpl(getJcrPath("/jcr:content/jcr:uuid"),
133             new StringValue(new String JavaDoc(Base64.encodeBase64( (containerPath/*+"/jcr:content"*/).getBytes()))), PropertyType.STRING) ;
134
135       ////////////////////////
136
// relPath = "/jcr:content/exo:content";
137
///////////////////////
138

139       if (relPath.equals("/jcr:content/exo:content")) {
140         FileInputStream JavaDoc fis = new FileInputStream JavaDoc(storage);
141         if(fis.available() == 0)
142             return null;
143         byte[] buffer = new byte[fis.available()];
144         fis.read(buffer);
145         fis.close();
146         return new PropertyImpl(getJcrPath("/jcr:content/exo:content"),
147             new StringValue(new String JavaDoc(buffer)), PropertyType.STRING);
148       }
149
150     } catch (Exception JavaDoc e) {
151       e.printStackTrace();
152       return null;
153     }
154
155     return null;
156   }
157
158   private void cleanContent() {
159     FileOutputStream JavaDoc fos = null;
160     try {
161       fos = new FileOutputStream JavaDoc(storage);
162       fos.write(new byte[0]);
163       fos.close();
164     } catch (Exception JavaDoc e) {
165       e.printStackTrace();
166     }
167
168   }
169
170   private void updateContent(Node node) throws RepositoryException {
171     try {
172       FileOutputStream JavaDoc fos = new FileOutputStream JavaDoc(storage);
173       System.out.println("EXO:CONTENT ---- "+node);
174       Property prop = ((NodeImpl)node).getPermanentProperty("exo:content");
175       if(prop == null)
176         return;
177 // throw new RepositoryException("FileNodeContainer.updateProperty() failed. Property exo:content not found in "+node.getNode().getPath());
178
// log.debug("EXO:Content --" + prop + "= " + prop.getString());
179
byte[] buffer = prop.getString().getBytes();
180       fos.write(buffer);
181       fos.close();
182 // log.debug("FileNodeContainer.updateContent(): EXO:Content length =" + storage.length());
183
} catch (Exception JavaDoc e) {
184         e.printStackTrace();
185         throw new RepositoryException("FileNodeContainer.updateProperty() failed. Reason:"+e);
186     }
187   }
188
189 }
190
Popular Tags