/*
* DBeaver - Universal Database Manager
* Copyright (C) 2010-2017 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.core;
import org.eclipse.core.resources.IWorkspace;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.Platform;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.ui.plugin.AbstractUIPlugin;
import org.jkiss.dbeaver.ModelPreferences;
import org.jkiss.dbeaver.model.preferences.DBPPreferenceStore;
import org.jkiss.dbeaver.model.impl.preferences.BundlePreferenceStore;
import org.jkiss.dbeaver.model.runtime.features.DBRFeatureRegistry;
import org.jkiss.dbeaver.utils.GeneralUtils;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintStream;
import java.util.MissingResourceException;
import java.util.ResourceBundle;
/**
* The activator class controls the plug-in life cycle
*/
public class DBeaverActivator extends AbstractUIPlugin {
// The shared instance
private static DBeaverActivator instance;
private ResourceBundle pluginResourceBundle, coreResourceBundle;
private PrintStream debugWriter;
private DBPPreferenceStore preferences;
public DBeaverActivator() {
}
public static DBeaverActivator getInstance() {
return instance;
}
@Override
public void start(BundleContext context)
throws Exception {
super.start(context);
instance = this;
Bundle bundle = getBundle();
ModelPreferences.setMainBundle(bundle);
preferences = new BundlePreferenceStore(bundle);
DBRFeatureRegistry.getInstance().registerFeatures(CoreFeatures.class);
DBeaverUI.getInstance();
try {
coreResourceBundle = ResourceBundle.getBundle(CoreMessages.BUNDLE_NAME);
pluginResourceBundle = Platform.getResourceBundle(bundle);
} catch (MissingResourceException x) {
coreResourceBundle = null;
}
}
@Override
public void stop(BundleContext context)
throws Exception {
this.shutdownUI();
this.shutdownCore();
if (debugWriter != null) {
debugWriter.close();
debugWriter = null;
}
instance = null;
super.stop(context);
}
private void shutdownUI() {
DBeaverUI.disposeUI();
}
/**
* Returns configuration file
*/
public static File getConfigurationFile(String fileName)
{
return new File(getInstance().getStateLocation().toFile(), fileName);
}
public synchronized PrintStream getDebugWriter() {
if (debugWriter == null) {
File logPath = GeneralUtils.getMetadataFolder();
File debugLogFile = new File(logPath, "dbeaver-debug.log"); //$NON-NLS-1$
if (debugLogFile.exists()) {
if (!debugLogFile.delete()) {
System.err.println("Can't delete debug log file"); //$NON-NLS-1$
}
}
try {
debugWriter = new PrintStream(debugLogFile);
} catch (FileNotFoundException e) {
e.printStackTrace(System.err);
}
}
return debugWriter;
}
/**
* Returns an image descriptor for the image file at the given
* plug-in relative path
*
* @param path the path
* @return the image descriptor
*/
public static ImageDescriptor getImageDescriptor(String path) {
return imageDescriptorFromPlugin(DBeaverCore.PLUGIN_ID, path);
}
/**
* Returns the plugin's resource bundle,
*
* @return core resource bundle
*/
public static ResourceBundle getCoreResourceBundle() {
return getInstance().coreResourceBundle;
}
public static ResourceBundle getPluginResourceBundle() {
return getInstance().pluginResourceBundle;
}
public DBPPreferenceStore getPreferences() {
return preferences;
}
/**
* Returns the workspace instance.
*/
public static IWorkspace getWorkspace() {
return ResourcesPlugin.getWorkspace();
}
private void shutdownCore() {
try {
// Dispose core
if (DBeaverCore.instance != null) {
DBeaverCore.instance.dispose();
}
} catch (Throwable e) {
e.printStackTrace();
logMessage("Internal error after shutdown process:" + e.getMessage()); //$NON-NLS-1$
}
}
private void logMessage(String message) {
getDebugWriter().print(message);
}
}