Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

LegacyExtensionStorage.java 3.0KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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/extensions.idx}.
  30. *
  31. * @author Decebal Suiu
  32. */
  33. public class LegacyExtensionStorage extends ExtensionStorage {
  34. public static final String EXTENSIONS_RESOURCE = "META-INF/extensions.idx";
  35. public LegacyExtensionStorage(ExtensionAnnotationProcessor processor) {
  36. super(processor);
  37. }
  38. @Override
  39. public Map<String, Set<String>> read() {
  40. Map<String, Set<String>> extensions = new HashMap<>();
  41. try {
  42. FileObject file = getFiler().getResource(StandardLocation.CLASS_OUTPUT, "", EXTENSIONS_RESOURCE);
  43. // TODO try to calculate the extension point
  44. Set<String> entries = new HashSet<>();
  45. read(file.openReader(true), entries);
  46. extensions.put(null, entries);
  47. } catch (FileNotFoundException | NoSuchFileException e) {
  48. // doesn't exist, ignore
  49. } catch (FilerException e) {
  50. // re-opening the file for reading or after writing is ignorable
  51. } catch (IOException e) {
  52. error(e.getMessage());
  53. }
  54. return extensions;
  55. }
  56. @Override
  57. public void write(Map<String, Set<String>> extensions) {
  58. try {
  59. FileObject file = getFiler().createResource(StandardLocation.CLASS_OUTPUT, "", EXTENSIONS_RESOURCE);
  60. BufferedWriter writer = new BufferedWriter(file.openWriter());
  61. writer.write("# Generated by PF4J"); // write header
  62. writer.newLine();
  63. for (Map.Entry<String, Set<String>> entry : extensions.entrySet()) {
  64. for (String extension : entry.getValue()) {
  65. writer.write(extension);
  66. writer.newLine();
  67. }
  68. }
  69. writer.close();
  70. } catch (FileNotFoundException e) {
  71. // it's the first time, create the file
  72. } catch (FilerException e) {
  73. // re-opening the file for reading or after writing is ignorable
  74. } catch (IOException e) {
  75. error(e.toString());
  76. }
  77. }
  78. }