KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > outerj > daisy > books > store > impl > UserBookInstance


1 /*
2  * Copyright 2004 Outerthought bvba and Schaubroeck nv
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.outerj.daisy.books.store.impl;
17
18 import org.outerj.daisy.books.store.*;
19 import org.outerj.daisy.repository.Repository;
20 import org.apache.cocoon.matching.helpers.WildcardHelper;
21 import org.outerx.daisy.x10Bookstoremeta.ResourcePropertiesDocument;
22
23 import java.io.File JavaDoc;
24 import java.io.IOException JavaDoc;
25 import java.io.InputStream JavaDoc;
26 import java.io.OutputStream JavaDoc;
27 import java.net.URI JavaDoc;
28 import java.util.HashMap JavaDoc;
29
30 public class UserBookInstance implements BookInstance {
31     private CommonBookInstance delegate;
32     private Repository repository;
33     private boolean locked = false;
34
35     /**
36      * @param locked normally always false, except when a new (and locked) book instance was created
37      */

38     UserBookInstance(CommonBookInstance delegate, Repository repository, boolean locked) {
39         this.delegate = delegate;
40         this.repository = repository;
41         this.locked = locked;
42     }
43
44     File JavaDoc getDirectory() {
45         return delegate.getDirectory();
46     }
47
48     private boolean isLockedBySomeoneElse() {
49         if (!locked) {
50             return delegate.isLocked();
51         } else {
52             return false;
53         }
54     }
55
56     private boolean isLockedByMe() {
57         return locked;
58     }
59
60     private AclResult checkCanRead() {
61         if (isLockedByMe())
62             return new AclResult(true, true);
63         if (isLockedBySomeoneElse())
64             throw new BookStoreException("This book instance is current locked for updating. Try again later.");
65         AclResult aclResult = BookAclEvaluator.evaluate(delegate.getAcl(), repository.getUserId(), repository.getActiveRoleIds());
66         if (!aclResult.canRead())
67             throw new BookStoreAccessDeniedException();
68         return aclResult;
69     }
70
71     private void checkCanRead(String JavaDoc path) {
72         AclResult result = checkCanRead();
73         if (!result.canManage()) {
74             // If someone only has read access but not write access, we do not allow access
75
// to all resources in the book instance.
76
if (path.charAt(0) != '/')
77                 path = "/" + path;
78             boolean ok = false;
79             for (int i = 0; i < PUBLIC_RESOURCES_PATTERNS.length; i++) {
80                 HashMap JavaDoc matchMap = new HashMap JavaDoc();
81                 if (WildcardHelper.match(matchMap, path, PUBLIC_RESOURCES_PATTERNS[i])) {
82                     ok = true;
83                     break;
84                 }
85             }
86             if (!ok)
87                 throw new BookStoreAccessDeniedException("Access denied to " + path);
88         }
89     }
90
91     private void checkCanWrite() {
92         if (!isLockedByMe())
93             throw new BookStoreException("A lock is required to update a book instance.");
94         AclResult aclResult = BookAclEvaluator.evaluate(delegate.getAcl(), repository.getUserId(), repository.getActiveRoleIds());
95         if (!aclResult.canRead())
96             throw new BookStoreAccessDeniedException();
97     }
98
99     public void lock() {
100         if (isLockedByMe() || isLockedBySomeoneElse()) {
101             throw new BookStoreException("This book instance is alredy locked.");
102         }
103
104         AclResult aclResult = BookAclEvaluator.evaluate(delegate.getAcl(), repository.getUserId(), repository.getActiveRoleIds());
105         if (!aclResult.canManage())
106             throw new BookStoreAccessDeniedException("You are not allowed to take a lock on the book instance \"" + getName() + "\".");
107
108         File JavaDoc lockFile = new File JavaDoc(delegate.getDirectory(), CommonBookInstance.LOCK_FILE_NAME);
109         try {
110             if (lockFile.createNewFile()) {
111                 locked = true;
112             } else {
113                 throw new BookStoreException("This book instance is already locked.");
114             }
115         } catch (IOException JavaDoc e) {
116             throw new BookStoreException("IO Exception while trying to take a lock on a book instance.", e);
117         }
118     }
119
120     public void unlock() {
121         if (!isLockedByMe()) {
122             throw new BookStoreException("You do not have a lock on this book instance.");
123         }
124
125         if (locked) {
126             File JavaDoc lockFile = new File JavaDoc(delegate.getDirectory(), CommonBookInstance.LOCK_FILE_NAME);
127             boolean success = lockFile.delete();
128             if (!success)
129                 throw new BookStoreException("Could not remove lock on book instance.");
130             locked = false;
131         }
132     }
133
134     public String JavaDoc getName() {
135         return delegate.getName();
136     }
137
138     private static final int[][] PUBLIC_RESOURCES_PATTERNS = new int[][] {
139             WildcardHelper.compilePattern("/data/resources/**"),
140             WildcardHelper.compilePattern("/publications/*/output/**")
141     };
142
143     public InputStream JavaDoc getResource(String JavaDoc path) {
144         if (path == null || path.length() == 0)
145             throw new IllegalArgumentException JavaDoc("path argument can not be null or empty");
146         checkCanRead(path);
147         return delegate.getResource(path);
148     }
149
150     public ResourcePropertiesDocument getResourceProperties(String JavaDoc path) {
151         if (path == null || path.length() == 0)
152             throw new IllegalArgumentException JavaDoc("path argument can not be null or empty");
153         checkCanRead(path);
154         return delegate.getResourceProperties(path);
155     }
156
157     public void storeResource(String JavaDoc path, InputStream JavaDoc is) {
158         checkCanWrite();
159         delegate.storeResource(path, is);
160     }
161
162     public void storeResourceProperties(String JavaDoc path, ResourcePropertiesDocument resourcePropertiesDocument) {
163         checkCanWrite();
164         delegate.storeResourceProperties(path, resourcePropertiesDocument);
165     }
166
167     public OutputStream JavaDoc getResourceOutputStream(String JavaDoc path) throws IOException JavaDoc {
168         checkCanWrite();
169         return delegate.getResourceOutputStream(path);
170     }
171
172     public boolean rename(String JavaDoc path, String JavaDoc newName) {
173         checkCanWrite();
174         return delegate.rename(path, newName);
175     }
176
177     public boolean exists(String JavaDoc path) {
178         checkCanRead(path);
179         return delegate.exists(path);
180     }
181
182     public long getLastModified(String JavaDoc path) {
183         checkCanRead(path);
184         return delegate.getLastModified(path);
185     }
186
187     public long getContentLength(String JavaDoc path) {
188         checkCanRead(path);
189         return delegate.getContentLength(path);
190     }
191
192     public BookAcl getAcl() {
193         checkCanRead();
194         return delegate.getAcl();
195     }
196
197     public void setAcl(BookAcl bookAcl) {
198         checkCanWrite();
199         delegate.setAcl(bookAcl);
200     }
201
202     public boolean canManage() {
203         AclResult aclResult = BookAclEvaluator.evaluate(delegate.getAcl(), repository.getUserId(), repository.getActiveRoleIds());
204         return aclResult.canManage();
205     }
206
207     public URI JavaDoc getResourceURI(String JavaDoc path) {
208         checkCanRead(path);
209         return delegate.getResourceURI(path);
210     }
211
212     public PublicationsInfo getPublicationsInfo() {
213         checkCanRead();
214         return delegate.getPublicationsInfo();
215     }
216
217     public void addPublication(PublicationInfo publicationInfo) {
218         checkCanWrite();
219         delegate.addPublication(publicationInfo);
220     }
221
222     public void setPublications(PublicationsInfo publicationsInfo) {
223         checkCanWrite();
224         delegate.setPublications(publicationsInfo);
225     }
226
227     public BookInstanceMetaData getMetaData() {
228         checkCanRead();
229         return delegate.getMetaData();
230     }
231
232     public void setMetaData(BookInstanceMetaData metaData) {
233         checkCanWrite();
234         delegate.setMetaData(metaData);
235     }
236
237     public String JavaDoc[] getDescendantPaths(String JavaDoc path) {
238         checkCanRead();
239         return delegate.getDescendantPaths(path);
240     }
241 }
242
Popular Tags