提交 b2401277 编写于 作者: J jurgen

Maven cache save

上级 066e2254
......@@ -165,7 +165,7 @@ public class MavenArtifact
}
@NotNull
private String getVersionFileName(String version, String fileType) {
String getVersionFileName(String version, String fileType) {
return artifactId + "-" + version + "." + fileType;
}
......@@ -199,7 +199,7 @@ public class MavenArtifact
// No version info. Some artifacts do not have older versions in metadata.xml so just warn
log.debug("Artifact '" + artifactId + "' do not have version '" + versionStr + "' info in metadata");
}
version = new MavenLocalVersion(this, versionStr, getVersionFileName(versionStr, FILE_JAR), new Date());
version = new MavenLocalVersion(this, versionStr, new Date());
version.getMetaData(monitor);
localVersions.add(version);
}
......
......@@ -130,20 +130,15 @@ public class MavenArtifactVersion {
return this.dependencies;
}
List<MavenArtifactDependency> getDependencies() {
return dependencies;
}
@Override
public String toString() {
return localVersion.toString();
}
private enum ParserState {
ROOT,
PARENT,
PROPERTIES,
LICENSE,
DEPENDENCIES,
DEPENDENCY
}
private void loadPOM(DBRProgressMonitor monitor) throws IOException {
String pomURL = localVersion.getArtifact().getFileURL(localVersion.getVersion(), MavenArtifact.FILE_POM);
monitor.subTask("Load POM " + localVersion);
......@@ -272,4 +267,5 @@ public class MavenArtifactVersion {
private String evaluateString(String value) {
return GeneralUtils.replaceVariables(value, variableResolver);
}
}
\ No newline at end of file
......@@ -41,10 +41,10 @@ public class MavenLocalVersion
private Date updateTime;
private MavenArtifactVersion metaData;
public MavenLocalVersion(MavenArtifact artifact, String version, String fileName, Date updateTime) {
public MavenLocalVersion(MavenArtifact artifact, String version, Date updateTime) {
this.artifact = artifact;
this.version = version;
this.fileName = fileName;
this.fileName = artifact.getVersionFileName(version, MavenArtifact.FILE_JAR);
this.updateTime = updateTime;
}
......@@ -94,6 +94,10 @@ public class MavenLocalVersion
return metaData;
}
MavenArtifactVersion getMetaData() {
return metaData;
}
@Override
public String toString() {
return artifact.toString() + ":" + version;
......
......@@ -18,11 +18,15 @@
package org.jkiss.dbeaver.registry.maven;
import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.Status;
import org.jkiss.code.NotNull;
import org.jkiss.code.Nullable;
import org.jkiss.dbeaver.DBeaverPreferences;
import org.jkiss.dbeaver.core.DBeaverCore;
import org.jkiss.dbeaver.model.runtime.DBRProgressMonitor;
import org.jkiss.dbeaver.runtime.AbstractJob;
import org.jkiss.utils.CommonUtils;
import java.io.IOException;
......@@ -72,6 +76,9 @@ public class MavenRegistry
MAVEN_LOCAL_REPO_NAME,
localRepoURL,
true);
// Start config saver
new ConfigSaver().schedule(ConfigSaver.SAVE_PERIOD);
}
public void loadCustomRepositories() {
......@@ -188,4 +195,23 @@ public class MavenRegistry
return null;
}
private class ConfigSaver extends AbstractJob {
public static final int SAVE_PERIOD = 1000;
protected ConfigSaver() {
super("Maven local cache persister");
}
@Override
protected IStatus run(DBRProgressMonitor monitor) {
for (MavenRepository repository : repositories) {
repository.saveCacheIfNeeded();
}
schedule(SAVE_PERIOD);
return Status.OK_STATUS;
}
}
}
......@@ -33,10 +33,7 @@ import org.jkiss.utils.xml.XMLException;
import org.xml.sax.Attributes;
import java.io.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.*;
/**
* Maven repository manager.
......@@ -52,6 +49,7 @@ public class MavenRepository
private static final String TAG_CACHE = "cache";
private static final String TAG_ARTIFACT = "artifact";
private static final String TAG_VERSION = "version";
private static final String TAG_DEPENDENCY = "dependency";
public static final String ATTR_NAME = "name";
public static final String ATTR_URL = "url";
......@@ -59,7 +57,9 @@ public class MavenRepository
public static final String ATTR_ARTIFACT_ID = "artifactId";
public static final String ATTR_ACTIVE_VERSION = "activeVersion";
public static final String ATTR_VERSION = "version";
public static final String ATTR_FILE = "file";
public static final String ATTR_PATH = "path";
public static final String ATTR_SCOPE = "scope";
private static final String ATTR_PARENT = "parent";
public static final String ATTR_UPDATE_TIME = "updateTime";
private String id;
......@@ -70,7 +70,7 @@ public class MavenRepository
private transient volatile boolean needsToSave = false;
private List<MavenArtifact> cachedArtifacts = new ArrayList<MavenArtifact>();
private List<MavenArtifact> cachedArtifacts = new ArrayList<>();
public MavenRepository(IConfigurationElement config)
{
......@@ -116,7 +116,7 @@ public class MavenRepository
}
@Nullable
public MavenArtifact findArtifact(@NotNull String groupId, @NotNull String artifactId, boolean resolve) {
public synchronized MavenArtifact findArtifact(@NotNull String groupId, @NotNull String artifactId, boolean resolve) {
for (MavenArtifact artifact : cachedArtifacts) {
if (artifact.getGroupId().equals(groupId) && artifact.getArtifactId().equals(artifactId)) {
return artifact;
......@@ -137,7 +137,7 @@ public class MavenRepository
return null;
}
void resetArtifactCache(@NotNull String groupId, @NotNull String artifactId) {
synchronized void resetArtifactCache(@NotNull String groupId, @NotNull String artifactId) {
for (Iterator<MavenArtifact> iterator = cachedArtifacts.iterator(); iterator.hasNext(); ) {
MavenArtifact artifact = iterator.next();
if (artifact.getGroupId().equals(groupId) && artifact.getArtifactId().equals(artifactId)) {
......@@ -146,11 +146,6 @@ public class MavenRepository
}
}
private synchronized void addCachedArtifact(@NotNull MavenArtifact artifact) {
cachedArtifacts.add(artifact);
// saveCache();
}
File getLocalCacheDir()
{
File homeFolder = new File(DBeaverActivator.getInstance().getStateLocation().toFile(), "maven/" + id + "/");
......@@ -187,7 +182,6 @@ public class MavenRepository
MavenLocalVersion version = new MavenLocalVersion(
lastArtifact,
atts.getValue(ATTR_VERSION),
atts.getValue(ATTR_FILE),
new Date(Long.parseLong(atts.getValue(ATTR_UPDATE_TIME))));
lastArtifact.addLocalVersion(version);
}
......@@ -213,6 +207,13 @@ public class MavenRepository
}
}
void saveCacheIfNeeded() {
if (needsToSave) {
saveCache();
needsToSave = false;
}
}
synchronized private void saveCache() {
try {
File cacheDir = getLocalCacheDir();
......@@ -226,30 +227,47 @@ public class MavenRepository
try {
XMLBuilder xml = new XMLBuilder(out, "utf-8");
xml.setButify(true);
xml.startElement(TAG_CACHE);
xml.addAttribute(ATTR_NAME, name);
xml.addAttribute(ATTR_URL, url);
try (XMLBuilder.Element e = xml.startElement(TAG_CACHE)) {
xml.addAttribute(ATTR_NAME, name);
xml.addAttribute(ATTR_URL, url);
for (MavenArtifact artifact : cachedArtifacts) {
if (CommonUtils.isEmpty(artifact.getLocalVersions())) {
continue;
}
xml.startElement(TAG_ARTIFACT);
xml.addAttribute(ATTR_GROUP_ID, artifact.getGroupId());
xml.addAttribute(ATTR_ARTIFACT_ID, artifact.getArtifactId());
xml.addAttribute(ATTR_ACTIVE_VERSION, artifact.getActiveVersion());
for (MavenLocalVersion version : artifact.getLocalVersions()) {
xml.startElement(TAG_VERSION);
xml.addAttribute(ATTR_VERSION, version.getVersion());
xml.addAttribute(ATTR_FILE, version.getFileName());
xml.addAttribute(ATTR_UPDATE_TIME, version.getUpdateTime().getTime());
xml.endElement();
for (MavenArtifact artifact : cachedArtifacts) {
if (CommonUtils.isEmpty(artifact.getLocalVersions())) {
continue;
}
try (XMLBuilder.Element e1 = xml.startElement(TAG_ARTIFACT)) {
xml.addAttribute(ATTR_GROUP_ID, artifact.getGroupId());
xml.addAttribute(ATTR_ARTIFACT_ID, artifact.getArtifactId());
xml.addAttribute(ATTR_ACTIVE_VERSION, artifact.getActiveVersion());
for (MavenLocalVersion version : artifact.getLocalVersions()) {
try (XMLBuilder.Element e2 = xml.startElement(TAG_VERSION)) {
xml.addAttribute(ATTR_VERSION, version.getVersion());
xml.addAttribute(ATTR_UPDATE_TIME, version.getUpdateTime().getTime());
MavenArtifactVersion metaData = version.getMetaData();
if (metaData != null) {
MavenArtifactReference parentReference = metaData.getParentReference();
if (parentReference != null) {
xml.addAttribute(ATTR_PARENT, parentReference.getPath());
}
List<MavenArtifactDependency> dependencies = metaData.getDependencies();
if (dependencies != null) {
for (MavenArtifactDependency dependency : dependencies) {
try (XMLBuilder.Element e3 = xml.startElement(TAG_DEPENDENCY)) {
xml.addAttribute(ATTR_PATH, dependency.getPath());
if (dependency.getScope() != MavenArtifactDependency.Scope.COMPILE) {
xml.addAttribute(ATTR_SCOPE, dependency.getScope().name().toLowerCase(Locale.ENGLISH));
}
}
}
}
}
}
}
}
}
xml.endElement();
}
xml.endElement();
xml.flush();
} finally {
IOUtils.close(out);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册