mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-13 00:45:44 +01:00
Find update steps and execute them with bootstrap context
This commit is contained in:
@@ -0,0 +1,8 @@
|
|||||||
|
package sonia.scm.migration;
|
||||||
|
|
||||||
|
import sonia.scm.plugin.ExtensionPoint;
|
||||||
|
|
||||||
|
@ExtensionPoint
|
||||||
|
public interface UpdateStep {
|
||||||
|
void doUpdate();
|
||||||
|
}
|
||||||
20
scm-webapp/src/main/java/sonia/scm/MigrationEngine.java
Normal file
20
scm-webapp/src/main/java/sonia/scm/MigrationEngine.java
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
package sonia.scm;
|
||||||
|
|
||||||
|
import sonia.scm.migration.UpdateStep;
|
||||||
|
|
||||||
|
import javax.inject.Inject;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
public class MigrationEngine {
|
||||||
|
|
||||||
|
private final Set<UpdateStep> steps;
|
||||||
|
|
||||||
|
@Inject
|
||||||
|
public MigrationEngine(Set<UpdateStep> steps) {
|
||||||
|
this.steps = steps;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void migrate() {
|
||||||
|
steps.forEach(UpdateStep::doUpdate);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,9 +1,9 @@
|
|||||||
/**
|
/**
|
||||||
* Copyright (c) 2010, Sebastian Sdorra All rights reserved.
|
* Copyright (c) 2010, Sebastian Sdorra All rights reserved.
|
||||||
*
|
* <p>
|
||||||
* Redistribution and use in source and binary forms, with or without
|
* Redistribution and use in source and binary forms, with or without
|
||||||
* modification, are permitted provided that the following conditions are met:
|
* modification, are permitted provided that the following conditions are met:
|
||||||
*
|
* <p>
|
||||||
* 1. Redistributions of source code must retain the above copyright notice,
|
* 1. Redistributions of source code must retain the above copyright notice,
|
||||||
* this list of conditions and the following disclaimer. 2. Redistributions in
|
* this list of conditions and the following disclaimer. 2. Redistributions in
|
||||||
* binary form must reproduce the above copyright notice, this list of
|
* binary form must reproduce the above copyright notice, this list of
|
||||||
@@ -11,7 +11,7 @@
|
|||||||
* materials provided with the distribution. 3. Neither the name of SCM-Manager;
|
* materials provided with the distribution. 3. Neither the name of SCM-Manager;
|
||||||
* nor the names of its contributors may be used to endorse or promote products
|
* nor the names of its contributors may be used to endorse or promote products
|
||||||
* derived from this software without specific prior written permission.
|
* derived from this software without specific prior written permission.
|
||||||
*
|
* <p>
|
||||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||||
@@ -22,9 +22,8 @@
|
|||||||
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
*
|
* <p>
|
||||||
* http://bitbucket.org/sdorra/scm-manager
|
* http://bitbucket.org/sdorra/scm-manager
|
||||||
*
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
@@ -40,7 +39,7 @@ import com.google.inject.Module;
|
|||||||
import com.google.inject.assistedinject.FactoryModuleBuilder;
|
import com.google.inject.assistedinject.FactoryModuleBuilder;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
import sonia.scm.BootstrapModule;
|
import sonia.scm.MigrationEngine;
|
||||||
import sonia.scm.SCMContext;
|
import sonia.scm.SCMContext;
|
||||||
import sonia.scm.ScmContextListener;
|
import sonia.scm.ScmContextListener;
|
||||||
import sonia.scm.Stage;
|
import sonia.scm.Stage;
|
||||||
@@ -77,8 +76,7 @@ import java.util.Set;
|
|||||||
*
|
*
|
||||||
* @author Sebastian Sdorra
|
* @author Sebastian Sdorra
|
||||||
*/
|
*/
|
||||||
public class BootstrapContextListener implements ServletContextListener
|
public class BootstrapContextListener implements ServletContextListener {
|
||||||
{
|
|
||||||
|
|
||||||
/** Field description */
|
/** Field description */
|
||||||
private static final String DIRECTORY_PLUGINS = "plugins";
|
private static final String DIRECTORY_PLUGINS = "plugins";
|
||||||
@@ -105,22 +103,16 @@ public class BootstrapContextListener implements ServletContextListener
|
|||||||
* @param sce
|
* @param sce
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public void contextDestroyed(ServletContextEvent sce)
|
public void contextDestroyed(ServletContextEvent sce) {
|
||||||
{
|
|
||||||
contextListener.contextDestroyed(sce);
|
contextListener.contextDestroyed(sce);
|
||||||
|
|
||||||
for (PluginWrapper plugin : contextListener.getPlugins())
|
for (PluginWrapper plugin : contextListener.getPlugins()) {
|
||||||
{
|
|
||||||
ClassLoader pcl = plugin.getClassLoader();
|
ClassLoader pcl = plugin.getClassLoader();
|
||||||
|
|
||||||
if (pcl instanceof Closeable)
|
if (pcl instanceof Closeable) {
|
||||||
{
|
try {
|
||||||
try
|
|
||||||
{
|
|
||||||
((Closeable) pcl).close();
|
((Closeable) pcl).close();
|
||||||
}
|
} catch (IOException ex) {
|
||||||
catch (IOException ex)
|
|
||||||
{
|
|
||||||
logger.warn("could not close plugin classloader", ex);
|
logger.warn("could not close plugin classloader", ex);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -137,14 +129,12 @@ public class BootstrapContextListener implements ServletContextListener
|
|||||||
* @param sce
|
* @param sce
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public void contextInitialized(ServletContextEvent sce)
|
public void contextInitialized(ServletContextEvent sce) {
|
||||||
{
|
|
||||||
context = sce.getServletContext();
|
context = sce.getServletContext();
|
||||||
|
|
||||||
File pluginDirectory = getPluginDirectory();
|
File pluginDirectory = getPluginDirectory();
|
||||||
|
|
||||||
try
|
try {
|
||||||
{
|
|
||||||
if (!isCorePluginExtractionDisabled()) {
|
if (!isCorePluginExtractionDisabled()) {
|
||||||
extractCorePlugins(context, pluginDirectory);
|
extractCorePlugins(context, pluginDirectory);
|
||||||
} else {
|
} else {
|
||||||
@@ -162,15 +152,17 @@ public class BootstrapContextListener implements ServletContextListener
|
|||||||
|
|
||||||
Injector bootstrapInjector = Guice.createInjector(bootstrapModule, scmContextListenerModule);
|
Injector bootstrapInjector = Guice.createInjector(bootstrapModule, scmContextListenerModule);
|
||||||
|
|
||||||
|
|
||||||
|
Injector migrationInjector = bootstrapInjector.createChildInjector(new UpdateStepModule(pluginLoader));
|
||||||
|
|
||||||
|
MigrationEngine stepEngine = migrationInjector.getInstance(MigrationEngine.class);
|
||||||
|
stepEngine.migrate();
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
contextListener = bootstrapInjector.getInstance(ScmContextListener.Factory.class).create(cl, plugins);
|
contextListener = bootstrapInjector.getInstance(ScmContextListener.Factory.class).create(cl, plugins);
|
||||||
|
} catch (IOException ex) {
|
||||||
// Set<MigrationStep> steps = bootstrapInjector.getInstance(....);
|
|
||||||
// migrate
|
|
||||||
|
|
||||||
// contextListener = new ScmContextListener(cl, plugins);
|
|
||||||
}
|
|
||||||
catch (IOException ex)
|
|
||||||
{
|
|
||||||
throw new PluginLoadException("could not load plugins", ex);
|
throw new PluginLoadException("could not load plugins", ex);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -178,8 +170,7 @@ public class BootstrapContextListener implements ServletContextListener
|
|||||||
|
|
||||||
// register for restart events
|
// register for restart events
|
||||||
if (!registered
|
if (!registered
|
||||||
&& (SCMContext.getContext().getStage() == Stage.DEVELOPMENT))
|
&& (SCMContext.getContext().getStage() == Stage.DEVELOPMENT)) {
|
||||||
{
|
|
||||||
logger.info("register for restart events");
|
logger.info("register for restart events");
|
||||||
ScmEventBus.getInstance().register(this);
|
ScmEventBus.getInstance().register(this);
|
||||||
registered = true;
|
registered = true;
|
||||||
@@ -202,8 +193,7 @@ public class BootstrapContextListener implements ServletContextListener
|
|||||||
*/
|
*/
|
||||||
private void extractCorePlugin(ServletContext context, File pluginDirectory,
|
private void extractCorePlugin(ServletContext context, File pluginDirectory,
|
||||||
PluginIndexEntry entry)
|
PluginIndexEntry entry)
|
||||||
throws IOException
|
throws IOException {
|
||||||
{
|
|
||||||
URL url = context.getResource(PLUGIN_DIRECTORY.concat(entry.getName()));
|
URL url = context.getResource(PLUGIN_DIRECTORY.concat(entry.getName()));
|
||||||
SmpArchive archive = SmpArchive.create(url);
|
SmpArchive archive = SmpArchive.create(url);
|
||||||
Plugin plugin = archive.getPlugin();
|
Plugin plugin = archive.getPlugin();
|
||||||
@@ -212,30 +202,22 @@ public class BootstrapContextListener implements ServletContextListener
|
|||||||
plugin);
|
plugin);
|
||||||
File checksumFile = PluginsInternal.getChecksumFile(directory);
|
File checksumFile = PluginsInternal.getChecksumFile(directory);
|
||||||
|
|
||||||
if (!directory.exists())
|
if (!directory.exists()) {
|
||||||
{
|
|
||||||
logger.warn("install plugin {}", plugin.getInformation().getId());
|
logger.warn("install plugin {}", plugin.getInformation().getId());
|
||||||
PluginsInternal.extract(archive, entry.getChecksum(), directory,
|
PluginsInternal.extract(archive, entry.getChecksum(), directory,
|
||||||
checksumFile, true);
|
checksumFile, true);
|
||||||
}
|
} else if (!checksumFile.exists()) {
|
||||||
else if (!checksumFile.exists())
|
|
||||||
{
|
|
||||||
logger.warn("plugin directory {} exists without checksum file.",
|
logger.warn("plugin directory {} exists without checksum file.",
|
||||||
directory);
|
directory);
|
||||||
PluginsInternal.extract(archive, entry.getChecksum(), directory,
|
PluginsInternal.extract(archive, entry.getChecksum(), directory,
|
||||||
checksumFile, true);
|
checksumFile, true);
|
||||||
}
|
} else {
|
||||||
else
|
|
||||||
{
|
|
||||||
String checksum = Files.toString(checksumFile, Charsets.UTF_8).trim();
|
String checksum = Files.toString(checksumFile, Charsets.UTF_8).trim();
|
||||||
|
|
||||||
if (checksum.equals(entry.getChecksum()))
|
if (checksum.equals(entry.getChecksum())) {
|
||||||
{
|
|
||||||
logger.debug("plugin {} is up to date",
|
logger.debug("plugin {} is up to date",
|
||||||
plugin.getInformation().getId());
|
plugin.getInformation().getId());
|
||||||
}
|
} else {
|
||||||
else
|
|
||||||
{
|
|
||||||
logger.warn("checksum mismatch of pluing {}, start update",
|
logger.warn("checksum mismatch of pluing {}, start update",
|
||||||
plugin.getInformation().getId());
|
plugin.getInformation().getId());
|
||||||
PluginsInternal.extract(archive, entry.getChecksum(), directory,
|
PluginsInternal.extract(archive, entry.getChecksum(), directory,
|
||||||
@@ -253,14 +235,12 @@ public class BootstrapContextListener implements ServletContextListener
|
|||||||
*
|
*
|
||||||
* @throws IOException
|
* @throws IOException
|
||||||
*/
|
*/
|
||||||
private void extractCorePlugins(ServletContext context, File pluginDirectory) throws IOException
|
private void extractCorePlugins(ServletContext context, File pluginDirectory) throws IOException {
|
||||||
{
|
|
||||||
IOUtil.mkdirs(pluginDirectory);
|
IOUtil.mkdirs(pluginDirectory);
|
||||||
|
|
||||||
PluginIndex index = readCorePluginIndex(context);
|
PluginIndex index = readCorePluginIndex(context);
|
||||||
|
|
||||||
for (PluginIndexEntry entry : index)
|
for (PluginIndexEntry entry : index) {
|
||||||
{
|
|
||||||
extractCorePlugin(context, pluginDirectory, entry);
|
extractCorePlugin(context, pluginDirectory, entry);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -273,27 +253,20 @@ public class BootstrapContextListener implements ServletContextListener
|
|||||||
*
|
*
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
private PluginIndex readCorePluginIndex(ServletContext context)
|
private PluginIndex readCorePluginIndex(ServletContext context) {
|
||||||
{
|
|
||||||
PluginIndex index = null;
|
PluginIndex index = null;
|
||||||
|
|
||||||
try
|
try {
|
||||||
{
|
|
||||||
URL indexUrl = context.getResource(PLUGIN_COREINDEX);
|
URL indexUrl = context.getResource(PLUGIN_COREINDEX);
|
||||||
|
|
||||||
if (indexUrl == null)
|
if (indexUrl == null) {
|
||||||
{
|
|
||||||
throw new PluginException("no core plugin index found");
|
throw new PluginException("no core plugin index found");
|
||||||
}
|
}
|
||||||
|
|
||||||
index = JAXB.unmarshal(indexUrl, PluginIndex.class);
|
index = JAXB.unmarshal(indexUrl, PluginIndex.class);
|
||||||
}
|
} catch (MalformedURLException ex) {
|
||||||
catch (MalformedURLException ex)
|
|
||||||
{
|
|
||||||
throw new PluginException("could not load core plugin index", ex);
|
throw new PluginException("could not load core plugin index", ex);
|
||||||
}
|
} catch (DataBindingException ex) {
|
||||||
catch (DataBindingException ex)
|
|
||||||
{
|
|
||||||
throw new PluginException("could not unmarshall core plugin index", ex);
|
throw new PluginException("could not unmarshall core plugin index", ex);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -308,8 +281,7 @@ public class BootstrapContextListener implements ServletContextListener
|
|||||||
*
|
*
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
private File getPluginDirectory()
|
private File getPluginDirectory() {
|
||||||
{
|
|
||||||
File baseDirectory = SCMContext.getContext().getBaseDirectory();
|
File baseDirectory = SCMContext.getContext().getBaseDirectory();
|
||||||
|
|
||||||
return new File(baseDirectory, DIRECTORY_PLUGINS);
|
return new File(baseDirectory, DIRECTORY_PLUGINS);
|
||||||
@@ -326,8 +298,7 @@ public class BootstrapContextListener implements ServletContextListener
|
|||||||
*/
|
*/
|
||||||
@XmlAccessorType(XmlAccessType.FIELD)
|
@XmlAccessorType(XmlAccessType.FIELD)
|
||||||
@XmlRootElement(name = "plugin-index")
|
@XmlRootElement(name = "plugin-index")
|
||||||
private static class PluginIndex implements Iterable<PluginIndexEntry>
|
private static class PluginIndex implements Iterable<PluginIndexEntry> {
|
||||||
{
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Method description
|
* Method description
|
||||||
@@ -336,8 +307,7 @@ public class BootstrapContextListener implements ServletContextListener
|
|||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public Iterator<PluginIndexEntry> iterator()
|
public Iterator<PluginIndexEntry> iterator() {
|
||||||
{
|
|
||||||
return getPlugins().iterator();
|
return getPlugins().iterator();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -349,10 +319,8 @@ public class BootstrapContextListener implements ServletContextListener
|
|||||||
*
|
*
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
public List<PluginIndexEntry> getPlugins()
|
public List<PluginIndexEntry> getPlugins() {
|
||||||
{
|
if (plugins == null) {
|
||||||
if (plugins == null)
|
|
||||||
{
|
|
||||||
plugins = ImmutableList.of();
|
plugins = ImmutableList.of();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -376,8 +344,7 @@ public class BootstrapContextListener implements ServletContextListener
|
|||||||
*/
|
*/
|
||||||
@XmlRootElement(name = "plugins")
|
@XmlRootElement(name = "plugins")
|
||||||
@XmlAccessorType(XmlAccessType.FIELD)
|
@XmlAccessorType(XmlAccessType.FIELD)
|
||||||
private static class PluginIndexEntry
|
private static class PluginIndexEntry {
|
||||||
{
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Method description
|
* Method description
|
||||||
@@ -385,8 +352,7 @@ public class BootstrapContextListener implements ServletContextListener
|
|||||||
*
|
*
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
public String getChecksum()
|
public String getChecksum() {
|
||||||
{
|
|
||||||
return checksum;
|
return checksum;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -396,8 +362,7 @@ public class BootstrapContextListener implements ServletContextListener
|
|||||||
*
|
*
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
public String getName()
|
public String getName() {
|
||||||
{
|
|
||||||
return name;
|
return name;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,9 +1,12 @@
|
|||||||
package sonia.scm;
|
package sonia.scm.boot;
|
||||||
|
|
||||||
import com.google.inject.AbstractModule;
|
import com.google.inject.AbstractModule;
|
||||||
import com.google.inject.throwingproviders.ThrowingProviderBinder;
|
import com.google.inject.throwingproviders.ThrowingProviderBinder;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
import sonia.scm.ClassOverrides;
|
||||||
|
import sonia.scm.SCMContext;
|
||||||
|
import sonia.scm.SCMContextProvider;
|
||||||
import sonia.scm.io.DefaultFileSystem;
|
import sonia.scm.io.DefaultFileSystem;
|
||||||
import sonia.scm.io.FileSystem;
|
import sonia.scm.io.FileSystem;
|
||||||
import sonia.scm.plugin.DefaultPluginLoader;
|
import sonia.scm.plugin.DefaultPluginLoader;
|
||||||
@@ -28,7 +31,7 @@ public class BootstrapModule extends AbstractModule {
|
|||||||
|
|
||||||
private final ClassOverrides overrides;
|
private final ClassOverrides overrides;
|
||||||
|
|
||||||
public BootstrapModule(DefaultPluginLoader pluginLoader) {
|
BootstrapModule(DefaultPluginLoader pluginLoader) {
|
||||||
this.overrides = ClassOverrides.findOverrides(pluginLoader.getUberClassLoader());
|
this.overrides = ClassOverrides.findOverrides(pluginLoader.getUberClassLoader());
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -0,0 +1,24 @@
|
|||||||
|
package sonia.scm.boot;
|
||||||
|
|
||||||
|
import com.google.inject.AbstractModule;
|
||||||
|
import com.google.inject.multibindings.Multibinder;
|
||||||
|
import sonia.scm.migration.UpdateStep;
|
||||||
|
import sonia.scm.plugin.PluginLoader;
|
||||||
|
|
||||||
|
class UpdateStepModule extends AbstractModule {
|
||||||
|
|
||||||
|
private final PluginLoader pluginLoader;
|
||||||
|
|
||||||
|
UpdateStepModule(PluginLoader pluginLoader) {
|
||||||
|
this.pluginLoader = pluginLoader;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void configure() {
|
||||||
|
Multibinder<UpdateStep> updateStepBinder = Multibinder.newSetBinder(binder(), UpdateStep.class);
|
||||||
|
pluginLoader
|
||||||
|
.getExtensionProcessor()
|
||||||
|
.byExtensionPoint(UpdateStep.class)
|
||||||
|
.forEach(stepClass -> updateStepBinder.addBinding().to(stepClass));
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user