KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > exoplatform > services > jcr > impl > storage > inmemory > ContainerImpl


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.inmemory;
7
8
9 import org.apache.commons.logging.Log;
10 import org.exoplatform.services.jcr.core.NodeData;
11 import org.exoplatform.services.jcr.impl.core.NodeImpl;
12 import org.exoplatform.services.jcr.impl.core.PropertyImpl;
13 import org.exoplatform.services.jcr.storage.WorkspaceContainer;
14 import org.exoplatform.services.jcr.util.PathUtil;
15 import org.exoplatform.services.log.LogUtil;
16
17 import javax.jcr.*;
18 import javax.jcr.query.Query;
19 import javax.jcr.query.QueryResult;
20 import java.util.*;
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: ContainerImpl.java,v 1.27 2004/09/16 15:26:54 geaz Exp $
28  */

29
30 public class ContainerImpl implements WorkspaceContainer {
31
32   private Log log;
33
34   private Map workspace;
35   private String JavaDoc name;
36
37   public ContainerImpl(String JavaDoc name) throws RepositoryException {
38     this(name, "nt:default");
39   }
40
41   public ContainerImpl(String JavaDoc name, String JavaDoc rootNodeType) throws RepositoryException {
42     log = LogUtil.getLog("org.exoplatform.services.jcr");
43     log.debug("inmemory ContainerImpl(), root " + rootNodeType);
44     this.name = name;
45     workspace = WorkspaceContainerRegistry.getInstance().getWorkspaceContainer(name, rootNodeType);
46
47     workspace.put(ROOT_PATH, new NodeImpl(ROOT_PATH, rootNodeType));
48
49   }
50
51   public String JavaDoc getName() {
52     return name;
53   }
54
55
56   public NodeData getRootNode() {
57
58     // Copy of permanent node
59
try {
60        return (NodeData) new NodeImpl( (NodeImpl) workspace.get(ROOT_PATH) );
61     } catch (Exception JavaDoc e) {
62        log.error("getRootNode() failed: "+ e.getMessage());
63        return null;
64     }
65
66   }
67
68
69   public NodeData getNodeByPath(String JavaDoc absPath) {
70
71     log.debug("Inmemory Container looks up node : " + absPath + " in workspace container: "+name);
72     Object JavaDoc obj = workspace.get(absPath);
73
74     if(obj == null)
75         return null;
76
77      // Copy of permanent node
78
try {
79         return new NodeImpl((NodeImpl) obj);
80      } catch (Exception JavaDoc e) {
81         log.error("getNodeByPath() failed: <" + absPath + "> " + e.getMessage());
82      }
83
84     return null;
85   }
86
87
88   public List getChildren(String JavaDoc path) {
89     Collection keys = workspace.keySet();
90     Iterator it = keys.iterator();
91     ArrayList children = new ArrayList();
92     while (it.hasNext()) {
93       String JavaDoc curPath = (String JavaDoc) it.next();
94       if (PathUtil.isDescendant(curPath, path, true)) {
95           children.add(curPath);
96       }
97     }
98     return children;
99   }
100
101   public QueryResult getQueryResult(Query query) {
102     return null;
103   }
104
105   synchronized public void add(Node node) throws RepositoryException {
106
107     if (node.getPath().equals("/"))
108       return;
109
110     if (workspace.get(node.getPath()) != null)
111       throw new ItemExistsException("Container.add(NodeImpl node) item <" + node.getPath() + "> already exists!");
112
113     NodeImpl newNode = new NodeImpl((NodeImpl) node);
114     workspace.put(newNode.getPath(), newNode);
115
116     log.debug("InmemoryContainer added " + node +" to workspace container: "+name);
117   }
118
119   synchronized public void delete(String JavaDoc absPath) {
120     workspace.remove(absPath);
121     log.debug("InmemoryContainer removed " + absPath);
122   }
123
124
125   synchronized public void update(Node node) throws RepositoryException {
126
127     if (workspace.get(node.getPath()) == null)
128       return;
129
130     NodeImpl newNode = new NodeImpl((NodeImpl) node);
131     workspace.put(node.getPath(), newNode);
132     log.debug("InmemoryContainer updated " + newNode);
133   }
134
135 }
136
Popular Tags