001: /*
002: * $Id: Main.java 6115 2006-01-30 04:50:47Z dfs $
003: *
004: * Copyright 2006 Daniel F. Savarese
005: *
006: * Licensed under the Apache License, Version 2.0 (the "License");
007: * you may not use this file except in compliance with the License.
008: * You may obtain a copy of the License at
009: *
010: * http://www.savarese.org/software/ApacheLicense-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS,
014: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: */
018:
019: package example.uniquery;
020:
021: import java.io.IOException;
022: import java.util.Map;
023: import javax.swing.*;
024: import javax.management.*;
025: import javax.management.remote.*;
026: import javax.wsdl.*;
027:
028: import org.apache.wsif.util.*;
029:
030: import org.savarese.unicorn.ui.*;
031:
032: /**
033: * An example program demonstrating how to use
034: * {@link org.savarese.unicorn.ui.ObjectPanelModel ObjectPanelModel},
035: * {@link org.savarese.unicorn.ui.ObjectPanelModelFactory ObjectPanelModelFactory},
036: * and {@link org.savarese.unicorn.ui.ObjectInspectorPanel ObjectInspectorPanel}.
037: * It assembles a GUI for accessing MBeans, JavaBeans, and Web services.
038: */
039: public final class Main {
040: static final class JavaBeanPanelModelFactory implements
041: ObjectPanelModelFactory {
042: public ObjectPanelModel createObjectPanelModel(String objectName)
043: throws ClassNotFoundException, InstantiationException,
044: IllegalAccessException {
045: return new JavaBeanPanelModel(Class.forName(objectName)
046: .newInstance());
047: }
048: }
049:
050: static final class WebServicePanelModelFactory implements
051: ObjectPanelModelFactory {
052: public ObjectPanelModel createObjectPanelModel(String objectName)
053: throws WSDLException {
054: Definition wsdl = WSIFUtils.readWSDL(null, objectName);
055: Map services = wsdl.getServices();
056:
057: // return first service found
058: for (Object service : services.values())
059: return new WebServicePanelModel(wsdl, (Service) service);
060:
061: return null;
062: }
063: }
064:
065: static final class MBeanPanelModelFactory implements
066: ObjectPanelModelFactory {
067: private JMXConnector connector = null;
068:
069: // Expects URLs of form: JMXServiceURL?ObjectName
070: // e.g., service:jmx:jmxmp://localhost:8082?domain:name=Status,instance=2
071: public ObjectPanelModel createObjectPanelModel(String objectName)
072: throws Exception {
073: close();
074:
075: String serviceURL = objectName.substring(0, objectName
076: .indexOf('?'));
077: String mbeanName = objectName.substring(objectName
078: .indexOf('?') + 1);
079:
080: connector = JMXConnectorFactory.newJMXConnector(
081: new JMXServiceURL(serviceURL), null);
082: connector.connect();
083:
084: return new MBeanPanelModel(new ObjectName(mbeanName),
085: connector.getMBeanServerConnection());
086: }
087:
088: public void close() throws IOException {
089: if (connector != null)
090: connector.close();
091: }
092:
093: }
094:
095: private Main() {
096: }
097:
098: // Some WSDL test URLs
099: //http://www.dneonline.com/calculator.asmx?WSDL
100: //http://www.webservicex.net/stockquote.asmx?WSDL
101: public static final void main(String[] args) throws Exception {
102: ObjectInspectorPanel inspector = new ObjectInspectorPanel();
103: inspector.addObjectPanelModelFactory(
104: "JavaBean (enter class name)",
105: new JavaBeanPanelModelFactory());
106: inspector.addObjectPanelModelFactory(
107: "Web Service (enter WSDL URL)",
108: new WebServicePanelModelFactory());
109: final MBeanPanelModelFactory mbeanFactory = new MBeanPanelModelFactory();
110:
111: inspector.addObjectPanelModelFactory(
112: "MBean (enter JMXServiceURL?ObjectName)", mbeanFactory);
113:
114: Runtime.getRuntime().addShutdownHook(new Thread() {
115: public void run() {
116: try {
117: mbeanFactory.close();
118: } catch (IOException ioe) {
119: ioe.printStackTrace();
120: }
121: }
122: });
123:
124: JFrame frame = new JFrame("Uniquery");
125: frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
126: frame.getContentPane().add(inspector);
127: frame.setSize(400, 400);
128: frame.setVisible(true);
129: }
130: }
|