01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * 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
74: .getReference(refId);
75: }
76: // create the mbeans
77: List onames = new ArrayList();
78:
79: for (int i = 0; i < mbeans.size(); i++) {
80: MLETTask mbean = (MLETTask) mbeans.get(i);
81: mbean.execute();
82: onames.add(mbean.getObjectName());
83: }
84:
85: if (action == null) {
86: // default: init and start
87: reg.invoke(onames, "init", false);
88: reg.invoke(onames, "start", false);
89: } else {
90: reg.invoke(onames, action, false);
91: }
92:
93: } catch (Exception ex) {
94: log.error("Error ", ex);
95: }
96: }
97: }
|