KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > slide > store > txfile > TxFileContentStore


1 /*
2  * $Header: /home/cvs/jakarta-slide/src/stores/org/apache/slide/store/txfile/TxFileContentStore.java,v 1.10 2004/07/28 09:33:57 ib Exp $
3  * $Revision: 1.10 $
4  * $Date: 2004/07/28 09:33:57 $
5  *
6  * ====================================================================
7  *
8  * Copyright 1999-2002 The Apache Software Foundation
9  *
10  * Licensed under the Apache License, Version 2.0 (the "License");
11  * you may not use this file except in compliance with the License.
12  * You may obtain a copy of the License at
13  *
14  * http://www.apache.org/licenses/LICENSE-2.0
15  *
16  * Unless required by applicable law or agreed to in writing, software
17  * distributed under the License is distributed on an "AS IS" BASIS,
18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  * See the License for the specific language governing permissions and
20  * limitations under the License.
21  *
22  */

23
24 package org.apache.slide.store.txfile;
25
26 import java.io.IOException JavaDoc;
27 import java.io.InputStream JavaDoc;
28 import java.io.OutputStream JavaDoc;
29
30 import org.apache.commons.transaction.file.ResourceManagerException;
31 import org.apache.slide.common.ServiceAccessException;
32 import org.apache.slide.common.Uri;
33 import org.apache.slide.content.NodeRevisionContent;
34 import org.apache.slide.content.NodeRevisionDescriptor;
35 import org.apache.slide.content.RevisionAlreadyExistException;
36 import org.apache.slide.content.RevisionNotFoundException;
37 import org.apache.slide.store.ContentStore;
38
39 import org.apache.slide.store.util.*;
40
41 /**
42  * Transactional content file store.
43  *
44  */

45 public class TxFileContentStore extends AbstractTxFileStoreService implements ContentStore {
46
47     protected static final String JavaDoc LOG_CHANNEL = "file-content-store";
48     
49     public NodeRevisionContent retrieveRevisionContent(Uri uri, NodeRevisionDescriptor revisionDescriptor)
50         throws ServiceAccessException, RevisionNotFoundException {
51
52         String JavaDoc revisionUri = uri.toString() + "_" + revisionDescriptor.getRevisionNumber();
53
54         try {
55             Object JavaDoc txId = getActiveTxId();
56             InputStream JavaDoc is;
57             if (txId != null) {
58                 is = rm.readResource(txId, revisionUri);
59             } else {
60                 is = rm.readResource(revisionUri);
61             }
62             NodeRevisionContent result = new NodeRevisionContent();
63             result.setContent(is);
64             return result;
65         } catch (ResourceManagerException e) {
66             if (e.getStatus() == ResourceManagerException.ERR_NO_SUCH_RESOURCE) {
67                 throw new RevisionNotFoundException(uri.toString(), revisionDescriptor.getRevisionNumber());
68             } else {
69                 throwInternalError(e, uri.toString());
70                 return null; // XXX fake (is never called)
71
}
72         }
73     }
74
75     public void createRevisionContent(
76         Uri uri,
77         NodeRevisionDescriptor revisionDescriptor,
78         NodeRevisionContent revisionContent)
79         throws ServiceAccessException, RevisionAlreadyExistException {
80         String JavaDoc revisionUri = revisionUri = uri.toString() + "_" + revisionDescriptor.getRevisionNumber();
81
82         try {
83             rm.createResource(getActiveTxId(), revisionUri, false);
84             storeRevisionContent(uri, revisionDescriptor, revisionContent);
85         } catch (RevisionNotFoundException e) {
86             // Can not be, as we just created it. If it unexpectedly is, this is fatal
87
throwInternalError(e, uri.toString());
88         } catch (ResourceManagerException e) {
89             if (e.getStatus() == ResourceManagerException.ERR_RESOURCE_EXISTS) {
90                 throw new RevisionAlreadyExistException(uri.toString(), revisionDescriptor.getRevisionNumber());
91             } else {
92                 throwInternalError(e, uri.toString());
93             }
94         }
95     }
96
97     public void storeRevisionContent(
98         Uri uri,
99         NodeRevisionDescriptor revisionDescriptor,
100         NodeRevisionContent revisionContent)
101         throws ServiceAccessException, RevisionNotFoundException {
102         String JavaDoc revisionUri = revisionUri = uri.toString() + "_" + revisionDescriptor.getRevisionNumber();
103
104         OutputStream JavaDoc os = null;
105         InputStream JavaDoc is = null;
106         try {
107             os = rm.writeResource(getActiveTxId(), revisionUri);
108             is = revisionContent.streamContent();
109
110             if (is != null) {
111                 long contentBytes = FileHelper.copy(is, os);
112                 long contentLength = revisionDescriptor.getContentLength();
113                 revisionDescriptor.setContentLength(contentBytes);
114
115                 if (contentLength != -1 && contentBytes != contentLength) {
116                     rm.deleteResource(getActiveTxId(), revisionUri);
117                     throwInternalError("Content length does not match expected");
118                 }
119             }
120         } catch (IOException JavaDoc e) {
121             throwInternalError(e, uri.toString());
122         } catch (ResourceManagerException e) {
123             if (e.getStatus() == ResourceManagerException.ERR_NO_SUCH_RESOURCE) {
124                 throw new RevisionNotFoundException(uri.toString(), revisionDescriptor.getRevisionNumber());
125             } else {
126                 throwInternalError(e, uri.toString());
127             }
128         } finally {
129             try {
130                 if (os != null) {
131                     os.close();
132                 }
133             } catch (IOException JavaDoc e) {
134             }
135             try {
136                 if (is != null) {
137                     is.close();
138                 }
139             } catch (IOException JavaDoc e) {
140             }
141         }
142     }
143
144     public void removeRevisionContent(Uri uri, NodeRevisionDescriptor revisionDescriptor)
145         throws ServiceAccessException {
146         String JavaDoc revisionUri = uri.toString() + "_" + revisionDescriptor.getRevisionNumber();
147         try {
148             rm.deleteResource(getActiveTxId(), revisionUri);
149         } catch (ResourceManagerException e) {
150             throwInternalError(e, uri.toString());
151         }
152     }
153
154     public String JavaDoc toString() {
155         return "TxContentFileStore at " + storeDir + " working on " + workDir;
156     }
157
158     protected String JavaDoc getLogChannel() {
159         return LOG_CHANNEL;
160     }
161 }
162
Popular Tags