From eeaab4adb12982d6f668588bc1e66b8ac338fb8a Mon Sep 17 00:00:00 2001 From: Sebastian Sdorra Date: Sat, 12 Mar 2011 11:20:49 +0100 Subject: [PATCH] store plugin informations with jaxb --- .../scm/plugin/DefaultPluginBackend.java | 73 ++++++++-- .../sonia/scm/plugin/PluginBackendStore.java | 127 ++++++++++++++++++ .../scanner/PluginScannerTimerTask.java | 14 ++ .../src/main/webapp/META-INF/context.xml | 2 + 4 files changed, 207 insertions(+), 9 deletions(-) create mode 100644 scm-plugin-backend/src/main/java/sonia/scm/plugin/PluginBackendStore.java create mode 100644 scm-plugin-backend/src/main/webapp/META-INF/context.xml diff --git a/scm-plugin-backend/src/main/java/sonia/scm/plugin/DefaultPluginBackend.java b/scm-plugin-backend/src/main/java/sonia/scm/plugin/DefaultPluginBackend.java index e46fe85f5c..24be753ca7 100644 --- a/scm-plugin-backend/src/main/java/sonia/scm/plugin/DefaultPluginBackend.java +++ b/scm-plugin-backend/src/main/java/sonia/scm/plugin/DefaultPluginBackend.java @@ -38,6 +38,11 @@ package sonia.scm.plugin; import com.google.inject.Inject; import com.google.inject.name.Named; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import sonia.scm.ConfigurationException; + //~--- JDK imports ------------------------------------------------------------ import java.io.File; @@ -45,6 +50,9 @@ import java.io.File; import java.util.ArrayList; import java.util.List; +import javax.xml.bind.JAXBContext; +import javax.xml.bind.JAXBException; + /** * * @author Sebastian Sdorra @@ -52,6 +60,18 @@ import java.util.List; public class DefaultPluginBackend implements PluginBackend { + /** Field description */ + public static final String FILE_STORE = "store.xml"; + + /** Field description */ + private static final Object LOCK_OBJECT = new Object(); + + /** the logger for DefaultPluginBackend */ + private static final Logger logger = + LoggerFactory.getLogger(DefaultPluginBackend.class); + + //~--- constructors --------------------------------------------------------- + /** * Constructs ... * @@ -64,6 +84,23 @@ public class DefaultPluginBackend implements PluginBackend @Named(ScmBackendModule.DIRECTORY_PROPERTY) File baseDirectory) { this.baseDirectory = baseDirectory; + this.storeFile = new File(baseDirectory, FILE_STORE); + + try + { + storeContext = JAXBContext.newInstance(PluginBackendStore.class); + + if (storeFile.exists()) + { + pluginStore = + (PluginBackendStore) storeContext.createUnmarshaller().unmarshal( + storeFile); + } + } + catch (JAXBException ex) + { + throw new ConfigurationException(ex); + } } //~--- methods -------------------------------------------------------------- @@ -77,7 +114,14 @@ public class DefaultPluginBackend implements PluginBackend @Override public void addPlugin(PluginInformation plugin) { - plugins.add(plugin); + if (!pluginStore.contains(plugin)) + { + synchronized (LOCK_OBJECT) + { + pluginStore.add(plugin); + store(); + } + } } //~--- get methods ---------------------------------------------------------- @@ -103,7 +147,7 @@ public class DefaultPluginBackend implements PluginBackend @Override public List getPlugins() { - return plugins; + return pluginStore.getPlugins(); } /** @@ -120,7 +164,7 @@ public class DefaultPluginBackend implements PluginBackend List filteredPlugins = new ArrayList(); - for (PluginInformation plugin : plugins) + for (PluginInformation plugin : pluginStore) { if (filter.accept(plugin)) { @@ -131,17 +175,22 @@ public class DefaultPluginBackend implements PluginBackend return filteredPlugins; } - //~--- set methods ---------------------------------------------------------- + //~--- methods -------------------------------------------------------------- /** * Method description * - * - * @param plugins */ - public void setPlugins(List plugins) + private void store() { - this.plugins = plugins; + try + { + storeContext.createMarshaller().marshal(pluginStore, storeFile); + } + catch (JAXBException ex) + { + logger.error("could not store pluginStore", ex); + } } //~--- fields --------------------------------------------------------------- @@ -150,5 +199,11 @@ public class DefaultPluginBackend implements PluginBackend private File baseDirectory; /** Field description */ - private List plugins = new ArrayList(); + private PluginBackendStore pluginStore; + + /** Field description */ + private JAXBContext storeContext; + + /** Field description */ + private File storeFile; } diff --git a/scm-plugin-backend/src/main/java/sonia/scm/plugin/PluginBackendStore.java b/scm-plugin-backend/src/main/java/sonia/scm/plugin/PluginBackendStore.java new file mode 100644 index 0000000000..4fccfb7ffe --- /dev/null +++ b/scm-plugin-backend/src/main/java/sonia/scm/plugin/PluginBackendStore.java @@ -0,0 +1,127 @@ +/** + * Copyright (c) 2010, Sebastian Sdorra + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other 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 derived from this + * software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 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 OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * http://bitbucket.org/sdorra/scm-manager + * + */ + + + +package sonia.scm.plugin; + +//~--- JDK imports ------------------------------------------------------------ + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; + +import javax.xml.bind.annotation.XmlAccessType; +import javax.xml.bind.annotation.XmlAccessorType; +import javax.xml.bind.annotation.XmlElement; +import javax.xml.bind.annotation.XmlElementWrapper; +import javax.xml.bind.annotation.XmlRootElement; + +/** + * + * @author Sebastian Sdorra + */ +@XmlAccessorType(XmlAccessType.FIELD) +@XmlRootElement(name = "plugin-backend") +public class PluginBackendStore implements Iterable +{ + + /** + * Method description + * + * + * @param e + * + * @return + */ + public boolean add(PluginInformation e) + { + return plugins.add(e); + } + + /** + * Method description + * + * + * @param plugin + * + * @return + */ + public boolean contains(PluginInformation plugin) + { + return plugins.contains(plugin); + } + + /** + * Method description + * + * + * @return + */ + @Override + public Iterator iterator() + { + return plugins.iterator(); + } + + //~--- get methods ---------------------------------------------------------- + + /** + * Method description + * + * + * @return + */ + public List getPlugins() + { + return plugins; + } + + //~--- set methods ---------------------------------------------------------- + + /** + * Method description + * + * + * @param plugins + */ + public void setPlugins(List plugins) + { + this.plugins = plugins; + } + + //~--- fields --------------------------------------------------------------- + + /** Field description */ + @XmlElement(name = "plugin") + @XmlElementWrapper(name = "plugins") + private List plugins = new ArrayList(); +} diff --git a/scm-plugin-backend/src/main/java/sonia/scm/plugin/scanner/PluginScannerTimerTask.java b/scm-plugin-backend/src/main/java/sonia/scm/plugin/scanner/PluginScannerTimerTask.java index 3ce6f0c852..0f4a6c7f49 100644 --- a/scm-plugin-backend/src/main/java/sonia/scm/plugin/scanner/PluginScannerTimerTask.java +++ b/scm-plugin-backend/src/main/java/sonia/scm/plugin/scanner/PluginScannerTimerTask.java @@ -35,6 +35,9 @@ package sonia.scm.plugin.scanner; //~--- non-JDK imports -------------------------------------------------------- +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + import sonia.scm.plugin.BackendConfiguration; import sonia.scm.plugin.PluginBackend; @@ -51,6 +54,12 @@ import java.util.TimerTask; public class PluginScannerTimerTask extends TimerTask { + /** the logger for PluginScannerTimerTask */ + private static final Logger logger = + LoggerFactory.getLogger(PluginScannerTimerTask.class); + + //~--- constructors --------------------------------------------------------- + /** * Constructs ... * @@ -76,6 +85,11 @@ public class PluginScannerTimerTask extends TimerTask @Override public void run() { + if (logger.isInfoEnabled()) + { + logger.info("start scann"); + } + for (File directory : configuration.getDirectories()) { PluginScanner scanner = scannerFactory.createScanner(); diff --git a/scm-plugin-backend/src/main/webapp/META-INF/context.xml b/scm-plugin-backend/src/main/webapp/META-INF/context.xml new file mode 100644 index 0000000000..46c75c8b89 --- /dev/null +++ b/scm-plugin-backend/src/main/webapp/META-INF/context.xml @@ -0,0 +1,2 @@ + +