You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

DefaultPluginDescriptorFinder.java 2.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. /*
  2. * Copyright 2013 Decebal Suiu
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package org.pf4j;
  17. import org.slf4j.Logger;
  18. import org.slf4j.LoggerFactory;
  19. import java.io.IOException;
  20. import java.io.InputStream;
  21. import java.nio.file.Files;
  22. import java.nio.file.Path;
  23. import java.nio.file.Paths;
  24. import java.util.jar.Manifest;
  25. /**
  26. * The default implementation for {@link PluginDescriptorFinder}.
  27. * Now, this class it's a "link" to {@link ManifestPluginDescriptorFinder}.
  28. *
  29. * @author Decebal Suiu
  30. */
  31. public class DefaultPluginDescriptorFinder extends ManifestPluginDescriptorFinder {
  32. private static final Logger log = LoggerFactory.getLogger(ManifestPluginDescriptorFinder.class);
  33. private PluginClasspath pluginClasspath;
  34. public DefaultPluginDescriptorFinder(PluginClasspath pluginClasspath) {
  35. this.pluginClasspath = pluginClasspath;
  36. }
  37. @Override
  38. public Manifest readManifest(Path pluginPath) throws PluginException {
  39. // TODO it's ok with first classes root? Another idea is to specify in PluginClasspath the folder.
  40. if (pluginClasspath.getClassesDirectories().size() == 0) {
  41. throw new PluginException("Failed to read manifest, no classes folder in classpath");
  42. }
  43. String classes = pluginClasspath.getClassesDirectories().get(0);
  44. Path manifestPath = pluginPath.resolve(Paths.get(classes,"/META-INF/MANIFEST.MF"));
  45. log.debug("Lookup plugin descriptor in '{}'", manifestPath);
  46. if (Files.notExists(manifestPath)) {
  47. throw new PluginException("Cannot find '{}' path", manifestPath);
  48. }
  49. try (InputStream input = Files.newInputStream(manifestPath)) {
  50. return new Manifest(input);
  51. } catch (IOException e) {
  52. throw new PluginException(e.getMessage(), e);
  53. }
  54. }
  55. }