Skip to content

Commit

Permalink
488126: PluginUtil for retrieving the current AbstractUIPlugin
Browse files Browse the repository at this point in the history
Manually changed also a few examples for the new DSL generation

Change-Id: I4ed13fb2a730057940c62fb8a882cfeff89221db
Task-Url: https://bugs.eclipse.org/bugs/show_bug.cgi?id=488126
  • Loading branch information
LorenzoBettini committed Feb 21, 2016
1 parent e5c34dd commit 8eb2182
Show file tree
Hide file tree
Showing 10 changed files with 145 additions and 90 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,28 +10,15 @@
*******************************************************************************/
package org.eclipse.emf.parsley.examples.firstexample;

import org.osgi.framework.Bundle;

import org.eclipse.emf.parsley.EmfParsleyExtensionFactory;
import org.eclipse.emf.parsley.EmfParsleyGuiceModule;
import org.eclipse.emf.parsley.runtime.ui.AbstractGuiceAwareExecutableExtensionFactory;

import com.google.inject.Injector;

public class FirstexampleExecutableExtensionFactory extends
EmfParsleyExtensionFactory {

@Override
protected Bundle getBundle() {
return FirstexampleActivator.getDefault().getBundle();
}
public class FirstexampleExecutableExtensionFactory extends AbstractGuiceAwareExecutableExtensionFactory {

@Override
protected EmfParsleyGuiceModule getModule() {
return FirstexampleActivator.getDefault().createModule();
protected Injector getInjector() throws Exception {
return FirstexampleInjectorProvider.getInjector();
}

@Override
protected Injector getInjector() {
return FirstexampleActivator.getDefault().getInjector();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package org.eclipse.emf.parsley.examples.firstexample;

import static com.google.inject.Guice.createInjector;
import static org.eclipse.emf.parsley.runtime.ui.PluginUtil.getBundle;
import static org.eclipse.emf.parsley.runtime.ui.PluginUtil.getPlugin;

import com.google.inject.Injector;

public class FirstexampleInjectorProvider {

private static Injector injector;

public static synchronized Injector getInjector() throws Exception {
if (injector == null) {
injector = createInjector(
new FirstexampleGuiceModule(getPlugin(getBundle(FirstexampleInjectorProvider.class))));
}
return injector;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,28 +10,14 @@
*******************************************************************************/
package org.eclipse.emf.parsley.examples.mail.accountsview;

import org.eclipse.emf.parsley.EmfParsleyExtensionFactory;
import org.eclipse.emf.parsley.EmfParsleyGuiceModule;
import org.osgi.framework.Bundle;

import org.eclipse.emf.parsley.runtime.ui.AbstractGuiceAwareExecutableExtensionFactory;

import com.google.inject.Injector;

public class AccountsviewExecutableExtensionFactory extends
EmfParsleyExtensionFactory {

@Override
protected Bundle getBundle() {
return AccountsviewActivator.getDefault().getBundle();
}

@Override
protected EmfParsleyGuiceModule getModule() {
return AccountsviewActivator.getDefault().createModule();
}
public class AccountsviewExecutableExtensionFactory extends AbstractGuiceAwareExecutableExtensionFactory {

@Override
protected Injector getInjector() {
return AccountsviewActivator.getDefault().getInjector();
protected Injector getInjector() throws Exception {
return AccountsviewInjectorProvider.getInjector();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package org.eclipse.emf.parsley.examples.mail.accountsview;

import static com.google.inject.Guice.createInjector;
import static org.eclipse.emf.parsley.runtime.ui.PluginUtil.getBundle;
import static org.eclipse.emf.parsley.runtime.ui.PluginUtil.getPlugin;

import com.google.inject.Injector;

public class AccountsviewInjectorProvider {

private static Injector injector;

public static synchronized Injector getInjector() throws Exception {
if (injector == null) {
injector = createInjector(
new AccountsviewGuiceModule(getPlugin(getBundle(AccountsviewInjectorProvider.class))));
}
return injector;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,28 +10,15 @@
*******************************************************************************/
package org.eclipse.emf.parsley.examples.mail.mailsview;

import org.eclipse.emf.parsley.EmfParsleyExtensionFactory;
import org.eclipse.emf.parsley.EmfParsleyGuiceModule;
import org.osgi.framework.Bundle;

import org.eclipse.emf.parsley.runtime.ui.AbstractGuiceAwareExecutableExtensionFactory;

import com.google.inject.Injector;

public class MailsviewExecutableExtensionFactory extends
EmfParsleyExtensionFactory {

@Override
protected Bundle getBundle() {
return MailsviewActivator.getDefault().getBundle();
}
public class MailsviewExecutableExtensionFactory extends AbstractGuiceAwareExecutableExtensionFactory {

@Override
protected EmfParsleyGuiceModule getModule() {
return MailsviewActivator.getDefault().createModule();
protected Injector getInjector() throws Exception {
return MailsviewInjectorProvider.getInjector();
}

@Override
protected Injector getInjector() {
return MailsviewActivator.getDefault().getInjector();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package org.eclipse.emf.parsley.examples.mail.mailsview;

import static com.google.inject.Guice.createInjector;
import static org.eclipse.emf.parsley.runtime.ui.PluginUtil.getBundle;
import static org.eclipse.emf.parsley.runtime.ui.PluginUtil.getPlugin;

import com.google.inject.Injector;

public class MailsviewInjectorProvider {

private static Injector injector;

public static synchronized Injector getInjector() throws Exception {
if (injector == null) {
injector = createInjector(
new MailsviewGuiceModule(getPlugin(getBundle(MailsviewInjectorProvider.class))));
}
return injector;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,46 +12,31 @@
package org.eclipse.emf.parsley;

import static com.google.inject.Guice.createInjector;
import org.eclipse.emf.parsley.runtime.ui.AbstractGuiceAwareExecutableExtensionFactory;

import org.osgi.framework.Bundle;
import org.eclipse.emf.parsley.runtime.ui.AbstractGuiceAwareExecutableExtensionFactory;

import com.google.inject.Injector;

/**
* Base class for executable extension factories, for example, for
* the ones generated by the DSL.
*
* @author Lorenzo Bettini
*
*/
public class EmfParsleyExtensionFactory extends
AbstractGuiceAwareExecutableExtensionFactory {

/**
* Remember to override this method in your plugin, if you plan to create an
* instance of a class of your plugin (otherwise the classloader will not
* find it); if in your plugin you use guice only to inject your specific
* implementation into an instance of a class from this very plugin, then
* you do not need to override this method.
*
* @see org.eclipse.xtext.ui.guice.AbstractGuiceAwareExecutableExtensionFactory#getBundle()
*/
@Override
protected Bundle getBundle() {
return EmfParsleyActivator.getDefault().getBundle();
}
public class EmfParsleyExtensionFactory extends AbstractGuiceAwareExecutableExtensionFactory {

private Injector injector;

/*
* (non-Javadoc)
*
* @see
* org.eclipse.xtext.ui.guice.AbstractGuiceAwareExecutableExtensionFactory
* #getInjector()
*/
@Override
protected Injector getInjector() {
return createInjector(getModule());
protected Injector getInjector() throws Exception {
if (injector == null) {
injector = createInjector(getModule());
}
return injector;
}

protected EmfParsleyGuiceModule getModule() {
protected EmfParsleyGuiceModule getModule() throws Exception {
return new EmfParsleyGuiceModule(EmfParsleyActivator.getDefault());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,11 @@ public Object create() throws CoreException {
throw new CoreException(new Status(IStatus.ERROR, getBundle().getSymbolicName(), e.getMessage() + " ExtensionFactory: "+ getClass().getName(), e));
}
}

protected abstract Bundle getBundle();
protected abstract Injector getInjector();

protected Bundle getBundle() {
return PluginUtil.getBundle(getClass());
}

protected abstract Injector getInjector() throws Exception;
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*******************************************************************************
* Copyright (c) 2016 RCP Vision (http://www.rcp-vision.com) and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Lorenzo Bettini - initial API and implementation
*******************************************************************************/
package org.eclipse.emf.parsley.runtime.ui;

import java.lang.reflect.Method;
import java.util.Dictionary;

import org.eclipse.ui.plugin.AbstractUIPlugin;
import org.osgi.framework.Bundle;
import org.osgi.framework.Constants;
import org.osgi.framework.FrameworkUtil;

/**
* Utility methods for plug-ins.
*
* @author Lorenzo Bettini - initial API and implementation
*
*/
public class PluginUtil {

protected PluginUtil() {

}

public static Bundle getBundle(Class<?> classFromBundle) {
return FrameworkUtil.getBundle(classFromBundle);
}

/**
* Retrieves the {@link AbstractUIPlugin} activator from the specified {@link Bundle},
* via reflection, assuming that the activator has a static method getDefault returning
* the singleton instance.
*
* @param bundle
* @return
* @throws Exception
*/
public static AbstractUIPlugin getPlugin(Bundle bundle) throws Exception {
final Dictionary<String, String> headers = bundle.getHeaders();
String activator = headers.get(Constants.BUNDLE_ACTIVATOR);
Class<?> activatorClass = bundle.loadClass(activator);
Method method = activatorClass.getMethod("getDefault");
Object activatorInstance = method.invoke(null);
return (AbstractUIPlugin) activatorInstance;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
import org.eclipse.emf.parsley.tests.EmfParsleySwtBotTestsActivator;
import org.osgi.framework.Bundle;

import com.google.inject.Injector;

/**
* Uses a custom label provider
*
Expand All @@ -36,9 +34,4 @@ protected Bundle getBundle() {
return EmfParsleySwtBotTestsActivator.getDefault().getBundle();
}

@Override
public Injector getInjector() {
return super.getInjector();
}

}

0 comments on commit 8eb2182

Please sign in to comment.