Save This Page
Home » geronimo-2.2-source-release » org.apache.geronimo.console » [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   package org.apache.geronimo.console;
   18   
   19   import java.io.IOException;
   20   import java.lang.ref.SoftReference;
   21   import java.lang.reflect.Method;
   22   import java.lang.reflect.Modifier;
   23   import java.text.MessageFormat;
   24   import java.util.ArrayList;
   25   import java.util.List;
   26   
   27   import javax.portlet.ActionRequest;
   28   import javax.portlet.GenericPortlet;
   29   import javax.portlet.PortletException;
   30   import javax.portlet.PortletRequest;
   31   import javax.portlet.RenderRequest;
   32   import javax.portlet.RenderResponse;
   33   
   34   import org.apache.geronimo.console.i18n.ConsoleResourceRegistry;
   35   import org.apache.geronimo.console.message.CommonMessage;
   36   import org.apache.geronimo.console.message.ErrorMessage;
   37   import org.apache.geronimo.console.message.InfoMessage;
   38   import org.apache.geronimo.console.message.WarnMessage;
   39   import org.apache.geronimo.console.util.PortletManager;
   40   import org.slf4j.Logger;
   41   import org.slf4j.LoggerFactory;
   42   
   43   /**
   44    * Superclass with some generic functionality for console portlets
   45    *
   46    * @version $Rev: 750949 $ $Date: 2009-03-06 07:59:53 -0800 (Fri, 06 Mar 2009) $
   47    */
   48   public class BasePortlet extends GenericPortlet {
   49       private static final Logger log = LoggerFactory.getLogger(BasePortlet.class);
   50       protected final static String WEB_SERVER_JETTY = "jetty";
   51       protected final static String WEB_SERVER_TOMCAT = "tomcat";
   52       protected final static String WEB_SERVER_GENERIC = "generic";
   53       private static final String COMMON_MESSAGES = "commonMessages";
   54       private static final String FMT_LOCALE = "javax.servlet.jsp.jstl.fmt.locale.request";
   55       private static final String BASENAME = "portletinfo";
   56       private static ConsoleResourceRegistry resourceRegistry;
   57       
   58       static {
   59           try {
   60               resourceRegistry = (ConsoleResourceRegistry) PortletManager.getKernel().getGBean(ConsoleResourceRegistry.class);
   61           } catch (Exception e) {
   62               log.error("Cannot get the console resource registery service", e);
   63           }
   64       }    
   65   
   66       protected final static String getWebServerType(Class cls) {
   67           Class[] intfs = cls.getInterfaces();
   68           for (int i = 0; i < intfs.length; i++) {
   69               Class intf = intfs[i];
   70               if(intf.getName().indexOf("Jetty") > -1) {
   71                   return WEB_SERVER_JETTY;
   72               } else if(intf.getName().indexOf("Tomcat") > -1) {
   73                   return WEB_SERVER_TOMCAT;
   74               }
   75           }
   76           return WEB_SERVER_GENERIC;
   77       }
   78   
   79       public final static void setProperty(Object target, String name, Object value) {
   80           boolean found = false;
   81           Class cls = target.getClass();
   82           String setter = "set"+Character.toUpperCase(name.charAt(0))+name.substring(1);
   83           Method[] list = cls.getMethods();
   84           for (int i = 0; i < list.length; i++) {
   85               Method method = list[i];
   86               if(method.getName().equals(setter) && method.getParameterTypes().length == 1 && Modifier.isPublic(method.getModifiers()) &&
   87                       !Modifier.isStatic(method.getModifiers())) {
   88                   found = true;
   89                   try {
   90                       method.invoke(target, new Object[]{value});
   91                   } catch (Exception e) {
   92                       log.error("Unable to set property "+name+" on "+target.getClass().getName());
   93                   }
   94                   break;
   95               }
   96           }
   97           if(!found) {
   98               throw new IllegalArgumentException("No such method found ("+setter+" on "+target.getClass().getName()+")");
   99           }
  100       }
  101   
  102       public final static Object getProperty(Object target, String name) {
  103           Class cls = target.getClass();
  104           String getter = "get"+Character.toUpperCase(name.charAt(0))+name.substring(1);
  105           String booleanGetter = "is"+Character.toUpperCase(name.charAt(0))+name.substring(1);
  106           Method[] list = cls.getMethods();
  107           for (int i = 0; i < list.length; i++) {
  108               Method method = list[i];
  109               String methodName = method.getName();
  110               if( (methodName.equals(getter) || methodName.equals(booleanGetter))
  111                   && method.getParameterTypes().length == 0 && Modifier.isPublic(method.getModifiers()) &&
  112                       !Modifier.isStatic(method.getModifiers())) {
  113                   try {
  114                       return method.invoke(target, new Object[0]);
  115                   } catch (Exception e) {
  116                       log.error("Unable to get property "+name+" on "+target.getClass().getName());
  117                   }
  118                   break;
  119               }
  120           }
  121           throw new IllegalArgumentException("No such method found ("+getter+" on "+target.getClass().getName()+")");
  122       }
  123   
  124       public final static Object callOperation(Object target, String operation, Object[] args) {
  125           Class cls = target.getClass();
  126           Method[] list = cls.getMethods();
  127           for (int i = 0; i < list.length; i++) {
  128               Method method = list[i];
  129               if(method.getName().equals(operation) && ((args == null && method.getParameterTypes().length == 0) || (args != null && args.length == method.getParameterTypes().length))
  130                       && Modifier.isPublic(method.getModifiers()) && !Modifier.isStatic(method.getModifiers())) {
  131                   try {
  132                       return method.invoke(target, args);
  133                   } catch (Exception e) {
  134                       log.error("Unable to invoke "+operation+" on "+target.getClass().getName());
  135                   }
  136                   break;
  137               }
  138           }
  139           throw new IllegalArgumentException("No such method found ("+operation+" on "+target.getClass().getName()+")");
  140       }
  141   
  142       @Override
  143       public void render(RenderRequest request, RenderResponse response) throws PortletException, IOException {
  144           @SuppressWarnings("unchecked")
  145           SoftReference<List<CommonMessage>> msgRef = (SoftReference<List<CommonMessage>>) request.getPortletSession().getAttribute(COMMON_MESSAGES);
  146           if (null != msgRef && null != msgRef.get()) {
  147               request.setAttribute(COMMON_MESSAGES, msgRef.get());
  148           }
  149           request.getPortletSession().removeAttribute(COMMON_MESSAGES);
  150   
  151           request.setAttribute(FMT_LOCALE, request.getLocale());
  152           
  153           super.render(request, response);
  154       }
  155   
  156       public final void addErrorMessage(PortletRequest request, String... messages) {
  157           addCommonMessage(CommonMessage.Type.Error, request, messages);
  158       }
  159   
  160       public final void addWarningMessage(PortletRequest request, String... messages) {
  161           addCommonMessage(CommonMessage.Type.Warn, request, messages);
  162       }
  163   
  164       public final void addInfoMessage(PortletRequest request, String... messages) {
  165           addCommonMessage(CommonMessage.Type.Info, request, messages);
  166       }
  167   
  168       public final String getLocalizedString(PortletRequest request, String key, Object... vars) {
  169           String value = resourceRegistry.handleGetObject(BASENAME, request.getLocale(), key);
  170           if (null == value || 0 == value.length()) return key;     
  171           return MessageFormat.format(value, vars);
  172       }
  173   
  174       private void addCommonMessage(CommonMessage.Type type, PortletRequest request, String[] messages) {
  175           if (null != messages && 0 != messages.length) {
  176               if (1 == messages.length) {
  177                   addCommonMessage(type, request, messages[0], null);
  178               } else {
  179                   StringBuilder sb = new StringBuilder();
  180                   for (String message : messages) {
  181                       sb.append(message + "<br>");
  182                   }
  183                   addCommonMessage(type, request, messages[0], sb.toString());
  184               }
  185           }
  186       }
  187   
  188       private void addCommonMessage(CommonMessage.Type type, PortletRequest request, String abbr, String detail) {
  189           if (request instanceof ActionRequest) {
  190               List<CommonMessage> messages;
  191               @SuppressWarnings("unchecked")
  192               SoftReference<List<CommonMessage>> msgRef = (SoftReference<List<CommonMessage>>) request.getPortletSession().getAttribute(COMMON_MESSAGES);
  193               if (null == msgRef || null == msgRef.get()) {
  194                   messages = new ArrayList<CommonMessage>();
  195                   msgRef = new SoftReference<List<CommonMessage>>(messages);
  196                   request.getPortletSession().setAttribute(COMMON_MESSAGES, msgRef);
  197               } else {
  198                   messages = msgRef.get();
  199               }
  200               addCommonMessage(type, messages, abbr, detail);
  201           } else {
  202               @SuppressWarnings("unchecked")
  203               List<CommonMessage> messages = (List<CommonMessage>) request.getAttribute(COMMON_MESSAGES);
  204               if (null == messages) {
  205                   messages = new ArrayList<CommonMessage>();
  206                   request.setAttribute(COMMON_MESSAGES, messages);
  207               }
  208               addCommonMessage(type, messages, abbr, detail);
  209           }
  210       }
  211   
  212       private void addCommonMessage(CommonMessage.Type type, List<CommonMessage> messages, String abbr, String detail) {
  213           switch (type) {
  214           case Error:
  215               messages.add(new ErrorMessage(abbr, detail));
  216               break;
  217           case Warn:
  218               messages.add(new WarnMessage(abbr, detail));
  219               break;
  220           case Info:
  221               messages.add(new InfoMessage(abbr, detail));
  222               break;
  223           }
  224       }
  225   
  226   }

Save This Page
Home » geronimo-2.2-source-release » org.apache.geronimo.console » [javadoc | source]