PageRenderTime 42ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/src/org/parosproxy/paros/core/scanner/PluginFactory.java

http://andiparos.googlecode.com/
Java | 240 lines | 160 code | 33 blank | 47 comment | 32 complexity | 49fd61b64567ecf19b0247426ef6df8c MD5 | raw file
Possible License(s): GPL-2.0, LGPL-3.0
  1. /*
  2. *
  3. * Paros and its related class files.
  4. *
  5. * Paros is an HTTP/HTTPS proxy for assessing web application security.
  6. * Copyright (C) 2003-2004 Chinotec Technologies Company
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the Clarified Artistic License
  10. * as published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * Clarified Artistic License for more details.
  16. *
  17. * You should have received a copy of the Clarified Artistic License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  20. */
  21. package org.parosproxy.paros.core.scanner;
  22. import java.util.Iterator;
  23. import java.util.List;
  24. import java.util.TreeMap;
  25. import java.util.Vector;
  26. import org.apache.commons.configuration.Configuration;
  27. import org.apache.commons.logging.Log;
  28. import org.apache.commons.logging.LogFactory;
  29. import org.parosproxy.paros.Constant;
  30. import org.parosproxy.paros.common.DynamicLoader;
  31. /**
  32. *
  33. * To change the template for this generated type comment go to Window -
  34. * Preferences - Java - Code Generation - Code and Comments
  35. */
  36. public class PluginFactory {
  37. private static Log log = LogFactory.getLog(PluginFactory.class);
  38. private static Vector<Plugin> listAllPlugin = new Vector<Plugin>();
  39. private static TreeMap<Integer, Plugin> mapAllPlugin = new TreeMap<Integer, Plugin>();
  40. private static TreeMap<String, Plugin> mapAllPluginOrderCodeName = new TreeMap<String, Plugin>();
  41. private static DynamicLoader loader = null;
  42. private Vector<Plugin> listPending = new Vector<Plugin>();
  43. private Vector<Plugin> listRunning = new Vector<Plugin>();
  44. private Vector<Plugin> listCompleted = new Vector<Plugin>();
  45. private int totalPluginToRun = 0;
  46. /**
  47. *
  48. */
  49. public PluginFactory() {
  50. super();
  51. Iterator<Plugin> iterator = null;
  52. Plugin plugin = null;
  53. synchronized (mapAllPlugin) {
  54. // pass 1 - enable all plugin's dependency
  55. iterator = mapAllPlugin.values().iterator();
  56. while (iterator.hasNext()) {
  57. plugin = iterator.next();
  58. if (plugin.isEnabled()) {
  59. enableDependency(plugin);
  60. }
  61. }
  62. // pass 2 - put enabled dependency in listPending
  63. iterator = mapAllPlugin.values().iterator();
  64. while (iterator.hasNext()) {
  65. plugin = iterator.next();
  66. if (plugin.isEnabled()) {
  67. listPending.add(plugin);
  68. }
  69. }
  70. totalPluginToRun = listPending.size();
  71. }
  72. }
  73. private void enableDependency(Plugin plugin) {
  74. String[] dependency = plugin.getDependency();
  75. if (dependency == null) {
  76. return;
  77. }
  78. for (int i = 0; i < dependency.length; i++) {
  79. try {
  80. Object obj = mapAllPluginOrderCodeName.get(dependency[i]);
  81. if (obj == null)
  82. continue;
  83. Plugin p = (Plugin) obj;
  84. p.setEnabled(true);
  85. enableDependency(p);
  86. } catch (Exception e) {
  87. }
  88. }
  89. }
  90. public synchronized static void loadAllPlugin(Configuration config) {
  91. if (loader == null) {
  92. loader = new DynamicLoader(Constant.FOLDER_PLUGIN,
  93. "org.parosproxy.paros.core.scanner.plugin");
  94. }
  95. List<Object> listTest = loader.getFilteredObject(AbstractPlugin.class);
  96. synchronized (mapAllPlugin) {
  97. mapAllPlugin.clear();
  98. for (int i = 0; i < listTest.size(); i++) {
  99. Plugin plugin = (Plugin) listTest.get(i);
  100. plugin.setConfig(config);
  101. plugin.createParamIfNotExist();
  102. if (!plugin.isVisible()) {
  103. continue;
  104. }
  105. // plugin.setEnabled(true);
  106. log.info("loaded plugin " + plugin.getName());
  107. mapAllPlugin.put(new Integer(plugin.getId()), plugin);
  108. mapAllPluginOrderCodeName.put(plugin.getCodeName(), plugin);
  109. }
  110. Iterator<Plugin> iterator = mapAllPlugin.values().iterator();
  111. while (iterator.hasNext()) {
  112. listAllPlugin.add(iterator.next());
  113. }
  114. }
  115. }
  116. public static List<Plugin> getAllPlugin() {
  117. return listAllPlugin;
  118. }
  119. public static Plugin getPlugin(int id) {
  120. Plugin test = (Plugin) mapAllPlugin.get(new Integer(id));
  121. return test;
  122. }
  123. public static void setAllPluginEnabled(boolean enabled) {
  124. for (int i = 0; i < listAllPlugin.size(); i++) {
  125. Plugin plugin = listAllPlugin.get(i);
  126. plugin.setEnabled(enabled);
  127. }
  128. }
  129. synchronized boolean existPluginToRun() {
  130. if (probeNextPlugin() != null) {
  131. return true;
  132. }
  133. // no test ready to run. still exist test if due to dependency
  134. if (!listPending.isEmpty() || !listRunning.isEmpty()) {
  135. return true;
  136. }
  137. return false;
  138. }
  139. /**
  140. * Get next test ready to be run. Null = none. Test dependendent on others
  141. * will not be obtained.
  142. *
  143. * @return
  144. */
  145. private Plugin probeNextPlugin() {
  146. Plugin plugin = null;
  147. int i = 0;
  148. while (plugin == null && i < listPending.size()) {
  149. plugin = (Plugin) listPending.get(i);
  150. if (isAllDependencyCompleted(plugin)) {
  151. return plugin;
  152. }
  153. i++;
  154. }
  155. return null;
  156. }
  157. /**
  158. * Get next plugin ready to be run without any dependency outstanding.
  159. *
  160. * @return new instance of next plugin to be run.
  161. */
  162. synchronized Plugin nextPlugin() {
  163. Plugin plugin = null;
  164. plugin = probeNextPlugin();
  165. if (plugin == null) {
  166. return null;
  167. }
  168. listPending.remove(plugin);
  169. listRunning.add(plugin);
  170. return plugin;
  171. }
  172. private boolean isAllDependencyCompleted(Plugin plugin) {
  173. // note the plugin object checked may not be the exact plugin object
  174. // stored in the completed list.
  175. // but the comparison is basing on pluginId (see equals method) so it
  176. // will work.
  177. String[] dependency = plugin.getDependency();
  178. if (dependency == null) {
  179. return true;
  180. }
  181. synchronized (listCompleted) {
  182. for (int i = 0; i < dependency.length; i++) {
  183. boolean isFound = false;
  184. for (int j = 0; j < listCompleted.size() && !isFound; j++) {
  185. Plugin completed = (Plugin) listCompleted.get(j);
  186. if (completed.getCodeName().equalsIgnoreCase(dependency[i])) {
  187. isFound = true;
  188. }
  189. }
  190. if (!isFound) {
  191. return false;
  192. }
  193. }
  194. }
  195. return true;
  196. }
  197. synchronized void setRunningPluginCompleted(Plugin plugin) {
  198. listRunning.remove(plugin);
  199. listCompleted.add(plugin);
  200. }
  201. int totalPluginToRun() {
  202. return totalPluginToRun;
  203. }
  204. int totalPluginCompleted() {
  205. return listCompleted.size();
  206. }
  207. }