Files
SCM-Manager/scm-webapp/src/main/java/sonia/scm/plugin/ExplodedSmp.java

164 lines
4.8 KiB
Java
Raw Normal View History

/*
* MIT License
2014-07-13 09:47:14 +02:00
*
* Copyright (c) 2020-present Cloudogu GmbH and Contributors
2014-07-13 09:47:14 +02:00
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
2014-07-13 09:47:14 +02:00
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
2014-07-13 09:47:14 +02:00
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
2014-07-13 09:47:14 +02:00
*/
2020-08-06 21:35:12 +02:00
2014-07-13 09:47:14 +02:00
package sonia.scm.plugin;
//~--- non-JDK imports --------------------------------------------------------
import com.google.common.base.Function;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
2014-07-13 09:47:14 +02:00
import java.nio.file.Files;
2014-07-13 09:47:14 +02:00
import java.nio.file.Path;
import java.util.Objects;
2014-07-13 09:47:14 +02:00
2020-08-06 21:35:12 +02:00
//~--- JDK imports ------------------------------------------------------------
2014-07-13 09:47:14 +02:00
/**
2014-10-10 21:26:47 +02:00
* The ExplodedSmp object represents an extracted SCM-Manager plugin. The object
* contains the path to the plugin directory and loads the plugin descriptor.
* The ExplodedSmp can be created with the {@link #create(java.nio.file.Path)}
* method.
2014-07-13 09:47:14 +02:00
*
* @author Sebastian Sdorra
*/
public final class ExplodedSmp {
2014-07-13 09:47:14 +02:00
private static final Logger logger = LoggerFactory.getLogger(ExplodedSmp.class);
2014-07-13 09:47:14 +02:00
/**
* Constructs ...
*
* @param path
2014-07-13 13:47:35 +02:00
* @param plugin
2014-07-13 09:47:14 +02:00
*/
ExplodedSmp(Path path, InstalledPluginDescriptor plugin) {
logger.trace("create exploded scm for plugin {} and dependencies {}", plugin.getInformation().getName(), plugin.getDependencies());
2014-07-13 09:47:14 +02:00
this.path = path;
2014-07-13 13:47:35 +02:00
this.plugin = plugin;
2014-07-13 09:47:14 +02:00
}
//~--- methods --------------------------------------------------------------
/**
2014-10-10 21:26:47 +02:00
* Creates a new ExplodedSmp object.
2014-07-13 09:47:14 +02:00
*
2014-10-10 21:26:47 +02:00
* @param directory directory containing an extracted SCM-Manager plugin.
* @return ExplodedSmp object
* @throws PluginException if the path does not contain an plugin descriptor
* or the plugin descriptor could not be parsed
2014-07-13 09:47:14 +02:00
*/
public static ExplodedSmp create(Path directory) {
2014-07-13 13:47:35 +02:00
Path desc = directory.resolve(PluginConstants.FILE_DESCRIPTOR);
2014-07-13 09:47:14 +02:00
2014-07-13 13:47:35 +02:00
return new ExplodedSmp(directory, Plugins.parsePluginDescriptor(desc));
2014-07-13 09:47:14 +02:00
}
//~--- get methods ----------------------------------------------------------
/**
* Returns {@code true} if the exploded smp contains a core plugin
*
* @return {@code true} for a core plugin
* @since 2.30.0
*/
public boolean isCore() {
return Files.exists(path.resolve(PluginConstants.FILE_CORE));
}
2014-07-13 09:47:14 +02:00
/**
2014-10-10 21:26:47 +02:00
* Returns the path to the plugin directory.
2014-07-13 09:47:14 +02:00
*
2014-10-10 21:26:47 +02:00
* @return to plugin directory
2014-07-13 09:47:14 +02:00
*/
public Path getPath() {
2014-07-13 09:47:14 +02:00
return path;
}
/**
2014-10-10 21:26:47 +02:00
* Returns parsed plugin descriptor.
2014-07-13 09:47:14 +02:00
*
2014-10-10 21:26:47 +02:00
* @return plugin descriptor
2014-07-13 09:47:14 +02:00
*/
public InstalledPluginDescriptor getPlugin() {
2014-07-13 13:47:35 +02:00
return plugin;
2014-07-13 09:47:14 +02:00
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
ExplodedSmp that = (ExplodedSmp) o;
return path.equals(that.path);
}
@Override
public int hashCode() {
return Objects.hash(path);
}
@Override
public String toString() {
PluginInformation information = plugin.getInformation();
return information.getName() + "@" + information.getVersion() + " (" + path + ")";
}
2014-07-13 09:47:14 +02:00
//~--- inner classes --------------------------------------------------------
/**
2014-10-10 21:26:47 +02:00
* Transforms {@link Path} to {@link ExplodedSmp}.
2014-07-13 09:47:14 +02:00
*/
public static class PathTransformer implements Function<Path, ExplodedSmp> {
2014-07-13 09:47:14 +02:00
/**
2014-10-10 21:26:47 +02:00
* Transforms {@link Path} to {@link ExplodedSmp}. The path must contain an
* extracted SCM-Manager plugin.
2014-07-13 09:47:14 +02:00
*
2014-10-10 21:26:47 +02:00
* @param directory directory containing exploded plugin
* @return exploded smp object
* @throws PluginException if the path does not contain an extracted
* SCM-Manager plugin.
2014-07-13 09:47:14 +02:00
*/
@Override
public ExplodedSmp apply(Path directory) {
2014-10-10 21:26:47 +02:00
return ExplodedSmp.create(directory);
2014-07-13 09:47:14 +02:00
}
}
//~--- fields ---------------------------------------------------------------
/**
* directory
*/
2014-07-13 09:47:14 +02:00
private final Path path;
/**
* plugin object
*/
private final InstalledPluginDescriptor plugin;
2014-07-13 09:47:14 +02:00
}