improve bundle/unbundle api, by using ByteSource and ByteSink

This commit is contained in:
Sebastian Sdorra
2014-11-02 10:44:17 +01:00
parent 514e82498a
commit 75c244fcdb
12 changed files with 326 additions and 161 deletions

View File

@@ -35,6 +35,11 @@ package sonia.scm.repository.api;
//~--- non-JDK imports --------------------------------------------------------
import com.google.common.io.ByteSink;
import com.google.common.io.ByteStreams;
import com.google.common.io.Files;
import com.google.common.io.OutputSupplier;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -43,14 +48,17 @@ import sonia.scm.repository.RepositoryException;
import sonia.scm.repository.spi.BundleCommand;
import sonia.scm.repository.spi.BundleCommandRequest;
import static com.google.common.base.Preconditions.*;
//~--- JDK imports ------------------------------------------------------------
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
/**
*
* @author Sebastian Sdorra <sebastian.sdorra@triology.de>
* @author Sebastian Sdorra <s.sdorra@gmail.com>
* @since 1.43
*/
public final class BundleCommandBuilder
@@ -92,15 +100,81 @@ public final class BundleCommandBuilder
public BundleResponse bundle(File outputFile)
throws IOException, RepositoryException
{
BundleCommandRequest request = new BundleCommandRequest();
checkArgument((outputFile != null) &&!outputFile.exists(),
"file is null or exists already");
BundleCommandRequest request =
new BundleCommandRequest(Files.asByteSink(outputFile));
request.setArchive(outputFile);
logger.info("create bundle at {} for repository {}", outputFile,
repository.getId());
return bundleCommand.bundle(request);
}
/**
* Method description
*
*
* @param outputStream
*
* @return
*
* @throws IOException
* @throws RepositoryException
*/
public BundleResponse bundle(OutputStream outputStream)
throws IOException, RepositoryException
{
checkNotNull(outputStream, "output stream is required");
logger.info("bundle {} to output stream", repository.getId());
return bundleCommand.bundle(
new BundleCommandRequest(asByteSink(outputStream)));
}
/**
* Method description
*
*
* @param sink
*
* @return
*
* @throws IOException
* @throws RepositoryException
*/
public BundleResponse bundle(ByteSink sink)
throws IOException, RepositoryException
{
checkNotNull(sink, "byte sink is required");
logger.info("bundle {} to byte sink");
return bundleCommand.bundle(new BundleCommandRequest(sink));
}
/**
* Method description
*
*
* @param outputStream
*
* @return
*/
private ByteSink asByteSink(final OutputStream outputStream)
{
return ByteStreams.asByteSink(new OutputSupplier<OutputStream>()
{
@Override
public OutputStream getOutput() throws IOException
{
return outputStream;
}
});
}
//~--- fields ---------------------------------------------------------------
/** Field description */

View File

@@ -35,7 +35,7 @@ package sonia.scm.repository.api;
/**
*
* @author Sebastian Sdorra <sebastian.sdorra@triology.de>
* @author Sebastian Sdorra <s.sdorra@gmail.com>
* @since 1.43
*/
public class BundleResponse {}

View File

@@ -35,6 +35,11 @@ package sonia.scm.repository.api;
//~--- non-JDK imports --------------------------------------------------------
import com.google.common.io.ByteSource;
import com.google.common.io.ByteStreams;
import com.google.common.io.Files;
import com.google.common.io.InputSupplier;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -43,14 +48,17 @@ import sonia.scm.repository.RepositoryException;
import sonia.scm.repository.spi.UnbundleCommand;
import sonia.scm.repository.spi.UnbundleCommandRequest;
import static com.google.common.base.Preconditions.*;
//~--- JDK imports ------------------------------------------------------------
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
/**
*
* @author Sebastian Sdorra <sebastian.sdorra@triology.de>
* @author Sebastian Sdorra <s.sdorra@gmail.com>
* @since 1.43
*/
public final class UnbundleCommandBuilder
@@ -92,15 +100,79 @@ public final class UnbundleCommandBuilder
public UnbundleResponse unbundle(File inputFile)
throws IOException, RepositoryException
{
UnbundleCommandRequest request = new UnbundleCommandRequest();
checkArgument((inputFile != null) && inputFile.exists(),
"existing file is required");
request.setArchive(inputFile);
UnbundleCommandRequest request =
new UnbundleCommandRequest(Files.asByteSource(inputFile));
logger.info("unbundle archive {} at {}", inputFile, repository.getId());
return unbundleCommand.unbundle(request);
}
/**
* Method description
*
*
* @param inputStream
*
* @return
*
* @throws IOException
* @throws RepositoryException
*/
public UnbundleResponse unbundle(InputStream inputStream)
throws IOException, RepositoryException
{
checkNotNull(inputStream, "input stream is required");
logger.info("unbundle archive from stream");
return unbundleCommand.unbundle(
new UnbundleCommandRequest(asByteSource(inputStream)));
}
/**
* Method description
*
*
* @param byteSource
*
* @return
*
* @throws IOException
* @throws RepositoryException
*/
public UnbundleResponse unbundle(ByteSource byteSource)
throws IOException, RepositoryException
{
checkNotNull(byteSource, "byte source is required");
logger.info("unbundle from byte source");
return unbundleCommand.unbundle(new UnbundleCommandRequest(byteSource));
}
/**
* Method description
*
*
* @param inputStream
*
* @return
*/
private ByteSource asByteSource(final InputStream inputStream)
{
return ByteStreams.asByteSource(new InputSupplier<InputStream>()
{
@Override
public InputStream getInput() throws IOException
{
return inputStream;
}
});
}
//~--- fields ---------------------------------------------------------------
/** Field description */

