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 4.3KB

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