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.geronimo.console.ajax;
019    
020    import java.util.Set;
021    
022    import javax.servlet.http.HttpSession;
023    import javax.servlet.http.HttpServletRequest;
024    
025    import org.apache.geronimo.console.car.ManagementHelper;
026    import org.apache.geronimo.gbean.AbstractName;
027    import org.apache.geronimo.gbean.AbstractNameQuery;
028    import org.apache.geronimo.kernel.Kernel;
029    import org.apache.geronimo.kernel.KernelRegistry;
030    import org.apache.geronimo.system.plugin.DownloadResults;
031    import org.apache.geronimo.system.plugin.PluginInstaller;
032    import org.apache.geronimo.system.plugin.PluginInstallerGBean;
033    import org.directwebremoting.ScriptSession;
034    import org.directwebremoting.WebContext;
035    import org.directwebremoting.WebContextFactory;
036    import org.directwebremoting.annotations.RemoteMethod;
037    import org.directwebremoting.annotations.RemoteProxy;
038    import org.directwebremoting.proxy.ScriptProxy;
039    
040    /**
041     * Provides information to an AJAX client during server side activities.
042     */
043    @RemoteProxy
044    public class ProgressMonitor {
045        
046        private PluginInstaller pluginInstaller = null;
047    
048        @RemoteMethod
049        public void getProgressInfo(Integer downloadKey) throws Exception {
050            // DWR objects
051            WebContext wctx = WebContextFactory.get();
052            ScriptSession scriptSession = wctx.getScriptSession();
053            //DownloadResults results = getPluginInstaller().checkOnInstall(downloadKey);
054            ScriptProxy scriptProxy = new ScriptProxy();
055            scriptProxy.addScriptSession(scriptSession);
056            
057            PluginInstallerGBean pluginInstallerInternal = (PluginInstallerGBean) getPluginInstaller();
058            DownloadResults results = pluginInstallerInternal.checkOnInstall(downloadKey, false);
059            
060            //In the event results.isFinished is passed in true during polling
061            scriptProxy.addFunctionCall("setMainMessage", results.getCurrentMessage());
062            scriptProxy.addFunctionCall("setProgressCurrentFile", results.getCurrentFile());
063            
064            while (!results.isFinished()) {
065                // update the progress bar
066                scriptProxy.addFunctionCall("setProgressCurrentFile", results.getCurrentFile());
067                scriptProxy.addFunctionCall("setProgressPercent", results.getCurrentFilePercent());
068                scriptProxy.addFunctionCall("setMainMessage", results.getCurrentMessage());
069                
070                // get an update on the download progress, sleep time reduce to poll faster for smaller files
071                Thread.sleep(100);
072                //results = getPluginInstaller().checkOnInstall(downloadKey);
073                results = pluginInstallerInternal.checkOnInstall(downloadKey, false);
074            }
075            
076            if(results.isFailed()) {
077                scriptProxy.addFunctionCall("setErrorMessage", results.getFailure().toString());
078                throw new Exception("Unable to install configuration", results.getFailure());
079            }
080            
081            //Fills bar at the end in the event the poller didn't catch the 100
082            scriptProxy.addFunctionCall("setProgressFull");
083            scriptProxy.addFunctionCall("setFinished");
084            
085        }
086        
087        private synchronized PluginInstaller getPluginInstaller() throws Exception {
088            if (pluginInstaller == null) {
089                Kernel kernel = KernelRegistry.getSingleKernel();
090                Set<AbstractName> pluginInstallers = kernel.listGBeans(new AbstractNameQuery(PluginInstaller.class.getName()));
091                if (pluginInstallers.size() == 0) {
092                    throw new IllegalStateException("No plugin installer registered");
093                }
094                pluginInstaller = (PluginInstaller) kernel.getGBean(pluginInstallers.iterator().next());
095            }
096            return pluginInstaller;
097        }
098    }