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.

ServiceProviderExtensionStorage.java 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /*
  2. * Copyright (C) 2012-present the original author or authors.
  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.processor;
  17. import javax.annotation.processing.FilerException;
  18. import javax.tools.FileObject;
  19. import javax.tools.StandardLocation;
  20. import java.io.BufferedWriter;
  21. import java.io.FileNotFoundException;
  22. import java.io.IOException;
  23. import java.nio.file.NoSuchFileException;
  24. import java.util.HashMap;
  25. import java.util.HashSet;
  26. import java.util.Map;
  27. import java.util.Set;
  28. /**
  29. * Stores {@link org.pf4j.Extension}s in {@code META-INF/services}.
  30. *
  31. * @author Decebal Suiu
  32. */
  33. public class ServiceProviderExtensionStorage extends ExtensionStorage {
  34. public static final String EXTENSIONS_RESOURCE = "META-INF/services";
  35. public ServiceProviderExtensionStorage(ExtensionAnnotationProcessor processor) {
  36. super(processor);
  37. }
  38. @Override
  39. public Map<String, Set<String>> read() {
  40. Map<String, Set<String>> extensions = new HashMap<>();
  41. for (String extensionPoint : processor.getExtensions().keySet()) {
  42. try {
  43. FileObject file = getFiler().getResource(StandardLocation.CLASS_OUTPUT, "", EXTENSIONS_RESOURCE
  44. + "/" + extensionPoint);
  45. Set<String> entries = new HashSet<>();
  46. read(file.openReader(true), entries);
  47. extensions.put(extensionPoint, entries);
  48. } catch (FileNotFoundException | NoSuchFileException e) {
  49. // doesn't exist, ignore
  50. } catch (FilerException e) {
  51. // re-opening the file for reading or after writing is ignorable
  52. } catch (IOException e) {
  53. error(e.getMessage());
  54. }
  55. }
  56. return extensions;
  57. }
  58. @Override
  59. public void write(Map<String, Set<String>> extensions) {
  60. for (Map.Entry<String, Set<String>> entry : extensions.entrySet()) {
  61. String extensionPoint = entry.getKey();
  62. try {
  63. FileObject file = getFiler().createResource(StandardLocation.CLASS_OUTPUT, "", EXTENSIONS_RESOURCE
  64. + "/" + extensionPoint);
  65. BufferedWriter writer = new BufferedWriter(file.openWriter());
  66. // write header
  67. writer.write("# Generated by PF4J"); // write header
  68. writer.newLine();
  69. // write extensions
  70. for (String extension : entry.getValue()) {
  71. writer.write(extension);
  72. if (!isExtensionOld(extensionPoint, extension)) {
  73. writer.write(" # pf4j extension");
  74. }
  75. writer.newLine();
  76. }
  77. writer.close();
  78. } catch (FileNotFoundException e) {
  79. // it's the first time, create the file
  80. } catch (FilerException e) {
  81. // re-opening the file for reading or after writing is ignorable
  82. } catch (IOException e) {
  83. error(e.toString());
  84. }
  85. }
  86. }
  87. private boolean isExtensionOld(String extensionPoint, String extension) {
  88. return processor.getOldExtensions().containsKey(extensionPoint)
  89. && processor.getOldExtensions().get(extensionPoint).contains(extension);
  90. }
  91. }