/***************************************************************************** | |
* Copyright (c) 2018 CEA LIST. | |
* | |
* 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: | |
* Arnault Lapitre (CEA LIST) arnault.lapitre@cea.fr | |
* - Initial API and implementation | |
*****************************************************************************/ | |
package org.eclipse.efm.sew.xtext.tests; | |
import com.google.inject.Guice; | |
import com.google.inject.Injector; | |
import org.eclipse.efm.sew.xtext.SEWRuntimeModule; | |
import org.eclipse.efm.sew.xtext.SEWStandaloneSetup; | |
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 SEWInjectorProvider implements IInjectorProvider, IRegistryConfigurator { | |
protected GlobalStateMemento stateBeforeInjectorCreation; | |
protected GlobalStateMemento stateAfterInjectorCreation; | |
protected Injector injector; | |
static { | |
GlobalRegistries.initializeDefaults(); | |
} | |
@Override | |
public Injector getInjector() { | |
if (injector == null) { | |
this.injector = internalCreateInjector(); | |
stateAfterInjectorCreation = GlobalRegistries.makeCopyOfGlobalState(); | |
} | |
return injector; | |
} | |
protected Injector internalCreateInjector() { | |
return new SEWStandaloneSetup() { | |
@Override | |
public Injector createInjector() { | |
return Guice.createInjector(createRuntimeModule()); | |
} | |
}.createInjectorAndDoEMFRegistration(); | |
} | |
protected SEWRuntimeModule createRuntimeModule() { | |
// make it work also with Maven/Tycho and OSGI | |
// see https://bugs.eclipse.org/bugs/show_bug.cgi?id=493672 | |
return new SEWRuntimeModule() { | |
@Override | |
public ClassLoader bindClassLoaderToInstance() { | |
return SEWInjectorProvider.class | |
.getClassLoader(); | |
} | |
}; | |
} | |
@Override | |
public void restoreRegistry() { | |
stateBeforeInjectorCreation.restoreGlobalState(); | |
stateBeforeInjectorCreation = null; | |
} | |
@Override | |
public void setupRegistry() { | |
stateBeforeInjectorCreation = GlobalRegistries.makeCopyOfGlobalState(); | |
if (injector == null) { | |
getInjector(); | |
} | |
stateAfterInjectorCreation.restoreGlobalState(); | |
} | |
} |