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

ArgumentProcessorRegistry.java 6.3 kB

8 years ago
11 years ago
7 years ago
9 years ago
8 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. */
  18. package org.apache.tools.ant;
  19. import java.io.BufferedReader;
  20. import java.io.IOException;
  21. import java.io.InputStream;
  22. import java.io.InputStreamReader;
  23. import java.io.UnsupportedEncodingException;
  24. import java.net.URL;
  25. import java.net.URLConnection;
  26. import java.util.ArrayList;
  27. import java.util.Collections;
  28. import java.util.List;
  29. import org.apache.tools.ant.util.FileUtils;
  30. import org.apache.tools.ant.util.LoaderUtils;
  31. /**
  32. * The global registry for {@link ArgumentProcessor}s.
  33. * <p>
  34. * An {@link ArgumentProcessor} implementation can be registered via the system
  35. * property <code>org.apache.tools.ant.ArgumentProcessor</code>, or via a JDK1.3
  36. * 'service', by putting the fully qualified name of the implementation into the
  37. * file <code>META-INF/services/org.apache.tools.ant.ArgumentProcessor</code>
  38. * <p>
  39. * Use the system property <code>ant.argument-processor.debug</code> to enable
  40. * the print of debug log.
  41. *
  42. * @since 1.9
  43. */
  44. public class ArgumentProcessorRegistry {
  45. private static final String DEBUG_ARGUMENT_PROCESSOR_REPOSITORY = "ant.argument-processor-repo.debug";
  46. // The message log level is not accessible here because everything
  47. // is instantiated statically
  48. private static final boolean DEBUG = "true".equals(System.getProperty(DEBUG_ARGUMENT_PROCESSOR_REPOSITORY));
  49. private static final String SERVICE_ID = "META-INF/services/org.apache.tools.ant.ArgumentProcessor";
  50. private static ArgumentProcessorRegistry instance = new ArgumentProcessorRegistry();
  51. private List<ArgumentProcessor> processors = new ArrayList<>();
  52. public static ArgumentProcessorRegistry getInstance() {
  53. return instance;
  54. }
  55. private ArgumentProcessorRegistry() {
  56. collectArgumentProcessors();
  57. }
  58. public List<ArgumentProcessor> getProcessors() {
  59. return processors;
  60. }
  61. private void collectArgumentProcessors() {
  62. try {
  63. ClassLoader classLoader = LoaderUtils.getContextClassLoader();
  64. if (classLoader != null) {
  65. for (URL resource : Collections.list(classLoader.getResources(SERVICE_ID))) {
  66. URLConnection conn = resource.openConnection();
  67. conn.setUseCaches(false);
  68. ArgumentProcessor processor = getProcessorByService(conn.getInputStream());
  69. registerArgumentProcessor(processor);
  70. }
  71. }
  72. InputStream systemResource = ClassLoader.getSystemResourceAsStream(SERVICE_ID);
  73. if (systemResource != null) { //NOSONAR
  74. ArgumentProcessor processor = getProcessorByService(systemResource);
  75. registerArgumentProcessor(processor);
  76. }
  77. } catch (Exception e) {
  78. System.err.println("Unable to load ArgumentProcessor from service "
  79. + SERVICE_ID + " (" + e.getClass().getName() + ": "
  80. + e.getMessage() + ")");
  81. if (DEBUG) {
  82. e.printStackTrace(System.err); //NOSONAR
  83. }
  84. }
  85. }
  86. public void registerArgumentProcessor(String helperClassName)
  87. throws BuildException {
  88. registerArgumentProcessor(getProcessor(helperClassName));
  89. }
  90. public void registerArgumentProcessor(
  91. Class< ? extends ArgumentProcessor> helperClass)
  92. throws BuildException {
  93. registerArgumentProcessor(getProcessor(helperClass));
  94. }
  95. private ArgumentProcessor getProcessor(String helperClassName) {
  96. try {
  97. @SuppressWarnings("unchecked")
  98. Class< ? extends ArgumentProcessor> cl = (Class< ? extends ArgumentProcessor>) Class.forName(helperClassName);
  99. return getProcessor(cl);
  100. } catch (ClassNotFoundException e) {
  101. throw new BuildException("Argument processor class "
  102. + helperClassName + " was not found", e);
  103. }
  104. }
  105. private ArgumentProcessor getProcessor(
  106. Class< ? extends ArgumentProcessor> processorClass) {
  107. ArgumentProcessor processor;
  108. try {
  109. processor = processorClass.getConstructor().newInstance();
  110. } catch (Exception e) {
  111. throw new BuildException("The argument processor class"
  112. + processorClass.getClass().getName()
  113. + " could not be instantiated with a default constructor",
  114. e);
  115. }
  116. return processor;
  117. }
  118. public void registerArgumentProcessor(ArgumentProcessor processor) {
  119. if (processor == null) {
  120. return;
  121. }
  122. processors.add(processor);
  123. if (DEBUG) {
  124. System.out.println("Argument processor "
  125. + processor.getClass().getName() + " registered.");
  126. }
  127. }
  128. private ArgumentProcessor getProcessorByService(InputStream is)
  129. throws IOException {
  130. InputStreamReader isr = null;
  131. try {
  132. try {
  133. isr = new InputStreamReader(is, "UTF-8");
  134. } catch (UnsupportedEncodingException e) {
  135. isr = new InputStreamReader(is);
  136. }
  137. BufferedReader rd = new BufferedReader(isr);
  138. String processorClassName = rd.readLine();
  139. if (processorClassName != null && !processorClassName.isEmpty()) {
  140. return getProcessor(processorClassName);
  141. }
  142. } finally {
  143. FileUtils.close(isr);
  144. }
  145. return null;
  146. }
  147. }