blob: 39b4cb6ab412b55c8f8212d4bc4f98b40456ed45 [file] [log] [blame]
/**
*
* Copyright (c) 2011, 2016 - Loetz GmbH&Co.KG (69115 Heidelberg, Germany)
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Christophe Loetz (Loetz GmbH&Co.KG) - initial implementation
*
* generated by Xtext 2.11.0
*
*/
package org.eclipse.osbp.xtext.chart.tests;
import com.google.inject.Guice;
import com.google.inject.Injector;
import org.eclipse.osbp.xtext.chart.ChartDSLRuntimeModule;
import org.eclipse.osbp.xtext.chart.ChartDSLStandaloneSetup;
import org.eclipse.xtext.testing.GlobalRegistries;
import org.eclipse.xtext.testing.GlobalRegistries.GlobalStateMemento;
import org.eclipse.xtext.testing.IInjectorProvider;
import org.eclipse.xtext.testing.IRegistryConfigurator;
public class ChartDSLInjectorProvider implements IInjectorProvider, IRegistryConfigurator {
protected GlobalStateMemento stateBeforeInjectorCreation;
protected GlobalStateMemento stateAfterInjectorCreation;
protected Injector injector;
static {
GlobalRegistries.initializeDefaults();
}
@Override
public Injector getInjector() {
if (injector == null) {
stateBeforeInjectorCreation = GlobalRegistries.makeCopyOfGlobalState();
this.injector = internalCreateInjector();
stateAfterInjectorCreation = GlobalRegistries.makeCopyOfGlobalState();
}
return injector;
}
protected Injector internalCreateInjector() {
return new ChartDSLStandaloneSetup() {
@Override
public Injector createInjector() {
return Guice.createInjector(createRuntimeModule());
}
}.createInjectorAndDoEMFRegistration();
}
protected ChartDSLRuntimeModule createRuntimeModule() {
// make it work also with Maven/Tycho and OSGI
// see https://bugs.eclipse.org/bugs/show_bug.cgi?id=493672
return new ChartDSLRuntimeModule() {
@Override
public ClassLoader bindClassLoaderToInstance() {
return ChartDSLInjectorProvider.class
.getClassLoader();
}
};
}
@Override
public void restoreRegistry() {
stateBeforeInjectorCreation.restoreGlobalState();
}
@Override
public void setupRegistry() {
getInjector();
stateAfterInjectorCreation.restoreGlobalState();
}
}