KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Header: /home/cvs/jakarta-slide/src/stores/org/apache/slide/store/txfile/XMLResourceDescriptor.java,v 1.15 2004/07/28 09:33:56 ib Exp $
3  * $Revision: 1.15 $
4  * $Date: 2004/07/28 09:33:56 $
5  *
6  * ====================================================================
7  *
8  * Copyright 1999-2004 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.FileResourceManager;
31 import org.apache.commons.transaction.file.ResourceManagerException;
32 import org.apache.commons.transaction.util.LoggerFacade;
33 import org.apache.slide.common.ServiceAccessException;
34 import org.apache.slide.common.Uri;
35 import org.apache.slide.structure.ObjectAlreadyExistsException;
36 import org.apache.slide.structure.ObjectNotFoundException;
37 import org.jdom.JDOMException;
38
39 /**
40  * XML descriptor as a resource in a {@link FileResourceManager}.
41  *
42  * @see FileResourceManager
43  * @see TxXMLFileDescriptorsStore
44  */

45 public class XMLResourceDescriptor extends AbstractXMLResourceDescriptor {
46
47     protected static final String JavaDoc PATH_EXTENSION = ".def.xml";
48     
49     protected final FileResourceManager rm;
50     protected final TxXMLFileDescriptorsStore store;
51     protected final String JavaDoc loadPath;
52     protected LoggerFacade logger;
53
54     /**
55      * Creates an XML descriptor resource.
56      *
57      * @param uri uri of the resource
58      * @param store store to use for error reporting
59      * @param rm resource manager to load / store this descriptor from / to
60      * @param txId identifier for the transaction in which the descriptor is to be managed
61      * @param characterEncoding charcter enconding used to store this descriptor in XML
62      * @throws ServiceAccessException if anything goes wrong at system level
63      */

64     public XMLResourceDescriptor(
65         Uri uri,
66         TxXMLFileDescriptorsStore store,
67         FileResourceManager rm,
68         Object JavaDoc txId,
69         String JavaDoc characterEncoding)
70         throws ServiceAccessException {
71
72         super(uri, txId, characterEncoding);
73
74         logger = rm.getLogger().createLogger(XMLResourceDescriptor.class.getName());
75
76         this.rm = rm;
77         this.store = store;
78
79         this.loadPath = uri.toString() + PATH_EXTENSION;
80     }
81
82     /**
83      * Stores this descriptor to the resource manager.
84      *
85      * @throws ServiceAccessException if anything goes wrong at system level
86      * @throws ObjectNotFoundException if the descriptor has not been created before
87      */

88     public void save() throws ServiceAccessException, ObjectNotFoundException {
89         if (txId == null) {
90             store.throwInternalError("Not inside tx");
91         }
92         logger.logFine("Tx " + txId + " saves data for " + loadPath);
93
94         OutputStream JavaDoc os = null;
95         try {
96             os = rm.writeResource(txId, loadPath);
97             save(os);
98             registeredForSaving = false;
99         } catch (ResourceManagerException e) {
100             if (e.getStatus() == ResourceManagerException.ERR_NO_SUCH_RESOURCE) {
101                 throw new ObjectNotFoundException(uri);
102             } else {
103                 store.throwInternalError(e, uri);
104             }
105         } catch (IOException JavaDoc e) {
106             store.throwInternalError(e);
107         } finally {
108             try {
109                 if (os != null) {
110                     os.close();
111                 }
112             } catch (IOException JavaDoc e) {
113             }
114         }
115     }
116
117     /**
118      * Creates this descriptor in the resource manager.
119      *
120      * @throws ServiceAccessException if anything goes wrong at system level
121      * @throws ObjectAlreadyExistsException if the descriptor already exists
122      */

123     public void create() throws ServiceAccessException, ObjectAlreadyExistsException {
124         logger.logFiner("Tx " + txId + " creates " + loadPath);
125         if (txId == null) {
126             store.throwInternalError("Not inside tx");
127         }
128         try {
129             rm.createResource(txId, loadPath, false);
130             init();
131         } catch (ResourceManagerException e) {
132             if (e.getStatus() == ResourceManagerException.ERR_RESOURCE_EXISTS) {
133                 throw new ObjectAlreadyExistsException(uri.toString());
134             } else {
135                 store.throwInternalError(e, uri);
136             }
137         }
138     }
139
140     /**
141      * Deletes this descriptor from the resource manager.
142      *
143      * @throws ServiceAccessException if anything goes wrong at system level
144      * @throws ObjectNotFoundException if the descriptor does not exist
145      */

146     public void delete() throws ServiceAccessException, ObjectNotFoundException {
147         logger.logFiner("Tx " + txId + " deletes " + loadPath);
148         if (txId == null) {
149             store.throwInternalError("Not inside tx");
150         }
151         try {
152             rm.deleteResource(txId, loadPath, false);
153             object = null;
154             registeredForSaving = false; // do not save what is no longer there
155
} catch (ResourceManagerException e) {
156             if (e.getStatus() == ResourceManagerException.ERR_NO_SUCH_RESOURCE) {
157                 throw new ObjectNotFoundException(uri.toString());
158             } else {
159                 store.throwInternalError(e, uri);
160             }
161         }
162     }
163
164     /**
165      * Loads this descriptor from the resource manager.
166      *
167      * @throws ServiceAccessException if anything goes wrong at system level
168      * @throws ObjectNotFoundException if the descriptor does not exist
169      */

170     public void load() throws ServiceAccessException, ObjectNotFoundException {
171         logger.logFiner("Tx " + txId + " loads data for " + loadPath);
172
173         InputStream JavaDoc is = null;
174         try {
175             if (txId != null) {
176                 if (rm.resourceExists(txId, loadPath)) {
177                     is = rm.readResource(txId, loadPath);
178                     load(is);
179                 } else {
180                     init();
181                 }
182             } else {
183                 logger.logFinest("Faking read access from outside tx for " + loadPath);
184                 if (rm.resourceExists(loadPath)) {
185                     is = rm.readResource(loadPath);
186                     load(is);
187                 } else {
188                     init();
189                 }
190             }
191         } catch (JDOMException je) {
192             store.throwInternalError(je, uri);
193         } catch (IOException JavaDoc ioe) {
194             store.throwInternalError(ioe, uri);
195         } catch (ResourceManagerException e) {
196             if (e.getStatus() == ResourceManagerException.ERR_NO_SUCH_RESOURCE) {
197                 throw new ObjectNotFoundException(uri);
198             } else {
199                 store.throwInternalError(e, uri);
200             }
201         } finally {
202             try {
203                 if (is != null) {
204                     is.close();
205                 }
206             } catch (IOException JavaDoc e) {
207             }
208         }
209     }
210
211 }
212
Popular Tags