001package org.apache.archiva.repository.storage; 002 003/* 004 * Licensed to the Apache Software Foundation (ASF) under one 005 * or more contributor license agreements. See the NOTICE file 006 * distributed with this work for additional information 007 * regarding copyright ownership. The ASF licenses this file 008 * to you under the Apache License, Version 2.0 (the 009 * "License"); you may not use this file except in compliance 010 * with the License. You may obtain a copy of the License at 011 * 012 * http://www.apache.org/licenses/LICENSE-2.0 013 * 014 * Unless required by applicable law or agreed to in writing, 015 * software distributed under the License is distributed on an 016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 017 * KIND, either express or implied. See the License for the 018 * specific language governing permissions and limitations 019 * under the License. 020 */ 021 022import java.io.IOException; 023import java.io.InputStream; 024import java.io.OutputStream; 025import java.nio.channels.ReadableByteChannel; 026import java.nio.channels.WritableByteChannel; 027import java.nio.file.Path; 028import java.time.Instant; 029import java.util.List; 030 031/** 032 * A instance of this interface represents information about a specific asset in a repository. 033 * The asset may be an real artifact, a directory, or a virtual asset. 034 * 035 * Each asset has a unique path relative to the repository. 036 * 037 * The implementation may read the data directly from the filesystem or underlying storage implementation. 038 * 039 * @author Martin Stockhammer <martin_s@apache.org> 040 */ 041public interface StorageAsset 042{ 043 044 /** 045 * Returns the storage this asset belongs to. 046 * @return 047 */ 048 RepositoryStorage getStorage(); 049 050 /** 051 * Returns the complete path relative to the repository to the given asset. 052 * 053 * @return A path starting with '/' that uniquely identifies the asset in the repository. 054 */ 055 String getPath(); 056 057 /** 058 * Returns the name of the asset. It may be just the filename. 059 * @return 060 */ 061 String getName(); 062 063 /** 064 * Returns the time of the last modification. 065 * 066 * @return 067 */ 068 Instant getModificationTime(); 069 070 /** 071 * Returns true, if this asset is a container type and contains further child assets. 072 * @return 073 */ 074 boolean isContainer(); 075 076 /** 077 * List the child assets. 078 * 079 * @return The list of children. If there are no children and if the asset is not a container, a empty list will be returned. 080 */ 081 List<StorageAsset> list(); 082 083 /** 084 * The size in bytes of the asset. If the asset does not have a size, -1 should be returned. 085 * 086 * @return The size if the asset has a size, otherwise -1 087 */ 088 long getSize(); 089 090 /** 091 * Returns the input stream of the artifact content. 092 * It will throw a IOException, if the stream could not be created. 093 * Implementations should create a new stream instance for each invocation and make sure that the 094 * stream is proper closed after usage. 095 * 096 * @return The InputStream representing the content of the artifact. 097 * @throws IOException 098 */ 099 InputStream getReadStream() throws IOException; 100 101 /** 102 * Returns a NIO representation of the data. 103 * 104 * @return A channel to the asset data. 105 * @throws IOException 106 */ 107 ReadableByteChannel getReadChannel() throws IOException; 108 109 /** 110 * 111 * Returns an output stream where you can write data to the asset. The operation is not locked or synchronized. 112 * User of this method have to make sure, that the stream is proper closed after usage. 113 * 114 * @param replace If true, the original data will be replaced, otherwise the data will be appended. 115 * @return The OutputStream where the data can be written. 116 * @throws IOException 117 */ 118 OutputStream getWriteStream( boolean replace) throws IOException; 119 120 /** 121 * Returns a NIO representation of the asset where you can write the data. 122 * 123 * @param replace True, if the content should be replaced by the data written to the stream. 124 * @return The Channel for writing the data. 125 * @throws IOException 126 */ 127 WritableByteChannel getWriteChannel( boolean replace) throws IOException; 128 129 /** 130 * Replaces the content. The implementation may do an atomic move operation, or keep a backup. If 131 * the operation fails, the implementation should try to restore the old data, if possible. 132 * 133 * The original file may be deleted, if the storage was successful. 134 * 135 * @param newData Replaces the data by the content of the given file. 136 */ 137 boolean replaceDataFromFile( Path newData) throws IOException; 138 139 /** 140 * Returns true, if the asset exists. 141 * 142 * @return True, if the asset exists, otherwise false. 143 */ 144 boolean exists(); 145 146 /** 147 * Creates the asset in the underlying storage, if it does not exist. 148 */ 149 void create() throws IOException; 150 151 /** 152 * Returns the real path to the asset, if it exist. Not all implementations may implement this method. 153 * The method throws {@link UnsupportedOperationException}, if and only if {@link #isFileBased()} returns false. 154 * 155 * @return The filesystem path to the asset. 156 * @throws UnsupportedOperationException If the underlying storage is not file based. 157 */ 158 Path getFilePath() throws UnsupportedOperationException; 159 160 /** 161 * Returns true, if the asset can return a file path for the given asset. If this is true, the {@link #getFilePath()} 162 * will not throw a {@link UnsupportedOperationException} 163 * 164 * @return 165 */ 166 boolean isFileBased(); 167 168 /** 169 * Returns true, if there is a parent to this asset. 170 * @return 171 */ 172 boolean hasParent(); 173 174 /** 175 * Returns the parent of this asset. 176 * @return The asset, or <code>null</code>, if it does not exist. 177 */ 178 StorageAsset getParent(); 179 180 /** 181 * Returns the asset relative to the given path 182 * @param toPath 183 * @return 184 */ 185 StorageAsset resolve(String toPath); 186}