1 23 24 package org.apache.slide.store.txfile; 25 26 import java.io.IOException ; 27 import java.io.InputStream ; 28 import java.io.OutputStream ; 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 45 public class XMLResourceDescriptor extends AbstractXMLResourceDescriptor { 46 47 protected static final String PATH_EXTENSION = ".def.xml"; 48 49 protected final FileResourceManager rm; 50 protected final TxXMLFileDescriptorsStore store; 51 protected final String loadPath; 52 protected LoggerFacade logger; 53 54 64 public XMLResourceDescriptor( 65 Uri uri, 66 TxXMLFileDescriptorsStore store, 67 FileResourceManager rm, 68 Object txId, 69 String 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 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 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 e) { 106 store.throwInternalError(e); 107 } finally { 108 try { 109 if (os != null) { 110 os.close(); 111 } 112 } catch (IOException e) { 113 } 114 } 115 } 116 117 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 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; } 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 170 public void load() throws ServiceAccessException, ObjectNotFoundException { 171 logger.logFiner("Tx " + txId + " loads data for " + loadPath); 172 173 InputStream 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 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 e) { 207 } 208 } 209 } 210 211 } 212 | Popular Tags |