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