fixed bus in transaction managment
This commit is contained in:
@@ -51,48 +51,45 @@ public class PacmanServiceEJB implements PacmanService
|
||||
{
|
||||
Set<String> knownPkg = new HashSet<>();
|
||||
//Elimina i pacchetti sul DB che non esistono più nel filesystem
|
||||
List<PkgData> listaDB = em.createQuery("SELECT p FROM PkgData p", PkgData.class).getResultList();
|
||||
try
|
||||
{
|
||||
ut.setTransactionTimeout(1000);
|
||||
ut.begin();
|
||||
List<PkgData> listaDB = em.createQuery("SELECT p FROM PkgData p", PkgData.class).getResultList();
|
||||
for (PkgData p : listaDB)
|
||||
{
|
||||
knownPkg.add(p.filePath);
|
||||
File file = new File(p.filePath);
|
||||
if (!file.exists())
|
||||
{
|
||||
ut.begin();
|
||||
log.log(Level.INFO, String.format("Removing package %s", file.getName()));
|
||||
em.remove(p);
|
||||
}
|
||||
}
|
||||
ut.commit();
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (NotSupportedException | SystemException | HeuristicMixedException | RollbackException | HeuristicRollbackException e)
|
||||
} catch (NotSupportedException | SystemException | HeuristicMixedException | RollbackException | HeuristicRollbackException e)
|
||||
{
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
//Aggiunge sul DB i pacchetti presenti nel filesystem
|
||||
Collection<File> ls = FileUtils.listFiles(new File(ctx.getSystemProperties().getProperty("RepoFolder")), new RegexFileFilter(".*\\.pkg\\.tar\\.xz"), DirectoryFileFilter.DIRECTORY);
|
||||
|
||||
try
|
||||
{
|
||||
ut.begin();
|
||||
for (File file : ls)
|
||||
{
|
||||
if (!knownPkg.contains(file.getAbsolutePath()))
|
||||
{
|
||||
try
|
||||
{
|
||||
ut.setTransactionTimeout(1000);
|
||||
ut.begin();
|
||||
parseFile(file);
|
||||
}
|
||||
}
|
||||
ut.commit();
|
||||
} catch (Exception e)
|
||||
{
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Resource
|
||||
private UserTransaction ut;
|
||||
|
@@ -2,25 +2,18 @@ package service;
|
||||
|
||||
import model.PkgData;
|
||||
import model.PkgList;
|
||||
import org.apache.commons.io.IOUtils;
|
||||
import pacbase.Parser;
|
||||
import persistence.QueryEngine;
|
||||
|
||||
import javax.ejb.Singleton;
|
||||
import javax.inject.Inject;
|
||||
import javax.persistence.EntityManager;
|
||||
import javax.persistence.PersistenceContext;
|
||||
import javax.persistence.TypedQuery;
|
||||
import javax.persistence.*;
|
||||
import javax.ws.rs.*;
|
||||
import javax.ws.rs.core.Context;
|
||||
import javax.ws.rs.core.MediaType;
|
||||
import javax.ws.rs.core.Response;
|
||||
import javax.ws.rs.core.UriInfo;
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import javax.ws.rs.core.*;
|
||||
import java.io.*;
|
||||
import java.net.URI;
|
||||
import java.util.List;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
@Path("/pkg")
|
||||
@@ -90,25 +83,66 @@ public class PacmanWebService
|
||||
{
|
||||
TypedQuery<PkgData> hquery = em.createQuery(hashQuery, PkgData.class);
|
||||
if(md5sum != null) hquery.setParameter("md5sum", md5sum);
|
||||
return manageQueryResult(hquery.getResultList());
|
||||
return manageQueryResult(hquery.getResultList(), true);
|
||||
}
|
||||
|
||||
private Response getPackageByFileName(String file)
|
||||
{
|
||||
TypedQuery<PkgData> fnquery = em.createQuery(fileNameQuery, PkgData.class);
|
||||
fnquery.setParameter("fileName", file);
|
||||
return manageQueryResult(fnquery.getResultList());
|
||||
return manageQueryResult(fnquery.getResultList(), true);
|
||||
}
|
||||
|
||||
@GET
|
||||
@Path("download/{filename}")
|
||||
@Produces(MediaType.APPLICATION_OCTET_STREAM)
|
||||
public Response downloadPackage(@PathParam("filename") String fileName)
|
||||
{
|
||||
TypedQuery<PkgData> fnquery = em.createQuery(fileNameQuery, PkgData.class);
|
||||
fnquery.setParameter("fileName", fileName);
|
||||
try
|
||||
{
|
||||
PkgData pkg = fnquery.getSingleResult();
|
||||
StreamingOutput stream = (OutputStream output) ->
|
||||
{
|
||||
FileInputStream input = new FileInputStream(pkg.filePath);
|
||||
try {
|
||||
int bytes;
|
||||
while ((bytes = input.read()) != -1) {
|
||||
output.write(bytes);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
throw new WebApplicationException(e);
|
||||
} finally {
|
||||
if (output != null) output.close();
|
||||
if (input != null) input.close();
|
||||
}
|
||||
};
|
||||
return Response.ok(stream).header("Content-Length", new File(pkg.filePath).length()).build();
|
||||
}
|
||||
catch (NonUniqueResultException e)
|
||||
{
|
||||
throw new InternalServerErrorException();
|
||||
}
|
||||
catch (NoResultException e)
|
||||
{
|
||||
throw new NotFoundException();
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
@POST
|
||||
@Path("/pkg/upload")
|
||||
@Path("/upload")
|
||||
@Consumes(MediaType.APPLICATION_OCTET_STREAM)
|
||||
public Response createPackage(byte[] input, @MatrixParam("filename") String filename) throws Exception
|
||||
public Response createPackage(InputStream input, @MatrixParam("filename") String filename) throws Exception
|
||||
{
|
||||
if (filename == null)
|
||||
throw new BadRequestException();
|
||||
|
||||
String hash = Parser.computeMD5(new ByteArrayInputStream(input));
|
||||
String hash = Parser.computeMD5(input);
|
||||
TypedQuery<PkgData> hquery = em.createQuery(hashQuery, PkgData.class);
|
||||
hquery.setParameter("md5sum", hash);
|
||||
|
||||
@@ -120,9 +154,10 @@ public class PacmanWebService
|
||||
}
|
||||
else
|
||||
{
|
||||
input.reset();
|
||||
File file = new File("/tmp/sdf"/*ctx.getSystemProperties().getProperty("RepoFolder")*/, filename);
|
||||
FileOutputStream fos = new FileOutputStream(file);
|
||||
fos.write(input);
|
||||
IOUtils.copy(input,fos);
|
||||
fos.close();
|
||||
PkgData pkg = Parser.parseFile(file);
|
||||
em.persist(pkg);
|
||||
@@ -132,6 +167,11 @@ public class PacmanWebService
|
||||
}
|
||||
|
||||
private Response manageQueryResult(List<PkgData> list)
|
||||
{
|
||||
return manageQueryResult(list, false);
|
||||
}
|
||||
|
||||
private Response manageQueryResult(List<PkgData> list, boolean singleResult)
|
||||
{
|
||||
// log.log(Level.INFO, "size: " + list.size());
|
||||
PkgList pkgList = new PkgList(list);
|
||||
@@ -139,11 +179,18 @@ public class PacmanWebService
|
||||
{
|
||||
throw new NotFoundException();
|
||||
}
|
||||
else if(pkgList.size()==1)
|
||||
else if(singleResult)
|
||||
{
|
||||
if(pkgList.size()==1)
|
||||
{
|
||||
return Response.ok(pkgList.get(0)).build();
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new NonUniqueResultException("The returned list does not contain a single element");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
return Response.ok(pkgList).build();
|
||||
}
|
||||
|
@@ -58,7 +58,7 @@ public class ClientTest
|
||||
RegisterBuiltin.register(instance);
|
||||
instance.registerProvider(ResteasyJacksonProvider.class);
|
||||
Client client = ClientBuilder.newClient();
|
||||
UriBuilder builder = UriBuilder.fromUri("http://localhost:8080/").path("jpacrepo-1.0/rest/pkg/pkg/upload");
|
||||
UriBuilder builder = UriBuilder.fromUri("http://localhost:8080/").path("jpacrepo-1.0/rest/pkg/upload");
|
||||
builder.matrixParam("filename", "k290-fnkeyctl-1.2-1-x86_64.pkg.tar.xz");
|
||||
WebTarget target = client.target(builder.build());
|
||||
FileInputStream fis = new FileInputStream(new File("/tmp/k290-fnkeyctl-1.2-1-x86_64.pkg.tar.xz"));
|
||||
|
Reference in New Issue
Block a user