View Javadoc

1   /*
2    * Copyright 2000-2002,2004 The Apache Software Foundation.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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              // create the mbeans
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                  // default: init and start
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  }