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.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 45 public class TxFileContentStore extends AbstractTxFileStoreService implements ContentStore { 46 47 protected static final String LOG_CHANNEL = "file-content-store"; 48 49 public NodeRevisionContent retrieveRevisionContent(Uri uri, NodeRevisionDescriptor revisionDescriptor) 50 throws ServiceAccessException, RevisionNotFoundException { 51 52 String revisionUri = uri.toString() + "_" + revisionDescriptor.getRevisionNumber(); 53 54 try { 55 Object txId = getActiveTxId(); 56 InputStream 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; } 72 } 73 } 74 75 public void createRevisionContent( 76 Uri uri, 77 NodeRevisionDescriptor revisionDescriptor, 78 NodeRevisionContent revisionContent) 79 throws ServiceAccessException, RevisionAlreadyExistException { 80 String 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 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 revisionUri = revisionUri = uri.toString() + "_" + revisionDescriptor.getRevisionNumber(); 103 104 OutputStream os = null; 105 InputStream 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 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 e) { 134 } 135 try { 136 if (is != null) { 137 is.close(); 138 } 139 } catch (IOException e) { 140 } 141 } 142 } 143 144 public void removeRevisionContent(Uri uri, NodeRevisionDescriptor revisionDescriptor) 145 throws ServiceAccessException { 146 String 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 toString() { 155 return "TxContentFileStore at " + storeDir + " working on " + workDir; 156 } 157 158 protected String getLogChannel() { 159 return LOG_CHANNEL; 160 } 161 } 162 | Popular Tags |