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    package org.apache.servicemix.jbi.management.task;
018    
019    import java.io.File;
020    import java.io.FileInputStream;
021    import java.io.IOException;
022    import java.util.ArrayList;
023    import java.util.Iterator;
024    import java.util.List;
025    import java.util.Properties;
026    
027    import org.apache.servicemix.jbi.framework.AdminCommandsServiceMBean;
028    import org.apache.tools.ant.BuildException;
029    
030    /**
031     * Install a Component
032     * 
033     * @version $Revision: 564607 $
034     */
035    public class InstallComponentTask extends JbiTask {
036    
037        private String file; // file to install
038    
039        private String paramsFile;
040    
041        private List nestedParams;
042    
043        private boolean deferExceptions;
044    
045        public boolean isDeferExceptions() {
046            return deferExceptions;
047        }
048    
049        public void setDeferExceptions(boolean deferExceptions) {
050            this.deferExceptions = deferExceptions;
051        }
052    
053        /**
054         * @return Returns the file.
055         */
056        public String getFile() {
057            return file;
058        }
059    
060        /**
061         * @param file
062         *            The file to set.
063         */
064        public void setFile(String file) {
065            this.file = file;
066        }
067    
068        public String getParams() {
069            return paramsFile;
070        }
071    
072        public void setParams(String params) {
073            this.paramsFile = params;
074        }
075    
076        public Param createParam() {
077            Param p = new Param();
078            if (nestedParams == null) {
079                nestedParams = new ArrayList();
080            }
081            nestedParams.add(p);
082            return p;
083        }
084    
085        /**
086         * execute the task
087         * 
088         * @throws BuildException
089         */
090        public void doExecute(AdminCommandsServiceMBean acs) throws Exception {
091            if (file == null) {
092                throw new BuildException("null file - file should be an archive");
093            }
094            if (!file.endsWith(".zip") && !file.endsWith(".jar")) {
095                throw new BuildException("file: " + file + " is not an archive");
096            }
097            File archive = new File(file);
098            String location = archive.getAbsolutePath();
099            if (!archive.isFile()) {
100                // if it's not a file, assume it's a url and pass it along
101                location = file;
102            }
103            Properties props = getProperties();
104            acs.installComponent(location, props, deferExceptions);
105        }
106    
107        private Properties getProperties() throws IOException {
108            Properties props = new Properties();
109            if (paramsFile != null) {
110                props.load(new FileInputStream(paramsFile));
111            }
112            if (nestedParams != null) {
113                for (Iterator iter = nestedParams.iterator(); iter.hasNext();) {
114                    Param p = (Param) iter.next();
115                    props.setProperty(p.getName(), p.getValue());
116                }
117            }
118            return props;
119        }
120    
121        public static class Param {
122            private String name;
123    
124            private String value;
125    
126            public String getName() {
127                return name;
128            }
129    
130            public void setName(String name) {
131                this.name = name;
132            }
133    
134            public String getValue() {
135                return value;
136            }
137    
138            public void setValue(String value) {
139                this.value = value;
140            }
141        }
142    
143    }