Java tutorial
/* * ==================================================================== * This file is part of the ebXML Registry by Icar Cnr v3.2 * ("eRICv32" in the following disclaimer). * * "eRICv32" is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * "eRICv32" is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License Version 3 * along with "eRICv32". If not, see <http://www.gnu.org/licenses/>. * * eRICv32 is a forked, derivative work, based on: * - freebXML Registry, a royalty-free, open source implementation of the ebXML Registry standard, * which was published under the "freebxml License, Version 1.1"; * - ebXML OMAR v3.2 Edition, published under the GNU GPL v3 by S. Krushe & P. Arwanitis. * * All derivative software changes and additions are made under * * Copyright (C) 2013 Ing. Antonio Messina <messina@pa.icar.cnr.it> * * This software consists of voluntary contributions made by many * individuals on behalf of the freebxml Software Foundation. For more * information on the freebxml Software Foundation, please see * "http://www.freebxml.org/". * * This product includes software developed by the Apache Software * Foundation (http://www.apache.org/). * * ==================================================================== */ package it.cnr.icar.eric.server.repository.filesystem; import it.cnr.icar.eric.common.RepositoryItem; import it.cnr.icar.eric.common.RepositoryItemImpl; import it.cnr.icar.eric.common.Utility; import it.cnr.icar.eric.server.common.RegistryProperties; import it.cnr.icar.eric.server.common.ServerRequestContext; import it.cnr.icar.eric.server.repository.AbstractRepositoryManager; import it.cnr.icar.eric.server.repository.RepositoryItemKey; import it.cnr.icar.eric.server.repository.RepositoryManager; import it.cnr.icar.eric.server.util.ServerResourceBundle; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.util.Iterator; import java.util.List; import javax.activation.DataHandler; import javax.activation.FileDataSource; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerFactory; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamResult; import javax.xml.transform.stream.StreamSource; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import javax.xml.registry.RegistryException; import org.w3c.dom.Element; /** * FileSystem-based Repository Manager to control inserting, updating * and deleting repository items. * * @author Adrian Chong * @author Peter Burgess * * @version $Version: $ [$Date: 2006/08/24 20:42:34 $] */ public class FileSystemRepositoryManager extends AbstractRepositoryManager { /** Singleton instance of RepositoryManager */ private static RepositoryManager instance = null; private static final Log log = LogFactory.getLog(FileSystemRepositoryManager.class); private String repositoryRoot; protected FileSystemRepositoryManager() { repositoryRoot = RegistryProperties.getInstance().getProperty("eric.repository.home"); } /** * Gets the path for a RepositoryItem given its id. */ private String getRepositoryItemPath(String id) throws RegistryException { //Strip urn:uuid since that is not part of file name id = Utility.getInstance().stripId(id); String repositoryItemPath = repositoryRoot + "/" + id; return repositoryItemPath; } /** * Gets the RepositoryItem as a stream of XML markup given its id. * * @param id Unique id for repository item * @return a <code>StreamSource</code> value * @exception RegistryException if an error occurs */ public StreamSource getAsStreamSource(String id) throws RegistryException { StreamSource riFileSrc = null; try { File riFile = new File(getRepositoryItemPath(id)); riFileSrc = new StreamSource(new FileInputStream(riFile)); } catch (Exception e) { throw new RegistryException(e); } return riFileSrc; } /** * Singleton instance accessor. */ public synchronized static RepositoryManager getInstance() { if (instance == null) { instance = new FileSystemRepositoryManager(); } return instance; } /** * Insert the repository item. * @param fileName It should be the UUID. It will remove "urn:uuid:". * @param item The repository item. */ public void insert(ServerRequestContext context, RepositoryItem item) throws RegistryException { try { String id = item.getId(); // Strip off the "urn:uuid:" id = Utility.getInstance().stripId(id); String itemPath = getRepositoryItemPath(id); log.debug("itemPath = " + itemPath); File itemFile = new File(itemPath); if (itemFile.exists()) { String errmsg = ServerResourceBundle.getInstance() .getString("message.RepositoryItemWithIdAlreadyExists", new Object[] { item.getId() }); log.error(errmsg); throw new RegistryException(errmsg); } //Writing out the RepositoryItem itself FileOutputStream fos = new FileOutputStream(itemPath); item.getDataHandler().writeTo(fos); fos.flush(); fos.close(); // Writing out the ri 's signature // if it exists. Element sigElement = item.getSignatureElement(); File itemSig = new File(getRepositoryItemPath(id) + ".sig"); if (itemSig.exists()) { String errmsg = ServerResourceBundle.getInstance() .getString("message.PayloadSignatureAlreadyExists", new Object[] { item.getId() }); log.error(errmsg); throw new RegistryException(errmsg); } FileOutputStream sigFos = new FileOutputStream(itemSig); TransformerFactory tf = TransformerFactory.newInstance(); Transformer trans = tf.newTransformer(); trans.transform(new DOMSource(sigElement), new StreamResult(sigFos)); sigFos.flush(); sigFos.close(); } catch (Exception e) { log.error(e); throw new RegistryException(e); } } /** * Returns the RepositoryItem with the given unique ID. * * @param id Unique id for repository item * @return RepositoryItem instance * @exception RegistryException */ @SuppressWarnings({ "unused", "deprecation" }) public RepositoryItem getRepositoryItem(String id) throws RegistryException { RepositoryItem repositoryItem = null; String origId = id; // Strip off the "urn:uuid:" id = Utility.getInstance().stripId(id); try { String path = getRepositoryItemPath(id); File riFile = new File(path); if (!riFile.exists()) { String errmsg = ServerResourceBundle.getInstance().getString("message.RepositoryItemDoesNotExist", new Object[] { id }); log.error(errmsg); throw new RegistryException(errmsg); } DataHandler contentDataHandler = new DataHandler(new FileDataSource(riFile)); Element sigElement = null; path += ".sig"; File sigFile = new File(path); if (!sigFile.exists()) { String errmsg = "Payload signature for repository item id=\"" + id + "\" does not exist!"; //log.error(errmsg); throw new RegistryException(errmsg); } else { javax.xml.parsers.DocumentBuilderFactory dbf = javax.xml.parsers.DocumentBuilderFactory .newInstance(); dbf.setNamespaceAware(true); javax.xml.parsers.DocumentBuilder db = dbf.newDocumentBuilder(); org.w3c.dom.Document sigDoc = db.parse(new FileInputStream(sigFile)); repositoryItem = new RepositoryItemImpl(id, sigDoc.getDocumentElement(), contentDataHandler); } } catch (RegistryException e) { throw e; } catch (Exception e) { throw new RegistryException(e); } return repositoryItem; } /** * Delete the repository item. * @param id It should be the UUID. It will remove "urn:uuid:". * @throws RegistryException if the item does not exist */ public void delete(String id) throws RegistryException { // Strip off the "urn:uuid:" id = Utility.getInstance().stripId(id); String path = getRepositoryItemPath(id); log.debug("path = " + path); //RepositoryItem file File riFile = new File(path); //RepositoryItem XML signature file File riSigFile = new File(path + ".sig"); if (!riFile.exists()) { String errmsg = ServerResourceBundle.getInstance().getString("message.RepositoryItemDoesNotExist", new Object[] { id }); log.error(errmsg); throw new RegistryException(errmsg); } boolean deletedOK = riFile.delete(); if (riSigFile.exists()) { deletedOK = riSigFile.delete(); if (!deletedOK) { //log error String errmsg = ServerResourceBundle.getInstance().getString("message.CannotDeletePayloadSignature", new Object[] { id }); log.error(errmsg); } } if (deletedOK) { String msg = "deleted OK"; log.debug(msg); } else { String msg = ServerResourceBundle.getInstance().getString("message.deletedNOK"); log.error(msg); } } /** * Return a List of non-existent repository items * @param ids The List of repository items id. It will remove "urn:uuid:". */ public List<String> itemsExist(List<String> ids) throws RegistryException { List<String> notExistIds = new java.util.ArrayList<String>(); Iterator<?> iter = ids.iterator(); while (iter.hasNext()) { String id = (String) iter.next(); // Strip off the "urn:uuid:" id = Utility.getInstance().stripId(id); File f = new File(getRepositoryItemPath(id)); if (!f.isFile()) { notExistIds.add("urn:uuid:" + id); } } return notExistIds; } /** * Determines if RepositoryItem exists for specified key. * * @return true if a RepositoryItem exists for specified key, false otherwise * @param key The RepositoryItemKey. */ public boolean itemExists(RepositoryItemKey key) throws RegistryException { throw new RegistryException(ServerResourceBundle.getInstance().getString("message.unimplemented")); } /** * Get the size of a repository item in bytes. * @param itemId The id of repository item, with "urn:uuid:". * @return 0 if the file does not exist. */ public long getItemSize(String itemId) throws RegistryException { File f = new File(getRepositoryItemPath(itemId)); return f.length(); } public void delete(it.cnr.icar.eric.server.repository.RepositoryItemKey key) throws RegistryException { throw new RegistryException(ServerResourceBundle.getInstance().getString("message.unimplemented")); } public RepositoryItem getRepositoryItem(it.cnr.icar.eric.server.repository.RepositoryItemKey key) throws RegistryException { throw new RegistryException(ServerResourceBundle.getInstance().getString("message.unimplemented")); } }