KickJava   Java API By Example, From Geeks To Geeks.

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


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

6
7 package org.exoplatform.services.jcr.impl.storage.filesystem;
8
9
10 import org.apache.commons.logging.Log;
11
12 import java.io.File JavaDoc;
13
14
15 import java.util.List JavaDoc;
16 import javax.jcr.query.QueryResult;
17 import javax.jcr.query.Query;
18 import java.util.ArrayList JavaDoc;
19
20
21 import java.io.IOException JavaDoc;
22
23
24 import javax.jcr.*;
25
26 import org.apache.commons.codec.binary.Base64;
27 import org.exoplatform.services.jcr.core.NodeChange;
28 import org.exoplatform.services.jcr.core.NodeData;
29 import org.exoplatform.services.jcr.impl.core.NodeImpl;
30 import org.exoplatform.services.jcr.impl.core.PropertyImpl;
31 import org.exoplatform.services.jcr.impl.core.nodetype.NodeTypeManagerImpl;
32 import org.exoplatform.services.jcr.impl.storage.filesystem.nodedata.NodeContainer;
33 import org.exoplatform.services.jcr.impl.storage.filesystem.nodedata.NodeContainerResolver;
34 import org.exoplatform.services.jcr.storage.Container;
35 import org.exoplatform.services.jcr.storage.WorkspaceContainer;
36 import org.exoplatform.services.log.LogUtil;
37
38
39 /**
40  * Created by The eXo Platform SARL .
41  * 1. Dir is node of name=folder_name, path=relative path
42  * type nt:folder lastModified=created=File.lastModified() jcr:UUID=canonical_file_path
43  * 2. File is node of name=file_name, path=relative_path
44  * jcr:primaryType=nt:file jcr:lastModified=jcr:created=File.lastModified()
45  * jcr:content=file_content jcr:UUID=canonical_file_path
46  * 3. File CAN contain node of other type if:
47  * - it is xml file and has extension .xml and xml def (starts with <?xml....
48  * - it has format of serialized node ( <sv:node ...) Othervice content of file is jcr:content Note:
49  * ???? by default (2) file extension is ignored ????
50  *
51  * @author <a HREF="mailto:geaz@users.sourceforge.net">Gennady Azarenkov</a>
52  * @version $Id: SimpleFsContainerImpl.java,v 1.23 2004/11/02 18:34:39 geaz Exp $
53  */

54 public class SimpleFsContainerImpl implements WorkspaceContainer {
55
56   private static long id = System.currentTimeMillis();
57   private static NodeContainerResolver resolver = new NodeContainerResolver();
58   private Log log;
59   private File JavaDoc rootFile;
60   private NodeImpl rootNode;
61   private String JavaDoc name;
62
63
64   public SimpleFsContainerImpl(String JavaDoc name, String JavaDoc rootPath) throws RepositoryException {
65     log = LogUtil.getLog("org.exoplatform.services.jcr");
66     rootFile = new File JavaDoc(rootPath);
67     if (!rootFile.exists())
68       rootFile.mkdirs();
69
70     String JavaDoc rootType = "nt:folder";
71     if (!rootFile.isDirectory())
72       rootType = "nt:file";
73     if (!rootFile.exists())
74       throw new RepositoryException("SimpleFsContainerImpl could not be created. File or folder '" + rootFile + "' not found.");
75     ArrayList JavaDoc props = new ArrayList JavaDoc();
76     props.add(new PropertyImpl("/jcr:primaryType", new StringValue(rootType), PropertyType.STRING));
77     rootNode = new NodeImpl(ROOT_PATH, props);
78     //log.debug("FS ContainerImpl() Root "+rootNode.getPrimaryNodeType());
79
}
80
81
82    public String JavaDoc getName() {
83     return name;
84   }
85
86   public File JavaDoc getRootFile() {
87     return rootFile;
88   }
89
90   public NodeData getRootNode() {
91     return rootNode;
92   }
93
94   public NodeData getNodeByPath(String JavaDoc absPath) {
95     try {
96       NodeContainer container = resolver.resolve(rootFile, absPath);
97
98 // System.out.println("Container ---"+container+" "+rootFile.getCanonicalPath()+" "+absPath);
99
return container.getNodeByPath(container.parseRelPath(absPath));
100     } catch (Exception JavaDoc e) {
101       log.debug("GetNodeByPath throws exception " + e.getMessage());
102       e.printStackTrace();
103       return null;
104     }
105   }
106 /*
107   public NodeData getNodeById(String UUID) {
108
109     log.debug("FS Container getByUUID path --- "+new String(Base64.decodeBase64(UUID.getBytes()))+"/jcr:content");
110     return getNodeByPath( new String(Base64.decodeBase64(UUID.getBytes()))+"/jcr:content");
111 // throw new RuntimeException("getNodeById() is not supported by the container implementation!");
112   }
113 */

114   public List JavaDoc getChildren(String JavaDoc path) {
115     try {
116       NodeContainer container = resolver.resolve(rootFile, path);
117       return container.getChildren(container.parseRelPath(path));
118     } catch (Exception JavaDoc e) {
119       log.debug("SimpleFSContainer.GetChildren() throws exception " + e.getMessage());
120       e.printStackTrace();
121       return null;
122     }
123   }
124
125   public QueryResult getQueryResult(Query query) {
126     return null;
127   }
128
129   synchronized public void add(Node node) throws ItemExistsException, RepositoryException {
130     try {
131       NodeContainer container = resolver.create(rootFile, (NodeData)node);
132       log.debug("SFS Container add -->" + container);
133       container.add(node);
134     } catch (IOException JavaDoc e) {
135       throw new RepositoryException("SimpleFsContainerImpl.add() failed. Reason:" + e);
136     } catch (PathNotFoundException e) {
137       throw new RepositoryException("SimpleFsContainerImpl.add() failed. Reason:" + e);
138     }
139   }
140
141   synchronized public void update(Node node) throws RepositoryException {
142     try {
143       NodeContainer container = resolver.resolve(rootFile, node.getPath());
144       container.update(node);
145     } catch (Exception JavaDoc e) {
146       log.debug("Update Node throws exception " + e.getMessage());
147     }
148   }
149
150   synchronized public void delete(String JavaDoc absPath) {
151     try {
152       NodeContainer container = resolver.resolve(rootFile, absPath);
153       log.debug("FS Container delete node:" + absPath);
154       container.delete(absPath);
155     } catch (Exception JavaDoc e) {
156       log.debug("DelNode throws exception " + e.getMessage());
157     }
158   }
159
160
161 }
162
Popular Tags