提交 30cdd029 编写于 作者: J jurgen

Maven model

上级 c2d608e9
......@@ -27,10 +27,13 @@ import org.jkiss.dbeaver.model.runtime.DBRProgressMonitor;
import org.jkiss.dbeaver.model.runtime.VoidProgressMonitor;
import org.jkiss.dbeaver.registry.maven.*;
import org.jkiss.dbeaver.ui.UIIcon;
import org.jkiss.utils.CommonUtils;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
/**
* DriverLibraryDescriptor
......@@ -41,6 +44,8 @@ public class DriverLibraryMavenArtifact extends DriverLibraryAbstract
public static final String PATH_PREFIX = "maven:/";
private List<DriverLibraryMavenDependency> dependencies;
public DriverLibraryMavenArtifact(DriverDescriptor driver, FileType type, String path) {
super(driver, type, path);
}
......@@ -123,19 +128,33 @@ public class DriverLibraryMavenArtifact extends DriverLibraryAbstract
@Nullable
@Override
public Collection<DBPDriverLibrary> getDependencies(DBRProgressMonitor monitor) throws IOException {
MavenLocalVersion localVersion = resolveLocalVersion(monitor, false);
if (localVersion == null) {
return null;
public Collection<? extends DBPDriverLibrary> getDependencies(DBRProgressMonitor monitor) throws IOException {
if (dependencies == null) {
dependencies = new ArrayList<>();
MavenLocalVersion localVersion = resolveLocalVersion(monitor, false);
if (localVersion != null) {
collectDependencies(monitor, localVersion);
}
}
return dependencies;
}
private void collectDependencies(DBRProgressMonitor monitor, MavenLocalVersion localVersion) throws IOException {
MavenArtifactVersion metaData = localVersion.getMetaData(monitor);
if (metaData == null) {
return null;
}
for (MavenArtifactDependency dependency : metaData.getDependencies()) {
System.out.println(dependency);
if (metaData != null) {
List<MavenArtifactDependency> dependencies = metaData.getDependencies();
if (!CommonUtils.isEmpty(dependencies)) {
for (MavenArtifactDependency dependency : dependencies) {
MavenArtifact depArtifact = MavenRegistry.getInstance().findArtifact(dependency.getArtifactReference());
if (depArtifact != null) {
MavenLocalVersion depLocalVersion = depArtifact.resolveVersion(monitor, dependency.getArtifactReference().getVersion());
if (depLocalVersion != null) {
this.dependencies.add(new DriverLibraryMavenDependency(this.getDriver(), depLocalVersion));
}
}
}
}
}
return null;
}
@NotNull
......@@ -165,7 +184,7 @@ public class DriverLibraryMavenArtifact extends DriverLibraryAbstract
super.downloadLibraryFile(monitor, forceUpdate);
}
private MavenLocalVersion resolveLocalVersion(DBRProgressMonitor monitor, boolean forceUpdate) throws IOException {
protected MavenLocalVersion resolveLocalVersion(DBRProgressMonitor monitor, boolean forceUpdate) throws IOException {
MavenArtifactReference artifactInfo = new MavenArtifactReference(path);
if (forceUpdate) {
MavenRegistry.getInstance().resetArtifactInfo(artifactInfo);
......
/*
* DBeaver - Universal Database Manager
* Copyright (C) 2010-2015 Serge Rieder (serge@jkiss.org)
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License (version 2)
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
package org.jkiss.dbeaver.registry.driver;
import org.eclipse.core.runtime.IConfigurationElement;
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.DBPDriverLibrary;
import org.jkiss.dbeaver.model.runtime.DBRProgressMonitor;
import org.jkiss.dbeaver.model.runtime.VoidProgressMonitor;
import org.jkiss.dbeaver.registry.maven.*;
import org.jkiss.dbeaver.ui.UIIcon;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
/**
* DriverLibraryDescriptor
*/
public class DriverLibraryMavenDependency extends DriverLibraryMavenArtifact
{
private MavenLocalVersion localVersion;
public DriverLibraryMavenDependency(DriverDescriptor driverDescriptor, MavenLocalVersion localVersion) {
super(driverDescriptor, FileType.jar, localVersion.getArtifact().toString());
this.localVersion = localVersion;
}
@Override
public boolean isResolved() {
return true;
}
@Nullable
private MavenArtifact getMavenArtifact() {
return localVersion.getArtifact();
}
@Nullable
@Override
public String getExternalURL() {
return localVersion.getExternalURL(MavenArtifact.FILE_JAR);
}
@Nullable
@Override
public File getLocalFile()
{
return localVersion.getCacheFile();
}
@NotNull
public String getDisplayName() {
return localVersion.getArtifact().getGroupId() + ":" + localVersion.getArtifact().getArtifactId() + ":" + localVersion.getVersion();
}
protected MavenLocalVersion resolveLocalVersion(DBRProgressMonitor monitor, boolean forceUpdate) throws IOException {
return localVersion;
}
}
......@@ -63,13 +63,15 @@ public class MavenArtifact
this.artifactId = artifactId;
}
public void loadMetadata() throws IOException {
public void loadMetadata(DBRProgressMonitor monitor) throws IOException {
latestVersion = null;
releaseVersion = null;
versions.clear();
lastUpdate = null;
String metadataPath = getArtifactDir() + MAVEN_METADATA_XML;
monitor.subTask("Load artifact metadata [" + metadataPath + "]");
System.out.println("Load metadata " + this);
try (InputStream mdStream = RuntimeUtils.openConnectionStream(metadataPath)) {
SAXReader reader = new SAXReader(mdStream);
reader.parse(new SAXListener() {
......@@ -185,7 +187,7 @@ public class MavenArtifact
return null;
}
public MavenLocalVersion makeLocalVersion(DBRProgressMonitor monitor, String versionStr, boolean setActive) throws IllegalArgumentException {
private MavenLocalVersion makeLocalVersion(DBRProgressMonitor monitor, String versionStr, boolean setActive) throws IllegalArgumentException {
MavenLocalVersion version = getLocalVersion(versionStr);
if (version == null) {
if (!versions.contains(versionStr)) {
......@@ -214,7 +216,9 @@ public class MavenArtifact
monitor.beginTask("Download Maven artifact '" + this + "'", 3);
try {
monitor.subTask("Download metadata from " + repository.getUrl());
loadMetadata();
if (versions.isEmpty()) {
loadMetadata(monitor);
}
monitor.worked(1);
String versionInfo = versionRef;
......@@ -253,11 +257,6 @@ public class MavenArtifact
}
monitor.subTask("Download binaries for version " + versionInfo);
MavenLocalVersion localVersion = getActiveLocalVersion();
if (localVersion != null && !localVersion.getCacheFile().exists()) {
log.debug("Drop previous local version '" + localVersion + "' - jar seems to be dropped");
removeLocalVersion(localVersion);
localVersion = null;
}
if (localVersion == null) {
localVersion = makeLocalVersion(monitor, versionInfo, true);
}
......
......@@ -60,11 +60,10 @@ public class MavenArtifactVersion {
public String get(String name) {
String value = properties.get(name);
if (value == null) {
if (parent != null) {
return parent.getMetaData(VoidProgressMonitor.INSTANCE).variableResolver.get(name);
}
if (name.equals(PROP_PROJECT_VERSION)) {
value = version;
} else if (parent != null) {
return parent.getMetaData(VoidProgressMonitor.INSTANCE).variableResolver.get(name);
}
}
return value;
......@@ -133,6 +132,8 @@ public class MavenArtifactVersion {
private void loadPOM(DBRProgressMonitor monitor) throws IOException {
String pomURL = localVersion.getArtifact().getFileURL(localVersion.getVersion(), MavenArtifact.FILE_POM);
monitor.subTask("Load POM [" + pomURL + "]");
System.out.println("Load POM " + localVersion.getArtifact().toString() + ":" + version);
Document pomDocument;
try (InputStream mdStream = RuntimeUtils.openConnectionStream(pomURL)) {
pomDocument = XMLUtils.parseDocument(mdStream);
......@@ -190,21 +191,31 @@ public class MavenArtifactVersion {
// Dependencies
Element dmElement = XMLUtils.getChildElement(root, "dependencyManagement");
if (dmElement != null) {
dependencyManagement = parseDependencies(dmElement);
dependencyManagement = parseDependencies(monitor, dmElement);
}
dependencies = parseDependencies(root);
dependencies = parseDependencies(monitor, root);
}
}
private List<MavenArtifactDependency> parseDependencies(Element element) {
private List<MavenArtifactDependency> parseDependencies(DBRProgressMonitor monitor, Element element) {
List<MavenArtifactDependency> result = new ArrayList<>();
Element dependenciesElement = XMLUtils.getChildElement(element, "dependencies");
if (dependenciesElement != null) {
for (Element dep : XMLUtils.getChildElementList(dependenciesElement, "dependency")) {
String groupId = XMLUtils.getChildElementBody(dep, "groupId");
String artifactId = XMLUtils.getChildElementBody(dep, "artifactId");
String version = XMLUtils.getChildElementBody(dep, "version");
if (CommonUtils.isEmpty(version)) {
version = findDependencyVersion(monitor, groupId, artifactId);
}
if (version == null) {
log.error("Can't resolve artifact [" + groupId + ":" + artifactId + "] version. Skip.");
continue;
}
MavenArtifactReference depRef = new MavenArtifactReference(
XMLUtils.getChildElementBody(dep, "groupId"),
XMLUtils.getChildElementBody(dep, "artifactId"),
evaluateString(XMLUtils.getChildElementBody(dep, "version"))
groupId,
artifactId,
evaluateString(version)
);
result.add(new MavenArtifactDependency(
depRef,
......@@ -216,6 +227,19 @@ public class MavenArtifactVersion {
return result;
}
private String findDependencyVersion(DBRProgressMonitor monitor, String groupId, String artifactId) {
if (dependencyManagement != null) {
for (MavenArtifactDependency dmArtifact : dependencyManagement) {
if (dmArtifact.getArtifactReference().getGroupId().equals(groupId) &&
dmArtifact.getArtifactReference().getArtifactId().equals(artifactId))
{
return dmArtifact.getArtifactReference().getVersion();
}
}
}
return parent == null ? null : parent.getMetaData(monitor).findDependencyVersion(monitor, groupId, artifactId);
}
private String evaluateString(String value) {
return GeneralUtils.replaceVariables(value, variableResolver);
}
......
......@@ -22,6 +22,7 @@ import org.jkiss.code.NotNull;
import org.jkiss.code.Nullable;
import org.jkiss.dbeaver.Log;
import org.jkiss.dbeaver.core.DBeaverActivator;
import org.jkiss.dbeaver.model.runtime.VoidProgressMonitor;
import org.jkiss.dbeaver.registry.RegistryConstants;
import org.jkiss.utils.CommonUtils;
import org.jkiss.utils.IOUtils;
......@@ -123,7 +124,7 @@ public class MavenRepository
// Not cached - look in remote repository
MavenArtifact artifact = new MavenArtifact(this, groupId, artifactId);
try {
artifact.loadMetadata();
artifact.loadMetadata(VoidProgressMonitor.INSTANCE);
} catch (IOException e) {
log.debug("Artifact [" + artifact + "] not found in repository [" + getUrl() + "]");
return null;
......
......@@ -29,6 +29,7 @@ import org.jkiss.dbeaver.model.DBPDriverLibrary;
import org.jkiss.dbeaver.model.runtime.DBRProgressMonitor;
import org.jkiss.dbeaver.model.runtime.DBRRunnableContext;
import org.jkiss.dbeaver.model.runtime.DBRRunnableWithProgress;
import org.jkiss.dbeaver.model.runtime.VoidProgressMonitor;
import org.jkiss.dbeaver.registry.driver.DriverDescriptor;
import org.jkiss.dbeaver.runtime.RunnableContextDelegate;
import org.jkiss.dbeaver.ui.DBeaverIcons;
......@@ -42,6 +43,8 @@ import java.util.List;
class DriverDownloadAutoPage extends DriverDownloadPage {
private Tree filesTree;
DriverDownloadAutoPage() {
super("Automatic download", "Download driver files", null);
setPageComplete(false);
......@@ -67,18 +70,11 @@ class DriverDownloadAutoPage extends DriverDownloadPage {
{
Group filesGroup = UIUtils.createControlGroup(composite, "Files required by driver", 1, -1, -1);
filesGroup.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
Tree filesTree = new Tree(filesGroup, SWT.BORDER | SWT.FULL_SELECTION);
filesTree = new Tree(filesGroup, SWT.BORDER | SWT.FULL_SELECTION);
filesTree.setHeaderVisible(true);
filesTree.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
UIUtils.createTreeColumn(filesTree, SWT.LEFT, "File");
UIUtils.createTreeColumn(filesTree, SWT.LEFT, "Version");
for (DBPDriverLibrary file : wizard.getFiles()) {
TreeItem item = new TreeItem(filesTree, SWT.NONE);
item.setImage(DBeaverIcons.getImage(file.getIcon()));
item.setText(0, file.getDisplayName());
item.setText(1, "");
}
UIUtils.packColumns(filesTree);
}
if (!wizard.isForceDownload()) {
......@@ -112,10 +108,18 @@ class DriverDownloadAutoPage extends DriverDownloadPage {
} catch (InvocationTargetException e) {
UIUtils.showErrorDialog(null, "Resolve libraries", "Error resolving driver libraries", e.getTargetException());
}
for (DBPDriverLibrary file : getWizard().getFiles()) {
TreeItem item = new TreeItem(filesTree, SWT.NONE);
item.setImage(DBeaverIcons.getImage(file.getIcon()));
item.setText(0, file.getDisplayName());
item.setText(1, "");
}
UIUtils.packColumns(filesTree);
}
private void resolveDependencies(DBRProgressMonitor monitor, DBPDriverLibrary library) throws IOException {
Collection<DBPDriverLibrary> dependencies = library.getDependencies(monitor);
Collection<? extends DBPDriverLibrary> dependencies = library.getDependencies(monitor);
if (dependencies != null && !dependencies.isEmpty()) {
for (DBPDriverLibrary dep : dependencies) {
resolveDependencies(monitor, dep);
......@@ -123,6 +127,20 @@ class DriverDownloadAutoPage extends DriverDownloadPage {
}
}
private void addDependencies(TreeItem parent, DBPDriverLibrary library) throws IOException {
Collection<? extends DBPDriverLibrary> dependencies = library.getDependencies(VoidProgressMonitor.INSTANCE);
if (dependencies != null && !dependencies.isEmpty()) {
for (DBPDriverLibrary dep : dependencies) {
TreeItem item = new TreeItem(parent, SWT.NONE);
item.setImage(DBeaverIcons.getImage(dep.getIcon()));
item.setText(0, dep.getDisplayName());
item.setText(1, "");
addDependencies(item, dep);
}
}
}
@Override
void performFinish() {
downloadLibraryFiles(new RunnableContextDelegate(getContainer()), getWizard().getFiles());
......
......@@ -80,7 +80,7 @@ public interface DBPDriverLibrary
boolean matchesCurrentPlatform();
@Nullable
Collection<DBPDriverLibrary> getDependencies(DBRProgressMonitor monitor) throws IOException;
Collection<? extends DBPDriverLibrary> getDependencies(DBRProgressMonitor monitor) throws IOException;
void downloadLibraryFile(DBRProgressMonitor monitor, boolean forceUpdate)
throws IOException, InterruptedException;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册