java rewrite

This commit is contained in:
2022-06-03 23:59:59 +08:00
parent 76608d5713
commit b59dcfd93c
68 changed files with 2218 additions and 1769 deletions

View File

@@ -1,30 +1,39 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.module.jaxb.JaxbAnnotationModule;
import lombok.SneakyThrows;
import net.woggioni.jpacrepo.model.Hasher;
import net.woggioni.jpacrepo.model.MD5InputStream;
import net.woggioni.jpacrepo.model.Parser;
import net.woggioni.jpacrepo.pacbase.CompressionFormat;
import net.woggioni.jpacrepo.pacbase.PkgData;
import net.woggioni.jpacrepo.service.PacmanServiceRemote;
import net.woggioni.jpacrepo.api.model.PkgData;
import net.woggioni.jpacrepo.api.service.PacmanServiceRemote;
import net.woggioni.jpacrepo.impl.model.CompressionFormatImpl;
import net.woggioni.jpacrepo.impl.model.PkgDataImpl;
import net.woggioni.jwo.Con;
import net.woggioni.jwo.Fun;
import net.woggioni.jwo.Hash;
import net.woggioni.jwo.JWO;
import org.jboss.resteasy.plugins.providers.RegisterBuiltin;
import org.jboss.resteasy.spi.ResteasyProviderFactory;
import javax.naming.*;
import javax.ws.rs.client.*;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriBuilder;
import jakarta.ws.rs.client.*;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.Response;
import jakarta.ws.rs.core.UriBuilder;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.security.DigestInputStream;
import java.security.MessageDigest;
import java.util.Properties;
import java.util.stream.Stream;
public class ClientTest {
@@ -59,8 +68,7 @@ public class ClientTest {
}
}
public void testPUT() throws Exception
{
public void testPUT() throws Exception {
ResteasyProviderFactory instance = ResteasyProviderFactory.getInstance();
RegisterBuiltin.register(instance);
Client client = ClientBuilder.newClient();
@@ -77,60 +85,39 @@ public class ClientTest {
assert Response.Status.CREATED.getStatusCode() == response.getStatus();
}
public void hashTest() throws Exception
{
String[] files = new String[]{"/var/cache/pacman/pkg/mesa-10.4.5-1-x86_64.pkg.tar.xz", "/var/cache/pacman/pkg/mesa-10.5.3-1-x86_64.pkg.tar.xz"};
for (String file : files)
{
MessageDigest md = MessageDigest.getInstance("MD5");
try (InputStream is = Files.newInputStream(Paths.get(file)))
{
DigestInputStream dis = new DigestInputStream(is, md);
dis.on(true);
// is.read();
long a = new File(file).length();
byte[] out = new byte[(int)a];
dis.read(out, 0, (int) a);
@Test
public void hashTest(@TempDir Path testDir) {
ClassLoader cl = getClass().getClassLoader();
Stream.of("gvfs-nfs-1.50.2-1-x86_64.pkg.tar.zst")
.forEach((Con<String>) resourceName -> {
Path tmpFile = testDir.resolve(resourceName);
try(InputStream is = cl.getResourceAsStream(resourceName)) {
Files.copy(is, tmpFile);
}
System.out.println(Hasher.bytesToHex(md.digest()));
System.out.println(Hasher.computeMD5(new FileInputStream(file)));
InputStream fis = new FileInputStream(file);
MD5InputStream h = new MD5InputStream(fis);
long a = new File(file).length();
byte[] out = new byte[(int)a];
h.read(out, 0, (int) a);
System.out.println(h.digest());
Path path = Paths.get(file);
PkgData p = Parser.parseFile(path, CompressionFormat.guess(path));
System.out.println(p.md5sum());
}
Hash hash;
try(InputStream is = Files.newInputStream(tmpFile)) {
hash = Hash.md5(is);
}
PkgData p = PkgDataImpl.parseFile(tmpFile, CompressionFormatImpl.guess(tmpFile));
Assertions.assertEquals(JWO.bytesToHex(hash.getBytes()), p.getMd5sum());
});
}
private static void traverseJndiNode(String nodeName, Context context)
{
try
{
private static void traverseJndiNode(String nodeName, Context context) {
try {
NamingEnumeration<NameClassPair> list = context.list(nodeName);
while (list.hasMore())
{
while (list.hasMore()) {
String childName = nodeName + "" + list.next().getName();
System.out.println(childName);
traverseJndiNode(childName, context);
}
} catch (NamingException ex)
{
} catch (NamingException ex) {
// We reached a leaf
}
}
public void invokeStatelessBean() throws Exception
{
@Test
public void invokeStatelessBean() throws Exception {
Properties prop = new Properties();
InputStream in = getClass().getClassLoader().getResourceAsStream("jboss-ejb-client.properties");
prop.load(in);
@@ -140,10 +127,10 @@ public class ClientTest {
prop.put(Context.PROVIDER_URL, "http-remoting://localhost:8080");
// prop.put(Context.PROVIDER_URL, "http-remoting://nuc:8080");
// prop.put(Context.PROVIDER_URL, "remote://odroid-u3:4447");
prop.put(Context.SECURITY_PRINCIPAL, "walter");
prop.put(Context.SECURITY_CREDENTIALS, "27ff5990757d1d");
// prop.put(Context.SECURITY_PRINCIPAL, "admin");
// prop.put(Context.SECURITY_CREDENTIALS, "123456");
// prop.put(Context.SECURITY_PRINCIPAL, "walter");
// prop.put(Context.SECURITY_CREDENTIALS, "27ff5990757d1d");
prop.put(Context.SECURITY_PRINCIPAL, "luser");
prop.put(Context.SECURITY_CREDENTIALS, "123456");
prop.put("jboss.naming.client.ejb.context", true);
Context context = new InitialContext(prop);
@@ -151,7 +138,7 @@ public class ClientTest {
traverseJndiNode("/", context);
// final PacmanService stateService = (PacmanService) ctx.lookup("/jpacrepo-1.0/remote/PacmanServiceEJB!service.PacmanService");
final PacmanServiceRemote service = (PacmanServiceRemote) ctx.lookup(
"/jpacrepo_2.12-2.0/PacmanServiceEJB!net.woggioni.jpacrepo.service.PacmanServiceRemote"
"/jpacrepo-2.0-SNAPSHOT/PacmanServiceEJB!net.woggioni.jpacrepo.service.PacmanServiceRemote"
);
// List<PkgData> pkgs = service.searchPackage("google-earth", null, null, 1, 10);
// System.out.println(new XStream().toXML(pkgs));

View File

@@ -1,9 +1,9 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.module.jaxb.JaxbAnnotationModule;
import lombok.SneakyThrows;
import net.woggioni.jpacrepo.model.Parser;
import net.woggioni.jpacrepo.api.model.PkgData;
import net.woggioni.jpacrepo.impl.model.PkgDataImpl;
import net.woggioni.jpacrepo.pacbase.CompressionFormat;
import net.woggioni.jpacrepo.pacbase.PkgData;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
@@ -26,7 +26,7 @@ public class ParseTest {
Files.list(Paths.get("/var/cache/pacman/pkg"))
.filter(Files::isRegularFile)
.filter(p -> pattern.matcher(p.getFileName().toString()).matches())
.map(path -> Parser.parseFile(path, CompressionFormat.guess(path)))
.map(path -> PkgDataImpl.parseFile(path, CompressionFormat.guess(path)))
.limit(10)
.map(new Function<PkgData, String>() {
@Override

View File

@@ -1,7 +1,7 @@
package net.woggioni.jpacrepo.annotation;
import javax.enterprise.inject.Alternative;
import javax.enterprise.inject.Stereotype;
import jakarta.enterprise.inject.Alternative;
import jakarta.enterprise.inject.Stereotype;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;

View File

@@ -1,8 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="INFO">
<Properties>
<Property name="log-path">$${env:APOLLO_ENVIRONMENT_ROOT:-build}/var/logs</Property>
</Properties>
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss,SSS} %highlight{[%p]} (%t) %c: %m%n"/>

View File

@@ -1,49 +0,0 @@
package net.woggioni.jpacrepo.client
import java.io.InputStream
import java.util.Properties
import javax.naming.{Context, InitialContext, NameClassPair, NamingEnumeration, NamingException}
import net.woggioni.jpacrepo.service.PacmanServiceRemote
import org.scalatest.flatspec.AnyFlatSpec
import org.scalatest.matchers.should.Matchers
class SyncDbTest extends AnyFlatSpec {
private def traverseJndiNode(nodeName: String, context: Context) {
try {
val list = context.list(nodeName)
while (list.hasMore) {
val childName = nodeName + list.next.getName
System.out.println(childName)
traverseJndiNode(childName, context)
}
} catch {
case _ : NamingException =>
}
}
"it" should "be possible to invoke syncDB remotely" in {
val prop = new Properties
val in = getClass.getClassLoader.getResourceAsStream("jboss-ejb-client.properties")
prop.load(in)
prop.put(Context.URL_PKG_PREFIXES, "org.jboss.ejb.client.naming")
prop.put(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.naming.remote.client.InitialContextFactory")
prop.put(Context.PROVIDER_URL, "http-remoting://localhost:5080")
// prop.put(Context.PROVIDER_URL, "http-remoting://nuc:8080");
// prop.put(Context.PROVIDER_URL, "remote://odroid-u3:4447");
prop.put(Context.SECURITY_PRINCIPAL, "walter")
prop.put(Context.SECURITY_CREDENTIALS, "27ff5990757d1d")
// prop.put(Context.SECURITY_PRINCIPAL, "admin");
// prop.put(Context.SECURITY_CREDENTIALS, "123456");
prop.put("jboss.naming.client.ejb.context", true)
val context = new InitialContext(prop)
val ctx = new InitialContext(prop)
traverseJndiNode("/", context)
// final PacmanService stateService = (PacmanService) ctx.lookup("/jpacrepo-1.0/remote/PacmanServiceEJB!service.PacmanService");
val service = ctx.lookup("/jpacrepo_2.13-2.0/PacmanServiceEJB!net.woggioni.jpacrepo.service.PacmanServiceRemote").asInstanceOf[PacmanServiceRemote]
// List<PkgData> pkgs = service.searchPackage("google-earth", null, null, 1, 10);
// System.out.println(new XStream().toXML(pkgs));
service.syncDB()
}
}

View File

@@ -1,26 +0,0 @@
package net.woggioni.jpacrepo.config
import net.woggioni.jpacrepo.factory.BeanFactory
import org.jboss.weld.environment.se.Weld
import org.scalatest.flatspec.AnyFlatSpec
import org.scalatest.matchers.must.Matchers
class AppConfigTest extends AnyFlatSpec with Matchers {
private val weld = new Weld
weld.disableDiscovery()
// weld.alternatives(classOf[TestPersistenceProducer])
weld.beanClasses(
// classOf[PacmanServiceEJB],
// classOf[PacmanWebService],
// classOf[AppConfig],
classOf[BeanFactory],
)
val container = weld.initialize()
"test" should "pass" in {
val appConfig = container.select(classOf[AppConfig]).get()
println(appConfig.repoFolder)
}
}

View File

@@ -1,49 +0,0 @@
package net.woggioni.jpacrepo.pacbase
import java.nio.file.{Files, Path, Paths}
import java.util
import java.util.regex.Pattern
import java.util.stream.Collectors
import javax.xml.bind.annotation.{XmlAccessType, XmlAccessorType, XmlElement, XmlRootElement}
import javax.xml.bind.{JAXBContext, Marshaller}
import net.woggioni.jpacrepo.model.Parser
import net.woggioni.jpacrepo.service.PkgDataList
import org.scalatest.flatspec.AnyFlatSpec
import scala.jdk.CollectionConverters._
import scala.annotation.meta.field
@XmlRootElement(name = "List")
@XmlAccessorType(XmlAccessType.FIELD)
class JaxbList2[T] private (@(XmlElement @field)(name = "Item") private val list : util.List[T]) {
def this(s : Seq[T]) {
this(s.toList.asJava)
}
def this() {
this(new util.ArrayList[T]())
}
}
class MarshalTest extends AnyFlatSpec {
"asdfs" should "sdfsd" in {
val context = JAXBContext.newInstance(classOf[PkgId], classOf[PkgDataList])
val mar = context.createMarshaller
mar.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true)
val pkgId2 = new PkgId
pkgId2.name = "linux"
pkgId2.version = "4.6.1"
pkgId2.arch = "x86_64"
val pattern = Pattern.compile(".*\\.pkg\\.tar\\.(zst)")
val pkgDatas = Files.list(Paths.get("/var/cache/pacman/pkg"))
.filter(Files.isRegularFile(_))
.filter((p: Path) => pattern.matcher(p.getFileName.toString).matches)
.limit(10)
.map(Parser.parseFile(_, CompressionFormat.Z_STANDARD))
.collect(Collectors.toList[PkgData])
val list = new PkgDataList(pkgDatas)
mar.marshal(list, System.out)
}
}

View File

@@ -1,11 +0,0 @@
package net.woggioni.jpacrepo.persistence
import javax.enterprise.inject.Produces
import javax.persistence.Persistence
import net.woggioni.jpacrepo.annotation.UnitTesting
class TestPersistenceProducer {
@Produces
@UnitTesting
def createEntityManagerFactory = Persistence.createEntityManagerFactory("test")
}

View File

@@ -1,38 +0,0 @@
package net.woggioni.jpacrepo.sample
import net.woggioni.jpacrepo.persistence.InitialSchemaAction
import net.woggioni.jpacrepo.version.VersionComparator
import org.scalatest.flatspec.AnyFlatSpec
import org.scalatest.matchers.should.Matchers
import scala.collection.mutable
class ExampleSpec extends AnyFlatSpec with Matchers {
def foo(a : Int)(b : String = "walter"): Unit = {
println(s"a: $a, b: $b")
}
def foo(c : Int): Unit = {
foo(a=c)("adfsda")
}
// "A Stack" should "pop values in last-in-first-out order" in {
// val stack = new mutable.Stack[Int]
// stack.push(1)
// stack.push(2)
// stack.pop() should be (2)
// stack.pop() should be (1)
// }
//
// it should "throw NoSuchElementException if an empty stack is popped" in {
// val emptyStack = new mutable.Stack[Int]
// a [NoSuchElementException] should be thrownBy {
// emptyStack.pop()
// }
// }
"sdfgf" should "dfgfd" in {
foo(a=5)()
}
}

View File

@@ -1,58 +0,0 @@
package net.woggioni.jpacrepo.service
import javax.enterprise.util.TypeLiteral
import net.woggioni.jpacrepo.factory.BeanFactory
import net.woggioni.jpacrepo.persistence.TestPersistenceProducer
import org.jboss.weld.environment.se.Weld
//object WeldContainer {
// private val weld = new Weld
// weld.disableDiscovery()
//
// weld.beanClasses(
// classOf[TestPersistenceProducer],
// classOf[PacmanServiceEJB],
// classOf[ApplicationContext],
// classOf[BeanFactory],
// )
//
// private val count = new AtomicInteger(0)
//}
//class WeldContainer extends AutoCloseable {
//
// val container = WeldContainer.weld.initialize()
// WeldContainer.count.incrementAndGet()
//
// override def close(): Unit = {
// container.close()
// if(WeldContainer.count.decrementAndGet() == 0) {
//// WeldContainer.weld.shutdown()
// }
// }
//}
class PacmanServiceEJBTest {
private val weld = new Weld
// weld.disableDiscovery()
weld.alternatives(classOf[TestPersistenceProducer])
////
weld.beanClasses(
classOf[PacmanServiceEJB],
classOf[PacmanWebService],
// classOf[ApplicationContext],
classOf[BeanFactory],
)
def test = {
val container = weld.initialize()
try {
val s = getClass.getResourceAsStream("/log4j2.xml")
val service = container.select(new TypeLiteral[PacmanServiceView] {}).get()
service.syncDB()
} finally {
container.close()
}
}
}

View File

@@ -1,33 +0,0 @@
package net.woggioni.jpacrepo.service
class PacmanWebServiceTest {
// val server = {
// val server = new UndertowJaxrsServer()
// server.start()
// import org.jboss.resteasy.spi.ResteasyDeployment
// val deployment = new ResteasyDeployment
// deployment.setInjectorFactoryClass("org.jboss.resteasy.cdi.CdiInjectorFactory")
// deployment.setApplicationClass(classOf[ApplicationConfig].getName)
// val di: DeploymentInfo = server.undertowDeployment(deployment)
// di.setClassLoader(classOf[ApplicationConfig].getClassLoader)
// di.setContextPath("/jpacrepo")
// di.setDeploymentName("jpacrepo")
// di.addListeners(Servlets.listener(classOf[Listener]))
// server.deploy(di)
// server
// }
// @Test
// def foo {
// val client = ClientBuilder.newClient()
// val webTarget = client.target(TestPortProvider.generateURL("/jpacrepo/rest/pkg/map"))
// val response = webTarget.request().get()
// val res = response.getEntity
// response.getStatus
// }
def boh = {
println(System.getProperty("net.woggioni.jpacrepo.configuration.file"))
}
}

View File

@@ -1,16 +0,0 @@
package net.woggioni.jpacrepo.version
import org.scalatest.flatspec.AnyFlatSpec
import org.scalatest.matchers.should.Matchers
import scala.util.Random
class VersionComparatorSpec extends AnyFlatSpec with Matchers {
"Version sorting" should "work as expected" in {
val vc = new VersionComparator
val originalList = List("1.0", "2.0", "3.0", "5.6.7.arch1-1", "5.6.7.arch3-1", "5.6.7.arch3-2", "20200421.78c0348-1")
val shuffledList = new Random(101325).shuffle(originalList)
val sortedList = shuffledList.sorted(Ordering.comparatorToOrdering(vc))
sortedList should be (originalList)
}
}