Home » geronimo-2.2-source-release » org.apache.geronimo.console.jmsmanager » [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.jmsmanager;
   19   
   20   import java.io.IOException;
   21   import java.util.ArrayList;
   22   import java.util.Collections;
   23   import java.util.HashMap;
   24   import java.util.HashSet;
   25   import java.util.Iterator;
   26   import java.util.List;
   27   import java.util.Map;
   28   import java.util.Set;
   29   
   30   import javax.jms.Connection;
   31   import javax.jms.ConnectionFactory;
   32   import javax.management.MalformedObjectNameException;
   33   import javax.management.ObjectName;
   34   import javax.portlet.ActionRequest;
   35   import javax.portlet.ActionResponse;
   36   import javax.portlet.PortletConfig;
   37   import javax.portlet.PortletContext;
   38   import javax.portlet.PortletException;
   39   import javax.portlet.PortletRequestDispatcher;
   40   import javax.portlet.RenderRequest;
   41   import javax.portlet.RenderResponse;
   42   import javax.portlet.WindowState;
   43   
   44   import org.apache.geronimo.console.BasePortlet;
   45   import org.apache.geronimo.console.jmsmanager.DataSourceInfo;
   46   import org.apache.geronimo.console.jmsmanager.activemqCF.ActiveMQConnectorHelper;
   47   import org.apache.geronimo.gbean.GAttributeInfo;
   48   import org.apache.geronimo.gbean.GBeanInfo;
   49   import org.apache.geronimo.kernel.Kernel;
   50   import org.apache.geronimo.kernel.KernelRegistry;
   51   import org.apache.geronimo.kernel.ObjectNameUtil;
   52   
   53   public class JMSConnectionFactoryManagerPortlet extends BasePortlet {
   54   
   55       private final static ActiveMQConnectorHelper helper = new ActiveMQConnectorHelper();
   56   
   57       private final static String PARENT_ID = "geronimo/activemq-broker/" + org.apache.geronimo.system.serverinfo.ServerConstants.getVersion() + "/car";
   58   
   59       private final static String ADD_MODE = "addACF";
   60   
   61       private final static String SUBMIT_CREATE = "Create";
   62   
   63       private final ObjectName DATABASE_QUERY = ObjectNameUtil
   64               .getObjectName("*:j2eeType=JCAManagedConnectionFactory,*");
   65   
   66       protected final String NORMAL_VIEW = "/WEB-INF/view/jmsmanager/activemq/normal.jsp";
   67   
   68       protected final String DETAIL_VIEW = "/WEB-INF/view/jmsmanager/activemq/detail.jsp";
   69   
   70       protected final String HELP_VIEW = "/WEB-INF/view/jmsmanager/activemq/help.jsp";
   71   
   72       protected final String ADD_VIEW = "/WEB-INF/view/jmsmanager/activemq/addACF.jsp";
   73   
   74       protected Kernel kernel;
   75   
   76       private PortletRequestDispatcher normalView;
   77   
   78       private PortletRequestDispatcher detailView;
   79   
   80       private PortletRequestDispatcher helpView;
   81   
   82       private PortletRequestDispatcher addView;
   83   
   84       private static final Set HIDDEN_ATTRIBUTES;
   85   
   86       static {
   87           HIDDEN_ATTRIBUTES = new HashSet();
   88           HIDDEN_ATTRIBUTES.add("kernel");
   89           HIDDEN_ATTRIBUTES.add("connectionImplClass");
   90           HIDDEN_ATTRIBUTES.add("connectionInterface");
   91           HIDDEN_ATTRIBUTES.add("connectionFactoryInterface");
   92           HIDDEN_ATTRIBUTES.add("connectionFactoryImplClass");
   93           HIDDEN_ATTRIBUTES.add("implementedInterfaces");
   94           HIDDEN_ATTRIBUTES.add("managedConnectionFactoryClass");
   95           HIDDEN_ATTRIBUTES.add("recoveryXAResources");
   96       }
   97   
   98       public void init(PortletConfig portletConfig) throws PortletException {
   99           super.init(portletConfig);
  100           kernel = KernelRegistry.getSingleKernel();
  101           PortletContext context = portletConfig.getPortletContext();
  102           normalView = context.getRequestDispatcher(NORMAL_VIEW);
  103           detailView = context.getRequestDispatcher(DETAIL_VIEW);
  104           helpView = context.getRequestDispatcher(HELP_VIEW);
  105           addView = context.getRequestDispatcher(ADD_VIEW);
  106       }
  107   
  108       public void processAction(ActionRequest actionRequest,
  109               ActionResponse actionResponse) throws PortletException, IOException {
  110           String mode = actionRequest.getParameter("mode");
  111           String submit = actionRequest.getParameter("submit");
  112   
  113           if (mode == null) {
  114               return;
  115           } else if (ADD_MODE.equals(mode)) {
  116               actionResponse.setRenderParameter("mode", mode);
  117               if (SUBMIT_CREATE.equals(submit)) {
  118                   String acfName = actionRequest.getParameter("acfName");
  119                   String serverURL = actionRequest.getParameter("serverURL");
  120                   String userName = actionRequest.getParameter("userName");
  121                   String pword = actionRequest.getParameter("pword");
  122                   String poolMaxSize = actionRequest.getParameter("poolMaxSize");
  123                   String blocking = actionRequest.getParameter("blocking");
  124   
  125                   String[] args = { trimStr(acfName), trimStr(PARENT_ID),
  126                           trimStr(acfName), trimStr(serverURL),
  127                           trimStr(userName), pword, trimStr(acfName),
  128                           trimStr(poolMaxSize), trimStr(blocking) };
  129                   helper.deployPlan(actionRequest, args);
  130                   // Set mode to list after creating the new ConnectionFactories
  131                   actionResponse.setRenderParameter("mode", "list");
  132               }
  133               return;
  134           }
  135   
  136           String name = actionRequest.getParameter("name");
  137           if (name != null) {
  138               actionResponse.setRenderParameter("mode", "list");
  139               return;
  140           }
  141       }
  142   
  143       protected void doView(RenderRequest renderRequest,
  144               RenderResponse renderResponse) throws IOException, PortletException {
  145           if (WindowState.MINIMIZED.equals(renderRequest.getWindowState())) {
  146               return;
  147           }
  148   
  149           List dependencies = helper.getDependencies(renderRequest);
  150           // pass them to the render request
  151           renderRequest.setAttribute("dependencies", (String[]) dependencies
  152                   .toArray(new String[dependencies.size()]));
  153   
  154           String name = renderRequest.getParameter("name");
  155           String mode = renderRequest.getParameter("mode");
  156           String check = renderRequest.getParameter("check");
  157   
  158           if (ADD_MODE.equals(mode)) {
  159               addView.include(renderRequest, renderResponse);
  160               return;
  161           }
  162   
  163           boolean test = false;
  164           if (name == null || mode == null) {
  165               mode = "list";
  166           }
  167           if ("true".equals(check)) {
  168               test = true;
  169           }
  170           if ("detail".equals(mode) || "config".equals(mode)) {
  171               renderDetail(renderRequest, renderResponse, name);
  172           } else {
  173               renderList(renderRequest, renderResponse, name, test);
  174           }
  175       }
  176   
  177       private void renderList(RenderRequest renderRequest,
  178               RenderResponse renderResponse, String name, boolean check)
  179               throws PortletException, IOException {
  180   
  181           Set gbeanNames = kernel.listGBeans(DATABASE_QUERY);
  182           List connectionFactories = new ArrayList(gbeanNames.size());
  183           for (Iterator i = gbeanNames.iterator(); i.hasNext();) {
  184               ObjectName gbeanName = (ObjectName) i.next();
  185   
  186               // check that this connector is a DataSource
  187               try {
  188                   Class cfInterface = Class.forName((String) kernel.getAttribute(
  189                           gbeanName, "connectionFactoryInterface"));
  190                   if (!(ConnectionFactory.class).isAssignableFrom(cfInterface)) {
  191                       continue;
  192                   }
  193               } catch (Exception e) {
  194                   throw new PortletException(e);
  195               }
  196   
  197               DataSourceInfo info = new DataSourceInfo();
  198               info.setObjectName(gbeanName);
  199               info.setName(gbeanName.getKeyProperty("name"));
  200               try {
  201                   info.setState(new Integer(kernel.getGBeanState(gbeanName)));
  202                   //check if user asked this connection to be tested
  203                   if ((gbeanName.toString().equals(name)) && (check)) {
  204                       info.setWorking(true);
  205                       try {
  206                           Object cf = kernel.invoke(gbeanName, "$getResource");
  207                           testConnection(cf);
  208                           info.setMessage("Connected");
  209                       } catch (Exception e) {
  210                           Throwable t = e;
  211                           String message = "Failed: ";
  212                           if (t.getMessage() != null) {
  213                               message = message + t.getMessage();
  214                           } else {
  215                               while (t.getMessage() == null) {
  216                                   t = t.getCause();
  217                                   if (t != null) {
  218                                       message = message + t.getMessage();
  219                                   } else {
  220                                       message = message + "Unknown reason";
  221                                   }
  222                               }
  223                           }
  224                           info.setMessage(message);
  225                       }
  226                   } else {
  227                       info.setWorking(false);
  228                   }
  229   
  230               } catch (Exception e) {
  231                   throw new PortletException(e);
  232               }
  233               connectionFactories.add(info);
  234           }
  235           Collections.sort(connectionFactories);
  236           renderRequest.setAttribute("cFactories", connectionFactories);
  237   
  238           normalView.include(renderRequest, renderResponse);
  239       }
  240   
  241       private void renderDetail(RenderRequest renderRequest,
  242               RenderResponse renderResponse, String name)
  243               throws PortletException, IOException {
  244           ObjectName gbeanName;
  245   
  246           try {
  247               gbeanName = new ObjectName(name);
  248           } catch (MalformedObjectNameException e) {
  249               throw new PortletException("Malformed parameter name: "
  250                       + renderRequest.getParameter("name"));
  251           }
  252   
  253           try {
  254               GBeanInfo gbeanInfo = kernel.getGBeanInfo(gbeanName);
  255               Set attributes = gbeanInfo.getAttributes();
  256               Map values = new HashMap(attributes.size());
  257               for (Iterator i = attributes.iterator(); i.hasNext();) {
  258                   GAttributeInfo attribute = (GAttributeInfo) i.next();
  259                   String gname = attribute.getName();
  260                   if (HIDDEN_ATTRIBUTES.contains(gname)) {
  261                       continue;
  262                   }
  263                   Object value = kernel.getAttribute(gbeanName, gname);
  264                   values.put(gname, value);
  265               }
  266               renderRequest.setAttribute("attributeMap", values);
  267           } catch (Exception e) {
  268               throw new PortletException(e);
  269           }
  270   
  271           detailView.include(renderRequest, renderResponse);
  272       }
  273   
  274       protected void doHelp(RenderRequest renderRequest,
  275               RenderResponse renderResponse) throws PortletException, IOException {
  276           helpView.include(renderRequest, renderResponse);
  277       }
  278   
  279       public void destroy() {
  280           normalView = null;
  281           helpView = null;
  282           addView = null;
  283           kernel = null;
  284           super.destroy();
  285       }
  286   
  287       private String trimStr(String str) {
  288           if (str != null) {
  289               return str.trim();
  290           }
  291           return "";
  292       }
  293   
  294       protected void testConnection(Object cf) throws Exception {
  295           ConnectionFactory jmscf = (ConnectionFactory) cf;
  296           Connection c = jmscf.createConnection();
  297           c.close();
  298       }
  299   
  300   }

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