1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.modeler.ant;
19
20 import java.util.ArrayList;
21 import java.util.List;
22
23 import org.apache.commons.logging.Log;
24 import org.apache.commons.logging.LogFactory;
25 import org.apache.commons.modeler.Registry;
26 import org.apache.tools.ant.BuildException;
27 import org.apache.tools.ant.Task;
28
29 /***
30 * Group a set of mbeans in a service, and perform actions on
31 * it.
32 *
33 *
34 */
35 public class ServiceTask extends Task {
36 private static Log log = LogFactory.getLog(ServiceTask.class);
37 List mbeans=new ArrayList();
38 String action;
39 String refId;
40
41 public ServiceTask() {
42 }
43
44 public void addMbean(MLETTask mbean) {
45 mbeans.add(mbean);
46 }
47
48 public List getMbeans() {
49 return mbeans;
50 }
51
52 /*** Set action to be executed on the mbean collection.
53 * If null - we'll perform init and start.
54 *
55 * @param action
56 */
57 public void setAction(String action) {
58 this.action=action;
59 }
60
61 /*** Perform the action on a previously declared service
62 *
63 */
64 public void setRefId( String ref ) {
65 this.refId=ref;
66 }
67
68 public void execute() throws BuildException {
69 try {
70 Registry reg=Registry.getRegistry();
71
72 if( refId != null ) {
73 ServiceTask stask=(ServiceTask)project.getReference(refId);
74 }
75
76 List onames=new ArrayList();
77
78 for( int i=0; i<mbeans.size(); i++ ) {
79 MLETTask mbean=(MLETTask)mbeans.get(i);
80 mbean.execute();
81 onames.add( mbean.getObjectName());
82 }
83
84 if( action==null ) {
85
86 reg.invoke(onames, "init", false);
87 reg.invoke(onames, "start", false);
88 } else {
89 reg.invoke(onames, action, false );
90 }
91
92 } catch(Exception ex) {
93 log.error("Error ", ex);
94 }
95 }
96 }