Home » geronimo-2.2-source-release » org.apache.geronimo.console.artifact » [javadoc | source]

    1   /**
    2    *  Licensed to the Apache Software Foundation (ASF) under one or more
    3    *  contributor license agreements.  See the NOTICE file distributed with
    4    *  this work for additional information regarding copyright ownership.
    5    *  The ASF licenses this file to You under the Apache License, Version 2.0
    6    *  (the "License"); you may not use this file except in compliance with
    7    *  the License.  You may obtain a copy of the License at
    8    *
    9    *     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.geronimo.console.artifact;
   19   
   20   import java.io.IOException;
   21   import java.io.Serializable;
   22   import java.util.ArrayList;
   23   import java.util.List;
   24   import java.util.Map;
   25   import java.util.Properties;
   26   import java.util.Collections;
   27   
   28   import javax.portlet.ActionRequest;
   29   import javax.portlet.ActionResponse;
   30   import javax.portlet.PortletConfig;
   31   import javax.portlet.PortletException;
   32   import javax.portlet.PortletRequest;
   33   import javax.portlet.PortletRequestDispatcher;
   34   import javax.portlet.RenderRequest;
   35   import javax.portlet.RenderResponse;
   36   import javax.portlet.WindowState;
   37   
   38   import org.apache.geronimo.console.BasePortlet;
   39   import org.apache.geronimo.kernel.GBeanNotFoundException;
   40   import org.apache.geronimo.kernel.InternalKernelException;
   41   import org.apache.geronimo.kernel.Kernel;
   42   import org.apache.geronimo.kernel.KernelRegistry;
   43   import org.slf4j.Logger;
   44   import org.slf4j.LoggerFactory;
   45   
   46   import org.apache.geronimo.system.resolver.ExplicitDefaultArtifactResolver;
   47   
   48   public class ArtifactViewPortlet extends BasePortlet {
   49       private Kernel kernel;
   50       private static final Logger log = LoggerFactory.getLogger(ArtifactViewPortlet.class);
   51       private static final String LIST_VIEW = "/WEB-INF/view/artifact/list.jsp";
   52       private static final String EDIT_VIEW = "/WEB-INF/view/artifact/edit.jsp";
   53       private static final String LIST_MODE = "list";
   54       private static final String EDIT_MODE = "edit";
   55       private static final String EDITING_MODE = "editing";
   56       private static final String REMOVE_MODE = "remove";
   57       private static final String MODE_KEY = "mode";
   58       private PortletRequestDispatcher listView;
   59       private PortletRequestDispatcher editView;
   60       private ExplicitDefaultArtifactResolver instance;
   61       
   62       public void init(PortletConfig portletConfig) throws PortletException {
   63           super.init(portletConfig);
   64           kernel = KernelRegistry.getSingleKernel();
   65           try {
   66               instance = kernel.getGBean(ExplicitDefaultArtifactResolver.class);
   67           } catch (GBeanNotFoundException e) {
   68               throw new PortletException(e);
   69           } catch (InternalKernelException e) {
   70               throw new PortletException(e);
   71           } catch (IllegalStateException e) {
   72               throw new PortletException(e);
   73           }
   74           listView = portletConfig.getPortletContext().getRequestDispatcher(LIST_VIEW);
   75           editView = portletConfig.getPortletContext().getRequestDispatcher(EDIT_VIEW);
   76       }
   77       
   78       public void destroy() {
   79           listView = null;
   80           editView = null;
   81           super.destroy();
   82       }
   83       
   84       public void processAction(ActionRequest actionRequest,
   85               ActionResponse actionResponse) throws PortletException, IOException {
   86           String mode = actionRequest.getParameter(MODE_KEY);
   87           if (mode == null) {
   88               mode = "";
   89           }
   90           actionResponse.setRenderParameter(MODE_KEY, mode);  
   91           String name = actionRequest.getParameter("name");
   92           if (name != null) {
   93               actionResponse.setRenderParameter("name", name); 
   94           }
   95           String aliases = actionRequest.getParameter("aliases");
   96           if (aliases != null) {
   97               actionResponse.setRenderParameter("aliases", aliases);
   98           }
   99       }
  100       
  101       protected void doView(RenderRequest renderRequest, RenderResponse renderResponse) throws IOException, PortletException {
  102           if (WindowState.MINIMIZED.equals(renderRequest.getWindowState())) {
  103               return;
  104           }
  105           try {
  106               String mode = renderRequest.getParameter(MODE_KEY);
  107               if (mode == null || mode.equals("")) {
  108                   mode = LIST_MODE;
  109               }
  110               if (mode.equals(LIST_MODE) && renderRequest.getParameter("name") != null) {
  111                   mode = EDITING_MODE;
  112               }
  113   
  114               if (mode.equals(LIST_MODE)) {
  115                   renderList(renderRequest, renderResponse);
  116               } else if (mode.equals(EDIT_MODE)) {
  117                   AliasesData data = new AliasesData(renderRequest.getParameter("name"), renderRequest.getParameter("aliases"));
  118                   renderEdit(renderRequest, renderResponse, data);
  119               } else if (mode.equals(REMOVE_MODE)) {
  120                   AliasesData data = new AliasesData(renderRequest.getParameter("name"), renderRequest.getParameter("aliases"));
  121                   renderRemove(renderRequest, renderResponse, data);
  122               } else if (mode.equals(EDITING_MODE)) {
  123                   AliasesData data = new AliasesData(renderRequest.getParameter("name"), renderRequest.getParameter("aliases"));
  124                   renderEditing(renderRequest, renderResponse, data);
  125               }
  126           } catch (Throwable e) {
  127               log.error("Unable to render portlet", e);
  128           }
  129       }
  130       
  131       private void renderList(RenderRequest renderRequest, RenderResponse renderResponse) throws IOException, PortletException {
  132           populateList(renderRequest);
  133           listView.include(renderRequest, renderResponse);
  134       }
  135   
  136       private void populateList(PortletRequest renderRequest) {
  137           List<AliasesData> list = new ArrayList<AliasesData>();
  138           Properties set = instance.getProperties();
  139           for (Map.Entry<Object, Object> entry : set.entrySet()) {
  140               String key = (String) entry.getKey();
  141               String resolvedString = (String) entry.getValue();
  142               list.add(new AliasesData(key, resolvedString));
  143           }
  144           Collections.sort(list);
  145           renderRequest.setAttribute("AliasesDatas", list);
  146       }
  147       
  148       private void renderEdit(RenderRequest renderRequest, RenderResponse renderResponse, AliasesData data) throws IOException, PortletException {      
  149           renderRequest.setAttribute("AliasesData", data);
  150           editView.include(renderRequest, renderResponse);
  151       }
  152       
  153       private void renderRemove(RenderRequest renderRequest, RenderResponse renderResponse, AliasesData data) throws IOException, PortletException {      
  154           Properties set = new Properties();
  155           if (data.name != null && data.aliases != null) {
  156               set.put(data.name, data.aliases);
  157               instance.removeAliases(set);
  158           }
  159           populateList(renderRequest);
  160           listView.include(renderRequest, renderResponse);
  161       }
  162       
  163       private void renderEditing(RenderRequest renderRequest, RenderResponse renderResponse, AliasesData data) throws IOException, PortletException {
  164           Properties set = new Properties();
  165           if (data.name != null && data.aliases != null) {
  166               set.put(data.name, data.aliases);
  167               instance.addAliases(set);
  168           }
  169           populateList(renderRequest);
  170           listView.include(renderRequest, renderResponse);
  171       }
  172   
  173       public static class AliasesData implements Serializable, Comparable<AliasesData> {
  174           private static final long serialVersionUID = 1L;
  175           private String name;
  176           private String aliases;
  177   
  178           public AliasesData() {
  179               
  180           }
  181           
  182           public void load(String name, String aliases) {
  183               this.name = name;
  184               this.aliases = aliases;
  185           }
  186           
  187           public AliasesData(String name, String aliases) {
  188               this.name = name;
  189               this.aliases = aliases;
  190           }
  191           
  192           public String getName() {
  193               return name;
  194           }
  195           
  196           public String getAliases() {
  197               return aliases;
  198           }
  199   
  200           public int compareTo(AliasesData aliasesData) {
  201               int val = name.compareTo(aliasesData.name);
  202               if (val != 0) {
  203                   return val;
  204               }
  205               return aliases.compareTo(aliasesData.aliases);
  206           }
  207       }
  208   }

Home » geronimo-2.2-source-release » org.apache.geronimo.console.artifact » [javadoc | source]