blob: 4589526e25776f6228dc156704ffeb16436ef658 [file] [log] [blame]
/*=============================================================================#
# Copyright (c) 2005, 2017 Stephan Wahlbrink and others.
#
# This program and the accompanying materials are made available under the
# terms of the Eclipse Public License 2.0 which is available at
# https://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
# which is available at https://www.apache.org/licenses/LICENSE-2.0.
#
# SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
#
# Contributors:
# Stephan Wahlbrink <sw@wahlbrink.eu> - initial API and implementation
#=============================================================================*/
package org.eclipse.statet.internal.r.core;
import java.util.ArrayList;
import java.util.List;
import org.osgi.framework.BundleContext;
import org.osgi.util.tracker.ServiceTracker;
import org.eclipse.core.net.proxy.IProxyService;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Plugin;
import org.eclipse.core.runtime.Status;
import org.eclipse.statet.jcommons.lang.Disposable;
import org.eclipse.statet.ecommons.preferences.core.util.PreferenceUtils;
import org.eclipse.statet.internal.r.core.pkgmanager.REnvPkgManager;
import org.eclipse.statet.internal.r.core.renv.REnvManager;
import org.eclipse.statet.internal.r.core.rhelp.RHelpManager;
import org.eclipse.statet.internal.r.core.sourcemodel.RModelManager;
import org.eclipse.statet.r.core.IRCoreAccess;
import org.eclipse.statet.r.core.RCore;
/**
* The main plug-in class to be used in the desktop.
*/
public class RCorePlugin extends Plugin {
private static RCorePlugin instance;
/**
* Returns the shared plug-in instance
*
* @return the shared instance
*/
public static RCorePlugin getInstance() {
return instance;
}
public static final void log(final IStatus status) {
final Plugin plugin= getInstance();
if (plugin != null) {
plugin.getLog().log(status);
}
}
public static final void logError(final int code, final String message, final Throwable e) {
final Plugin plugin= getInstance();
if (plugin != null) {
plugin.getLog().log(new Status(IStatus.ERROR, RCore.BUNDLE_ID, code, message, e));
}
}
private boolean started;
private final List<Disposable> disposables= new ArrayList<>();
private REnvManager rEnvManager;
private RCoreAccess workspaceCoreAccess;
private RCoreAccess defaultsCoreAccess;
private RModelManager rModelManager;
private ResourceTracker resourceTracker;
private REnvPkgManager rEnvPkgManager;
private RHelpManager rHelpManager;
private ServiceTracker proxyService;
/**
* The constructor.
*/
public RCorePlugin() {
}
/**
* This method is called upon plug-in activation
*/
@Override
public void start(final BundleContext context) throws Exception {
super.start(context);
instance= this;
this.rEnvManager= new REnvManager();
this.workspaceCoreAccess= new RCoreAccess(
PreferenceUtils.getInstancePrefs(),
this.rEnvManager.getDefault() );
this.rModelManager= new RModelManager();
this.resourceTracker= new ResourceTracker(this.rModelManager);
this.rEnvPkgManager= new REnvPkgManager(this.rEnvManager);
this.rHelpManager= new RHelpManager();
this.disposables.add(this.rHelpManager);
this.started= true;
}
/**
* This method is called when the plug-in is stopped
*/
@Override
public void stop(final BundleContext context) throws Exception {
try {
synchronized (this) {
this.started= false;
}
if (this.resourceTracker != null) {
try {
this.resourceTracker.dispose();
}
catch (final Exception e) {}
this.resourceTracker= null;
}
if (this.rModelManager != null) {
this.rModelManager.dispose();
this.rModelManager= null;
}
if (this.workspaceCoreAccess != null) {
this.workspaceCoreAccess.dispose();
this.workspaceCoreAccess= null;
}
if (this.defaultsCoreAccess != null) {
this.defaultsCoreAccess.dispose();
this.defaultsCoreAccess= null;
}
if (this.rEnvManager != null) {
this.rEnvManager.dispose();
this.rEnvManager= null;
}
for (final Disposable listener : this.disposables) {
try {
listener.dispose();
}
catch (final Throwable e) {
getLog().log(new Status(IStatus.ERROR, RCore.BUNDLE_ID, 0, "Error occured when dispose module", e));
}
}
this.disposables.clear();
}
finally {
instance= null;
super.stop(context);
}
}
private void checkStarted() {
if (!this.started) {
throw new IllegalStateException("Plug-in is not started.");
}
}
public REnvManager getREnvManager() {
return this.rEnvManager;
}
public RModelManager getRModelManager() {
return this.rModelManager;
}
public ResourceTracker getResourceTracker() {
return this.resourceTracker;
}
public REnvPkgManager getREnvPkgManager() {
return this.rEnvPkgManager;
}
public RHelpManager getRHelpManager() {
return this.rHelpManager;
}
public synchronized IRCoreAccess getWorkspaceRCoreAccess() {
if (this.workspaceCoreAccess == null) {
checkStarted();
}
return this.workspaceCoreAccess;
}
public synchronized IRCoreAccess getDefaultsRCoreAccess() {
if (this.defaultsCoreAccess == null) {
checkStarted();
this.defaultsCoreAccess= new RCoreAccess(
PreferenceUtils.getDefaultPrefs(),
this.rEnvManager.getDefault() );
}
return this.defaultsCoreAccess;
}
public synchronized IProxyService getProxyService() {
if (this.proxyService == null) {
checkStarted();
this.proxyService= new ServiceTracker(getBundle().getBundleContext(),
IProxyService.class.getName(), null );
this.proxyService.open();
}
return (IProxyService) this.proxyService.getService();
}
}