提交 33f18e93 编写于 作者: S Serge Rider

Utilities model


Former-commit-id: e9e8bc1c
上级 562ec363
......@@ -74,6 +74,7 @@ Export-Package: org.jkiss.dbeaver,
org.jkiss.dbeaver.runtime.resource.links,
org.jkiss.dbeaver.runtime.ui,
org.jkiss.dbeaver.runtime.ui.console,
org.jkiss.dbeaver.runtime.utils,
org.jkiss.dbeaver.utils
Bundle-ClassPath: .
Require-Bundle: org.eclipse.equinox.security,
......
......@@ -8,6 +8,7 @@ extension-point.org.jkiss.dbeaver.pluginService.name = Plugin service
extension-point.org.jkiss.dbeaver.dataFormatter.name = Data value formatters
extension-point.org.jkiss.dbeaver.aggregateFunction.name = Aggregate Functions
extension-point.org.jkiss.dbeaver.dataTypeProvider.name = DataType provider
extension-point.org.jkiss.dbeaver.utils.name = Database utilities
extension.org.jkiss.dbeaver.DBeaverNature.name = DBeaver Nature
......
......@@ -23,6 +23,7 @@
<extension-point id="org.jkiss.dbeaver.application" name="%extension-point.org.jkiss.dbeaver.application.name" schema="schema/org.jkiss.dbeaver.application.exsd"/>
<extension-point id="org.jkiss.dbeaver.dataFormatter" name="%extension-point.org.jkiss.dbeaver.dataFormatter.name" schema="schema/org.jkiss.dbeaver.dataFormatter.exsd"/>
<extension-point id="org.jkiss.dbeaver.dataTypeProvider" name="%extension-point.org.jkiss.dbeaver.dataTypeProvider.name" schema="schema/org.jkiss.dbeaver.dataTypeProvider.exsd"/>
<extension-point id="org.jkiss.dbeaver.utils" name="%extension-point.org.jkiss.dbeaver.utils.name" schema="schema/org.jkiss.dbeaver.utils.exsd"/>
<extension point="org.eclipse.core.resources.natures" id="org.jkiss.dbeaver.DBeaverNature" name="%extension.org.jkiss.dbeaver.DBeaverNature.name">
<runtime>
......
<?xml version='1.0' encoding='UTF-8'?>
<!-- Schema file written by PDE -->
<schema targetNamespace="org.jkiss.dbeaver.core" xmlns="http://www.w3.org/2001/XMLSchema">
<annotation>
<appInfo>
<meta.schema plugin="org.jkiss.dbeaver.core" id="org.jkiss.dbeaver.utils" name="Database utilities"/>
</appInfo>
<documentation>
Headless database utilities
</documentation>
</annotation>
<element name="extension">
<annotation>
<appInfo>
<meta.element />
</appInfo>
</annotation>
<complexType>
<sequence>
<element ref="utility" minOccurs="0" maxOccurs="unbounded"/>
</sequence>
<attribute name="point" type="string" use="required">
<annotation>
<documentation>
</documentation>
</annotation>
</attribute>
<attribute name="id" type="string">
<annotation>
<documentation>
</documentation>
</annotation>
</attribute>
<attribute name="name" type="string">
<annotation>
<documentation>
</documentation>
<appInfo>
<meta.attribute translatable="true"/>
</appInfo>
</annotation>
</attribute>
</complexType>
</element>
<element name="utility">
<complexType>
<sequence minOccurs="0" maxOccurs="unbounded">
<element ref="propertyGroup" minOccurs="0" maxOccurs="unbounded"/>
</sequence>
<attribute name="id" type="string" use="required">
<annotation>
<documentation>
</documentation>
<appInfo>
<meta.attribute kind="identifier"/>
</appInfo>
</annotation>
</attribute>
<attribute name="class" type="string" use="required">
<annotation>
<documentation>
</documentation>
<appInfo>
<meta.attribute kind="java" basedOn=":org.jkiss.dbeaver.model.data.DBDDataFormatter"/>
</appInfo>
</annotation>
</attribute>
<attribute name="label" type="string" use="required">
<annotation>
<documentation>
</documentation>
<appInfo>
<meta.attribute translatable="true"/>
</appInfo>
</annotation>
</attribute>
<attribute name="description" type="string">
<annotation>
<documentation>
</documentation>
<appInfo>
<meta.attribute translatable="true"/>
</appInfo>
</annotation>
</attribute>
</complexType>
</element>
<element name="property">
<complexType>
<attribute name="id" type="string" use="required">
<annotation>
<documentation>
</documentation>
</annotation>
</attribute>
<attribute name="label" type="string" use="required">
<annotation>
<documentation>
</documentation>
<appInfo>
<meta.attribute translatable="true"/>
</appInfo>
</annotation>
</attribute>
<attribute name="description" type="string">
<annotation>
<documentation>
</documentation>
<appInfo>
<meta.attribute translatable="true"/>
</appInfo>
</annotation>
</attribute>
<attribute name="type" type="string">
<annotation>
<documentation>
</documentation>
</annotation>
</attribute>
<attribute name="required" type="boolean">
<annotation>
<documentation>
</documentation>
</annotation>
</attribute>
<attribute name="defaultValue" type="string">
<annotation>
<documentation>
</documentation>
</annotation>
</attribute>
<attribute name="validValues" type="string">
<annotation>
<documentation>
</documentation>
</annotation>
</attribute>
</complexType>
</element>
<element name="propertyGroup">
<complexType>
<sequence minOccurs="0" maxOccurs="unbounded">
<element ref="property"/>
</sequence>
<attribute name="label" type="string" use="required">
<annotation>
<documentation>
</documentation>
<appInfo>
<meta.attribute translatable="true"/>
</appInfo>
</annotation>
</attribute>
</complexType>
</element>
<annotation>
<appInfo>
<meta.section type="since"/>
</appInfo>
<documentation>
[Enter the first release in which this extension point appears.]
</documentation>
</annotation>
<annotation>
<appInfo>
<meta.section type="examples"/>
</appInfo>
<documentation>
[Enter extension point usage example here.]
</documentation>
</annotation>
<annotation>
<appInfo>
<meta.section type="apiinfo"/>
</appInfo>
<documentation>
[Enter API information here.]
</documentation>
</annotation>
<annotation>
<appInfo>
<meta.section type="implementation"/>
</appInfo>
<documentation>
[Enter information about supplied implementation of this extension point.]
</documentation>
</annotation>
</schema>
......@@ -14,7 +14,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jkiss.dbeaver.model.impl.app;
package org.jkiss.dbeaver.runtime.encode;
import org.jkiss.utils.IOUtils;
......
/*
* DBeaver - Universal Database Manager
* Copyright (C) 2010-2019 Serge Rider (serge@jkiss.org)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jkiss.dbeaver.runtime.utils;
import org.jkiss.dbeaver.DBException;
import org.jkiss.dbeaver.model.runtime.DBRProgressMonitor;
/**
* Action utility.
*/
public interface DBUActionUtility extends DBUUtility {
void runUtility(DBRProgressMonitor monitor) throws DBException;
}
/*
* DBeaver - Universal Database Manager
* Copyright (C) 2010-2019 Serge Rider (serge@jkiss.org)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jkiss.dbeaver.runtime.utils;
import org.jkiss.dbeaver.DBException;
import org.jkiss.dbeaver.model.runtime.DBRProgressMonitor;
/**
* Script utility.
* Generates target database script as a result of utility work
*/
public interface DBUScriptUtility extends DBUUtility {
String generateScript(DBRProgressMonitor monitor)
throws DBException;
}
/*
* DBeaver - Universal Database Manager
* Copyright (C) 2010-2019 Serge Rider (serge@jkiss.org)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jkiss.dbeaver.runtime.utils;
import org.jkiss.dbeaver.DBException;
import org.jkiss.dbeaver.model.app.DBPProject;
import org.jkiss.dbeaver.model.struct.DBSObject;
import java.util.Collection;
import java.util.Locale;
import java.util.Map;
/**
* Database utility.
*/
public interface DBUUtility {
void initTool(DBPProject project, Collection<DBSObject> objects)
throws DBException;
void updateToolSettings(Locale locale, Map<Object, Object> properties)
throws DBException;
}
/*
* DBeaver - Universal Database Manager
* Copyright (C) 2010-2019 Serge Rider (serge@jkiss.org)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jkiss.dbeaver.runtime.utils;
import org.eclipse.core.runtime.IConfigurationElement;
import org.jkiss.dbeaver.Log;
import org.jkiss.dbeaver.model.data.DBDDataFormatter;
import org.jkiss.dbeaver.model.impl.AbstractContextDescriptor;
import org.jkiss.dbeaver.model.impl.PropertyDescriptor;
import org.jkiss.dbeaver.model.preferences.DBPPropertyDescriptor;
import java.util.ArrayList;
import java.util.List;
/**
* DataFormatterDescriptor
*/
public class UtilityTaskDescriptor extends AbstractContextDescriptor
{
private static final Log log = Log.getLog(UtilityTaskDescriptor.class);
public static final String EXTENSION_ID = "org.jkiss.dbeaver.utils"; //$NON-NLS-1$
private String id;
private String name;
private String description;
private List<DBPPropertyDescriptor> properties = new ArrayList<>();
private ObjectType utilityType;
UtilityTaskDescriptor(IConfigurationElement config)
{
super(config);
this.id = config.getAttribute("id");
this.utilityType = new ObjectType(config.getAttribute("class"));
this.name = config.getAttribute("label");
this.description = config.getAttribute("description");
IConfigurationElement[] propElements = config.getChildren(PropertyDescriptor.TAG_PROPERTY_GROUP);
for (IConfigurationElement prop : propElements) {
properties.addAll(PropertyDescriptor.extractProperties(prop));
}
}
public String getId()
{
return id;
}
public String getName()
{
return name;
}
public String getDescription()
{
return description;
}
public List<DBPPropertyDescriptor> getProperties() {
return properties;
}
public DBDDataFormatter createFormatter() throws IllegalAccessException, InstantiationException
{
Class<? extends DBDDataFormatter> clazz = utilityType.getObjectClass(DBDDataFormatter.class);
if (clazz == null) {
return null;
}
return clazz.newInstance();
}
}
/*
* DBeaver - Universal Database Manager
* Copyright (C) 2010-2019 Serge Rider (serge@jkiss.org)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jkiss.dbeaver.runtime.utils;
import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.IExtensionRegistry;
import org.eclipse.core.runtime.Platform;
import org.jkiss.code.Nullable;
import org.jkiss.dbeaver.DBException;
import org.jkiss.dbeaver.Log;
import org.jkiss.dbeaver.model.app.DBPDataFormatterRegistry;
import org.jkiss.dbeaver.model.data.DBDDataFormatterProfile;
import org.jkiss.dbeaver.model.impl.preferences.SimplePreferenceStore;
import org.jkiss.dbeaver.registry.formatter.DataFormatterProfile;
import org.jkiss.dbeaver.runtime.DBWorkbench;
import org.jkiss.dbeaver.utils.GeneralUtils;
import org.jkiss.utils.CommonUtils;
import org.jkiss.utils.xml.SAXListener;
import org.jkiss.utils.xml.SAXReader;
import org.jkiss.utils.xml.XMLBuilder;
import org.jkiss.utils.xml.XMLException;
import org.xml.sax.Attributes;
import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class UtilityTaskRegistry implements DBPDataFormatterRegistry
{
private static final Log log = Log.getLog(UtilityTaskRegistry.class);
public static final String CONFIG_FILE_NAME = "dataformat-profiles.xml"; //$NON-NLS-1$
private static UtilityTaskRegistry instance = null;
public synchronized static UtilityTaskRegistry getInstance()
{
if (instance == null) {
instance = new UtilityTaskRegistry(Platform.getExtensionRegistry());
}
return instance;
}
private final List<UtilityTaskDescriptor> dataFormatterList = new ArrayList<>();
private final Map<String, UtilityTaskDescriptor> dataFormatterMap = new HashMap<>();
private DBDDataFormatterProfile globalProfile;
private List<DBDDataFormatterProfile> customProfiles = null;
private UtilityTaskRegistry(IExtensionRegistry registry)
{
// Load data formatters from external plugins
{
IConfigurationElement[] extElements = registry.getConfigurationElementsFor(UtilityTaskDescriptor.EXTENSION_ID);
for (IConfigurationElement ext : extElements) {
UtilityTaskDescriptor formatterDescriptor = new UtilityTaskDescriptor(ext);
dataFormatterList.add(formatterDescriptor);
dataFormatterMap.put(formatterDescriptor.getId(), formatterDescriptor);
}
}
}
public void dispose()
{
this.dataFormatterList.clear();
this.dataFormatterMap.clear();
this.globalProfile = null;
}
////////////////////////////////////////////////////
// Data formatters
public List<UtilityTaskDescriptor> getDataFormatters()
{
return dataFormatterList;
}
public UtilityTaskDescriptor getDataFormatter(String typeId)
{
return dataFormatterMap.get(typeId);
}
@Override
public synchronized DBDDataFormatterProfile getGlobalProfile()
{
if (globalProfile == null) {
globalProfile = new DataFormatterProfile(
"Global",
DBWorkbench.getPlatform().getPreferenceStore());
}
return globalProfile;
}
@Override
@Nullable
public DBDDataFormatterProfile getCustomProfile(String name)
{
for (DBDDataFormatterProfile profile : getCustomProfiles()) {
if (profile.getProfileName().equals(name)) {
return profile;
}
}
return null;
}
@Override
public synchronized List<DBDDataFormatterProfile> getCustomProfiles()
{
if (customProfiles == null) {
loadProfiles();
}
return customProfiles;
}
private void loadProfiles()
{
customProfiles = new ArrayList<>();
File storeFile = DBWorkbench.getPlatform().getConfigurationFile(CONFIG_FILE_NAME);
if (!storeFile.exists()) {
return;
}
try {
try (InputStream is = new FileInputStream(storeFile)) {
SAXReader parser = new SAXReader(is);
try {
parser.parse(new FormattersParser());
} catch (XMLException ex) {
throw new DBException("Datasource config parse error", ex);
}
} catch (DBException ex) {
log.warn("Can't load profiles config from " + storeFile.getPath(), ex);
}
}
catch (IOException ex) {
log.warn("IO error", ex);
}
}
private void saveProfiles()
{
if (customProfiles == null) {
return;
}
File storeFile = DBWorkbench.getPlatform().getConfigurationFile(CONFIG_FILE_NAME);
try (OutputStream os = new FileOutputStream(storeFile)) {
XMLBuilder xml = new XMLBuilder(os, GeneralUtils.UTF8_ENCODING);
xml.setButify(true);
xml.startElement("profiles");
for (DBDDataFormatterProfile profile : customProfiles) {
xml.startElement("profile");
xml.addAttribute("name", profile.getProfileName());
SimplePreferenceStore store = (SimplePreferenceStore) profile.getPreferenceStore();
Map<String, String> props = store.getProperties();
if (props != null) {
for (Map.Entry<String,String> entry : props.entrySet()) {
xml.startElement("property");
xml.addAttribute("name", entry.getKey());
xml.addAttribute("value", entry.getValue());
xml.endElement();
}
}
xml.endElement();
}
xml.endElement();
xml.flush();
}
catch (IOException ex) {
log.warn("IO error", ex);
}
}
public DBDDataFormatterProfile createCustomProfile(String profileName)
{
getCustomProfiles();
DBDDataFormatterProfile profile = new DataFormatterProfile(profileName, new CustomProfileStore());
customProfiles.add(profile);
saveProfiles();
return profile;
}
public void deleteCustomProfile(DBDDataFormatterProfile profile)
{
getCustomProfiles();
if (customProfiles.remove(profile)) {
saveProfiles();
}
}
private class CustomProfileStore extends SimplePreferenceStore {
private CustomProfileStore()
{
super(DBWorkbench.getPlatform().getPreferenceStore());
}
@Override
public void save() throws IOException
{
saveProfiles();
}
}
private class FormattersParser extends SAXListener.BaseListener
{
private String profileName;
private SimplePreferenceStore curStore;
@Override
public void saxStartElement(SAXReader reader, String namespaceURI, String localName, Attributes atts)
throws XMLException
{
if (localName.equals("profile")) {
curStore = new CustomProfileStore();
profileName = atts.getValue("name");
} else if (localName.equals("property")) {
if (curStore != null) {
curStore.setValue(
atts.getValue("name"),
atts.getValue("value"));
}
}
}
@Override
public void saxEndElement(SAXReader reader, String namespaceURI, String localName)
throws XMLException
{
if (localName.equals("profile")) {
if (!CommonUtils.isEmpty(profileName)) {
DataFormatterProfile profile = new DataFormatterProfile(profileName, curStore);
customProfiles.add(profile);
}
}
}
}
}
......@@ -33,7 +33,7 @@ import org.jkiss.dbeaver.model.app.DBASecureStorage;
import org.jkiss.dbeaver.model.app.DBPDataSourceRegistry;
import org.jkiss.dbeaver.model.connection.*;
import org.jkiss.dbeaver.model.data.json.JSONUtils;
import org.jkiss.dbeaver.model.impl.app.ContentEncrypter;
import org.jkiss.dbeaver.runtime.encode.ContentEncrypter;
import org.jkiss.dbeaver.model.impl.preferences.SimplePreferenceStore;
import org.jkiss.dbeaver.model.net.DBWHandlerConfiguration;
import org.jkiss.dbeaver.model.net.DBWNetworkProfile;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册