001 /*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017
018 package org.apache.commons.modeler.ant;
019
020 import java.util.ArrayList;
021 import java.util.List;
022
023 import org.apache.commons.logging.Log;
024 import org.apache.commons.logging.LogFactory;
025 import org.apache.commons.modeler.Registry;
026 import org.apache.tools.ant.BuildException;
027 import org.apache.tools.ant.Task;
028
029 /**
030 * Group a set of mbeans in a service, and perform actions on
031 * it.
032 *
033 *
034 */
035 public class ServiceTask extends Task {
036 private static Log log = LogFactory.getLog(ServiceTask.class);
037 List mbeans=new ArrayList();
038 String action;
039 String refId;
040
041 public ServiceTask() {
042 }
043
044 public void addMbean(MLETTask mbean) {
045 mbeans.add(mbean);
046 }
047
048 public List getMbeans() {
049 return mbeans;
050 }
051
052 /** Set action to be executed on the mbean collection.
053 * If null - we'll perform init and start.
054 *
055 * @param action
056 */
057 public void setAction(String action) {
058 this.action=action;
059 }
060
061 /** Perform the action on a previously declared service
062 *
063 */
064 public void setRefId( String ref ) {
065 this.refId=ref;
066 }
067
068 public void execute() throws BuildException {
069 try {
070 Registry reg=Registry.getRegistry();
071
072 if( refId != null ) {
073 ServiceTask stask=(ServiceTask)project.getReference(refId);
074 }
075 // create the mbeans
076 List onames=new ArrayList();
077
078 for( int i=0; i<mbeans.size(); i++ ) {
079 MLETTask mbean=(MLETTask)mbeans.get(i);
080 mbean.execute();
081 onames.add( mbean.getObjectName());
082 }
083
084 if( action==null ) {
085 // default: init and start
086 reg.invoke(onames, "init", false);
087 reg.invoke(onames, "start", false);
088 } else {
089 reg.invoke(onames, action, false );
090 }
091
092 } catch(Exception ex) {
093 log.error("Error ", ex);
094 }
095 }
096 }