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.

LegacyExtensionStorage.java 3.7KB

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