Home » geronimo-2.2-source-release » org.apache.geronimo.monitoring.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.monitoring.console;
   18   
   19   import java.net.InetAddress;
   20   import java.sql.Connection;
   21   import java.sql.Statement;
   22   import java.io.IOException;
   23   import java.text.Format;
   24   import java.text.SimpleDateFormat;
   25   import java.util.ArrayList;
   26   import java.util.Date;
   27   import java.util.HashMap;
   28   import java.util.Hashtable;
   29   import java.util.Iterator;
   30   import java.util.Properties;
   31   import java.util.Set;
   32   import java.util.TreeMap;
   33   import java.util.TreeSet;
   34   
   35   import javax.management.MBeanServerConnection;
   36   import javax.management.ObjectName;
   37   import javax.management.remote.JMXConnector;
   38   import javax.management.remote.JMXConnectorFactory;
   39   import javax.management.remote.JMXServiceURL;
   40   import javax.naming.Context;
   41   import javax.naming.InitialContext;
   42   
   43   import org.apache.geronimo.console.util.PortletManager;
   44   import org.apache.geronimo.crypto.EncryptionManager;
   45   import org.apache.geronimo.monitoring.MasterRemoteControlRemote;
   46   import org.apache.geronimo.monitoring.console.data.Node;
   47   import org.apache.geronimo.system.jmx.RealMBeanServerReference;
   48   
   49   public class MRCConnector {
   50   
   51       private static String PATH = null;
   52       private MBeanServerConnection mbServerConn;
   53       private MasterRemoteControlRemote mrc = null;
   54       private String protocol = "EJB";
   55       private JMXConnector connector = null;
   56       MRCConnector() {
   57   
   58       }
   59   
   60       public MRCConnector(Node node) throws Exception {
   61           this(node.getHost(), node.getUserName(), node.getPassword(), node.getPort(), node.getProtocol());
   62       }
   63   
   64       /**
   65        * @param ip -
   66        *            IP address of mrc-server to connect to
   67        * @param userName -
   68        *            Username for JMX connection to the host
   69        * @param password -
   70        *            Password for JMX connection to the host
   71        * @throws Exception -
   72        *             If the connection to mrc-server fails
   73        */
   74       public MRCConnector(String ip, String userName, String password, int port,
   75               String protocol) throws Exception {
   76           // decrypt the password
   77           password = (String) EncryptionManager.decrypt(password);
   78           this.protocol = protocol;
   79   
   80           if (isEjbProtocol()) {
   81   
   82               try {
   83                   Properties props = new Properties();
   84                   props
   85                           .setProperty(Context.INITIAL_CONTEXT_FACTORY,
   86                                   "org.apache.openejb.client.RemoteInitialContextFactory");
   87                   props.setProperty(Context.PROVIDER_URL, "ejbd://" + ip + ":"
   88                           + port);
   89                   props.setProperty(Context.SECURITY_PRINCIPAL, userName);
   90                   props.setProperty(Context.SECURITY_CREDENTIALS, password);
   91                   props.setProperty("openejb.authentication.realmName",
   92                           "geronimo-admin");
   93                   Context ic = new InitialContext(props);
   94                   mrc = (MasterRemoteControlRemote) ic
   95                           .lookup("ejb/mgmt/MRCRemote");
   96               } catch (Exception e) {
   97                   throw e;
   98               }
   99   
  100           } else {
  101               try {
  102                   InetAddress host = InetAddress.getLocalHost();//maybe throw a UnknownHostException 
  103                   if (ip.equals("localhost") || ip.equals(host.getHostAddress())
  104                                               || ip.equals(host.getHostName())
  105                                               ||ip.equals("127.0.0.1")) {
  106                       mbServerConn = ((RealMBeanServerReference)PortletManager.getKernel().getGBean("MBeanServerReference")).getMBeanServer();
  107                   } else {
  108                   JMXServiceURL serviceURL = new JMXServiceURL(
  109                           "service:jmx:rmi:///jndi/rmi://" + ip + ":" + port
  110                                   + "/JMXConnector");
  111                   Hashtable<String, Object> env = new Hashtable<String, Object>();
  112                   String[] credentials = new String[2];
  113                   credentials[0] = userName;
  114                   credentials[1] = password;
  115                   env.put(JMXConnector.CREDENTIALS, credentials);
  116                   connector = JMXConnectorFactory.connect(
  117                           serviceURL, env);
  118                   mbServerConn = connector.getMBeanServerConnection();
  119                   }
  120   
  121                   // retrieve the mbean name to the agent-car-jmx plugin
  122                   if(PATH == null) {
  123                       Set<ObjectName> mbeanNames = mbServerConn.queryNames(new ObjectName("*:name=MasterRemoteControlJMX,*"), null);
  124                       for(Iterator<ObjectName> it = mbeanNames.iterator(); it.hasNext(); ) {
  125                           String mbeanName = ((ObjectName)it.next()).getCanonicalName();
  126                           if(mbeanName.contains("agent-car-jmx") &&
  127                              mbeanName.contains("MasterRemoteControlJMX") &&
  128                              mbeanName.contains("GBean")) {
  129                               PATH = mbeanName;
  130                               break;
  131                           }
  132                       }
  133   
  134                       // there must be a PATH to agent-car-jmx plugin
  135                       if(PATH == null) {
  136                           throw new Exception("[ERROR] Required mbean not found: agent-car-jmx");
  137                       }
  138                   }
  139               } catch (Exception e) {
  140                   e.printStackTrace();
  141                   throw e;
  142               }
  143   
  144           }
  145           // when the code has reach this point, a connection was successfully
  146           // established
  147           // so we need to update the last_seen attribute for the server
  148   //        Format formatter = null;
  149   //        formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  150   //        Date date = new Date(System.currentTimeMillis());
  151   //        String currentTime = formatter.format(date);
  152   
  153   //        Connection conn = DBManager.createConnection();
  154   //        try {
  155   //            Statement stmt = conn.createStatement();
  156   //            stmt.executeUpdate("UPDATE SERVERS SET LAST_SEEN = '" + currentTime
  157   //                    + "' WHERE IP='" + ip + "'");
  158   //        } catch (Exception e) {
  159   //            throw e;
  160   //        } finally {
  161   //            try {
  162   //                if (conn != null) {
  163   //                    conn.close();
  164   //                }
  165   //            } catch (Exception e) {
  166   //
  167   //            }
  168   //        }
  169       }
  170       
  171       public void dispose()
  172       {
  173           try{
  174               if("JMX".equals(this.protocol) && null != this.connector){
  175                   connector.close();
  176                   connector = null;
  177               }
  178                   
  179           }
  180           catch(IOException ex)
  181           {
  182           }
  183       }
  184       
  185       private boolean isEjbProtocol() {
  186           return "EJB".equals(protocol);
  187       }
  188   
  189       /**
  190        * @return - Returns an Long representing the current snapshot duration set
  191        *         on the server side
  192        * @throws Exception -
  193        *             If the connection to the MRC-Server fails
  194        */
  195       public Long getSnapshotDuration() throws Exception {
  196           if (isEjbProtocol()) {
  197   
  198               return mrc.getSnapshotDuration();
  199   
  200           } else {
  201   
  202               return (Long) mbServerConn.invoke(new ObjectName(PATH),
  203                       "getSnapshotDuration", new Object[] {}, new String[] {});
  204           }
  205       }
  206   
  207       /**
  208        * @return - Returns an ArrayList of String objects containing a listing of
  209        *         all statistics values being collected
  210        * @throws Exception -
  211        *             If the connection to the MRC-Server fails
  212        */
  213       @SuppressWarnings("unchecked")
  214       public HashMap<String, ArrayList<String>> getDataNameList()
  215               throws Exception {
  216   
  217           HashMap<String, ArrayList<String>> DataNameList = new HashMap<String, ArrayList<String>>();
  218   
  219           if (isEjbProtocol()) {
  220   
  221               try {
  222                   DataNameList = mrc.getAllSnapshotStatAttributes();
  223               } catch (Exception e) {
  224                   e.printStackTrace();
  225               }
  226   
  227           } else {
  228               try {
  229                   DataNameList = (HashMap<String, ArrayList<String>>) mbServerConn
  230                           .invoke(new ObjectName(PATH),
  231                                   "getAllSnapshotStatAttributes",
  232                                   new Object[] {}, new String[] {});
  233               } catch (Exception e) {
  234                   e.printStackTrace();
  235               }
  236           }
  237           // Strip out snapshot_date and snapshot_time, we know these exist
  238           for (Iterator<String> it = DataNameList.keySet().iterator(); it
  239                   .hasNext();) {
  240               String mbeanName = it.next();
  241               DataNameList.get(mbeanName).remove("snapshot_date");
  242               DataNameList.get(mbeanName).remove("snapshot_time");
  243           }
  244           return DataNameList;
  245       }
  246   
  247       /**
  248        * @param snapCount -
  249        *            Number of snapshots to request from the server
  250        * @param skipCount -
  251        *            Every nth snapshot. A value of 1 will be every 1. A value of 2
  252        *            will be every other.
  253        * @return - Returns an ArrayList of Map objects.
  254        * @throws Exception -
  255        *             If the connection to the MRC-Server fails
  256        */
  257       @SuppressWarnings("unchecked")
  258       public ArrayList<HashMap<String, HashMap<String, Object>>> getSnapshots(
  259               int snapCount, int skipCount) throws Exception {
  260           ArrayList<HashMap<String, HashMap<String, Object>>> snapshotList = null;
  261           if (isEjbProtocol()) {
  262   
  263               snapshotList = mrc.fetchSnapshotData(snapCount, skipCount);
  264   
  265           } else {
  266               snapshotList = (ArrayList<HashMap<String, HashMap<String, Object>>>) mbServerConn
  267                       .invoke(new ObjectName(PATH), "fetchSnapshotData",
  268                               new Object[] { snapCount, skipCount },
  269                               new String[] { "java.lang.Integer",
  270                                       "java.lang.Integer" });
  271           }
  272           // Check if snapshotList is empty
  273           if (snapshotList.size() == 0) {
  274               return snapshotList;
  275           }
  276           /*
  277            * If there are not enough snapshots available to fill the requested
  278            * number, insert some with values of 0 and the proper times.
  279            */
  280           while (snapshotList.size() < snapCount) {
  281               // Temporary, always is first element (oldest)
  282               HashMap<String, HashMap<String, Object>> mapTimeFix = snapshotList
  283                       .get(0);
  284   
  285               // Temporary map, used to generate blank data to be added to
  286               // the
  287               // list at position 0
  288               HashMap<String, HashMap<String, Object>> tempMap = new HashMap<String, HashMap<String, Object>>();
  289   
  290               // Temporary submap, used to store 0 elements to be added to
  291               // the
  292               // tempmap
  293               HashMap<String, Object> subMap = new HashMap<String, Object>();
  294   
  295               // Calculate appropriate time, add it to the submap, then
  296               // add
  297               // that to the tempMap
  298               subMap.put("snapshot_time", ((Long) mapTimeFix.get("times").get(
  299                       "snapshot_time") - (getSnapshotDuration() * skipCount)));
  300               Format formatter = null;
  301               formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  302               Date date = new Date((Long) subMap.get("snapshot_time"));
  303               subMap.put("snapshot_date", formatter.format(date));
  304   
  305               // Add the submap back to the tempmap
  306               tempMap.put("times", new HashMap<String, Object>(subMap));
  307   
  308               // Clear out the subMap for use again
  309               subMap.clear();
  310   
  311               // Run through the mbeans
  312   
  313               // Run through the mbeans
  314               for (Iterator<String> it = mapTimeFix.keySet().iterator(); it
  315                       .hasNext();) {
  316                   // get the mbean name
  317                   String mbeanName = it.next();
  318                   HashMap<String, Object> stats = null;
  319                   // Verify that it's not times
  320   
  321                   if (mbeanName.equals(new String("times"))) {
  322   
  323                   } else {
  324                       stats = mapTimeFix.get(mbeanName);
  325                       // Run through the stats elements for the particular
  326                       // mbean
  327                       for (Iterator<String> itt = stats.keySet().iterator(); itt
  328                               .hasNext();) {
  329                           String key = itt.next();
  330                           // Place faux data into the submap
  331                           subMap.put(key, new Long(0));
  332                       }
  333                       // Add the submap to the tempmap, and clear it
  334                       tempMap.put(mbeanName, new HashMap<String, Object>(subMap));
  335                   }
  336               }
  337               snapshotList.add(0, new HashMap<String, HashMap<String, Object>>(
  338                       tempMap));
  339           }
  340   
  341           /*
  342            * This is where we will be inserting data to fill 'gaps' in the
  343            * snapshots The initial for-loop will travel from the most recent
  344            * snapshot to the oldest, checking that the snapshot_time along the way
  345            * all align with what they should be
  346            */
  347           for (int i = snapshotList.size() - 1; i > 0; i--) {
  348               if (i > 0) {
  349                   HashMap<String, HashMap<String, Object>> mapTimeFix = snapshotList
  350                           .get(i);
  351                   HashMap<String, HashMap<String, Object>> mapTimeFix2 = snapshotList
  352                           .get(i - 1);
  353                   // here is where we will in missing data
  354                   while (((((Long) mapTimeFix.get("times").get("snapshot_time") / 1000) / 60)
  355                           - (((Long) mapTimeFix2.get("times")
  356                                   .get("snapshot_time") / 1000) / 60) > (((getSnapshotDuration() / 1000) / 60) * skipCount))) {
  357                       HashMap<String, HashMap<String, Object>> tempMap = new HashMap<String, HashMap<String, Object>>();
  358                       HashMap<String, Object> subMap = new HashMap<String, Object>();
  359   
  360                       for (Iterator<String> it = mapTimeFix.keySet().iterator(); it
  361                               .hasNext();) {
  362                           // get the mbean name
  363                           String mbeanName = it.next();
  364                           HashMap<String, Object> stats = null;
  365                           // Verify that it's not times
  366                           if (!mbeanName.equals("times")) {
  367                               stats = mapTimeFix.get(mbeanName);
  368                               // Run through the stats elements for the
  369                               // particular
  370                               // mbean
  371                               for (Iterator<String> itt = stats.keySet()
  372                                       .iterator(); itt.hasNext();) {
  373                                   String key = itt.next();
  374                                   // Place faux data into the submap
  375                                   subMap.put(key, new Long(0));
  376                               }
  377                               // Add the submap to the tempmap, and clear it
  378                               tempMap.put(mbeanName, new HashMap<String, Object>(
  379                                       subMap));
  380                               subMap.clear();
  381                           }
  382                       }
  383   
  384                       subMap.put("snapshot_time", new Long((Long) mapTimeFix.get(
  385                               "times").get("snapshot_time")
  386                               - (getSnapshotDuration() * skipCount)));
  387                       Format formatter = null;
  388                       formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  389                       Date date = new Date((Long) subMap.get("snapshot_time"));
  390                       subMap.put("snapshot_date", formatter.format(date));
  391                       tempMap.put("times", new HashMap<String, Object>(subMap));
  392                       subMap.clear();
  393                       snapshotList.add(i,
  394                               new HashMap<String, HashMap<String, Object>>(
  395                                       tempMap));
  396                       snapshotList.remove(0);
  397                       mapTimeFix = tempMap;
  398                       mapTimeFix2 = snapshotList.get(i - 1);
  399                   }
  400               }
  401           }
  402           return snapshotList;
  403       }
  404   
  405       @SuppressWarnings("unchecked")
  406       public TreeMap<Long, Long> getSpecificStatistics(String mbeanName,
  407               String statsName, int snapCount, int skipCount, boolean showArchive)
  408               throws Exception {
  409           TreeMap<Long, Long> snapshotList = null;
  410           if (isEjbProtocol()) {
  411   
  412               snapshotList = mrc.getSpecificStatistics(mbeanName, statsName,
  413                       snapCount, skipCount, showArchive);
  414   
  415           } else {
  416               snapshotList = (TreeMap<Long, Long>) mbServerConn.invoke(
  417                       new ObjectName(PATH), "getSpecificStatistics",
  418                       new Object[] { mbeanName, statsName, snapCount, skipCount,
  419                               showArchive }, new String[] { "java.lang.String",
  420                               "java.lang.String", "java.lang.Integer",
  421                               "java.lang.Integer", "java.lang.Boolean" });
  422   
  423           }
  424           // Check if snapshotList is empty
  425           if (snapshotList.size() == 0) {
  426               return snapshotList;
  427           }
  428           /*
  429            * If there are not enough snapshots available to fill the requested
  430            * number, insert some with values of 0 and the proper times.
  431            */
  432           while (snapshotList.size() < snapCount) {
  433               // Temporary, always is first element (oldest)
  434               Long timeFix = snapshotList.firstKey();
  435   
  436               // Calculate appropriate time, add it to the submap, then
  437               // add
  438               // that to the tempMap
  439               snapshotList.put((timeFix - (getSnapshotDuration() * skipCount)),
  440                       new Long(0));
  441           }
  442   
  443           /*
  444            * This is where we will be inserting data to fill 'gaps' in the
  445            * snapshots The initial for-loop will travel from the most recent
  446            * snapshot to the oldest, checking that the snapshot_time along the way
  447            * all align with what they should be
  448            */
  449           Set tempSet = snapshotList.keySet();
  450           ArrayList<Long> tempArray = new ArrayList(tempSet);
  451   
  452           for (int i = tempArray.size() - 1; i > 0; i--) {
  453               Long tempLong1 = tempArray.get(i);
  454               Long tempLong2 = tempArray.get(i - 1);
  455               // here is where we will in missing data
  456               while ((((tempLong1 / 1000) / 60) - ((tempLong2 / 1000) / 60) > (((getSnapshotDuration() / 1000) / 60) * skipCount))
  457                       && i > 0) {
  458                   tempLong1 = tempLong1 - (getSnapshotDuration() * skipCount);
  459                   snapshotList.remove(tempArray.get(0));
  460                   snapshotList.put(tempLong1, new Long(0));
  461                   tempArray.remove(0);
  462                   i--;
  463               }
  464           }
  465           return snapshotList;
  466       }
  467   
  468       @SuppressWarnings("unchecked")
  469       public HashMap<String, HashMap<String, Object>> getLatestSnapshots()
  470               throws Exception {
  471           int snapCount = 1;
  472           int skipCount = 1;
  473           ArrayList<HashMap<String, HashMap<String, Object>>> snapshotList = null;
  474           if (isEjbProtocol()) {
  475   
  476               snapshotList = mrc.fetchSnapshotData(snapCount, skipCount);
  477   
  478           } else {
  479               snapshotList = (ArrayList<HashMap<String, HashMap<String, Object>>>) mbServerConn
  480                       .invoke(new ObjectName(PATH), "fetchSnapshotData",
  481                               new Object[] { snapCount, skipCount },
  482                               new String[] { "java.lang.Integer",
  483                                       "java.lang.Integer" });
  484           }
  485           // Check if snapshotList is empty
  486           if (snapshotList.size() == 0) {
  487               return null;
  488           } else
  489               return snapshotList.get(0);
  490       }
  491   
  492       /**
  493        * @return - Returns a boolean indicating successful stop
  494        * @throws Exception -
  495        *             If the connection to the MRC-Server fails
  496        */
  497       public boolean stopSnapshotThread() throws Exception {
  498           if (isEjbProtocol()) {
  499   
  500               return mrc.stopSnapshot();
  501   
  502           } else {
  503               return (Boolean) mbServerConn.invoke(new ObjectName(PATH),
  504                       "stopSnapshot", new Object[] {}, new String[] {});
  505           }
  506       }
  507   
  508       /**
  509        * @return - Returns a boolean indicating successful stop
  510        * @throws Exception -
  511        *             If the connection to the MRC-Server fails
  512        */
  513       public boolean startSnapshotThread(long time) throws Exception {
  514           if (isEjbProtocol()) {
  515   
  516               return mrc.startSnapshot(time);
  517   
  518           } else {
  519               return (Boolean) mbServerConn.invoke(new ObjectName(PATH),
  520                       "startSnapshot", new Object[] { time },
  521                       new String[] { "java.lang.Long" });
  522           }
  523       }
  524   
  525       public int isSnapshotRunning() {
  526           Integer running = 0;
  527           if (isEjbProtocol()) {
  528   
  529               try {
  530                   if (mrc.isSnapshotRunning())
  531                       running = 1;
  532               } catch (Exception e) {
  533                   return 0;
  534               }
  535   
  536           } else {
  537               try {
  538                   running = (Integer) mbServerConn.invoke(new ObjectName(PATH),
  539                           "SnapshotStatus", new Object[] {}, new String[] {});
  540               } catch (Exception e) {
  541                   e.printStackTrace();
  542                   return 0;
  543               }
  544           }
  545           return running;
  546       }
  547   
  548       @SuppressWarnings("unchecked")
  549       public Set<String> getAllMbeanNames() throws Exception {
  550           if (isEjbProtocol()) {
  551   
  552               return mrc.getAllMBeanNames();
  553   
  554           } else {
  555               return (Set<String>) mbServerConn.invoke(new ObjectName(PATH),
  556                       "getAllMBeanNames", new Object[] {}, new String[] {});
  557           }
  558       }
  559   
  560       @SuppressWarnings("unchecked")
  561       public Set<String> getStatisticsProviderBeanNames() throws Exception {
  562           if (isEjbProtocol()) {
  563   
  564               return mrc.getStatisticsProviderMBeanNames();
  565   
  566           } else {
  567               return (Set<String>) mbServerConn.invoke(new ObjectName(PATH),
  568                       "getStatisticsProviderMBeanNames", new Object[] {},
  569                       new String[] {});
  570           }
  571       }
  572   
  573       @SuppressWarnings("unchecked")
  574       public HashMap<String, ArrayList<String>> getAllSnapshotStatAttributes()
  575               throws Exception {
  576           if (isEjbProtocol()) {
  577   
  578               return mrc.getAllSnapshotStatAttributes();
  579   
  580           } else {
  581               return (HashMap<String, ArrayList<String>>) mbServerConn.invoke(
  582                       new ObjectName(PATH), "getAllSnapshotStatAttributes",
  583                       new Object[] {}, new String[] {});
  584           }
  585       }
  586   
  587       @SuppressWarnings("unchecked")
  588       public Set<String> getTrackedBeans() throws Exception {
  589           if (isEjbProtocol()) {
  590   
  591               return mrc.getTrackedMBeans();
  592   
  593           } else {
  594               return (Set<String>) mbServerConn.invoke(new ObjectName(PATH),
  595                       "getTrackedMBeans", new Object[] {}, new String[] {});
  596           }
  597       }
  598   
  599       @SuppressWarnings("unchecked")
  600       public Set<String> getStatAttributesOnMBean(String mBean) throws Exception {
  601           HashMap<String, ArrayList<String>> allStatAttributes = getAllSnapshotStatAttributes();
  602           ArrayList<String> tempArrayList = allStatAttributes.get(mBean);
  603           Set<String> tempSet = new TreeSet<String>();
  604           Iterator it = tempArrayList.iterator();
  605           while (it.hasNext()) {
  606               tempSet.add(it.next().toString());
  607           }
  608           return tempSet;
  609       }
  610   
  611       @SuppressWarnings("unchecked")
  612       public Set<String> getTrackedBeansPretty() throws Exception {
  613           Set trackedBeans = getTrackedBeans();
  614           Set prettybeans = new TreeSet();
  615           Iterator it = trackedBeans.iterator();
  616           while (it.hasNext()) {
  617               String[] temparray1 = it.next().toString().split("name=");
  618               String[] temparray2 = temparray1[1].split(",");
  619               String[] temparray3 = temparray2[0].split("/");
  620               String mbeanName = null;
  621               if (temparray3.length > 1)
  622                   mbeanName = temparray3[1];
  623               else
  624                   mbeanName = temparray2[0];
  625               prettybeans.add(mbeanName);
  626           }
  627           return prettybeans;
  628       }
  629   
  630       @SuppressWarnings("unchecked")
  631       public TreeMap<String, String> getTrackedBeansMap() throws Exception {
  632           Set trackedBeans = getTrackedBeans();
  633           TreeMap<String, String> beanMap = new TreeMap<String, String>();
  634           Iterator it = trackedBeans.iterator();
  635           while (it.hasNext()) {
  636               String mbeanName = it.next().toString();
  637               String[] temparray1 = mbeanName.split("name=");
  638               String[] temparray2 = temparray1[1].split(",");
  639               String[] temparray3 = temparray2[0].split("/");
  640               String mbeanNamePretty = null;
  641               if (temparray3.length > 1)
  642                   mbeanNamePretty = temparray3[1];
  643               else
  644                   mbeanNamePretty = temparray2[0];
  645               beanMap.put(mbeanNamePretty, mbeanName);
  646           }
  647           return beanMap;
  648       }
  649   
  650       @SuppressWarnings("unchecked")
  651       public Set<String> getStatisticsProviderBeanNamesPretty() throws Exception {
  652           Set availableBeans = getStatisticsProviderBeanNames();
  653           Set prettybeans = new TreeSet();
  654           Iterator it = availableBeans.iterator();
  655           while (it.hasNext()) {
  656               String[] temparray1 = it.next().toString().split("name=");
  657               String[] temparray2 = temparray1[1].split(",");
  658               String[] temparray3 = temparray2[0].split("/");
  659               String mbeanName = null;
  660               if (temparray3.length > 1)
  661                   mbeanName = temparray3[1];
  662               else
  663                   mbeanName = temparray2[0];
  664               prettybeans.add(mbeanName);
  665           }
  666           return prettybeans;
  667       }
  668   
  669       @SuppressWarnings("unchecked")
  670       public TreeMap<String, String> getStatisticsProviderBeanNamesMap()
  671               throws Exception {
  672           Set availableBeans = getStatisticsProviderBeanNames();
  673           TreeMap<String, String> beanMap = new TreeMap<String, String>();
  674           Iterator it = availableBeans.iterator();
  675           while (it.hasNext()) {
  676               String mbeanName = it.next().toString();
  677               String[] temparray1 = mbeanName.split("name=");
  678               String[] temparray2 = temparray1[1].split(",");
  679               String[] temparray3 = temparray2[0].split("/");
  680               String mbeanNamePretty = null;
  681               if (temparray3.length > 1)
  682                   mbeanNamePretty = temparray3[1];
  683               else
  684                   mbeanNamePretty = temparray2[0];
  685               beanMap.put(mbeanNamePretty, mbeanName);
  686           }
  687           return beanMap;
  688       }
  689   
  690       @SuppressWarnings("unchecked")
  691       public Set<String> getFreeStatisticsProviderBeanNamesPretty()
  692               throws Exception {
  693           Set<String> availableBeans = getStatisticsProviderBeanNamesPretty();
  694           Set<String> usedBeans = getTrackedBeansPretty();
  695           Set freeBeans = new TreeSet();
  696           Iterator it = availableBeans.iterator();
  697           while (it.hasNext()) {
  698               String mbeanName = it.next().toString();
  699               if (!usedBeans.contains(mbeanName))
  700                   freeBeans.add(mbeanName);
  701           }
  702           return freeBeans;
  703       }
  704   
  705       @SuppressWarnings("unchecked")
  706       public Set<String> getFreeStatisticsProviderBeanNames() throws Exception {
  707           Set<String> availableBeans = getStatisticsProviderBeanNames();
  708           Set<String> usedBeans = getTrackedBeansPretty();
  709           Set freeBeans = new TreeSet();
  710           Iterator it = availableBeans.iterator();
  711           while (it.hasNext()) {
  712               String mbeanName = it.next().toString();
  713               String[] temparray1 = mbeanName.split("name=");
  714               String[] temparray2 = temparray1[1].split(",");
  715               String[] temparray3 = temparray2[0].split("/");
  716               String mbeanNamePretty = null;
  717               if (temparray3.length > 1)
  718                   mbeanNamePretty = temparray3[1];
  719               else
  720                   mbeanNamePretty = temparray2[0];
  721               if (!usedBeans.contains(mbeanNamePretty))
  722                   freeBeans.add(mbeanName);
  723           }
  724           return freeBeans;
  725       }
  726   
  727       @SuppressWarnings("unchecked")
  728       public TreeMap<String, String> getFreeStatisticsProviderBeanNamesMap()
  729               throws Exception {
  730           Set<String> availableBeans = getStatisticsProviderBeanNames();
  731           Set<String> usedBeans = getTrackedBeansPretty();
  732           TreeMap<String, String> beanMap = new TreeMap<String, String>();
  733           Iterator it = availableBeans.iterator();
  734           while (it.hasNext()) {
  735               String mbeanName = it.next().toString();
  736               String[] temparray1 = mbeanName.split("name=");
  737               String[] temparray2 = temparray1[1].split(",");
  738               String[] temparray3 = temparray2[0].split("/");
  739               String mbeanNamePretty = null;
  740               if (temparray3.length > 1)
  741                   mbeanNamePretty = temparray3[1];
  742               else
  743                   mbeanNamePretty = temparray2[0];
  744               if (!usedBeans.contains(mbeanNamePretty))
  745                   beanMap.put(mbeanNamePretty, mbeanName);
  746           }
  747           return beanMap;
  748       }
  749   
  750       @SuppressWarnings("unchecked")
  751       public boolean stopTrackingMbean(String MBean) throws Exception {
  752           if (isEjbProtocol()) {
  753   
  754               mrc.removeMBeanForSnapshot(MBean);
  755   
  756           } else {
  757               mbServerConn
  758                       .invoke(new ObjectName(PATH), "removeMBeanForSnapshot",
  759                               new Object[] { MBean },
  760                               new String[] { "java.lang.String" });
  761   
  762           }
  763           return true;
  764       }
  765   
  766       @SuppressWarnings("unchecked")
  767       public boolean startTrackingMbean(String MBean) throws Exception {
  768           if (isEjbProtocol()) {
  769   
  770               mrc.addMBeanForSnapshot(MBean);
  771   
  772           } else {
  773               mbServerConn
  774                       .invoke(new ObjectName(PATH), "addMBeanForSnapshot",
  775                               new Object[] { MBean },
  776                               new String[] { "java.lang.String" });
  777           }
  778           return true;
  779       }
  780   
  781       @SuppressWarnings("unchecked")
  782       public HashMap<String, Long> getStats(String MBean) throws Exception {
  783           if (isEjbProtocol()) {
  784   
  785               return mrc.getStats(MBean);
  786   
  787           } else {
  788               return (HashMap<String, Long>) mbServerConn.invoke(new ObjectName(
  789                       PATH), "getStats", new Object[] { MBean },
  790                       new String[] { "java.lang.String" });
  791           }
  792       }
  793   
  794       public void setSnapshotDuration(long duration) {
  795           if (isEjbProtocol()) {
  796   
  797               mrc.setSnapshotDuration(new Long(duration));
  798   
  799           } else {
  800               try {
  801   
  802                   mbServerConn.invoke(new ObjectName(PATH),
  803                           "setSnapshotDuration",
  804                           new Object[] { new Long(duration) },
  805                           new String[] { "java.lang.Long" });
  806               } catch (Exception e) {
  807                   e.printStackTrace();
  808               }
  809           }
  810       }
  811   
  812       public int getSnapshotRetention() {
  813           if (isEjbProtocol()) {
  814   
  815               return Integer.parseInt(mrc.getSnapshotRetention());
  816   
  817           } else {
  818               try {
  819                   return (Integer) mbServerConn.invoke(new ObjectName(PATH),
  820                           "getSnapshotRetention", new Object[] {},
  821                           new String[] {});
  822               } catch (Exception e) {
  823                   e.printStackTrace();
  824               }
  825           }
  826           return 0;
  827       }
  828   
  829       public void setSnapshotRetention(int duration) {
  830           if (isEjbProtocol()) {
  831   
  832               mrc.setSnapshotRetention(duration);
  833   
  834           } else {
  835               try {
  836                   mbServerConn.invoke(new ObjectName(PATH),
  837                           "setSnapshotRetention", new Object[] { duration },
  838                           new String[] { "java.lang.Integer" });
  839               } catch (Exception e) {
  840                   e.printStackTrace();
  841               }
  842           }
  843       }
  844   }

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