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.geronimo.console;
018    
019    import org.apache.commons.fileupload.portlet.PortletFileUpload;
020    
021    import javax.portlet.PortletRequestDispatcher;
022    import javax.portlet.PortletConfig;
023    import javax.portlet.PortletException;
024    import javax.portlet.ActionRequest;
025    import javax.portlet.ActionResponse;
026    import javax.portlet.RenderRequest;
027    import javax.portlet.RenderResponse;
028    import javax.enterprise.deploy.spi.status.ProgressObject;
029    import java.io.IOException;
030    import java.util.Map;
031    import java.util.HashMap;
032    import java.util.Properties;
033    
034    /**
035     * Base class for handlers for the multi page portlet.  Each one is expected
036     * to handle a single page -- the action request before the page is rendered,
037     * the render request, and the action request after the page is rendered.
038     *
039     * @version $Rev: 476061 $ $Date: 2006-11-17 01:36:50 -0500 (Fri, 17 Nov 2006) $
040     */
041    public abstract class MultiPageAbstractHandler {
042        protected final static String BEFORE_ACTION="-before";
043        protected final static String AFTER_ACTION="-after";
044        protected PortletRequestDispatcher view;
045        private final String mode;
046        private final String viewName;
047        private Map uploadFiles = new HashMap();
048        private Properties uploadFields = new Properties();
049    
050        protected MultiPageAbstractHandler(String mode, String viewName) {
051            this.mode = mode;
052            this.viewName = viewName;
053        }
054    
055        public String getMode() {
056            return mode;
057        }
058    
059        public void init(PortletConfig portletConfig) throws PortletException {
060            if(viewName != null) {
061                view = portletConfig.getPortletContext().getRequestDispatcher(viewName);
062            }
063        }
064    
065        public void destroy() {
066            view = null;
067        }
068    
069        public PortletRequestDispatcher getView() {
070            return view;
071        }
072    
073        protected static boolean isEmpty(String s) {
074            return s == null || s.trim().equals("");
075        }
076    
077        /**
078         * Returns the mode for the next screen to render (usually this one)
079         */
080        public abstract String actionBeforeView(ActionRequest request, ActionResponse response, MultiPageModel model) throws PortletException, IOException;
081    
082        public abstract void renderView(RenderRequest request, RenderResponse response, MultiPageModel model) throws PortletException, IOException;
083    
084        /**
085         * Returns the mode for the next screen to render (usually the one after this in the sequence)
086         */
087        public abstract String actionAfterView(ActionRequest request, ActionResponse response, MultiPageModel model) throws PortletException, IOException;
088    
089        public Map getUploadFiles() {
090            return uploadFiles;
091        }
092    
093        public Properties getUploadFields() {
094            return uploadFields;
095        }
096    
097        protected static void waitForProgress(ProgressObject po) {
098            while(po.getDeploymentStatus().isRunning()) {
099                try {
100                    Thread.sleep(100);
101                } catch (InterruptedException e) {
102                    e.printStackTrace();
103                }
104            }
105        }
106    
107    }