KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > invicta > handler > DependListHandler


1 package net.sf.invicta.handler;
2
3 import java.util.ArrayList JavaDoc;
4 import java.util.Iterator JavaDoc;
5 import java.util.List JavaDoc;
6 import java.util.Map JavaDoc;
7
8 import net.sf.invicta.InvictaConstants;
9 import net.sf.invicta.InvictaException;
10 import net.sf.invicta.api.InvictaComponent;
11
12 import org.apache.commons.lang.StringUtils;
13
14 /**
15  * Map Usage:
16  * target - (String, optional, "BUILD") Target of components to depend on. Can either be a free string or one of the following: BUILD, CLEAN, DIST, INIT
17  * all - (boolean, optional, false) Whether to depend on all components or only on dependent ones.
18  * exclude - (boolean, optional, false) Whether to exclude from the depend list all component that have the exclude flag on.
19  *
20  */

21 public class DependListHandler extends InvictaBasicHandler {
22         
23     private final static String JavaDoc INIT_TARGET_ALIAS = "INIT";
24     private final static String JavaDoc BUILD_TARGET_ALIAS = "BUILD";
25     private final static String JavaDoc CLEAN_TARGET_ALIAS = "CLEAN";
26     private final static String JavaDoc DIST_TARGET_ALIAS = "DIST";
27     private final static String JavaDoc DEFAULT_EMPTY_LIST = "init";
28     private final static String JavaDoc DEFAULT_DEPEND_TARGET = BUILD_TARGET_ALIAS;
29     private final static boolean DEFAULT_USE_ALL = false;
30     private final static boolean DEFAULT_EXCLUDE_MODE = false;
31     private final static String JavaDoc EXCLUDE_COMPONENT_PROPERTY = "exclude";
32     private final static boolean DEFAULT_EXCLUDE_COMPONENT = false;
33
34     /**
35      *
36      */

37     public String JavaDoc getName() {
38         return "dependList";
39     }
40     
41     /**
42      *
43      */

44     public String JavaDoc handle(Map JavaDoc paramsMap) throws InvictaException {
45             
46         String JavaDoc dependTarget = getParameter("target", DEFAULT_DEPEND_TARGET);
47         boolean all = getBooleanParameter("all", DEFAULT_USE_ALL);
48         boolean exclude = getBooleanParameter("exclude", DEFAULT_EXCLUDE_MODE);
49         return prepareDependList(all, dependTarget, exclude);
50     }
51
52     /**
53      *
54      */

55     public String JavaDoc handle(List JavaDoc params) throws InvictaException {
56         return prepareDependList(DEFAULT_USE_ALL, DEFAULT_DEPEND_TARGET, DEFAULT_EXCLUDE_MODE);
57     }
58
59     /**
60      *
61      */

62     protected String JavaDoc prepareDependList(boolean all, String JavaDoc dependTarget, boolean exclude) {
63             List JavaDoc resultDependList = new ArrayList JavaDoc();
64             String JavaDoc dependList = "";
65             List JavaDoc componentList;
66             if (all) {
67                 componentList = getProject().getComponents();
68             } else {
69                 componentList = getComponent().getDependComponents();
70             }
71             
72             for (Iterator JavaDoc iter = componentList.iterator(); iter.hasNext();) {
73                 InvictaComponent dependentComponent = (InvictaComponent) iter.next();
74                 
75                 if ((exclude) && (excludeComponent(dependentComponent)))
76                     continue;
77                 
78                 String JavaDoc fullDependTarget = dependentComponent.getName() + InvictaConstants.TARGET_DELIMITER;
79                 if (dependTarget.equals(INIT_TARGET_ALIAS))
80                     fullDependTarget += dependentComponent.getInitTarget();
81                 else if (dependTarget.equals(BUILD_TARGET_ALIAS))
82                     fullDependTarget += dependentComponent.getBuildTarget();
83                 else if (dependTarget.equals(CLEAN_TARGET_ALIAS))
84                     fullDependTarget += dependentComponent.getCleanTarget();
85                 else if (dependTarget.equals(DIST_TARGET_ALIAS))
86                     fullDependTarget += dependentComponent.getDistTarget();
87                 else {
88                     // If this component doesn't have this target, don't add it.
89
if (!dependentComponent.containsTarget(dependTarget))
90                         continue;
91                     fullDependTarget += dependTarget;
92                 }
93                 
94                 resultDependList.add(fullDependTarget);
95             }
96             
97             String JavaDoc result = StringUtils.join(resultDependList.iterator(), InvictaConstants.DEPEND_LIST_DELIMITER);
98             if (result.length() == 0) {
99                 return getComponent().getName() +
100                     InvictaConstants.TARGET_DELIMITER + DEFAULT_EMPTY_LIST;
101             }
102             
103             return result;
104     }
105     
106     /**
107      *
108      */

109     protected boolean excludeComponent(InvictaComponent dependentComponent) {
110         // Check if a component should be excluded according to the
111
// optional exclude flag.
112
String JavaDoc propertyValue = dependentComponent.getPropertyValue(EXCLUDE_COMPONENT_PROPERTY);
113         boolean excludeComponentValue = DEFAULT_EXCLUDE_COMPONENT;
114         if (propertyValue != null)
115             excludeComponentValue = Boolean.valueOf(propertyValue).booleanValue();
116         return excludeComponentValue;
117     }
118         
119 }
120
Popular Tags