ConsoleAction.java
001 /*
002  *
003  * Licensed to the Apache Software Foundation (ASF) under one
004  * or more contributor license agreements.  See the NOTICE file
005  * distributed with this work for additional information
006  * regarding copyright ownership.  The ASF licenses this file
007  * to you under the Apache License, Version 2.0 (the
008  * "License"); you may not use this file except in compliance
009  * with the License.  You may obtain a copy of the License at
010  *
011  *   http://www.apache.org/licenses/LICENSE-2.0
012  *
013  * Unless required by applicable law or agreed to in writing,
014  * software distributed under the License is distributed on an
015  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
016  * KIND, either express or implied.  See the License for the
017  * specific language governing permissions and limitations
018  * under the License.
019  *
020  */
021 package org.apache.qpid.management.web.action;
022 
023 import java.io.File;
024 import java.io.IOException;
025 import java.lang.management.ManagementFactory;
026 import java.lang.management.OperatingSystemMXBean;
027 import java.lang.management.RuntimeMXBean;
028 import java.util.Date;
029 
030 import javax.servlet.RequestDispatcher;
031 import javax.servlet.ServletException;
032 import javax.servlet.http.HttpServlet;
033 import javax.servlet.http.HttpServletRequest;
034 import javax.servlet.http.HttpServletResponse;
035 
036 import org.apache.qpid.management.Messages;
037 import org.apache.qpid.management.Names;
038 import org.apache.qpid.transport.util.Logger;
039 
040 /**
041  * This action is the controller responsible to prepare data for the home 
042  * page (System Overview) of QMan admin console.
043  
044  @author Andrea Gazzarini
045  */
046 public class ConsoleAction extends HttpServlet
047 {
048   private static final long serialVersionUID = -2411413147821629363L;
049   
050   private static final Logger LOGGER = Logger.get(ConsoleAction.class);
051   
052   private Date _startDate;
053   
054   /**
055    * Initializes this controller.
056    * Simply it computes the start date of the application.
057    */
058   @Override
059   public void init()
060   {
061     _startDate = new Date();
062   }
063    
064   /**
065    *  Prepares data for System Overview admin console page and forward that data to that page.
066    *  
067    @throws ServletException when this controller is not able to forward to the appropriate view page.
068    @throws IOException when this controller is not able to forward to the appropriate view page.
069    */
070   @Override
071   protected void service(HttpServletRequest request, HttpServletResponse responsethrows ServletException, IOException
072   {
073     ConsoleModel model = new ConsoleModel();
074     model.setVersion("1.0");
075     model.setVersionName("Sofia");
076     model.setStartDate(_startDate);
077     model.setHost(System.getProperty(Names.ADAPTER_HOST_PROPERTY_NAME, "localhost"));
078     model.setPort(Integer.parseInt(System.getProperty(Names.ADAPTER_PORT_PROPERTY_NAME, "8080")));
079 
080     try 
081     {
082       OperatingSystemMXBean operatingSystem = ManagementFactory.getOperatingSystemMXBean();
083       model.setOsName(operatingSystem.getName());
084       model.setProcessors(operatingSystem.getAvailableProcessors());
085       model.setOsVersion(operatingSystem.getVersion());
086       model.setArchName(operatingSystem.getArch());
087     catch(Exception exception)
088     {
089       LOGGER.warn(exception,Messages.QMAN_300006_OS_MBEAN_FAILURE);
090       model.setOsName("N.A.");
091       model.setProcessors(null);
092       model.setOsVersion("N.A.");
093       model.setArchName("N.A.");      
094     }
095     
096     try 
097     {
098       RuntimeMXBean runtime = ManagementFactory.getRuntimeMXBean();
099       
100       String bootClasspath = runtime.getBootClassPath();
101       model.setBootClasspath(bootClasspath.split(File.pathSeparator));
102       
103       String classpath = runtime.getClassPath();
104       model.setClasspath(classpath.split(File.pathSeparator));
105       
106       model.setInputArguments(runtime.getInputArguments().toArray(new String[]{}));
107     catch(Exception exception)
108     {
109       LOGGER.warn(exception,Messages.QMAN_300007_RUNTIME_MBEAN_FAILURE);
110     }
111     
112     request.setAttribute("model", model);
113     
114     RequestDispatcher dispatcher = request.getRequestDispatcher("/console.jsp");
115     dispatcher.forward(request,response);
116   }
117 }