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>
*/
public abstract class AbstractSimpleRepositoryHandler<C extends SimpleRepositoryConfig>
extends AbstractRepositoryHandler<C>
extends AbstractRepositoryHandler<C> implements RepositoryDirectoryHandler
{
/** Field description */
@@ -249,6 +249,7 @@ public abstract class AbstractSimpleRepositoryHandler<C extends SimpleRepository
*
* @return
*/
@Override
public File getDirectory(Repository repository)
{
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 ------------------------------------------------------------
import java.util.Arrays;
import java.util.Collections;
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;
/**
@@ -59,8 +61,8 @@ import javax.xml.bind.annotation.XmlRootElement;
*
* @author Sebastian Sdorra
*/
@XmlRootElement(name = "repositories")
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "repositories")
public class Repository extends BasicPropertiesAware implements ModelObject
{
@@ -162,6 +164,8 @@ public class Repository extends BasicPropertiesAware implements ModelObject
repository.setPermissions(permissions);
repository.setPublicReadable(publicReadable);
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(creationDate, other.creationDate)
&& Objects.equal(lastModified, other.lastModified)
&& Objects.equal(properties, other.properties);
&& Objects.equal(properties, other.properties)
&& Objects.equal(healthCheckFailures, other.healthCheckFailures);
//J+
}
@@ -228,7 +233,8 @@ public class Repository extends BasicPropertiesAware implements ModelObject
public int hashCode()
{
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("creationDate", creationDate)
.add("properties", properties)
.add("healthCheckFailures", healthCheckFailures)
.toString();
//J+
}
@@ -293,6 +300,24 @@ public class Repository extends BasicPropertiesAware implements ModelObject
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}.
*
@@ -368,6 +393,19 @@ public class Repository extends BasicPropertiesAware implements ModelObject
return archived;
}
/**
* Method description
*
*
* @return
*
* @since 1.36
*/
public boolean isHealthy()
{
return Util.isEmpty(healthCheckFailures);
}
/**
* Returns true if the {@link Repository} is public readable.
*
@@ -513,6 +551,19 @@ public class Repository extends BasicPropertiesAware implements ModelObject
this.type = type;
}
/**
* Method description
*
*
* @param healthCheckFailures
*
* @since 1.36
*/
void setHealthCheckFailures(List<HealthCheckFailure> healthCheckFailures)
{
this.healthCheckFailures = healthCheckFailures;
}
//~--- fields ---------------------------------------------------------------
/** Field description */
@@ -524,6 +575,13 @@ public class Repository extends BasicPropertiesAware implements ModelObject
/** Field description */
private String description;
/**
* @since 1.36
*/
@XmlElement(name = "healthCheckFailure")
@XmlElementWrapper(name = "healthCheckFailures")
private List<HealthCheckFailure> healthCheckFailures;
/** Field description */
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);
}