View File

@@ -35,7 +35,7 @@ package sonia.scm.repository.api;
/**
*
* @author Sebastian Sdorra <sebastian.sdorra@triology.de>
* @author Sebastian Sdorra <s.sdorra@gmail.com>
* @since 1.43
*/
public class UnbundleResponse {}

View File

@@ -1,136 +0,0 @@
/**
* Copyright (c) 2010, Sebastian Sdorra
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3. Neither the name of SCM-Manager; nor the names of its
* contributors may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* http://bitbucket.org/sdorra/scm-manager
*
*/
package sonia.scm.repository.spi;
//~--- non-JDK imports --------------------------------------------------------
import com.google.common.base.Objects;
//~--- JDK imports ------------------------------------------------------------
import java.io.File;
/**
*
* @author Sebastian Sdorra <sebastian.sdorra@triology.de>
* @since 1.43
*/
public abstract class ArchiveCommandRequest
{
/**
* Method description
*
*
* @param obj
*
* @return
*/
@Override
public boolean equals(Object obj)
{
if (obj == null)
{
return false;
}
if (getClass() != obj.getClass())
{
return false;
}
final ArchiveCommandRequest other = (ArchiveCommandRequest) obj;
return Objects.equal(archive, other.archive);
}
/**
* Method description
*
*
* @return
*/
@Override
public int hashCode()
{
return Objects.hashCode(archive);
}
/**
* Method description
*
*
* @return
*/
@Override
public String toString()
{
//J-
return Objects.toStringHelper(this)
.add("archive", archive)
.toString();
//J+
}
//~--- get methods ----------------------------------------------------------
/**
* Method description
*
*
* @return
*/
public File getArchive()
{
return archive;
}
//~--- set methods ----------------------------------------------------------
/**
* Method description
*
*
* @param archive
*/
public void setArchive(File archive)
{
this.archive = archive;
}
//~--- fields ---------------------------------------------------------------
/** Field description */
private File archive;
}

View File

@@ -44,7 +44,7 @@ import java.io.IOException;
/**
*
* @author Sebastian Sdorra <sebastian.sdorra@triology.de>
* @author Sebastian Sdorra <s.sdorra@gmail.com>
* @since 1.43
*/
public interface BundleCommand

View File

@@ -33,9 +33,85 @@
package sonia.scm.repository.spi;
//~--- non-JDK imports --------------------------------------------------------
import com.google.common.base.Objects;
import com.google.common.io.ByteSink;
/**
*
* @author Sebastian Sdorra <sebastian.sdorra@triology.de>
* @author Sebastian Sdorra <s.sdorra@gmail.com>
* @since 1.43
*/
public class BundleCommandRequest extends ArchiveCommandRequest {}
public final class BundleCommandRequest
{
/**
* Constructs ...
*
*
* @param archive
*/
public BundleCommandRequest(ByteSink archive)
{
this.archive = archive;
}
//~--- methods --------------------------------------------------------------
/**
* Method description
*
*
* @param obj
*
* @return
*/
@Override
public boolean equals(Object obj)
{
if (obj == null)
{
return false;
}
if (getClass() != obj.getClass())
{
return false;
}
final BundleCommandRequest other = (BundleCommandRequest) obj;
return Objects.equal(archive, other.archive);
}
/**
* Method description
*
*
* @return
*/
@Override
public int hashCode()
{
return Objects.hashCode(archive);
}
//~--- get methods ----------------------------------------------------------
/**
* Method description
*
*
* @return
*/
ByteSink getArchive()
{
return archive;
}
//~--- fields ---------------------------------------------------------------
/** Field description */
private final ByteSink archive;
}

View File

@@ -44,7 +44,7 @@ import java.io.IOException;
/**
*
* @author Sebastian Sdorra <sebastian.sdorra@triology.de>
* @author Sebastian Sdorra <s.sdorra@gmail.com>
* @since 1.43
*/
public interface UnbundleCommand

View File

@@ -33,9 +33,85 @@
package sonia.scm.repository.spi;
//~--- non-JDK imports --------------------------------------------------------
import com.google.common.base.Objects;
import com.google.common.io.ByteSource;
/**
*
* @author Sebastian Sdorra <sebastian.sdorra@triology.de>
* @author Sebastian Sdorra <s.sdorra@gmail.com>
* @since 1.43
*/
public class UnbundleCommandRequest extends ArchiveCommandRequest {}
public final class UnbundleCommandRequest
{
/**
* Constructs ...
*
*
* @param archive
*/
public UnbundleCommandRequest(ByteSource archive)
{
this.archive = archive;
}
//~--- methods --------------------------------------------------------------
/**
* Method description
*
*
* @param obj
*
* @return
*/
@Override
public boolean equals(Object obj)
{
if (obj == null)
{
return false;
}
if (getClass() != obj.getClass())
{
return false;
}
final UnbundleCommandRequest other = (UnbundleCommandRequest) obj;
return Objects.equal(archive, other.archive);
}
/**
* Method description
*
*
* @return
*/
@Override
public int hashCode()
{
return Objects.hashCode(archive);
}
//~--- get methods ----------------------------------------------------------
/**
* Method description
*
*
* @return
*/
ByteSource getArchive()
{
return archive;
}
//~--- fields ---------------------------------------------------------------
/** Field description */
private final ByteSource archive;
}