Skip to content

Commit

Permalink
improve code to read update provider info directly from maven and add…
Browse files Browse the repository at this point in the history
… experimentell (hidden) functions to show the data
  • Loading branch information
michaeloffner committed Oct 7, 2023
1 parent 418bed7 commit 0079c85
Show file tree
Hide file tree
Showing 9 changed files with 560 additions and 68 deletions.
79 changes: 58 additions & 21 deletions core/src/main/java/lucee/runtime/config/maven/ArtifactReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@
import java.nio.charset.Charset;
import java.security.GeneralSecurityException;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Stack;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;

import org.osgi.framework.BundleException;
import org.osgi.framework.Version;
Expand All @@ -20,7 +22,6 @@
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;

import lucee.print;
import lucee.commons.io.IOUtil;
import lucee.commons.io.sax.SaxUtil;
import lucee.commons.lang.ExceptionUtil;
Expand All @@ -41,25 +42,20 @@ public final class ArtifactReader extends DefaultHandler {
private StringBuilder content = new StringBuilder();
private boolean insideArtifact;
private List<Version> tmpArtifacts = new ArrayList<>();
private Map<String, Repository> tmpNexuss = new HashMap<>();
private Map<String, String> tmpMeta = new HashMap<>();

private String listProvider;
private String group;
private String artifact;
private String key;
private boolean insideNexus;
private Repository nexus;

private static Map<String, Integer> totalCounts = new ConcurrentHashMap<>();
private static Map<String, Version[]> artifactss = new ConcurrentHashMap<>();
private static Map<String, List<Version>> versionss = new ConcurrentHashMap<>();
private static Map<String, Map<String, String>> metas = new ConcurrentHashMap<>();

/*
* public static void main(String[] args) throws Exception { ArtifactReader reader = new
* ArtifactReader(MavenUpdateProvider.DEFAULT_LIST_PROVIDER, MavenUpdateProvider.DEFAULT_GROUP,
* MavenUpdateProvider.DEFAULT_ARTIFACT); long start = System.currentTimeMillis(); reader.read();
* print.e(System.currentTimeMillis() - start); start = System.currentTimeMillis(); reader.read();
* print.e(System.currentTimeMillis() - start); print.e(reader.getMeta()); }
*/

ArtifactReader(String listProvider, String group, String artifact) {
this.listProvider = listProvider;
this.group = group;
Expand All @@ -71,14 +67,12 @@ public void read() throws IOException, GeneralSecurityException, SAXException {

Integer tc = totalCounts.get(key);

print.e("totalCount:" + tc);
// first we check the size
if (tc != null) {
// first we see if there is a change
read(1, 1);
// if it matches we take the cached data
int tmpTotalCount = Caster.toIntValue(tmpMeta.get("totalCount"), 0);
print.e("tmpTotalCount:" + tmpTotalCount);
clear();
if (tmpTotalCount == tc.intValue()) {
return;
Expand Down Expand Up @@ -106,7 +100,16 @@ public void read() throws IOException, GeneralSecurityException, SAXException {
break; // just in case
}
}
artifactss.put(key, toArray(tmpArtifacts));

// Sort using streams with custom comparator
List<Version> sorted = tmpArtifacts.stream().sorted(new Comparator<Version>() {
@Override
public int compare(Version l, Version r) {
return OSGiUtil.compare(l, r);
}
}).collect(Collectors.toList());

versionss.put(key, sorted);
metas.put(key, tmpMeta);

clear();
Expand All @@ -115,7 +118,6 @@ public void read() throws IOException, GeneralSecurityException, SAXException {
private void read(int from, int count) throws IOException, GeneralSecurityException, SAXException {

URL url = new URL(listProvider + "?g=" + group + "&a=" + artifact + "&c=sources&from=" + from + "&count=" + count);

HTTPResponse rsp = HTTPEngine4Impl.get(url, null, null, 0, true, null, null, null, null);
if (rsp != null) {
int sc = rsp.getStatusCode();
Expand All @@ -136,6 +138,7 @@ private void read(int from, int count) throws IOException, GeneralSecurityExcept

private void clear() {
tmpArtifacts = new ArrayList<>();
tmpNexuss = new HashMap<>();
tmpMeta = new HashMap<>();
tree.clear();
}
Expand Down Expand Up @@ -167,6 +170,12 @@ public void startElement(String uri, String name, String qName, Attributes atts)
if (tree.size() == 2 && "data".equals(tree.peek()) && "artifact".equals(name)) {
insideArtifact = true;
}
// enter nexus?
if (tree.size() == 2 && "repoDetails".equals(tree.peek()) && "org.sonatype.nexus.rest.model.NexusNGRepositoryDetail".equals(name)) {
insideNexus = true;
nexus = new Repository();
}

tree.add(qName);

}
Expand All @@ -181,6 +190,17 @@ public void endElement(String uri, String name, String qName) {
throw new RuntimeException(ExceptionUtil.toIOException(e));
}
}
if (insideNexus) {
String val = content.toString().trim();
if ("repositoryId".equals(name)) nexus.id = val;
else if ("repositoryId".equals(name)) nexus.id = val;
else if ("repositoryName".equals(name)) nexus.name = val;
else if ("repositoryContentClass".equals(name)) nexus.contentClass = val;
else if ("repositoryKind".equals(name)) nexus.kind = val;
else if ("repositoryPolicy".equals(name)) nexus.policy = val;
else if ("repositoryURL".equals(name)) nexus.URL = val;

}

// meta data
if (!insideArtifact && tree.size() == 2) {
Expand All @@ -194,6 +214,13 @@ public void endElement(String uri, String name, String qName) {
if (tree.size() == 2 && "data".equals(tree.peek()) && "artifact".equals(name)) {
insideArtifact = false;
}

// exit nexus?
if (tree.size() == 2 && "repoDetails".equals(tree.peek()) && "org.sonatype.nexus.rest.model.NexusNGRepositoryDetail".equals(name)) {
insideNexus = false;
tmpNexuss.put(nexus.policy, nexus);
nexus = null;
}
}

@Override
Expand All @@ -203,20 +230,16 @@ public void characters(char ch[], int start, int length) {

@Override
public void endDocument() throws SAXException {
/*
* Collections.sort(artifacts, new Comparator<Version>() {
*
* @Override public int compare(Version l, Version r) { return OSGiUtil.compare(l, r); } });
*/

super.endDocument();
}

public Map<String, String> getMeta() {
return metas.get(key);
}

public Version[] getArtifacts() {
return artifactss.get(key);
public List<Version> getVersions() {
return versionss.get(key);
}

private static Version[] toArray(List<Version> data) {
Expand All @@ -227,4 +250,18 @@ private static Version[] toArray(List<Version> data) {
}
return arr;
}

private static class Repository {
private String id;
private String name;
private String contentClass;
private String kind;
private String policy;
private String URL;

@Override
public String toString() {
return "id:" + id + ";name:" + name + ";contentClass:" + contentClass + ";kind:" + kind + ";policy:" + policy + ";URL:" + URL;
}
}
}
Loading

0 comments on commit 0079c85

Please sign in to comment.