提交 7b8920ca 编写于 作者: J jurgen

Maven artifact version info reading (POM)

UR connections refactored.

Former-commit-id: b4e55c57
上级 7b223f06
...@@ -38,10 +38,7 @@ import org.jkiss.dbeaver.utils.ContentUtils; ...@@ -38,10 +38,7 @@ import org.jkiss.dbeaver.utils.ContentUtils;
import org.jkiss.utils.CommonUtils; import org.jkiss.utils.CommonUtils;
import java.io.*; import java.io.*;
import java.net.HttpURLConnection; import java.net.*;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.URL;
import java.text.NumberFormat; import java.text.NumberFormat;
/** /**
...@@ -203,7 +200,7 @@ public class DriverFileDescriptor implements DBPDriverFile ...@@ -203,7 +200,7 @@ public class DriverFileDescriptor implements DBPDriverFile
if (artifact != null) { if (artifact != null) {
MavenLocalVersion localVersion = artifact.getActiveLocalVersion(); MavenLocalVersion localVersion = artifact.getActiveLocalVersion();
if (localVersion != null) { if (localVersion != null) {
return localVersion.getExternalURL(); return localVersion.getExternalURL(MavenArtifact.FILE_JAR);
} }
} }
return null; return null;
...@@ -319,36 +316,12 @@ public class DriverFileDescriptor implements DBPDriverFile ...@@ -319,36 +316,12 @@ public class DriverFileDescriptor implements DBPDriverFile
if (isMavenArtifact()) { if (isMavenArtifact()) {
downloadMavenArtifact(monitor); downloadMavenArtifact(monitor);
} }
DBPPreferenceStore prefs = DBeaverCore.getGlobalPreferenceStore();
String proxyHost = prefs.getString(DBeaverPreferences.UI_PROXY_HOST);
Proxy proxy = null;
if (!CommonUtils.isEmpty(proxyHost)) {
int proxyPort = prefs.getInt(DBeaverPreferences.UI_PROXY_PORT);
if (proxyPort <= 0) {
log.warn("Invalid proxy port: " + proxyPort);
}
proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyHost, proxyPort));
}
String externalURL = getExternalURL(); String externalURL = getExternalURL();
if (externalURL == null) { if (externalURL == null) {
throw new IOException("Unresolved file reference: " + getPath()); throw new IOException("Unresolved file reference: " + getPath());
} }
URL url = new URL(externalURL); final URLConnection connection = RuntimeUtils.openConnection(externalURL);
monitor.beginTask("Check file " + url.toString() + "...", 1);
monitor.subTask("Connecting to the server");
final HttpURLConnection connection = (HttpURLConnection) (proxy == null ? url.openConnection() : url.openConnection(proxy));
connection.setReadTimeout(10000);
connection.setConnectTimeout(10000);
connection.setRequestMethod("GET"); //$NON-NLS-1$
connection.setInstanceFollowRedirects(true);
connection.setRequestProperty(
"User-Agent", //$NON-NLS-1$
DBeaverCore.getProductTitle());
connection.connect();
if (connection.getResponseCode() != 200) {
throw new IOException("Can't find driver file '" + url + "': " + connection.getResponseMessage());
}
monitor.worked(1); monitor.worked(1);
monitor.done(); monitor.done();
......
...@@ -21,6 +21,7 @@ import org.jkiss.code.NotNull; ...@@ -21,6 +21,7 @@ import org.jkiss.code.NotNull;
import org.jkiss.code.Nullable; import org.jkiss.code.Nullable;
import org.jkiss.dbeaver.Log; import org.jkiss.dbeaver.Log;
import org.jkiss.dbeaver.model.runtime.DBRProgressMonitor; import org.jkiss.dbeaver.model.runtime.DBRProgressMonitor;
import org.jkiss.dbeaver.runtime.RuntimeUtils;
import org.jkiss.utils.CommonUtils; import org.jkiss.utils.CommonUtils;
import org.jkiss.utils.xml.SAXListener; import org.jkiss.utils.xml.SAXListener;
import org.jkiss.utils.xml.SAXReader; import org.jkiss.utils.xml.SAXReader;
...@@ -29,8 +30,6 @@ import org.xml.sax.Attributes; ...@@ -29,8 +30,6 @@ import org.xml.sax.Attributes;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.*; import java.util.*;
import java.util.regex.Pattern; import java.util.regex.Pattern;
...@@ -40,8 +39,12 @@ import java.util.regex.Pattern; ...@@ -40,8 +39,12 @@ import java.util.regex.Pattern;
public class MavenArtifact public class MavenArtifact
{ {
static final Log log = Log.getLog(MavenArtifact.class); static final Log log = Log.getLog(MavenArtifact.class);
public static final String MAVEN_METADATA_XML = "maven-metadata.xml"; public static final String MAVEN_METADATA_XML = "maven-metadata.xml";
public static final String FILE_JAR = "jar";
public static final String FILE_POM = "pom";
private final MavenRepository repository; private final MavenRepository repository;
private final String groupId; private final String groupId;
private final String artifactId; private final String artifactId;
...@@ -62,10 +65,7 @@ public class MavenArtifact ...@@ -62,10 +65,7 @@ public class MavenArtifact
public void loadMetadata() throws IOException { public void loadMetadata() throws IOException {
String metadataPath = getArtifactDir() + MAVEN_METADATA_XML; String metadataPath = getArtifactDir() + MAVEN_METADATA_XML;
URL url = new URL(metadataPath); InputStream mdStream = RuntimeUtils.openConnectionStream(metadataPath);
URLConnection connection = url.openConnection();
connection.connect();
InputStream mdStream = connection.getInputStream();
try { try {
SAXReader reader = new SAXReader(mdStream); SAXReader reader = new SAXReader(mdStream);
reader.parse(new SAXListener() { reader.parse(new SAXListener() {
...@@ -151,13 +151,13 @@ public class MavenArtifact ...@@ -151,13 +151,13 @@ public class MavenArtifact
return repository.getUrl() + dir + "/"; return repository.getUrl() + dir + "/";
} }
public String getFileURL(String version) { public String getFileURL(String version, String fileType) {
return getArtifactDir() + version + "/" + getVersionFileName(version); return getArtifactDir() + version + "/" + getVersionFileName(version, fileType);
} }
@NotNull @NotNull
private String getVersionFileName(String version) { private String getVersionFileName(String version, String fileType) {
return artifactId + "-" + version + ".jar"; return artifactId + "-" + version + "." + fileType;
} }
@Override @Override
...@@ -190,7 +190,7 @@ public class MavenArtifact ...@@ -190,7 +190,7 @@ public class MavenArtifact
// No version info. Some artifacts do not have older versions in metadata.xml so just warn // 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"); log.debug("Artifact '" + artifactId + "' do not have version '" + versionStr + "' info in metadata");
} }
version = new MavenLocalVersion(this, versionStr, getVersionFileName(versionStr), new Date()); version = new MavenLocalVersion(this, versionStr, getVersionFileName(versionStr, FILE_JAR), new Date());
localVersions.add(version); localVersions.add(version);
} }
if (setActive) { if (setActive) {
......
/*
* 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.maven;
import org.jkiss.dbeaver.Log;
import org.jkiss.dbeaver.runtime.RuntimeUtils;
import org.jkiss.utils.xml.SAXListener;
import org.jkiss.utils.xml.SAXReader;
import org.jkiss.utils.xml.XMLException;
import org.xml.sax.Attributes;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
/**
* Maven artifact version descriptor (POM).
*/
public class MavenArtifactVersion
{
static final Log log = Log.getLog(MavenArtifactVersion.class);
private MavenLocalVersion localVersion;
private String name;
private String version;
private String description;
private String url;
private List<String> licenses;
private List<MavenArtifactReference> dependencies;
public MavenArtifactVersion(MavenLocalVersion localVersion) throws IOException {
this.localVersion = localVersion;
loadPOM();
}
public MavenLocalVersion getLocalVersion() {
return localVersion;
}
public String getName() {
return name;
}
public String getVersion() {
return version;
}
public String getDescription() {
return description;
}
public String getUrl() {
return url;
}
public List<String> getLicenses() {
return licenses;
}
public List<MavenArtifactReference> getDependencies() {
return dependencies;
}
private void loadPOM() throws IOException {
String pomURL = localVersion.getArtifact().getFileURL(localVersion.getVersion(), MavenArtifact.FILE_POM);
InputStream mdStream = RuntimeUtils.openConnectionStream(pomURL);
try {
SAXReader reader = new SAXReader(mdStream);
reader.parse(new SAXListener() {
private boolean inRoot = true;
private String lastTag;
@Override
public void saxStartElement(SAXReader reader, String namespaceURI, String localName, Attributes atts) throws XMLException {
lastTag = localName;
}
@Override
public void saxText(SAXReader reader, String data) throws XMLException {
if (inRoot) {
if ("name".equals(lastTag)) {
name = data;
} else if ("version".equals(lastTag)) {
version = data;
} else if ("description".equals(lastTag)) {
description = data;
} else if ("url".equals(lastTag)) {
url = data;
}
}
}
@Override
public void saxEndElement(SAXReader reader, String namespaceURI, String localName) throws XMLException {
lastTag = null;
}
});
} catch (XMLException e) {
log.warn("Error parsing POM", e);
} finally {
mdStream.close();
}
}
@Override
public String toString() {
return localVersion.toString();
}
}
...@@ -21,7 +21,9 @@ import java.io.File; ...@@ -21,7 +21,9 @@ import java.io.File;
import java.util.Date; import java.util.Date;
/** /**
* Maven artifact descriptor * Maven artifact version info.
* It is a resolved version information. This version exists in maven repository and can be obtained.
* Also it is cached locally.
*/ */
public class MavenLocalVersion public class MavenLocalVersion
{ {
...@@ -37,6 +39,10 @@ public class MavenLocalVersion ...@@ -37,6 +39,10 @@ public class MavenLocalVersion
this.updateTime = updateTime; this.updateTime = updateTime;
} }
public MavenArtifact getArtifact() {
return artifact;
}
public String getVersion() { public String getVersion() {
return version; return version;
} }
...@@ -58,7 +64,7 @@ public class MavenLocalVersion ...@@ -58,7 +64,7 @@ public class MavenLocalVersion
return new File(artifact.getRepository().getLocalCacheDir(), artifact.getGroupId() + "/" + fileName); return new File(artifact.getRepository().getLocalCacheDir(), artifact.getGroupId() + "/" + fileName);
} }
public String getExternalURL() { public String getExternalURL(String fileType) {
return artifact.getFileURL(version); return artifact.getFileURL(version, fileType);
} }
} }
...@@ -18,6 +18,7 @@ ...@@ -18,6 +18,7 @@
package org.jkiss.dbeaver.registry.updater; package org.jkiss.dbeaver.registry.updater;
import org.jkiss.dbeaver.runtime.RuntimeUtils;
import org.jkiss.dbeaver.utils.ContentUtils; import org.jkiss.dbeaver.utils.ContentUtils;
import org.jkiss.utils.CommonUtils; import org.jkiss.utils.CommonUtils;
import org.jkiss.utils.xml.XMLException; import org.jkiss.utils.xml.XMLException;
...@@ -28,8 +29,6 @@ import org.w3c.dom.Element; ...@@ -28,8 +29,6 @@ import org.w3c.dom.Element;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.List; import java.util.List;
...@@ -53,26 +52,16 @@ public class VersionDescriptor { ...@@ -53,26 +52,16 @@ public class VersionDescriptor {
public VersionDescriptor(String fileAddr) public VersionDescriptor(String fileAddr)
throws IOException throws IOException
{ {
URL url = new URL(fileAddr);
URLConnection connection = url.openConnection();
try { try {
connection.setConnectTimeout(10000); InputStream inputStream = RuntimeUtils.openConnectionStream(fileAddr);
connection.setReadTimeout(10000);
connection.connect();
try { try {
InputStream inputStream = connection.getInputStream(); Document document = XMLUtils.parseDocument(inputStream);
try { parseVersionInfo(document);
Document document = XMLUtils.parseDocument(inputStream); } finally {
parseVersionInfo(document); ContentUtils.close(inputStream);
} finally {
ContentUtils.close(inputStream);
}
} catch (XMLException e) {
throw new IOException("XML parse error", e);
} }
} } catch (XMLException e) {
finally { throw new IOException("XML parse error", e);
// nothing
} }
} }
......
...@@ -22,9 +22,11 @@ import org.eclipse.core.runtime.jobs.Job; ...@@ -22,9 +22,11 @@ import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.jface.operation.IRunnableContext; import org.eclipse.jface.operation.IRunnableContext;
import org.eclipse.jface.operation.IRunnableWithProgress; import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.swt.program.Program; import org.eclipse.swt.program.Program;
import org.jkiss.dbeaver.DBeaverPreferences;
import org.jkiss.dbeaver.Log; import org.jkiss.dbeaver.Log;
import org.jkiss.dbeaver.core.CoreMessages; import org.jkiss.dbeaver.core.CoreMessages;
import org.jkiss.dbeaver.core.DBeaverCore; import org.jkiss.dbeaver.core.DBeaverCore;
import org.jkiss.dbeaver.model.DBPPreferenceStore;
import org.jkiss.dbeaver.model.runtime.DBRProgressMonitor; import org.jkiss.dbeaver.model.runtime.DBRProgressMonitor;
import org.jkiss.dbeaver.model.runtime.DBRRunnableWithProgress; import org.jkiss.dbeaver.model.runtime.DBRRunnableWithProgress;
import org.jkiss.dbeaver.runtime.load.ILoadService; import org.jkiss.dbeaver.runtime.load.ILoadService;
...@@ -32,13 +34,13 @@ import org.jkiss.dbeaver.runtime.load.ILoadVisualizer; ...@@ -32,13 +34,13 @@ import org.jkiss.dbeaver.runtime.load.ILoadVisualizer;
import org.jkiss.dbeaver.runtime.load.jobs.LoadingJob; import org.jkiss.dbeaver.runtime.load.jobs.LoadingJob;
import org.jkiss.dbeaver.utils.GeneralUtils; import org.jkiss.dbeaver.utils.GeneralUtils;
import org.jkiss.utils.ArrayUtils; import org.jkiss.utils.ArrayUtils;
import org.jkiss.utils.CommonUtils;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.InvocationTargetException;
import java.net.URI; import java.net.*;
import java.net.URISyntaxException;
import java.net.URL;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.Date; import java.util.Date;
import java.util.Locale; import java.util.Locale;
...@@ -260,4 +262,37 @@ public class RuntimeUtils { ...@@ -260,4 +262,37 @@ public class RuntimeUtils {
} }
} }
public static InputStream openConnectionStream(String urlString) throws IOException {
URLConnection connection = openConnection(urlString);
return connection.getInputStream();
}
public static URLConnection openConnection(String urlString) throws IOException {
DBPPreferenceStore prefs = DBeaverCore.getGlobalPreferenceStore();
String proxyHost = prefs.getString(DBeaverPreferences.UI_PROXY_HOST);
Proxy proxy = null;
if (!CommonUtils.isEmpty(proxyHost)) {
int proxyPort = prefs.getInt(DBeaverPreferences.UI_PROXY_PORT);
if (proxyPort <= 0) {
log.warn("Invalid proxy port: " + proxyPort);
}
proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyHost, proxyPort));
}
URL url = new URL(urlString);
final HttpURLConnection connection = (HttpURLConnection) (proxy == null ? url.openConnection() : url.openConnection(proxy));
connection.setReadTimeout(10000);
connection.setConnectTimeout(10000);
connection.setRequestMethod("GET"); //$NON-NLS-1$
connection.setInstanceFollowRedirects(true);
connection.setRequestProperty(
"User-Agent", //$NON-NLS-1$
DBeaverCore.getProductTitle());
connection.connect();
if (connection.getResponseCode() != 200) {
throw new IOException("File not found '" + urlString + "': " + connection.getResponseMessage());
}
return connection;
}
} }
...@@ -22,11 +22,11 @@ import org.eclipse.core.resources.*; ...@@ -22,11 +22,11 @@ import org.eclipse.core.resources.*;
import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath; import org.eclipse.core.runtime.IPath;
import org.jkiss.code.Nullable; import org.jkiss.code.Nullable;
import org.jkiss.dbeaver.ModelPreferences;
import org.jkiss.dbeaver.model.messages.ModelMessages;
import org.jkiss.dbeaver.Log; import org.jkiss.dbeaver.Log;
import org.jkiss.dbeaver.ModelPreferences;
import org.jkiss.dbeaver.model.DBPApplication; import org.jkiss.dbeaver.model.DBPApplication;
import org.jkiss.dbeaver.model.data.DBDContent; import org.jkiss.dbeaver.model.data.DBDContent;
import org.jkiss.dbeaver.model.messages.ModelMessages;
import org.jkiss.dbeaver.model.runtime.DBRProgressMonitor; import org.jkiss.dbeaver.model.runtime.DBRProgressMonitor;
import org.jkiss.utils.ArrayUtils; import org.jkiss.utils.ArrayUtils;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册