KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > console > navtree > AppletBrowser


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.console.navtree;
23
24 import java.net.URL JavaDoc;
25 import java.util.Properties JavaDoc;
26
27 import javax.swing.*;
28
29 import org.jboss.console.manager.interfaces.SimpleTreeNodeMenuEntry;
30 import org.jboss.console.manager.interfaces.TreeAction;
31 import org.jboss.console.manager.interfaces.impl.HttpLinkTreeAction;
32 import org.jboss.console.remote.AppletRemoteMBeanInvoker;
33 import org.jboss.console.remote.SimpleRemoteMBeanInvoker;
34
35 /**
36  * AdminTreeBrowser container for applets
37  *
38  * @see org.jboss.console.navtree.AdminTreeBrowser
39  *
40  * @author <a HREF="mailto:sacha.labourey@cogito-info.ch">Sacha Labourey</a>.
41  * @version $Revision: 37459 $
42  */

43 public class AppletBrowser extends javax.swing.JApplet JavaDoc
44 {
45    AdminTreeBrowser treeBrowser = null;
46    AppletAdminContext ctx = null;
47    
48    public static final String JavaDoc RIGHT_FRAME_NAME = "right";
49    protected String JavaDoc sessionId = null;
50    protected String JavaDoc pmJmxName = null;
51    
52
53    public AppletBrowser ()
54    {
55    }
56    
57    public void start ()
58    {
59       try
60       {
61          ctx = new AppletAdminContext ();
62          
63          initAppletParams();
64          
65          treeBrowser = new AdminTreeBrowser (ctx);
66          
67          initComponents ();
68          
69          initRefreshThread ();
70          
71       }
72       catch (Exception JavaDoc e)
73       {
74          e.printStackTrace ();
75       }
76    }
77    
78    public void refreshTree (boolean force)
79    {
80       treeBrowser.refreshTree(force);
81    }
82    
83    protected void initAppletParams()
84    {
85       sessionId = getParameter("SessionId");
86       if (sessionId != null)
87          sessionId = "jsessionid=" + sessionId;
88       else
89          sessionId = "";
90
91       this.pmJmxName = getParameter("PMJMXName");
92       if( pmJmxName == null )
93          pmJmxName = "jboss.admin:service=PluginManager";
94    }
95
96    protected void initComponents()
97    {
98       javax.swing.JTree JavaDoc tree = treeBrowser.getTree();
99       javax.swing.JScrollPane JavaDoc scrollPane = new javax.swing.JScrollPane JavaDoc(tree);
100       
101       scrollPane.setBorder(javax.swing.BorderFactory.createEmptyBorder(0,0,0,0));
102       
103       getContentPane().add(scrollPane, java.awt.BorderLayout.CENTER);
104       
105       //getContentPane().add(tree, java.awt.BorderLayout.CENTER);
106
}
107    
108    protected void initRefreshThread ()
109    {
110       try
111       {
112          String JavaDoc strRefreshSec = getParameter("RefreshTime");
113          if (strRefreshSec != null && !"".equals(strRefreshSec))
114          {
115             final long refresh = Long.parseLong(strRefreshSec);
116             Thread JavaDoc t = new Thread JavaDoc ( new Runnable JavaDoc()
117                {
118                   public synchronized void run ()
119                   {
120                      long timeout = refresh*1000;
121                      while (true)
122                      {
123                         try
124                         {
125                            this.wait(timeout);
126                            treeBrowser.refreshTree(false);
127                         }
128                         catch (Exception JavaDoc displayed)
129                         {
130                            //displayed.printStackTrace();
131
}
132                      }
133                   }
134                }
135             );
136             
137             t.start();
138          }
139       }
140       catch (Exception JavaDoc displayed)
141       {
142          displayed.printStackTrace();
143       }
144    }
145
146    /** Allow the applet to be run as an application:
147     * java -cp applet.jar org.jboss.console.navtree.AppletBrowser
148     * @param args
149     * @throws Exception
150     */

151    public static void main(String JavaDoc[] args) throws Exception JavaDoc
152    {
153       JApplet applet = new AppletBrowser();
154       applet.setStub(new MainAppletStub());
155       JFrame frame = new JFrame("Administration Console");
156       frame.getContentPane().add(applet);
157       frame.setSize(600, 500);
158       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
159       applet.init();
160       applet.start();
161       frame.setVisible(true);
162    }
163
164    public class AppletAdminContext implements TreeContext
165    {
166       
167       String JavaDoc webHost = null;
168       String JavaDoc hostname = null;
169       
170       org.jboss.console.remote.SimpleRemoteMBeanInvoker invoker = null;
171       
172       public AppletAdminContext ()
173       {
174          //webHost = getCodeBase ().toString ();
175
java.net.URL JavaDoc root = getCodeBase();
176          webHost = root.getProtocol() + ":";
177          if (root.getAuthority() != null && root.getAuthority().length() > 0) {
178             webHost+="//";
179             webHost+=root.getAuthority();
180          }
181          
182          if (!webHost.endsWith ("/"))
183             webHost = webHost + "/";
184             
185          hostname = getCodeBase ().getHost ();
186       }
187       
188       public synchronized SimpleRemoteMBeanInvoker getRemoteMBeanInvoker ()
189       {
190          if (invoker == null)
191          {
192             System.out.println (getCodeBase().toString() + "Invoker");
193             try
194             {
195                invoker = new AppletRemoteMBeanInvoker (getCodeBase().toString() + "Invoker");
196             }
197             catch (Exception JavaDoc displayed)
198             {
199                displayed.printStackTrace ();
200             }
201          }
202          
203          return invoker;
204       }
205       
206       public void doAdminTreeAction (TreeAction action)
207       {
208          if (action != null && action instanceof HttpLinkTreeAction)
209          {
210             HttpLinkTreeAction act = (HttpLinkTreeAction)action;
211             openLink (act.getTarget (), act.getFrame());
212          }
213       }
214       
215       public void doPopupMenuAction (SimpleTreeNodeMenuEntry entry)
216       {
217       
218          TreeAction ta = entry.getAction ();
219
220          if (ta instanceof HttpLinkTreeAction)
221          {
222             HttpLinkTreeAction act = (HttpLinkTreeAction)ta;
223             openLink ( act.getTarget (), act.getFrame());
224          }
225          else if (ta instanceof AppletTreeAction)
226          {
227             ((AppletTreeAction)ta).doAction(ctx, AppletBrowser.this);
228          }
229       }
230       
231       public java.util.Properties JavaDoc getJndiProperties ()
232       {
233          Properties JavaDoc props = new Properties JavaDoc (); // to be improved? (to read from Applet properties and pass as parameter
234
props.setProperty ("java.naming.factory.initial", "org.jnp.interfaces.NamingContextFactory");
235          props.setProperty ("java.naming.factory.url.pkgs", "org.jboss.naming:org.jnp.interfaces");
236          props.setProperty ("java.naming.provider.url", hostname);
237          
238          return props;
239       }
240       
241       public String JavaDoc getServiceJmxName () { return pmJmxName; }
242          
243       public void openLink (String JavaDoc target, String JavaDoc frame)
244       {
245          try
246          {
247             if (target == null)
248             {
249                return;
250             }
251             else
252             {
253                System.out.println(target);
254                if (frame == null)
255                   getAppletContext ().showDocument ( new URL JavaDoc(localizeUrl(target)), RIGHT_FRAME_NAME);
256                else
257                   getAppletContext ().showDocument ( new URL JavaDoc(localizeUrl(target)), frame);
258             }
259          }
260          catch (Exception JavaDoc tobad) { tobad.printStackTrace (); }
261       }
262       
263       public String JavaDoc localizeUrl (String JavaDoc sourceUrl)
264       {
265          String JavaDoc target = sourceUrl;
266          
267          if (target == null)
268             return null;
269          
270          if (!target.toLowerCase ().startsWith ("http"))
271          {
272             if (target.startsWith ("/"))
273                target = target.substring (1);
274             target = webHost + target;
275             
276             if (target.indexOf("?") >= 0)
277                 target = target + "&" + sessionId;
278             else
279                 target = target + ";" + sessionId;
280          }
281          
282          return target;
283       }
284    
285
286
287    }
288 }
289
Popular Tags