diff --git a/plugins/org.jkiss.dbeaver.core/src/org/jkiss/dbeaver/registry/DriverFileDescriptor.java b/plugins/org.jkiss.dbeaver.core/src/org/jkiss/dbeaver/registry/DriverFileDescriptor.java index 525828147673e16795d50db4c515a91b2c3097c2..36eb84d98804e7393b609885c740f06214423f33 100644 --- a/plugins/org.jkiss.dbeaver.core/src/org/jkiss/dbeaver/registry/DriverFileDescriptor.java +++ b/plugins/org.jkiss.dbeaver.core/src/org/jkiss/dbeaver/registry/DriverFileDescriptor.java @@ -38,10 +38,7 @@ import org.jkiss.dbeaver.utils.ContentUtils; import org.jkiss.utils.CommonUtils; import java.io.*; -import java.net.HttpURLConnection; -import java.net.InetSocketAddress; -import java.net.Proxy; -import java.net.URL; +import java.net.*; import java.text.NumberFormat; /** @@ -203,7 +200,7 @@ public class DriverFileDescriptor implements DBPDriverFile if (artifact != null) { MavenLocalVersion localVersion = artifact.getActiveLocalVersion(); if (localVersion != null) { - return localVersion.getExternalURL(); + return localVersion.getExternalURL(MavenArtifact.FILE_JAR); } } return null; @@ -319,36 +316,12 @@ public class DriverFileDescriptor implements DBPDriverFile if (isMavenArtifact()) { 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(); if (externalURL == null) { throw new IOException("Unresolved file reference: " + getPath()); } - URL url = new URL(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()); - } + final URLConnection connection = RuntimeUtils.openConnection(externalURL); monitor.worked(1); monitor.done(); diff --git a/plugins/org.jkiss.dbeaver.core/src/org/jkiss/dbeaver/registry/maven/MavenArtifact.java b/plugins/org.jkiss.dbeaver.core/src/org/jkiss/dbeaver/registry/maven/MavenArtifact.java index d4f447976589ca6cd16af4aefdac1470faf6cb4d..f2fe6930e3622f51495e489359b84ea1aa29cbc1 100644 --- a/plugins/org.jkiss.dbeaver.core/src/org/jkiss/dbeaver/registry/maven/MavenArtifact.java +++ b/plugins/org.jkiss.dbeaver.core/src/org/jkiss/dbeaver/registry/maven/MavenArtifact.java @@ -21,6 +21,7 @@ import org.jkiss.code.NotNull; import org.jkiss.code.Nullable; import org.jkiss.dbeaver.Log; import org.jkiss.dbeaver.model.runtime.DBRProgressMonitor; +import org.jkiss.dbeaver.runtime.RuntimeUtils; import org.jkiss.utils.CommonUtils; import org.jkiss.utils.xml.SAXListener; import org.jkiss.utils.xml.SAXReader; @@ -29,8 +30,6 @@ import org.xml.sax.Attributes; import java.io.IOException; import java.io.InputStream; -import java.net.URL; -import java.net.URLConnection; import java.util.*; import java.util.regex.Pattern; @@ -40,8 +39,12 @@ import java.util.regex.Pattern; public class MavenArtifact { static final Log log = Log.getLog(MavenArtifact.class); + 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 String groupId; private final String artifactId; @@ -62,10 +65,7 @@ public class MavenArtifact public void loadMetadata() throws IOException { String metadataPath = getArtifactDir() + MAVEN_METADATA_XML; - URL url = new URL(metadataPath); - URLConnection connection = url.openConnection(); - connection.connect(); - InputStream mdStream = connection.getInputStream(); + InputStream mdStream = RuntimeUtils.openConnectionStream(metadataPath); try { SAXReader reader = new SAXReader(mdStream); reader.parse(new SAXListener() { @@ -151,13 +151,13 @@ public class MavenArtifact return repository.getUrl() + dir + "/"; } - public String getFileURL(String version) { - return getArtifactDir() + version + "/" + getVersionFileName(version); + public String getFileURL(String version, String fileType) { + return getArtifactDir() + version + "/" + getVersionFileName(version, fileType); } @NotNull - private String getVersionFileName(String version) { - return artifactId + "-" + version + ".jar"; + private String getVersionFileName(String version, String fileType) { + return artifactId + "-" + version + "." + fileType; } @Override @@ -190,7 +190,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), new Date()); + version = new MavenLocalVersion(this, versionStr, getVersionFileName(versionStr, FILE_JAR), new Date()); localVersions.add(version); } if (setActive) { diff --git a/plugins/org.jkiss.dbeaver.core/src/org/jkiss/dbeaver/registry/maven/MavenArtifactVersion.java b/plugins/org.jkiss.dbeaver.core/src/org/jkiss/dbeaver/registry/maven/MavenArtifactVersion.java new file mode 100644 index 0000000000000000000000000000000000000000..70cf5450139a30f1505123e744cc3935037591f5 --- /dev/null +++ b/plugins/org.jkiss.dbeaver.core/src/org/jkiss/dbeaver/registry/maven/MavenArtifactVersion.java @@ -0,0 +1,123 @@ +/* + * 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 licenses; + private List 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 getLicenses() { + return licenses; + } + + public List 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(); + } + +} diff --git a/plugins/org.jkiss.dbeaver.core/src/org/jkiss/dbeaver/registry/maven/MavenLocalVersion.java b/plugins/org.jkiss.dbeaver.core/src/org/jkiss/dbeaver/registry/maven/MavenLocalVersion.java index 15e7c08f754236be07ad522c0d87a8507a7b38f8..331a78413a5c48b151e0fa7384d04bcd83b4287b 100644 --- a/plugins/org.jkiss.dbeaver.core/src/org/jkiss/dbeaver/registry/maven/MavenLocalVersion.java +++ b/plugins/org.jkiss.dbeaver.core/src/org/jkiss/dbeaver/registry/maven/MavenLocalVersion.java @@ -21,7 +21,9 @@ import java.io.File; 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 { @@ -37,6 +39,10 @@ public class MavenLocalVersion this.updateTime = updateTime; } + public MavenArtifact getArtifact() { + return artifact; + } + public String getVersion() { return version; } @@ -58,7 +64,7 @@ public class MavenLocalVersion return new File(artifact.getRepository().getLocalCacheDir(), artifact.getGroupId() + "/" + fileName); } - public String getExternalURL() { - return artifact.getFileURL(version); + public String getExternalURL(String fileType) { + return artifact.getFileURL(version, fileType); } } diff --git a/plugins/org.jkiss.dbeaver.core/src/org/jkiss/dbeaver/registry/updater/VersionDescriptor.java b/plugins/org.jkiss.dbeaver.core/src/org/jkiss/dbeaver/registry/updater/VersionDescriptor.java index a0a5fa8e0b4723e0601abc5d3ef8ee5eb6792e64..e521777ecf77e28cf77cd18856414f8efd7dbd88 100644 --- a/plugins/org.jkiss.dbeaver.core/src/org/jkiss/dbeaver/registry/updater/VersionDescriptor.java +++ b/plugins/org.jkiss.dbeaver.core/src/org/jkiss/dbeaver/registry/updater/VersionDescriptor.java @@ -18,6 +18,7 @@ package org.jkiss.dbeaver.registry.updater; +import org.jkiss.dbeaver.runtime.RuntimeUtils; import org.jkiss.dbeaver.utils.ContentUtils; import org.jkiss.utils.CommonUtils; import org.jkiss.utils.xml.XMLException; @@ -28,8 +29,6 @@ import org.w3c.dom.Element; import java.io.IOException; import java.io.InputStream; -import java.net.URL; -import java.net.URLConnection; import java.util.ArrayList; import java.util.Collection; import java.util.List; @@ -53,26 +52,16 @@ public class VersionDescriptor { public VersionDescriptor(String fileAddr) throws IOException { - URL url = new URL(fileAddr); - URLConnection connection = url.openConnection(); try { - connection.setConnectTimeout(10000); - connection.setReadTimeout(10000); - connection.connect(); + InputStream inputStream = RuntimeUtils.openConnectionStream(fileAddr); try { - InputStream inputStream = connection.getInputStream(); - try { - Document document = XMLUtils.parseDocument(inputStream); - parseVersionInfo(document); - } finally { - ContentUtils.close(inputStream); - } - } catch (XMLException e) { - throw new IOException("XML parse error", e); + Document document = XMLUtils.parseDocument(inputStream); + parseVersionInfo(document); + } finally { + ContentUtils.close(inputStream); } - } - finally { - // nothing + } catch (XMLException e) { + throw new IOException("XML parse error", e); } } diff --git a/plugins/org.jkiss.dbeaver.core/src/org/jkiss/dbeaver/runtime/RuntimeUtils.java b/plugins/org.jkiss.dbeaver.core/src/org/jkiss/dbeaver/runtime/RuntimeUtils.java index 28955e734e515478fee1c6e7061df914cf17c63d..b0f6fefdc7a5480636443ba8098230d9e1d64d8c 100644 --- a/plugins/org.jkiss.dbeaver.core/src/org/jkiss/dbeaver/runtime/RuntimeUtils.java +++ b/plugins/org.jkiss.dbeaver.core/src/org/jkiss/dbeaver/runtime/RuntimeUtils.java @@ -22,9 +22,11 @@ import org.eclipse.core.runtime.jobs.Job; import org.eclipse.jface.operation.IRunnableContext; import org.eclipse.jface.operation.IRunnableWithProgress; import org.eclipse.swt.program.Program; +import org.jkiss.dbeaver.DBeaverPreferences; import org.jkiss.dbeaver.Log; import org.jkiss.dbeaver.core.CoreMessages; 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.DBRRunnableWithProgress; import org.jkiss.dbeaver.runtime.load.ILoadService; @@ -32,13 +34,13 @@ import org.jkiss.dbeaver.runtime.load.ILoadVisualizer; import org.jkiss.dbeaver.runtime.load.jobs.LoadingJob; import org.jkiss.dbeaver.utils.GeneralUtils; import org.jkiss.utils.ArrayUtils; +import org.jkiss.utils.CommonUtils; import java.io.File; import java.io.IOException; +import java.io.InputStream; import java.lang.reflect.InvocationTargetException; -import java.net.URI; -import java.net.URISyntaxException; -import java.net.URL; +import java.net.*; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Locale; @@ -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; + } + } diff --git a/plugins/org.jkiss.dbeaver.model/src/org/jkiss/dbeaver/utils/ContentUtils.java b/plugins/org.jkiss.dbeaver.model/src/org/jkiss/dbeaver/utils/ContentUtils.java index 4ff4b60ccf424a8911855d26076f415d489bd573..4ae036f5c08d2122e9b6ec241267334950fd8ef0 100644 --- a/plugins/org.jkiss.dbeaver.model/src/org/jkiss/dbeaver/utils/ContentUtils.java +++ b/plugins/org.jkiss.dbeaver.model/src/org/jkiss/dbeaver/utils/ContentUtils.java @@ -22,11 +22,11 @@ import org.eclipse.core.resources.*; import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.IPath; 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.ModelPreferences; import org.jkiss.dbeaver.model.DBPApplication; import org.jkiss.dbeaver.model.data.DBDContent; +import org.jkiss.dbeaver.model.messages.ModelMessages; import org.jkiss.dbeaver.model.runtime.DBRProgressMonitor; import org.jkiss.utils.ArrayUtils;