KickJava   Java API By Example, From Geeks To Geeks.

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


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 javax.jcr.PathNotFoundException;
10 import javax.jcr.ItemExistsException;
11
12 import java.util.*;
13
14 import org.apache.commons.logging.Log;
15 import org.apache.commons.codec.binary.Base64;
16 import org.exoplatform.services.jcr.core.NodeData;
17 import org.exoplatform.services.jcr.core.ReferenceableNodeLocation;
18 import org.exoplatform.services.jcr.impl.storage.BaseRepositoryManager;
19 import org.exoplatform.services.log.LogUtil;
20
21 /**
22  * Created by The eXo Platform SARL .
23  *
24  * @author <a HREF="mailto:geaz@users.sourceforge.net">Gennady Azarenkov</a>
25  * @version $Id: RepositoryManagerImpl.java,v 1.5 2004/11/02 18:34:39 geaz Exp $
26  */

27
28 public class RepositoryManagerImpl extends BaseRepositoryManager {
29
30   protected Log log;
31
32   private static long id = System.currentTimeMillis();
33
34   private Map workspaceContainers;
35   private Map referenceTables;
36
37   public RepositoryManagerImpl(String JavaDoc name) {
38     log = LogUtil.getLog("org.exoplatform.services.jcr");
39     workspaceContainers = new HashMap();
40     referenceTables = new HashMap();
41   }
42
43   synchronized public String JavaDoc generateUUID(NodeData context) {
44     return String.valueOf(++id);
45   }
46
47   synchronized public ReferenceableNodeLocation getLocationByUUID(String JavaDoc workspaceContainerName, String JavaDoc UUID) throws PathNotFoundException {
48
49     log.debug("RepositoryManagerImpl is finding location by UUID "+UUID);
50
51     Map container = getWorkspaceContainer(workspaceContainerName);
52     if(container == null)
53       throw new PathNotFoundException("Workspace container '"+workspaceContainerName+"' not found.");
54
55     ReferenceableNodeLocation loc = (ReferenceableNodeLocation)container.get(UUID);
56     log.debug("RepositoryManagerImpl found location by UUID "+UUID+" "+loc);
57     if (loc == null)
58         throw new PathNotFoundException("UUID '"+UUID+"' not found.");
59
60     return loc;
61
62   }
63
64   synchronized public ReferenceableNodeLocation getLocationByPath(String JavaDoc workspaceContainerName, String JavaDoc path) throws PathNotFoundException {
65
66     log.debug("RepositoryManagerImpl is finding location by path "+path);
67
68      Map refTable = getReferenceTable(workspaceContainerName);
69      if(refTable == null) {
70         refTable = new HashMap();
71      }
72
73     String JavaDoc uuid = (String JavaDoc) refTable.get(path);
74
75     if ( uuid == null)
76         throw new PathNotFoundException("Path '"+path+"' not found.");
77
78     return getLocationByUUID(workspaceContainerName, uuid);
79
80   }
81
82   synchronized public void addLocation(String JavaDoc workspaceContainerName, String JavaDoc UUID, String JavaDoc path, boolean isNew) throws PathNotFoundException, ItemExistsException {
83
84     Map container = getWorkspaceContainer(workspaceContainerName);
85     if(container == null) {
86         throw new PathNotFoundException("RepositoryManagerImpl.addLocation() Container "+workspaceContainerName+" not found");
87 // container = new HashMap();
88
// workspaceContainers.put(workspaceContainerName, container);
89
}
90
91     Map refTable = getReferenceTable(workspaceContainerName);
92     if(refTable == null) {
93         throw new PathNotFoundException("RepositoryManagerImpl.addLocation() Container "+workspaceContainerName+" not found");
94
95 // refTable = new HashMap();
96
// referenceTables.put(workspaceContainerName, refTable);
97
}
98
99     String JavaDoc realPath;
100     refTable.put(path, UUID);
101     if(isNew) {
102       container.put(UUID, new ReferenceableNodeLocation(path, UUID, path));
103     } else {
104       ReferenceableNodeLocation loc = (ReferenceableNodeLocation)container.get(UUID);
105       loc.addReferencedPath(path);
106     }
107
108     log.debug("RepositoryManagerImpl added reference "+path+" isNew= "+isNew+" to "+UUID+" size "+refTable.size());
109
110   }
111
112   synchronized public void deleteLocationByPath(String JavaDoc workspaceContainerName, String JavaDoc path) {
113
114     Map refTable = getReferenceTable(workspaceContainerName);
115     if(refTable == null)
116         return;
117
118     String JavaDoc uuid = (String JavaDoc)refTable.get(path);
119     if(uuid == null)
120         return;
121
122     log.debug("RepositoryManagerImpl is deleting location by path "+path);
123
124     deleteLocationByUUID(workspaceContainerName, uuid);
125
126   }
127
128   synchronized public void deleteLocationByUUID(String JavaDoc workspaceContainerName, String JavaDoc UUID) {
129
130     Map refTable = getReferenceTable(workspaceContainerName);
131     if(refTable == null)
132         return;
133
134     log.debug("RepositoryManagerImpl is deleting location by UUID "+UUID);
135
136     Iterator entries = refTable.keySet().iterator();
137     ArrayList paths = new ArrayList();
138     while (entries.hasNext()) {
139       String JavaDoc testPath = (String JavaDoc) entries.next();
140       String JavaDoc testUUID = (String JavaDoc) refTable.get(testPath);
141       if(testUUID.equals(UUID))
142         paths.add(testPath);
143 // refTable.remove(testPath);
144
}
145
146     for(int i=0; i<paths.size(); i++) {
147         log.debug("RepositoryManagerImpl deleted path "+paths.get(i));
148         refTable.remove(paths.get(i));
149     }
150
151     Map container = getWorkspaceContainer(workspaceContainerName);
152     if(container != null)
153         container.remove(UUID);
154
155   }
156
157   synchronized public void addWorkspaceContainer(String JavaDoc workspaceContainerName) {
158
159     Map container = getWorkspaceContainer(workspaceContainerName);
160     if(container == null) {
161       container = new HashMap();
162       workspaceContainers.put(workspaceContainerName, container);
163       log.debug("RepositoryManagerImpl.addWorkspaceContainer() Container "+workspaceContainerName+" added");
164     } else {
165       log.debug("RepositoryManagerImpl.addWorkspaceContainer() Container "+workspaceContainerName+" already exists");
166     }
167
168     Map refTable = getReferenceTable(workspaceContainerName);
169     if(refTable == null) {
170       refTable = new HashMap();
171       referenceTables.put(workspaceContainerName, refTable);
172       log.debug("RepositoryManagerImpl.addWorkspaceContainer() Ref table "+workspaceContainerName+" added");
173     } else {
174       log.debug("RepositoryManagerImpl.addWorkspaceContainer() Ref table "+workspaceContainerName+" already exists");
175     }
176
177   }
178
179
180   private Map getWorkspaceContainer(String JavaDoc workspaceContainerName) {
181     return (Map) workspaceContainers.get(workspaceContainerName);
182   }
183
184   private Map getReferenceTable(String JavaDoc workspaceContainerName) {
185     return (Map) referenceTables.get(workspaceContainerName);
186   }
187
188 }
189
Popular Tags