View Javadoc
1 /* 2 * ==================================================================== 3 * 4 * This code is subject to the freebxml License, Version 1.1 5 * 6 * Copyright (c) 2001 - 2003 freebxml.org. All rights reserved. 7 * 8 * ==================================================================== 9 */ 10 package com.sun.ebxml.registry.repository.filesystem; 11 12 import java.io.File; 13 import java.io.FileOutputStream; 14 import java.io.FileReader; 15 import java.util.ArrayList; 16 import java.util.Iterator; 17 18 import javax.activation.DataHandler; 19 import javax.activation.FileDataSource; 20 21 import org.apache.commons.logging.LogFactory; 22 import org.apache.xml.security.signature.XMLSignature; 23 import org.apache.xml.serialize.OutputFormat; 24 import org.apache.xml.serialize.XMLSerializer; 25 26 import com.sun.ebxml.registry.RegistryException; 27 import com.sun.ebxml.registry.repository.AbstractRepositoryManager; 28 import com.sun.ebxml.registry.repository.RepositoryItem; 29 import com.sun.ebxml.registry.repository.RepositoryManager; 30 import com.sun.ebxml.registry.util.RegistryProperties; 31 import com.sun.ebxml.registry.util.Utility; 32 33 /*** 34 * FileSystem based Repository Manager to control inserting, updating and deleting repository 35 * items. 36 * 37 * @author Adrian Chong 38 * @author Peter Burgess 39 * 40 * @version $Version: $ [$Date: 2003/03/18 07:56:08 $] 41 */ 42 public class FileSystemRepositoryManager extends AbstractRepositoryManager { 43 44 private org.apache.commons.logging.Log log = 45 LogFactory.getLog(this.getClass()); 46 47 private static RepositoryManager instance = null; 48 private String repositoryRoot; 49 50 protected FileSystemRepositoryManager() { 51 repositoryRoot = RegistryProperties.getInstance().getProperty("ebxmlrr.repository.home"); 52 } 53 54 /*** 55 * Gets the path for a RepositoryItem given its id. 56 */ 57 public String getRepositoryItemPath(String id) throws RegistryException 58 { 59 //Strip urn:uuid since that is not part of file name 60 id = Utility.getInstance().stripId(id); 61 String repositoryItemPath = repositoryRoot + "/" + id; 62 return repositoryItemPath; 63 } 64 65 /*** 66 * Singleton instance accessor. 67 */ 68 public static RepositoryManager getInstance() { 69 if(instance == null) { 70 synchronized(FileSystemRepositoryManager.class) { 71 if(instance == null) { 72 instance = new FileSystemRepositoryManager(); 73 } 74 } 75 } 76 return instance; 77 } 78 79 /*** 80 * Insert the repository item. 81 * @param fileName It should be the UUID. It will remove "urn:uuid:". 82 * @param item The repository item. 83 */ 84 public void insert(RepositoryItem item) throws 85 RegistryException { 86 87 try { 88 String id = item.getId(); 89 // Strip off the "urn:uuid:" 90 id = Utility.getInstance().stripId(id); 91 String itemPath = getRepositoryItemPath(id); 92 93 log.debug("itemPath = " + itemPath); 94 95 File itemFile = new File(itemPath); 96 97 if(itemFile.exists()) { 98 String errmsg = "Repository item with id " + item.getId() + " already exists."; 99 log.error(errmsg); 100 throw new RegistryException(errmsg); 101 } 102 103 //Writing out the RepositoryItem itself 104 FileOutputStream fos = new FileOutputStream(itemPath); 105 item.getDataHandler().writeTo(fos); 106 fos.flush(); 107 fos.close(); 108 109 // Writing out the ri 's signature 110 // if it exists. 111 XMLSignature sig = item.getSignature(); 112 if(sig != null) { 113 File itemSig = new File(getRepositoryItemPath(id) + ".sig"); 114 if(itemSig.exists()) { 115 String errmsg = "Payload signature for repository item with id " 116 + item.getId() + " already exists."; 117 log.error(errmsg); 118 throw new RegistryException(errmsg); 119 } 120 FileOutputStream sigFos = new FileOutputStream(itemSig); 121 OutputFormat format = new OutputFormat("xml", "UTF-8", false); 122 format.setPreserveSpace(true); 123 new XMLSerializer(sigFos, format).serialize(sig.getElement()); 124 sigFos.flush(); 125 sigFos.close(); 126 } 127 128 } catch(Exception e) { 129 log.error(e); 130 throw new RegistryException(e); 131 } 132 } 133 134 /*** 135 * Returns the RepositoryItem with the given unique ID. 136 * 137 * @param id Unique id for repository item 138 * @return RepositoryItem instance 139 * @exception RegistryException 140 */ 141 public RepositoryItem getRepositoryItem(String id) throws RegistryException{ 142 143 RepositoryItem repositoryItem = null; 144 String origId = id; 145 // Strip off the "urn:uuid:" 146 id = Utility.getInstance().stripId(id); 147 148 try { 149 String path = getRepositoryItemPath(id); 150 File riFile = new File(path); 151 152 if(!riFile.exists()) { 153 String errmsg = "Repository item id=\"" + 154 id + "\" does not exist!"; 155 log.error(errmsg); 156 throw new RegistryException(errmsg); 157 } 158 159 DataHandler contentDataHandler = 160 new DataHandler(new FileDataSource(riFile)); 161 162 String signatureString = null; 163 path += ".sig"; 164 File sigFile = new File(path); 165 if(!sigFile.exists()) { 166 String errmsg = 167 "Payload signature for repository item id=\"" + 168 id + "\" does not exist!"; 169 log.error(errmsg); 170 //throw new RegistryException(errmsg); 171 } else { 172 FileReader fr = new FileReader(sigFile); 173 int c = 0; 174 StringBuffer sigBuf = new StringBuffer(); 175 while((c = fr.read() ) != -1) { 176 sigBuf.append((char)c); 177 } 178 signatureString = sigBuf.toString(); 179 } 180 181 repositoryItem = 182 new RepositoryItem(id, signatureString, contentDataHandler); 183 184 } catch(RegistryException e) { 185 throw e; 186 } catch(Exception e) { 187 throw new RegistryException(e); 188 } 189 190 return repositoryItem; 191 192 } 193 194 /*** 195 * Delete the repository item. 196 * @param id It should be the UUID. It will remove "urn:uuid:". 197 * @throws RegistryException if the item does not exist 198 */ 199 public void delete(String id) throws RegistryException 200 { 201 // Strip off the "urn:uuid:" 202 id = Utility.getInstance().stripId(id); 203 String path = getRepositoryItemPath(id); 204 205 log.debug("path = " + path); 206 207 //RepositoryItem file 208 File riFile = new File(path); 209 //RepositoryItem XML signature file 210 File riSigFile = new File(path + ".sig"); 211 212 if(!riFile.exists()) { 213 String errmsg = "Repository item with id " + 214 id + " does not exist"; 215 log.error(errmsg); 216 throw new RegistryException(errmsg); 217 } 218 219 boolean deletedOK = riFile.delete(); 220 221 if(riSigFile.exists()) { 222 deletedOK = riSigFile.delete(); 223 if(!deletedOK) { 224 //log error 225 String errmsg = "Cannot delete Payload signature file for" + 226 " Repository item with id " + id ; 227 log.error(errmsg); 228 } 229 } 230 231 if(deletedOK) { 232 String msg = "deleted OK"; 233 log.debug(msg); 234 } else { 235 String msg = "deleted NOK"; 236 log.error(msg); 237 } 238 } 239 240 241 /*** 242 * Return a ArrayList of non-existent repository items 243 * @param ids The ArrayList of repository items id. It will remove "urn:uuid:". 244 */ 245 public ArrayList itemsExist(ArrayList ids) throws RegistryException { 246 ArrayList notExistIds = new ArrayList(); 247 Iterator iter = ids.iterator(); 248 while(iter.hasNext()) { 249 String id = (String)iter.next(); 250 // Strip off the "urn:uuid:" 251 id = Utility.getInstance().stripId(id); 252 File f = new File (getRepositoryItemPath(id)); 253 if(!f.exists()) { 254 notExistIds.add("urn:uuid:" + id); 255 } 256 } 257 return notExistIds; 258 } 259 260 /*** 261 * Get the size of a repository item in bytes. 262 * @param itemId The id of repository item, with "urn:uuid:". 263 * @return 0 if the file does not exist. 264 */ 265 public long getItemSize(String itemId) throws RegistryException { 266 File f = new File(getRepositoryItemPath(itemId)); 267 return f.length(); 268 } 269 270 }

This page was automatically generated by Maven