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