001 /*
002 * Sonar, open source software quality management tool.
003 * Copyright (C) 2008-2012 SonarSource
004 * mailto:contact AT sonarsource DOT com
005 *
006 * Sonar is free software; you can redistribute it and/or
007 * modify it under the terms of the GNU Lesser General Public
008 * License as published by the Free Software Foundation; either
009 * version 3 of the License, or (at your option) any later version.
010 *
011 * Sonar is distributed in the hope that it will be useful,
012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014 * Lesser General Public License for more details.
015 *
016 * You should have received a copy of the GNU Lesser General Public
017 * License along with Sonar; if not, write to the Free Software
018 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
019 */
020 package org.sonar.server.configuration;
021
022 import com.thoughtworks.xstream.XStream;
023 import com.thoughtworks.xstream.converters.Converter;
024 import com.thoughtworks.xstream.converters.MarshallingContext;
025 import com.thoughtworks.xstream.converters.UnmarshallingContext;
026 import com.thoughtworks.xstream.io.HierarchicalStreamReader;
027 import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
028 import org.slf4j.LoggerFactory;
029 import org.sonar.api.database.DatabaseSession;
030 import org.sonar.api.measures.Metric;
031 import org.sonar.jpa.dao.MeasuresDao;
032
033 import java.util.Collection;
034
035 public class MetricsBackup implements Backupable {
036
037 private MeasuresDao measuresDao;
038
039 public MetricsBackup(DatabaseSession session) {
040 measuresDao = new MeasuresDao(session);
041 }
042
043 protected MetricsBackup() {
044 }
045
046 public void exportXml(SonarConfig sonarConfig) {
047 Collection<Metric> metrics = measuresDao.getUserDefinedMetrics();
048 exportXml(sonarConfig, metrics);
049 }
050
051 protected void exportXml(SonarConfig sonarConfig, Collection<Metric> metrics) {
052 sonarConfig.setMetrics(metrics);
053 }
054
055 public void importXml(SonarConfig sonarConfig) {
056 disableUserDefinedMetrics();
057 registerMetrics(sonarConfig);
058 }
059
060 protected void disableUserDefinedMetrics() {
061 LoggerFactory.getLogger(getClass()).info("Disable user-defined metrics");
062 Collection<Metric> dbMetrics = measuresDao.getUserDefinedMetrics();
063 measuresDao.disabledMetrics(dbMetrics);
064 }
065
066 protected void registerMetrics(SonarConfig sonarConfig) {
067 LoggerFactory.getLogger(getClass()).info("Restore metrics");
068 Collection<Metric> newMetrics = sonarConfig.getMetrics();
069 measuresDao.registerMetrics(newMetrics);
070 }
071
072 public void configure(XStream xStream) {
073 xStream.alias("metric", Metric.class);
074 xStream.omitField(Metric.class, "id");
075 xStream.omitField(Metric.class, "userManaged");
076 xStream.omitField(Metric.class, "comparable");
077 xStream.omitField(Metric.class, "enabled");
078 final Converter builtIn = xStream.getConverterLookup().lookupConverterForType(Metric.class);
079 xStream.registerConverter(new Converter() {
080 public void marshal(Object source, HierarchicalStreamWriter writer, MarshallingContext context) {
081 builtIn.marshal(source, writer, context);
082 }
083
084 public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) {
085 Metric unmarshalled = (Metric) builtIn.unmarshal(reader, context);
086 unmarshalled.setId(null); // See http://jira.codehaus.org/browse/SONAR-1819
087 unmarshalled.setUserManaged(true);
088 unmarshalled.setEnabled(true);
089 return unmarshalled;
090 }
091
092 public boolean canConvert(Class type) {
093 return Metric.class.equals(type);
094 }
095 });
096 }
097
098 }