View Javadoc

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