提交 06d7de81 编写于 作者: J jurgen

Dependencies model

Former-commit-id: 2a3ccd84
上级 b4b50497
......@@ -94,7 +94,7 @@ public class DriverDependencies implements DBPDriverDependencies
*/
private void resolveDependencies(DBRProgressMonitor monitor, DependencyNode ownerNode, Map<String, DBPDriverLibrary> libMap) throws IOException {
Collection<? extends DBPDriverLibrary> dependencies = ownerNode.library.getDependencies(monitor, ownerNode.owner);
Collection<? extends DBPDriverLibrary> dependencies = ownerNode.library.getDependencies(monitor);
if (dependencies != null && !dependencies.isEmpty()) {
for (DBPDriverLibrary dep : dependencies) {
DependencyNode node = new DependencyNode(ownerNode, dep);
......
......@@ -846,7 +846,9 @@ public class DriverDescriptor extends AbstractDescriptor implements DBPDriver
validateFilesPresence(runnableContext);
}
long st = System.currentTimeMillis();
resolveDependencies(runnableContext);
System.out.println("Resolve: " + (System.currentTimeMillis() - st) + "ms");
List<URL> libraryURLs = new ArrayList<>();
// Load libraries
......
......@@ -26,7 +26,6 @@ import org.jkiss.dbeaver.model.runtime.DBRProgressMonitor;
import org.jkiss.dbeaver.model.runtime.OSDescriptor;
import org.jkiss.dbeaver.registry.RegistryConstants;
import org.jkiss.dbeaver.runtime.RuntimeUtils;
import org.jkiss.dbeaver.utils.ContentUtils;
import org.jkiss.utils.CommonUtils;
import java.io.*;
......@@ -152,7 +151,7 @@ public abstract class DriverLibraryAbstract implements DBPDriverLibrary
return system == null || system.matches(DBeaverCore.getInstance().getLocalSystem());
}
public void downloadLibraryFile(@NotNull DBRProgressMonitor monitor, boolean forceUpdate) throws IOException, InterruptedException
public void downloadLibraryFile(@NotNull DBRProgressMonitor monitor, boolean forceUpdate, String taskName) throws IOException, InterruptedException
{
String externalURL = getExternalURL();
if (externalURL == null) {
......@@ -160,11 +159,19 @@ public abstract class DriverLibraryAbstract implements DBPDriverLibrary
}
final URLConnection connection = RuntimeUtils.openConnection(externalURL);
monitor.worked(1);
monitor.done();
final int contentLength = connection.getContentLength();
monitor.beginTask("Download " + externalURL, contentLength);
int contentLength = connection.getContentLength();
if (contentLength < 0) {
contentLength = 0;
}
int bufferLength = contentLength / 10;
if (bufferLength > 1000000) {
bufferLength = 1000000;
}
if (bufferLength < 50000) {
bufferLength = 50000;
}
monitor.beginTask(taskName + " - " + externalURL, contentLength);
boolean success = false;
final File localFile = getLocalFile();
if (localFile == null) {
......@@ -176,12 +183,10 @@ public abstract class DriverLibraryAbstract implements DBPDriverLibrary
log.warn("Can't create directory for local driver file '" + localDir.getAbsolutePath() + "'");
}
}
final OutputStream outputStream = new FileOutputStream(localFile);
try {
final InputStream inputStream = connection.getInputStream();
try {
try (final OutputStream outputStream = new FileOutputStream(localFile)) {
try (final InputStream inputStream = connection.getInputStream()) {
final NumberFormat numberFormat = NumberFormat.getNumberInstance();
byte[] buffer = new byte[10000];
byte[] buffer = new byte[bufferLength];
int totalRead = 0;
for (;;) {
if (monitor.isCanceled()) {
......@@ -198,18 +203,14 @@ public abstract class DriverLibraryAbstract implements DBPDriverLibrary
totalRead += count;
}
}
finally {
ContentUtils.close(inputStream);
}
} finally {
ContentUtils.close(outputStream);
if (!success) {
if (!localFile.delete()) {
log.warn("Can't delete local driver file '" + localFile.getAbsolutePath() + "'");
}
}
monitor.done();
}
monitor.done();
}
@Override
......
......@@ -23,7 +23,6 @@ import org.eclipse.core.runtime.Platform;
import org.jkiss.code.NotNull;
import org.jkiss.code.Nullable;
import org.jkiss.dbeaver.model.DBIcon;
import org.jkiss.dbeaver.model.connection.DBPDriverDependencies;
import org.jkiss.dbeaver.model.connection.DBPDriverLibrary;
import org.jkiss.dbeaver.model.runtime.DBRProgressMonitor;
import org.jkiss.dbeaver.ui.UIIcon;
......@@ -111,7 +110,7 @@ public class DriverLibraryLocal extends DriverLibraryAbstract
@Nullable
@Override
public Collection<? extends DBPDriverLibrary> getDependencies(@NotNull DBRProgressMonitor monitor, @Nullable DBPDriverDependencies.DependencyNode ownerNode) throws IOException {
public Collection<? extends DBPDriverLibrary> getDependencies(@NotNull DBRProgressMonitor monitor) throws IOException {
return null;
}
......
......@@ -22,7 +22,6 @@ import org.jkiss.code.NotNull;
import org.jkiss.code.Nullable;
import org.jkiss.dbeaver.Log;
import org.jkiss.dbeaver.model.DBIcon;
import org.jkiss.dbeaver.model.connection.DBPDriverDependencies;
import org.jkiss.dbeaver.model.connection.DBPDriverLibrary;
import org.jkiss.dbeaver.model.runtime.DBRProgressMonitor;
import org.jkiss.dbeaver.model.runtime.VoidProgressMonitor;
......@@ -134,7 +133,7 @@ public class DriverLibraryMavenArtifact extends DriverLibraryAbstract
@Nullable
@Override
public Collection<? extends DBPDriverLibrary> getDependencies(@NotNull DBRProgressMonitor monitor, @Nullable DBPDriverDependencies.DependencyNode ownerNode) throws IOException {
public Collection<? extends DBPDriverLibrary> getDependencies(@NotNull DBRProgressMonitor monitor) throws IOException {
List<DriverLibraryMavenDependency> dependencies = new ArrayList<>();
MavenLocalVersion localVersion = resolveLocalVersion(monitor, false);
if (localVersion != null) {
......@@ -142,15 +141,16 @@ public class DriverLibraryMavenArtifact extends DriverLibraryAbstract
List<MavenArtifactDependency> artifactDeps = metaData.getDependencies(monitor);
if (!CommonUtils.isEmpty(artifactDeps)) {
for (MavenArtifactDependency artifactDep : artifactDeps) {
if (isDependencyExcluded(monitor, artifactDep, ownerNode)) {
if (isDependencyExcluded(monitor, artifactDep)) {
continue;
}
MavenLocalVersion depLocalVersion = artifactDep.resolveDependency(monitor);
if (depLocalVersion != null) {
dependencies.add(
new DriverLibraryMavenDependency(
this.getDriver(),
depLocalVersion));
this,
depLocalVersion,
artifactDep));
}
}
}
......@@ -159,25 +159,7 @@ public class DriverLibraryMavenArtifact extends DriverLibraryAbstract
return dependencies;
}
private boolean isDependencyExcluded(DBRProgressMonitor monitor, MavenArtifactDependency dependency, DBPDriverDependencies.DependencyNode ownerNode) {
for (DBPDriverDependencies.DependencyNode node = ownerNode; node != null; node = node.owner) {
DBPDriverLibrary library = node.library;
if (library instanceof DriverLibraryMavenArtifact) {
MavenLocalVersion ownerVersion = ((DriverLibraryMavenArtifact) library).getMavenLocalVersion();
if (ownerVersion != null) {
for (MavenArtifactDependency ownerDependency : ownerVersion.getMetaData(monitor).getDependencies(monitor)) {
List<MavenArtifactReference> ownerDependencyExclusions = ownerDependency.getExclusions();
if (ownerDependencyExclusions != null) {
for (MavenArtifactReference exReference : ownerDependencyExclusions) {
if (exReference.getGroupId().equals(dependency.getGroupId()) && exReference.getArtifactId().equals(dependency.getArtifactId())) {
return true;
}
}
}
}
}
}
}
protected boolean isDependencyExcluded(DBRProgressMonitor monitor, MavenArtifactDependency dependency) {
return false;
}
......@@ -211,8 +193,8 @@ public class DriverLibraryMavenArtifact extends DriverLibraryAbstract
return UIIcon.APACHE;
}
public void downloadLibraryFile(@NotNull DBRProgressMonitor monitor, boolean forceUpdate) throws IOException, InterruptedException {
monitor.beginTask("Update version information", 1);
public void downloadLibraryFile(@NotNull DBRProgressMonitor monitor, boolean forceUpdate, String taskName) throws IOException, InterruptedException {
//monitor.beginTask(taskName + " - update version information", 1);
try {
MavenLocalVersion localVersion = resolveLocalVersion(monitor, forceUpdate);
if (localVersion.getArtifact().getRepository().isLocal()) {
......@@ -220,9 +202,9 @@ public class DriverLibraryMavenArtifact extends DriverLibraryAbstract
return;
}
} finally {
monitor.done();
//monitor.done();
}
super.downloadLibraryFile(monitor, forceUpdate);
super.downloadLibraryFile(monitor, forceUpdate, taskName);
}
protected MavenLocalVersion resolveLocalVersion(DBRProgressMonitor monitor, boolean forceUpdate) throws IOException {
......
......@@ -19,23 +19,32 @@ package org.jkiss.dbeaver.registry.driver;
import org.jkiss.code.NotNull;
import org.jkiss.code.Nullable;
import org.jkiss.dbeaver.model.connection.DBPDriverDependencies;
import org.jkiss.dbeaver.model.connection.DBPDriverLibrary;
import org.jkiss.dbeaver.model.runtime.DBRProgressMonitor;
import org.jkiss.dbeaver.registry.maven.MavenArtifact;
import org.jkiss.dbeaver.registry.maven.MavenArtifactDependency;
import org.jkiss.dbeaver.registry.maven.MavenArtifactReference;
import org.jkiss.dbeaver.registry.maven.MavenLocalVersion;
import java.io.File;
import java.io.IOException;
import java.util.List;
/**
* DriverLibraryDescriptor
*/
public class DriverLibraryMavenDependency extends DriverLibraryMavenArtifact
{
private DriverLibraryMavenArtifact parent;
private MavenLocalVersion localVersion;
private MavenArtifactDependency source;
public DriverLibraryMavenDependency(DriverDescriptor driverDescriptor, MavenLocalVersion localVersion) {
super(driverDescriptor, FileType.jar, PATH_PREFIX + localVersion.toString());
public DriverLibraryMavenDependency(DriverLibraryMavenArtifact parent, MavenLocalVersion localVersion, MavenArtifactDependency source) {
super(parent.getDriver(), FileType.jar, PATH_PREFIX + localVersion.toString());
this.parent = parent;
this.localVersion = localVersion;
this.source = source;
}
@Override
......@@ -60,4 +69,17 @@ public class DriverLibraryMavenDependency extends DriverLibraryMavenArtifact
return localVersion;
}
protected boolean isDependencyExcluded(DBRProgressMonitor monitor, MavenArtifactDependency dependency) {
List<MavenArtifactReference> exclusions = source.getExclusions();
if (exclusions != null) {
for (MavenArtifactReference exReference : exclusions) {
if (exReference.getGroupId().equals(dependency.getGroupId()) && exReference.getArtifactId().equals(dependency.getArtifactId())) {
return true;
}
}
}
return parent.isDependencyExcluded(monitor, dependency);
}
}
......@@ -207,7 +207,7 @@ public class MavenArtifact
@Override
public String toString() {
return groupId + ":" + artifactId;
return MavenArtifactReference.makeId(groupId, artifactId);
}
@Nullable
......
......@@ -30,16 +30,19 @@ public class MavenArtifactReference
private static final String DEFAULT_MAVEN_VERSION = VERSION_PATTERN_RELEASE;
@NotNull
private String groupId;
private final String groupId;
@NotNull
private String artifactId;
private final String artifactId;
@NotNull
private String version;
private final String version;
@NotNull
private final String id;
public MavenArtifactReference(@NotNull String groupId, @NotNull String artifactId, @NotNull String version) {
this.groupId = groupId;
this.artifactId = artifactId;
this.version = version;
this.id = makeId(groupId, artifactId);
}
public MavenArtifactReference(String ref) {
......@@ -53,6 +56,7 @@ public class MavenArtifactReference
groupId = mavenUri;
artifactId = mavenUri;
version = DEFAULT_MAVEN_VERSION;
id = makeId(groupId, artifactId);
return;
}
groupId = mavenUri.substring(0, divPos);
......@@ -64,6 +68,7 @@ public class MavenArtifactReference
artifactId = mavenUri.substring(divPos + 1, divPos2);
version = mavenUri.substring(divPos2 + 1);
}
id = makeId(groupId, artifactId);
}
@NotNull
......@@ -71,29 +76,21 @@ public class MavenArtifactReference
return groupId;
}
public void setGroupId(@NotNull String groupId) {
this.groupId = groupId;
}
@NotNull
public String getArtifactId() {
return artifactId;
}
public void setArtifactId(@NotNull String artifactId) {
this.artifactId = artifactId;
}
@NotNull
public String getVersion() {
return version;
}
public void setVersion(@NotNull String version) {
this.version = version;
@NotNull
public String getId() {
return id;
}
public String getPath() {
return groupId + ":" + artifactId + ":" + version;
}
......@@ -107,4 +104,9 @@ public class MavenArtifactReference
public int hashCode() {
return groupId.hashCode() + artifactId.hashCode() + version.hashCode();
}
static String makeId(String groupId, String artifactId) {
return groupId + ":" + artifactId;
}
}
......@@ -265,7 +265,7 @@ public class MavenArtifactVersion {
boolean optional = CommonUtils.getBoolean(XMLUtils.getChildElementBody(dep, "optional"), false);
// TODO: maybe we should include some of them
if (depManagement || (!optional && (scope == MavenArtifactDependency.Scope.COMPILE || scope == MavenArtifactDependency.Scope.RUNTIME))) {
if (depManagement || (!optional && includesScope(scope))) {
MavenArtifactDependency dependency = new MavenArtifactDependency(
evaluateString(groupId),
evaluateString(artifactId),
......@@ -294,6 +294,13 @@ public class MavenArtifactVersion {
return result;
}
private boolean includesScope(MavenArtifactDependency.Scope scope) {
return
scope == MavenArtifactDependency.Scope.COMPILE ||
scope == MavenArtifactDependency.Scope.RUNTIME ||
scope == MavenArtifactDependency.Scope.PROVIDED;
}
private String findDependencyVersion(DBRProgressMonitor monitor, String groupId, String artifactId) {
if (dependencyManagement != null) {
for (MavenArtifactDependency dmArtifact : dependencyManagement) {
......
......@@ -61,7 +61,9 @@ public class MavenRegistry
private void init() {
loadStandardRepositories();
loadCustomRepositories();
long st = System.currentTimeMillis();
loadCache();
System.out.println("Cache load: " + (System.currentTimeMillis() - st) + "ms");
// Start config saver
new ConfigSaver().schedule(ConfigSaver.SAVE_PERIOD);
}
......@@ -131,23 +133,18 @@ public class MavenRegistry
@Nullable
public MavenArtifact findArtifact(@NotNull MavenArtifactReference ref) {
return findArtifact(ref.getGroupId(), ref.getArtifactId(), true);
return findArtifact(ref, true);
}
@Nullable
public MavenArtifact findArtifact(@NotNull MavenArtifactReference ref, boolean resolve) {
return findArtifact(ref.getGroupId(), ref.getArtifactId(), resolve);
}
@Nullable
private MavenArtifact findArtifact(@NotNull String groupId, @NotNull String artifactId, boolean resolve) {
String fullId = groupId + ":" + artifactId;
String fullId = ref.getId();
if (notFoundArtifacts.contains(fullId)) {
return null;
}
MavenArtifact artifact = findInRepositories(groupId, artifactId, false);
MavenArtifact artifact = findInRepositories(ref, false);
if (artifact == null && resolve) {
artifact = findInRepositories(groupId, artifactId, true);
artifact = findInRepositories(ref, true);
}
if (artifact != null) {
return artifact;
......@@ -159,26 +156,24 @@ public class MavenRegistry
}
public void resetArtifactInfo(MavenArtifactReference artifactReference) {
String groupId = artifactReference.getGroupId();
String artifactId = artifactReference.getArtifactId();
String fullId = groupId + ":" + artifactId;
notFoundArtifacts.remove(fullId);
notFoundArtifacts.remove(artifactReference.getId());
for (MavenRepository repository : repositories) {
repository.resetArtifactCache(groupId, artifactId);
repository.resetArtifactCache(artifactReference);
}
localRepository.resetArtifactCache(groupId, artifactId);
localRepository.resetArtifactCache(artifactReference);
}
@Nullable
private MavenArtifact findInRepositories(@NotNull String groupId, @NotNull String artifactId, boolean resolve) {
private MavenArtifact findInRepositories(@NotNull MavenArtifactReference ref, boolean resolve) {
// Try all available repositories (without resolve)
for (MavenRepository repository : repositories) {
MavenArtifact artifact = repository.findArtifact(groupId, artifactId, resolve);
MavenArtifact artifact = repository.findArtifact(ref, resolve);
if (artifact != null) {
return artifact;
}
}
MavenArtifact artifact = localRepository.findArtifact(groupId, artifactId, resolve);
MavenArtifact artifact = localRepository.findArtifact(ref, resolve);
if (artifact != null) {
return artifact;
}
......
......@@ -77,7 +77,7 @@ public class MavenRepository
private transient volatile boolean needsToSave = false;
private List<MavenArtifact> cachedArtifacts = new ArrayList<>();
private Map<String, MavenArtifact> cachedArtifacts = new LinkedHashMap<>();
public MavenRepository(IConfigurationElement config)
{
......@@ -122,34 +122,24 @@ public class MavenRepository
}
@Nullable
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;
}
}
if (resolve) {
public synchronized MavenArtifact findArtifact(@NotNull MavenArtifactReference ref, boolean resolve) {
MavenArtifact artifact = cachedArtifacts.get(ref.getId());
if (artifact == null && resolve) {
// Not cached - look in remote repository
MavenArtifact artifact = new MavenArtifact(this, groupId, artifactId);
artifact = new MavenArtifact(this, ref.getGroupId(), ref.getArtifactId());
try {
artifact.loadMetadata(VoidProgressMonitor.INSTANCE);
} catch (IOException e) {
log.debug("Artifact [" + artifact + "] not found in repository [" + getUrl() + "]");
return null;
}
cachedArtifacts.add(artifact);
return artifact;
cachedArtifacts.put(ref.getId(), artifact);
}
return null;
return artifact;
}
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)) {
iterator.remove();
}
}
synchronized void resetArtifactCache(@NotNull MavenArtifactReference artifactReference) {
cachedArtifacts.remove(artifactReference.getId());
}
File getLocalCacheDir()
......@@ -220,7 +210,9 @@ public class MavenRepository
atts.getValue(ATTR_GROUP_ID),
atts.getValue(ATTR_ARTIFACT_ID));
lastArtifact.setActiveVersion(atts.getValue(ATTR_ACTIVE_VERSION));
cachedArtifacts.add(lastArtifact);
cachedArtifacts.put(
MavenArtifactReference.makeId(lastArtifact.getGroupId(), lastArtifact.getArtifactId()),
lastArtifact);
} else if (TAG_VERSION.equals(localName) && lastArtifact != null) {
Date updateTime = new Date();
try {
......@@ -352,7 +344,7 @@ public class MavenRepository
xml.addAttribute(ATTR_NAME, name);
xml.addAttribute(ATTR_URL, url);
for (MavenArtifact artifact : cachedArtifacts) {
for (MavenArtifact artifact : cachedArtifacts.values()) {
if (CommonUtils.isEmpty(artifact.getLocalVersions())) {
continue;
}
......
......@@ -173,7 +173,7 @@ class DriverDownloadAutoPage extends DriverDownloadPage {
List<DBPDriverLibrary> files = dependencies.getLibraryList();
for (int i = 0, filesSize = files.size(); i < filesSize; ) {
DBPDriverLibrary lib = files.get(i);
int result = downloadLibraryFile(runnableContext, lib);
int result = downloadLibraryFile(runnableContext, lib, "Download " + (i + 1) + "/" + filesSize);
switch (result) {
case IDialogConstants.CANCEL_ID:
case IDialogConstants.ABORT_ID:
......@@ -188,14 +188,14 @@ class DriverDownloadAutoPage extends DriverDownloadPage {
}
}
private int downloadLibraryFile(DBRRunnableContext runnableContext, final DBPDriverLibrary file)
private int downloadLibraryFile(DBRRunnableContext runnableContext, final DBPDriverLibrary file, final String taskName)
{
try {
runnableContext.run(true, true, new DBRRunnableWithProgress() {
@Override
public void run(DBRProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
try {
file.downloadLibraryFile(monitor, getWizard().isUpdateVersion());
file.downloadLibraryFile(monitor, false, taskName);
} catch (IOException e) {
throw new InvocationTargetException(e);
}
......
......@@ -107,9 +107,10 @@ class EditMavenArtifactDialog extends Dialog
@Override
protected void okPressed() {
artifact.setGroupId(groupText.getText());
artifact.setArtifactId(artifactText.getText());
artifact.setVersion(versionText.getText());
artifact = new MavenArtifactReference(
groupText.getText(),
artifactText.getText(),
versionText.getText());
super.okPressed();
}
}
\ No newline at end of file
......@@ -94,8 +94,8 @@ public interface DBPDriverLibrary
boolean matchesCurrentPlatform();
@Nullable
Collection<? extends DBPDriverLibrary> getDependencies(@NotNull DBRProgressMonitor monitor, @Nullable DBPDriverDependencies.DependencyNode ownerNode) throws IOException;
Collection<? extends DBPDriverLibrary> getDependencies(@NotNull DBRProgressMonitor monitor) throws IOException;
void downloadLibraryFile(@NotNull DBRProgressMonitor monitor, boolean forceUpdate)
void downloadLibraryFile(@NotNull DBRProgressMonitor monitor, boolean forceUpdate, String taskName)
throws IOException, InterruptedException;
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册