start implementation of repository health checks

This commit is contained in:
Sebastian Sdorra
2014-01-25 12:07:38 +01:00
parent 3e4473e6ea
commit 436c149e75
12 changed files with 1329 additions and 4 deletions

View File

@@ -64,7 +64,7 @@ import java.net.URL;
* @param <C> * @param <C>
*/ */
public abstract class AbstractSimpleRepositoryHandler<C extends SimpleRepositoryConfig> public abstract class AbstractSimpleRepositoryHandler<C extends SimpleRepositoryConfig>
extends AbstractRepositoryHandler<C> extends AbstractRepositoryHandler<C> implements RepositoryDirectoryHandler
{ {
/** Field description */ /** Field description */
@@ -249,6 +249,7 @@ public abstract class AbstractSimpleRepositoryHandler<C extends SimpleRepository
* *
* @return * @return
*/ */
@Override
public File getDirectory(Repository repository) public File getDirectory(Repository repository)
{ {
File directory = null; File directory = null;

View File

@@ -0,0 +1,212 @@
/**
* 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.repository;
//~--- non-JDK imports --------------------------------------------------------
import com.google.common.base.Preconditions;
import com.google.common.base.Strings;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
//~--- JDK imports ------------------------------------------------------------
import java.io.File;
/**
*
* @author Sebastian Sdorra
* @since 1.36
*/
public abstract class DirectoryHealthCheck implements HealthCheck
{
/** Field description */
private static final HealthCheckFailure NO_TYPE =
new HealthCheckFailure("2OOTx6ta71", "Repository has no type",
"The repository does not have a configured type.");
/** Field description */
private static final HealthCheckFailure NO_HANDLER =
new HealthCheckFailure("CqOTx7Jkq1", "No handler for repository type",
"There is no registered repository handler for the type of the repository.");
/** Field description */
private static final HealthCheckFailure NO_DIRECTORY =
new HealthCheckFailure("AcOTx7fD51", "handler could not return directory",
"The repository handler was not able to return a directory for the repository");
/** Field description */
private static final HealthCheckFailure DIRECTORY_DOES_NOT_EXISTS =
new HealthCheckFailure("1oOTx803F1",
"repository directory does not exists",
"The repository does not exists. Perhaps it was deleted outside of scm-manafer.");
/**
* the logger for DirectoryHealthCheck
*/
private static final Logger logger =
LoggerFactory.getLogger(DirectoryHealthCheck.class);
//~--- constructors ---------------------------------------------------------
/**
* Constructs ...
*
*
* @param repositoryManager
*/
protected DirectoryHealthCheck(RepositoryManager repositoryManager)
{
this.repositoryManager = repositoryManager;
}
//~--- methods --------------------------------------------------------------
/**
* Method description
*
*
* @param repository
* @param directory
*
* @return
*/
protected abstract HealthCheckResult check(Repository repository,
File directory);
/**
* Method description
*
*
* @param repository
*
* @return
*/
@Override
public HealthCheckResult check(Repository repository)
{
Preconditions.checkNotNull(repository, "repository is required");
HealthCheckResult result = HealthCheckResult.healthy();
if (isCheckResponsible(repository))
{
result = doCheck(repository);
}
else if (logger.isDebugEnabled())
{
logger.debug("check is not responsible for repository {}",
repository.getName());
}
return result;
}
//~--- get methods ----------------------------------------------------------
/**
* Method description
*
*
* @param repository
*
* @return
*/
protected boolean isCheckResponsible(Repository repository)
{
return true;
}
//~--- methods --------------------------------------------------------------
/**
* Method description
*
*
* @param repository
*
* @return
*/
private HealthCheckResult doCheck(Repository repository)
{
HealthCheckResult result;
String repositoryType = repository.getType();
if (Strings.isNullOrEmpty(repositoryType))
{
result = HealthCheckResult.unhealthy(NO_TYPE);
}
else
{
RepositoryHandler handler = repositoryManager.getHandler(repositoryType);
if (handler == null)
{
result = HealthCheckResult.unhealthy(NO_HANDLER);
}
else if (handler instanceof RepositoryDirectoryHandler)
{
File directory =
((RepositoryDirectoryHandler) handler).getDirectory(repository);
if (directory == null)
{
result = HealthCheckResult.unhealthy(NO_DIRECTORY);
}
else if (!directory.exists())
{
result = HealthCheckResult.unhealthy(DIRECTORY_DOES_NOT_EXISTS);
}
else
{
result = check(repository, directory);
}
}
else
{
logger.debug(
"repository handler {} does not act on direcotries, returning healthy",
repositoryType);
result = HealthCheckResult.healthy();
}
}
return result;
}
//~--- fields ---------------------------------------------------------------
/** Field description */
private final RepositoryManager repositoryManager;
}

View File

@@ -0,0 +1,54 @@
/**
* 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.repository;
import sonia.scm.plugin.ExtensionPoint;
/**
*
* @author Sebastian Sdorra
* @since 1.36
*/
@ExtensionPoint(multi = true)
public interface HealthCheck
{
/**
* Method description
*
*
* @param repository
*
* @return
*/
public HealthCheckResult check(Repository repository);
}

View File

@@ -0,0 +1,212 @@
/**
* 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.repository;
//~--- non-JDK imports --------------------------------------------------------
import com.google.common.base.Objects;
//~--- JDK imports ------------------------------------------------------------
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;
/**
*
* @author Sebastian Sdorra
* @since 1.36
*/
@XmlRootElement(name = "healthCheckFailure")
@XmlAccessorType(XmlAccessType.FIELD)
public final class HealthCheckFailure
{
/**
* Constructs ...
*
*/
HealthCheckFailure() {}
/**
* Constructs ...
*
* @param id
* @param summary
* @param description
*/
public HealthCheckFailure(String id, String summary, String description)
{
this(id, summary, null, description);
}
/**
* Constructs ...
*
* @param id
* @param summary
* @param url
* @param description
*/
public HealthCheckFailure(String id, String summary, String url,
String description)
{
this.id = id;
this.summary = summary;
this.url = url;
this.description = description;
}
//~--- methods --------------------------------------------------------------
/**
* Method description
*
*
* @param obj
*
* @return
*/
@Override
public boolean equals(Object obj)
{
if (obj == null)
{
return false;
}
if (getClass() != obj.getClass())
{
return false;
}
final HealthCheckFailure other = (HealthCheckFailure) obj;
//J-
return Objects.equal(id, other.id)
&& Objects.equal(summary, other.summary)
&& Objects.equal(url, other.url)
&& Objects.equal(description, other.description);
//J+
}
/**
* Method description
*
*
* @return
*/
@Override
public int hashCode()
{
return Objects.hashCode(id, summary, url, description);
}
/**
* Method description
*
*
* @return
*/
@Override
public String toString()
{
//J-
return Objects.toStringHelper(this)
.add("id", id)
.add("summary", summary)
.add("url", url)
.add("description", description)
.toString();
//J+
}
//~--- get methods ----------------------------------------------------------
/**
* Method description
*
*
* @return
*/
public String getDescription()
{
return description;
}
/**
* Method description
*
*
* @return
*/
public String getId()
{
return id;
}
/**
* Method description
*
*
* @return
*/
public String getSummary()
{
return summary;
}
/**
* Method description
*
*
* @return
*/
public String getUrl()
{
return url;
}
//~--- fields ---------------------------------------------------------------
/** Field description */
private String description;
/** Field description */
private String id;
/** Field description */
private String summary;
/** Field description */
private String url;
}

View File

@@ -0,0 +1,186 @@
/**
* 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.repository;
//~--- non-JDK imports --------------------------------------------------------
import com.google.common.collect.ImmutableSet;
//~--- JDK imports ------------------------------------------------------------
import java.util.Set;
/**
*
* @author Sebastian Sdorra
* @since 1.36
*/
public final class HealthCheckResult
{
/** Field description */
private static final HealthCheckResult HEALTHY =
new HealthCheckResult(ImmutableSet.<HealthCheckFailure>of());
//~--- constructors ---------------------------------------------------------
/**
* Constructs ...
*
*
* @param failures
*/
private HealthCheckResult(Set<HealthCheckFailure> failures)
{
this.failures = failures;
}
//~--- methods --------------------------------------------------------------
/**
* Method description
*
*
* @return
*/
public static HealthCheckResult healthy()
{
return HEALTHY;
}
/**
* Method description
*
*
* @param failures
*
* @return
*/
public static HealthCheckResult unhealthy(
Iterable<HealthCheckFailure> failures)
{
return new HealthCheckResult(ImmutableSet.copyOf(failures));
}
/**
* Method description
*
*
* @param failure
* @param otherFailures
*
* @return
*/
public static HealthCheckResult unhealthy(HealthCheckFailure failure,
HealthCheckFailure... otherFailures)
{
//J-
return new HealthCheckResult(
ImmutableSet.<HealthCheckFailure>builder()
.add(failure)
.add(otherFailures)
.build()
);
//J+
}
/**
* Method description
*
*
* @param otherResult
*
* @return
*/
public HealthCheckResult merge(HealthCheckResult otherResult)
{
HealthCheckResult merged;
if ((otherResult == null) || otherResult.isHealthy())
{
merged = this;
}
else
{
//J-
merged = new HealthCheckResult(
ImmutableSet.<HealthCheckFailure>builder()
.addAll(failures)
.addAll(otherResult.failures)
.build()
);
//J+
}
return merged;
}
//~--- get methods ----------------------------------------------------------
/**
* Method description
*
*
* @return
*/
public Set<HealthCheckFailure> getFailures()
{
return failures;
}
/**
* Method description
*
*
* @return
*/
public boolean isHealthy()
{
return failures.isEmpty();
}
/**
* Method description
*
*
* @return
*/
public boolean isUnhealthy()
{
return !failures.isEmpty();
}
//~--- fields ---------------------------------------------------------------
/** Field description */
private final Set<HealthCheckFailure> failures;
}

View File

@@ -47,11 +47,13 @@ import sonia.scm.util.ValidationUtil;
//~--- JDK imports ------------------------------------------------------------ //~--- JDK imports ------------------------------------------------------------
import java.util.Arrays; import java.util.Arrays;
import java.util.Collections;
import java.util.List; import java.util.List;
import javax.xml.bind.annotation.XmlAccessType; import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType; import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlRootElement; import javax.xml.bind.annotation.XmlRootElement;
/** /**
@@ -59,8 +61,8 @@ import javax.xml.bind.annotation.XmlRootElement;
* *
* @author Sebastian Sdorra * @author Sebastian Sdorra
*/ */
@XmlRootElement(name = "repositories")
@XmlAccessorType(XmlAccessType.FIELD) @XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "repositories")
public class Repository extends BasicPropertiesAware implements ModelObject public class Repository extends BasicPropertiesAware implements ModelObject
{ {
@@ -162,6 +164,8 @@ public class Repository extends BasicPropertiesAware implements ModelObject
repository.setPermissions(permissions); repository.setPermissions(permissions);
repository.setPublicReadable(publicReadable); repository.setPublicReadable(publicReadable);
repository.setArchived(archived); repository.setArchived(archived);
// do not copy health check results
} }
/** /**
@@ -214,7 +218,8 @@ public class Repository extends BasicPropertiesAware implements ModelObject
&& Objects.equal(type, other.type) && Objects.equal(type, other.type)
&& Objects.equal(creationDate, other.creationDate) && Objects.equal(creationDate, other.creationDate)
&& Objects.equal(lastModified, other.lastModified) && Objects.equal(lastModified, other.lastModified)
&& Objects.equal(properties, other.properties); && Objects.equal(properties, other.properties)
&& Objects.equal(healthCheckFailures, other.healthCheckFailures);
//J+ //J+
} }
@@ -228,7 +233,8 @@ public class Repository extends BasicPropertiesAware implements ModelObject
public int hashCode() public int hashCode()
{ {
return Objects.hashCode(id, name, contact, description, publicReadable, return Objects.hashCode(id, name, contact, description, publicReadable,
archived, permissions, type, creationDate, lastModified, properties); archived, permissions, type, creationDate, lastModified, properties,
healthCheckFailures);
} }
/** /**
@@ -253,6 +259,7 @@ public class Repository extends BasicPropertiesAware implements ModelObject
.add("lastModified", lastModified) .add("lastModified", lastModified)
.add("creationDate", creationDate) .add("creationDate", creationDate)
.add("properties", properties) .add("properties", properties)
.add("healthCheckFailures", healthCheckFailures)
.toString(); .toString();
//J+ //J+
} }
@@ -293,6 +300,24 @@ public class Repository extends BasicPropertiesAware implements ModelObject
return description; return description;
} }
/**
* Method description
*
*
* @return
* @since 1.36
*/
@SuppressWarnings("unchecked")
public List<HealthCheckFailure> getHealthCheckFailures()
{
if (healthCheckFailures == null)
{
healthCheckFailures = Collections.EMPTY_LIST;
}
return healthCheckFailures;
}
/** /**
* Returns the unique id of the {@link Repository}. * Returns the unique id of the {@link Repository}.
* *
@@ -368,6 +393,19 @@ public class Repository extends BasicPropertiesAware implements ModelObject
return archived; return archived;
} }
/**
* Method description
*
*
* @return
*
* @since 1.36
*/
public boolean isHealthy()
{
return Util.isEmpty(healthCheckFailures);
}
/** /**
* Returns true if the {@link Repository} is public readable. * Returns true if the {@link Repository} is public readable.
* *
@@ -513,6 +551,19 @@ public class Repository extends BasicPropertiesAware implements ModelObject
this.type = type; this.type = type;
} }
/**
* Method description
*
*
* @param healthCheckFailures
*
* @since 1.36
*/
void setHealthCheckFailures(List<HealthCheckFailure> healthCheckFailures)
{
this.healthCheckFailures = healthCheckFailures;
}
//~--- fields --------------------------------------------------------------- //~--- fields ---------------------------------------------------------------
/** Field description */ /** Field description */
@@ -524,6 +575,13 @@ public class Repository extends BasicPropertiesAware implements ModelObject
/** Field description */ /** Field description */
private String description; private String description;
/**
* @since 1.36
*/
@XmlElement(name = "healthCheckFailure")
@XmlElementWrapper(name = "healthCheckFailures")
private List<HealthCheckFailure> healthCheckFailures;
/** Field description */ /** Field description */
private String id; private String id;

View File

@@ -0,0 +1,55 @@
/**
* 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.repository;
//~--- JDK imports ------------------------------------------------------------
import java.io.File;
/**
*
* @author Sebastian Sdorra
* @since 1.36
*/
public interface RepositoryDirectoryHandler extends RepositoryHandler
{
/**
* Method description
*
*
* @param repository
*
* @return
*/
public File getDirectory(Repository repository);
}

View File

@@ -0,0 +1,228 @@
/**
* 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.repository;
//~--- non-JDK imports --------------------------------------------------------
import com.google.common.base.Charsets;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Lists;
import com.google.common.io.Files;
import com.google.inject.Inject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.tmatesoft.svn.core.SVNException;
import org.tmatesoft.svn.core.SVNURL;
import org.tmatesoft.svn.core.io.SVNRepository;
import org.tmatesoft.svn.core.io.SVNRepositoryFactory;
import sonia.scm.plugin.ext.Extension;
//~--- JDK imports ------------------------------------------------------------
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Set;
/**
*
* @author Sebastian Sdorra
*/
@Extension
public class DBFormatHealthCheck extends DirectoryHealthCheck
{
/**
* the logger for DBFormatHealthCheck
*/
private static final Logger logger =
LoggerFactory.getLogger(DBFormatHealthCheck.class);
/** Field description */
private static final Set<String> INVALID_DBFORMAT = ImmutableSet.of("5");
/** Field description */
private static final HealthCheckFailure INCOMPATIBLE_DB_FORMAT =
new HealthCheckFailure("AnOTx99ex1", "Incompatible DB Format",
"https://bitbucket.org/sdorra/scm-manager/wiki/healtchecks/svn-incompatible-dbformat",
"The subversion db format is incompatible with the svn version used within scm-manager.");
/** Field description */
private static final String DBFORMAT =
"db".concat(File.separator).concat("format");
/** Field description */
private static final HealthCheckFailure COULD_NOT_READ_DB_FILE =
new HealthCheckFailure("4IOTx8pvv1", "Could not read db/format file",
"The db/format file of the repository was not readable.");
/** Field description */
private static final HealthCheckFailure COULD_NOT_OPEN_REPOSITORY =
new HealthCheckFailure("6TOTx9RLD1", "Could not open svn repository",
"The repository is not openable.");
/** Field description */
private static final HealthCheckFailure COULD_NOT_FIND_DB_FILE =
new HealthCheckFailure("A9OTx8leC1", "Could not find db/format file",
"The subversion repository does not contain the db/format file.");
//~--- constructors ---------------------------------------------------------
/**
* Constructs ...
*
*
* @param repositoryManager
*/
@Inject
public DBFormatHealthCheck(RepositoryManager repositoryManager)
{
super(repositoryManager);
}
//~--- methods --------------------------------------------------------------
/**
* Method description
*
*
* @param repository
* @param directory
*
* @return
*/
@Override
protected HealthCheckResult check(Repository repository, File directory)
{
List<HealthCheckFailure> failures = Lists.newArrayList();
checkIfRepositoryIsOpenable(failures, repository, directory);
checkForBadDBVersion(failures, repository, directory);
return failures.isEmpty()
? HealthCheckResult.healthy()
: HealthCheckResult.unhealthy(failures);
}
//~--- get methods ----------------------------------------------------------
/**
* Method description
*
*
* @param repository
*
* @return
*/
@Override
protected boolean isCheckResponsible(Repository repository)
{
return SvnRepositoryHandler.TYPE_NAME.equalsIgnoreCase(
repository.getType());
}
//~--- methods --------------------------------------------------------------
/**
* Method description
*
*
* @param failures
* @param repository
* @param directory
*/
private void checkForBadDBVersion(List<HealthCheckFailure> failures,
Repository repository, File directory)
{
File dbfile = new File(directory, DBFORMAT);
if (dbfile.exists())
{
try
{
String content = Files.readFirstLine(dbfile, Charsets.US_ASCII);
if ((content != null) && INVALID_DBFORMAT.contains(content.trim()))
{
failures.add(INCOMPATIBLE_DB_FORMAT);
}
}
catch (IOException ex)
{
failures.add(COULD_NOT_READ_DB_FILE);
logger.warn(
"could not read db/format of ".concat(repository.getName()), ex);
}
}
else
{
failures.add(COULD_NOT_FIND_DB_FILE);
logger.warn("repository {} does not have a {} file",
repository.getName(), DBFORMAT);
}
}
/**
* Method description
*
*
* @param failures
* @param repository
* @param directory
*/
private void checkIfRepositoryIsOpenable(List<HealthCheckFailure> failures,
Repository repository, File directory)
{
SVNRepository svn = null;
try
{
svn = SVNRepositoryFactory.create(SVNURL.fromFile(directory));
}
catch (SVNException ex)
{
failures.add(COULD_NOT_OPEN_REPOSITORY);
logger.warn(
"Could not open svn repository ".concat(repository.getName()), ex);
}
finally
{
SvnUtil.closeSession(svn);
}
}
}

View File

@@ -59,6 +59,7 @@ import java.util.List;
import javax.servlet.ServletContext; import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextEvent;
import sonia.scm.repository.HealthCheckContextListener;
/** /**
* *
@@ -141,6 +142,8 @@ public class ScmContextListener extends GuiceServletContextListener
// init servlet context listeners // init servlet context listeners
globalInjector.getInstance(ServletContextListenerHolder.class) globalInjector.getInstance(ServletContextListenerHolder.class)
.contextInitialized(servletContextEvent); .contextInitialized(servletContextEvent);
globalInjector.getInstance(HealthCheckContextListener.class)
.contextInitialized(servletContextEvent);
//J+ //J+
} }
} }

View File

@@ -145,6 +145,8 @@ import java.util.HashSet;
import java.util.Iterator; import java.util.Iterator;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
import sonia.scm.repository.HealthCheckContextListener;
import sonia.scm.repository.HealthChecker;
/** /**
* *
@@ -265,6 +267,9 @@ public class ScmServletModule extends ServletModule
bind(CipherHandler.class).toInstance(cu.getCipherHandler()); bind(CipherHandler.class).toInstance(cu.getCipherHandler());
bind(EncryptionHandler.class, MessageDigestEncryptionHandler.class); bind(EncryptionHandler.class, MessageDigestEncryptionHandler.class);
bind(FileSystem.class, DefaultFileSystem.class); bind(FileSystem.class, DefaultFileSystem.class);
// bind health check stuff
bind(HealthCheckContextListener.class);
// bind extensions // bind extensions
pluginLoader.processExtensions(binder()); pluginLoader.processExtensions(binder());

View File

@@ -0,0 +1,153 @@
/**
* 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.repository;
//~--- non-JDK imports --------------------------------------------------------
import com.google.inject.Inject;
import org.apache.shiro.SecurityUtils;
import sonia.scm.plugin.ext.Extension;
import sonia.scm.web.security.AdministrationContext;
import sonia.scm.web.security.PrivilegedAction;
//~--- JDK imports ------------------------------------------------------------
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
/**
*
* @author Sebastian Sdorra
*/
@Extension
public class HealthCheckContextListener implements ServletContextListener
{
/**
* Constructs ...
*
*
* @param context
*/
@Inject
public HealthCheckContextListener(AdministrationContext context)
{
this.context = context;
}
//~--- methods --------------------------------------------------------------
/**
* Method description
*
*
* @param sce
*/
@Override
public void contextDestroyed(ServletContextEvent sce)
{
// do nothing
}
/**
* Method description
*
*
* @param sce
*/
@Override
public void contextInitialized(ServletContextEvent sce)
{
context.runAsAdmin(HealthCheckStartupAction.class);
}
//~--- inner classes --------------------------------------------------------
/**
* Class description
*
*
* @version Enter version here..., 14/01/23
* @author Enter your name here...
*/
static class HealthCheckStartupAction implements PrivilegedAction
{
/**
* Constructs ...
*
*
* @param healthChecker
*/
@Inject
public HealthCheckStartupAction(HealthChecker healthChecker)
{
this.healthChecker = healthChecker;
}
//~--- methods ------------------------------------------------------------
/**
* Method description
*
*/
@Override
public void run()
{
// excute health checks for all repsitories asynchronous
SecurityUtils.getSubject().execute(new Runnable()
{
@Override
public void run()
{
healthChecker.checkAll();
}
});
}
//~--- fields -------------------------------------------------------------
/** Field description */
private final HealthChecker healthChecker;
}
//~--- fields ---------------------------------------------------------------
/** Field description */
private final AdministrationContext context;
}

View File

@@ -0,0 +1,158 @@
/**
* 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.repository;
//~--- non-JDK imports --------------------------------------------------------
import com.google.common.collect.ImmutableList;
import com.google.inject.Inject;
import org.apache.shiro.SecurityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import sonia.scm.security.Role;
//~--- JDK imports ------------------------------------------------------------
import java.io.IOException;
import java.util.Set;
/**
*
* @author Sebastian Sdorra
*/
public final class HealthChecker
{
/**
* the logger for HealthChecker
*/
private static final Logger logger =
LoggerFactory.getLogger(HealthChecker.class);
//~--- constructors ---------------------------------------------------------
/**
* Constructs ...
*
*
* @param checks
* @param repositoryManager
*/
@Inject
public HealthChecker(Set<HealthCheck> checks,
RepositoryManager repositoryManager)
{
this.checks = checks;
this.repositoryManager = repositoryManager;
}
//~--- methods --------------------------------------------------------------
/**
* Method description
*
*
* @param repository
*
* @throws IOException
* @throws RepositoryException
*/
public void check(Repository repository)
throws RepositoryException, IOException
{
logger.info("start health check for repository {}", repository.getName());
SecurityUtils.getSubject().checkRole(Role.ADMIN);
HealthCheckResult result = HealthCheckResult.healthy();
for (HealthCheck check : checks)
{
logger.trace("execute health check {} for repository {}",
check.getClass(), repository.getName());
result = result.merge(check.check(repository));
}
if (result.isUnhealthy())
{
logger.warn("repository {} is unhealthy: {}", repository.getName(),
result);
}
else
{
logger.info("repository {} is healthy", repository.getName());
}
if (!(repository.isHealthy() && result.isHealthy()))
{
logger.trace("store health check results for repository {}",
repository.getName());
repository.setHealthCheckFailures(
ImmutableList.copyOf(result.getFailures()));
repositoryManager.modify(repository);
}
}
/**
* Method description
*
*
*/
public void checkAll()
{
logger.debug("check health of all repositories");
SecurityUtils.getSubject().checkRole(Role.ADMIN);
for (Repository repository : repositoryManager.getAll())
{
try
{
check(repository);
}
catch (Exception ex)
{
logger.error("health check ends with exception", ex);
}
}
}
//~--- fields ---------------------------------------------------------------
/** Field description */
private final Set<HealthCheck> checks;
/** Field description */
private final RepositoryManager repositoryManager;
}