Stephane Frenot | 641ddbc | 2006-09-19 14:42:16 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | * or more contributor license agreements. See the NOTICE file |
| 4 | * distributed with this work for additional information |
| 5 | * regarding copyright ownership. The ASF licenses this file |
| 6 | * to you under the Apache License, Version 2.0 (the |
| 7 | * "License"); you may not use this file except in compliance |
| 8 | * with the License. You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, |
| 13 | * software distributed under the License is distributed on an |
| 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | * KIND, either express or implied. See the License for the |
| 16 | * specific language governing permissions and limitations |
| 17 | * under the License. |
Stephane Frenot | 4eb2a3d | 2006-07-21 10:19:58 +0000 | [diff] [blame] | 18 | */ |
| 19 | package org.apache.felix.mosgi.managedelements.osgiprobes.tab; |
| 20 | |
| 21 | import java.awt.Component; |
| 22 | import java.beans.PropertyChangeEvent; |
| 23 | import java.util.ArrayList; |
| 24 | import java.util.Vector; |
| 25 | import javax.management.MBeanAttributeInfo; |
| 26 | import javax.management.MBeanServerConnection; |
| 27 | import javax.management.ObjectName; |
| 28 | import javax.swing.JPanel; |
| 29 | import javax.swing.JScrollPane; |
| 30 | import javax.swing.JTable; |
| 31 | import javax.swing.table.DefaultTableModel; |
| 32 | import org.osgi.framework.BundleActivator; |
| 33 | import org.osgi.framework.BundleContext; |
| 34 | import org.osgi.framework.ServiceRegistration; |
| 35 | import org.apache.felix.mosgi.console.ifc.Plugin; |
| 36 | |
| 37 | public class OsgiProbesTabUI extends JPanel implements Plugin, BundleActivator { |
| 38 | |
| 39 | private String [] headers = {"Name", "Value"}; |
| 40 | private JTable innerTable; |
| 41 | |
| 42 | private BundleContext m_context = null; |
| 43 | private ServiceRegistration sreg = null; |
| 44 | |
| 45 | public OsgiProbesTabUI(){ |
| 46 | this.innerTable=new JTable(); |
| 47 | this.innerTable.setPreferredScrollableViewportSize(new java.awt.Dimension(500,150)); |
| 48 | JScrollPane table = new JScrollPane(this.innerTable); |
| 49 | this.add(table); |
| 50 | } |
| 51 | |
| 52 | |
| 53 | /////////////////////////////////////////// |
| 54 | // BundleActivator // |
| 55 | /////////////////////////////////////////// |
| 56 | public void start(BundleContext context) { |
| 57 | m_context = context; |
| 58 | this.registerServicePlugin(); |
| 59 | } |
| 60 | |
| 61 | public void stop(BundleContext context) { |
| 62 | } |
| 63 | |
| 64 | |
| 65 | |
| 66 | /////////////////////////////////////////// |
| 67 | // Plugin // |
| 68 | ////////////////////////////////////////// |
| 69 | public void registerServicePlugin(){ |
| 70 | sreg = m_context.registerService(Plugin.class.getName(), this, null); |
Stephane Frenot | 1ce47fd | 2006-10-02 13:51:53 +0000 | [diff] [blame] | 71 | |
Stephane Frenot | 4eb2a3d | 2006-07-21 10:19:58 +0000 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | public void unregisterServicePlugin(){ |
Stephane Frenot | 1ce47fd | 2006-10-02 13:51:53 +0000 | [diff] [blame] | 75 | if (sreg!=null){ |
| 76 | sreg.unregister(); |
| 77 | sreg=null; |
| 78 | } |
Stephane Frenot | 4eb2a3d | 2006-07-21 10:19:58 +0000 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | public String pluginLocation(){ |
| 82 | return m_context.getBundle().getLocation(); |
| 83 | } |
| 84 | |
| 85 | public String getName(){return "OSGi Platform";} |
| 86 | |
| 87 | public Component getGUI(){return this;} |
| 88 | |
| 89 | public void propertyChange(PropertyChangeEvent e){ |
| 90 | /* |
| 91 | * This a static tab, each new visit will provide the same |
| 92 | * information. A dynamic tab update is provided in LinuxTab |
| 93 | * |
| 94 | */ |
| 95 | if (e.getPropertyName().equals(Plugin.NEW_NODE_READY)){ |
| 96 | this.getProperties((MBeanServerConnection)e.getNewValue()); |
| 97 | }else if(e.getPropertyName().equals(Plugin.EMPTY_NODE)){ |
| 98 | this.innerTable.setModel(new DefaultTableModel()); |
| 99 | this.invalidate(); |
| 100 | this.validate(); |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | private void getProperties(MBeanServerConnection mbsc){ |
| 105 | try{ |
| 106 | ObjectName on=new ObjectName("TabUI:name=OsgiProbes"); |
| 107 | MBeanAttributeInfo attrInfo[] = mbsc.getMBeanInfo(on).getAttributes(); |
| 108 | Object content [][]=new String[attrInfo.length][2]; |
| 109 | for (int k=0;k<attrInfo.length;k++) { |
| 110 | content [k][0]=attrInfo[k].getName(); |
| 111 | content [k][1]=mbsc.getAttribute(on, attrInfo[k].getName()); |
| 112 | } |
| 113 | DefaultTableModel dtm=new DefaultTableModel(content, this.headers); |
| 114 | this.innerTable.setModel(dtm); |
| 115 | this.invalidate(); |
| 116 | this.validate(); |
| 117 | }catch (Exception e){ |
| 118 | e.printStackTrace(); |
| 119 | } |
| 120 | } |
| 121 | } |