提交 4d127287 编写于 作者: J jurgen

Registry refactoring

上级 78e6cf3d
......@@ -58,15 +58,6 @@ public class DriverClassLoader extends URLClassLoader
}
}
}
final Collection<String> pathList = driver.getOrderedPathList();
if (!CommonUtils.isEmpty(pathList)) {
for (String path : pathList) {
File localFile = new File(path, nativeName);
if (localFile.exists()) {
return localFile.getAbsolutePath();
}
}
}
return super.findLibrary(libname);
}
}
......@@ -104,7 +104,6 @@ public class DriverDescriptor extends AbstractDescriptor implements DBPDriver
private final List<DriverFileSource> fileSources = new ArrayList<DriverFileSource>();
private final List<DriverLibraryDescriptor> files = new ArrayList<DriverLibraryDescriptor>();
private final List<DriverLibraryDescriptor> origFiles = new ArrayList<DriverLibraryDescriptor>();
private final List<DriverPathDescriptor> pathList = new ArrayList<DriverPathDescriptor>();
private final List<DBPPropertyDescriptor> connectionPropertyDescriptors = new ArrayList<DBPPropertyDescriptor>();
private final List<OSDescriptor> supportedSystems = new ArrayList<OSDescriptor>();
......@@ -683,36 +682,11 @@ public class DriverDescriptor extends AbstractDescriptor implements DBPDriver
}
}
public Collection<String> getOrderedPathList()
{
if (pathList.isEmpty()) {
return Collections.emptyList();
}
List<String> result = new ArrayList<String>(pathList.size());
for (DriverPathDescriptor path : pathList) {
if (path.isEnabled()) {
result.add(path.getPath());
}
}
return result;
}
@NotNull
public List<DriverFileSource> getDriverFileSources() {
return fileSources;
}
@Override
public Collection<DriverPathDescriptor> getPathList()
{
return pathList;
}
void addPath(DriverPathDescriptor path)
{
pathList.add(path);
}
@Override
public List<DBPPropertyDescriptor> getConnectionPropertyDescriptors()
{
......@@ -951,7 +925,7 @@ public class DriverDescriptor extends AbstractDescriptor implements DBPDriver
public void run(DBRProgressMonitor monitor) throws InvocationTargetException, InterruptedException
{
try {
file.downloadLibraryFile(monitor, false);
DriverFileManager.downloadLibraryFile(monitor, file, false);
} catch (final Exception e) {
log.warn("Can't obtain driver license", e);
}
......@@ -1073,17 +1047,6 @@ public class DriverDescriptor extends AbstractDescriptor implements DBPDriver
xml.endElement();
}
// Path list
for (DriverPathDescriptor path : this.getPathList()) {
xml.startElement(RegistryConstants.TAG_PATH);
xml.addAttribute(RegistryConstants.ATTR_PATH, path.getPath());
if (!CommonUtils.isEmpty(path.getComment())) {
xml.addAttribute(RegistryConstants.ATTR_COMMENT, path.getComment());
}
xml.addAttribute(RegistryConstants.ATTR_ENABLED, path.isEnabled());
xml.endElement();
}
// Parameters
for (Map.Entry<Object, Object> paramEntry : customParameters.entrySet()) {
if (!CommonUtils.equalObjects(paramEntry.getValue(), defaultParameters.get(paramEntry.getKey()))) {
......@@ -1248,12 +1211,6 @@ public class DriverDescriptor extends AbstractDescriptor implements DBPDriver
}
} else if (localName.equals(RegistryConstants.TAG_CLIENT_HOME)) {
curDriver.addClientHomeId(atts.getValue(RegistryConstants.ATTR_ID));
} else if (localName.equals(RegistryConstants.TAG_PATH)) {
DriverPathDescriptor path = new DriverPathDescriptor();
path.setPath(atts.getValue(RegistryConstants.ATTR_PATH));
path.setComment(atts.getValue(RegistryConstants.ATTR_COMMENT));
path.setEnabled(CommonUtils.getBoolean(atts.getValue(RegistryConstants.ATTR_ENABLED), true));
curDriver.addPath(path);
} else if (localName.equals(RegistryConstants.TAG_PARAMETER)) {
if (curDriver == null) {
log.warn("Parameter outside of driver");
......
/*
* 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;
import org.jkiss.dbeaver.Log;
import org.jkiss.dbeaver.model.runtime.DBRProgressMonitor;
import org.jkiss.dbeaver.registry.maven.MavenArtifact;
import org.jkiss.dbeaver.registry.maven.MavenArtifactReference;
import org.jkiss.dbeaver.registry.maven.MavenLocalVersion;
import org.jkiss.dbeaver.registry.maven.MavenRegistry;
import org.jkiss.dbeaver.runtime.RuntimeUtils;
import org.jkiss.dbeaver.utils.ContentUtils;
import java.io.*;
import java.net.URLConnection;
import java.text.NumberFormat;
/**
* DriverFileManager
*/
public class DriverFileManager
{
static final Log log = Log.getLog(DriverFileManager.class);
public static void downloadLibraryFile(DBRProgressMonitor monitor, DriverLibraryDescriptor library, boolean updateVersion) throws IOException, InterruptedException
{
if (library.isMavenArtifact()) {
MavenArtifact artifact = downloadMavenArtifact(monitor, library, updateVersion);
if (artifact.getRepository().isLocal()) {
// No need to download local artifacts
return;
}
}
String externalURL = library.getExternalURL();
if (externalURL == null) {
throw new IOException("Unresolved file reference: " + library.getPath());
}
final URLConnection connection = RuntimeUtils.openConnection(externalURL);
monitor.worked(1);
monitor.done();
final int contentLength = connection.getContentLength();
monitor.beginTask("Download " + externalURL, contentLength);
boolean success = false;
final File localFile = library.getLocalFile();
if (localFile == null) {
throw new IOException("No target file for '" + library.getPath() + "'");
}
final File localDir = localFile.getParentFile();
if (!localDir.exists()) {
if (!localDir.mkdirs()) {
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 {
final NumberFormat numberFormat = NumberFormat.getNumberInstance();
byte[] buffer = new byte[10000];
int totalRead = 0;
for (;;) {
if (monitor.isCanceled()) {
throw new InterruptedException();
}
monitor.subTask(numberFormat.format(totalRead) + "/" + numberFormat.format(contentLength));
final int count = inputStream.read(buffer);
if (count <= 0) {
success = true;
break;
}
outputStream.write(buffer, 0, count);
monitor.worked(count);
totalRead += count;
}
}
finally {
ContentUtils.close(inputStream);
}
} finally {
ContentUtils.close(outputStream);
if (!success) {
if (!localFile.delete()) {
DriverLibraryDescriptor.log.warn("Can't delete local driver file '" + localFile.getAbsolutePath() + "'");
}
}
}
monitor.done();
}
private static MavenArtifact downloadMavenArtifact(DBRProgressMonitor monitor, DriverLibraryDescriptor library, boolean updateVersion) throws IOException {
MavenArtifactReference artifactInfo = new MavenArtifactReference(library.getPath());
if (updateVersion) {
MavenRegistry.getInstance().resetArtifactInfo(artifactInfo);
}
MavenArtifact artifact = MavenRegistry.getInstance().findArtifact(artifactInfo);
if (artifact == null) {
throw new IOException("Maven artifact '" + library.getPath() + "' not found");
}
if (updateVersion) {
artifact.loadMetadata();
} else {
MavenLocalVersion localVersion = artifact.getActiveLocalVersion();
if (localVersion != null && localVersion.getCacheFile().exists()) {
// Already cached
return artifact;
}
}
artifact.resolveVersion(monitor, artifactInfo.getVersion());
return artifact;
}
}
......@@ -25,7 +25,6 @@ import org.jkiss.code.Nullable;
import org.jkiss.dbeaver.Log;
import org.jkiss.dbeaver.core.DBeaverCore;
import org.jkiss.dbeaver.model.DBPDriverLibrary;
import org.jkiss.dbeaver.model.runtime.DBRProgressMonitor;
import org.jkiss.dbeaver.model.runtime.OSDescriptor;
import org.jkiss.dbeaver.model.runtime.VoidProgressMonitor;
import org.jkiss.dbeaver.registry.maven.MavenArtifact;
......@@ -33,12 +32,10 @@ import org.jkiss.dbeaver.registry.maven.MavenArtifactReference;
import org.jkiss.dbeaver.registry.maven.MavenLocalVersion;
import org.jkiss.dbeaver.registry.maven.MavenRegistry;
import org.jkiss.dbeaver.runtime.RuntimeUtils;
import org.jkiss.dbeaver.utils.ContentUtils;
import java.io.*;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.net.URLConnection;
import java.text.NumberFormat;
import java.util.Collection;
/**
......@@ -196,6 +193,7 @@ public class DriverLibraryDescriptor implements DBPDriverLibrary
}
@Nullable
@Override
public String getExternalURL() {
if (path.startsWith(FILE_SOURCE_PLATFORM)) {
return path;
......@@ -287,95 +285,6 @@ public class DriverLibraryDescriptor implements DBPDriverLibrary
return null;
}
public void downloadLibraryFile(DBRProgressMonitor monitor, boolean updateVersion) throws IOException, InterruptedException
{
if (isMavenArtifact()) {
MavenArtifact artifact = downloadMavenArtifact(monitor, updateVersion);
if (artifact.getRepository().isLocal()) {
// No need to download local artifacts
return;
}
}
String externalURL = getExternalURL();
if (externalURL == null) {
throw new IOException("Unresolved file reference: " + getPath());
}
final URLConnection connection = RuntimeUtils.openConnection(externalURL);
monitor.worked(1);
monitor.done();
final int contentLength = connection.getContentLength();
monitor.beginTask("Download " + externalURL, contentLength);
boolean success = false;
final File localFile = getLocalFile();
if (localFile == null) {
throw new IOException("No target file for '" + getPath() + "'");
}
final File localDir = localFile.getParentFile();
if (!localDir.exists()) {
if (!localDir.mkdirs()) {
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 {
final NumberFormat numberFormat = NumberFormat.getNumberInstance();
byte[] buffer = new byte[10000];
int totalRead = 0;
for (;;) {
if (monitor.isCanceled()) {
throw new InterruptedException();
}
monitor.subTask(numberFormat.format(totalRead) + "/" + numberFormat.format(contentLength));
final int count = inputStream.read(buffer);
if (count <= 0) {
success = true;
break;
}
outputStream.write(buffer, 0, count);
monitor.worked(count);
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();
}
private MavenArtifact downloadMavenArtifact(DBRProgressMonitor monitor, boolean updateVersion) throws IOException {
MavenArtifactReference artifactInfo = new MavenArtifactReference(path);
if (updateVersion) {
MavenRegistry.getInstance().resetArtifactInfo(artifactInfo);
}
MavenArtifact artifact = MavenRegistry.getInstance().findArtifact(artifactInfo);
if (artifact == null) {
throw new IOException("Maven artifact '" + path + "' not found");
}
if (updateVersion) {
artifact.loadMetadata();
} else {
MavenLocalVersion localVersion = artifact.getActiveLocalVersion();
if (localVersion != null && localVersion.getCacheFile().exists()) {
// Already cached
return artifact;
}
}
artifact.resolveVersion(monitor, artifactInfo.getVersion());
return artifact;
}
@NotNull
public String getDisplayName() {
if (isMavenArtifact()) {
......
/*
* 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;
import org.jkiss.dbeaver.Log;
import org.jkiss.dbeaver.model.DBPDriverLocalPath;
/**
* DriverPathDescriptor
*/
public class DriverPathDescriptor implements DBPDriverLocalPath
{
static final Log log = Log.getLog(DriverPathDescriptor.class);
private String path;
private String comment;
private boolean enabled;
@Override
public String getPath()
{
return path;
}
public void setPath(String path)
{
this.path = path;
}
@Override
public String getComment()
{
return comment;
}
public void setComment(String comment)
{
this.comment = comment;
}
public boolean isEnabled()
{
return enabled;
}
public void setEnabled(boolean enabled)
{
this.enabled = enabled;
}
}
......@@ -28,6 +28,7 @@ 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.registry.DriverDescriptor;
import org.jkiss.dbeaver.registry.DriverFileManager;
import org.jkiss.dbeaver.registry.DriverLibraryDescriptor;
import org.jkiss.dbeaver.runtime.RunnableContextDelegate;
import org.jkiss.dbeaver.ui.UIUtils;
......@@ -120,7 +121,7 @@ class DriverDownloadAutoPage extends DriverDownloadPage {
@Override
public void run(DBRProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
try {
file.downloadLibraryFile(monitor, getWizard().isUpdateVersion());
DriverFileManager.downloadLibraryFile(monitor, file, getWizard().isUpdateVersion());
} catch (IOException e) {
throw new InvocationTargetException(e);
}
......
......@@ -77,8 +77,6 @@ public interface DBPDriver extends DBPObject
Object getDriverParameter(String name);
Collection<? extends DBPDriverLocalPath> getPathList();
boolean isSupportedByLocalSystem();
Collection<String> getClientHomeIds();
......
......@@ -58,6 +58,9 @@ public interface DBPDriverLibrary
boolean isDownloadable();
@Nullable
String getExternalURL();
@Nullable
File getLocalFile();
......
/*
* 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.model;
/**
* DBPDriver local path
*/
public interface DBPDriverLocalPath
{
String getPath();
String getComment();
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册