|
What this is
Other links
The source code/* * Sun Public License Notice * * The contents of this file are subject to the Sun Public License * Version 1.0 (the "License"). You may not use this file except in * compliance with the License. A copy of the License is available at * http://www.sun.com/ * * The Original Code is NetBeans. The Initial Developer of the Original * Code is Sun Microsystems, Inc. Portions Copyright 1997-2004 Sun * Microsystems, Inc. All Rights Reserved. */ /** * Controller.java * * * Created: Fri Jan 19 16:21:06 2001 * * @author Ana von Klopp */ package org.netbeans.modules.web.monitor.client; import java.util.Comparator; import java.util.Enumeration; import java.util.Hashtable; import java.util.Vector; import java.io.File; import java.io.FileNotFoundException; import java.io.InputStreamReader; import java.io.IOException; import java.io.OutputStream; import java.io.PrintWriter; import java.io.StringWriter; import java.net.InetAddress; import java.net.MalformedURLException; import java.net.Socket; import java.net.UnknownHostException; import java.net.URL; import java.text.MessageFormat; import java.beans.PropertyChangeListener; import java.beans.PropertyChangeEvent; import javax.swing.SwingUtilities; import org.openide.DialogDisplayer; import org.openide.ErrorManager; import org.openide.NotifyDescriptor; import org.openide.awt.HtmlBrowser; import org.openide.cookies.InstanceCookie; import org.openide.loaders.DataFolder; import org.openide.loaders.DataObject; import org.openide.loaders.DataObjectNotFoundException; import org.openide.filesystems.FileAlreadyLockedException; import org.openide.filesystems.FileObject; import org.openide.filesystems.FileLock; import org.openide.filesystems.FileUtil; import org.openide.filesystems.FileSystem; import org.openide.filesystems.Repository; import org.openide.filesystems.URLMapper; import org.openide.nodes.Node; import org.openide.nodes.AbstractNode; import org.openide.nodes.Children; import org.openide.nodes.Children.SortedArray; import org.openide.options.SystemOption; import org.openide.util.Lookup; import org.openide.util.NbBundle; import org.netbeans.modules.web.monitor.server.Constants; import org.netbeans.modules.web.monitor.data.*; class Controller { // REPLAY strings - must be coordinated with server.MonitorFilter final static String REPLAY="netbeans.replay"; //NOI18N final static String PORT="netbeans.replay.port"; //NOI18N final static String REPLAYSTATUS="netbeans.replay.status"; //NOI18N final static String REPLAYSESSION="netbeans.replay.session"; //NOI18N static final boolean debug = false; //private transient static boolean starting = true; // Test server location and port // Should use InetAddress.getLocalhost() instead private transient static String server = "localhost"; //NOI18N private transient static int port = 8080; // Location of the files private static FileObject monDir = null; private static FileObject currDir = null; private static FileObject saveDir = null; private static FileObject replayDir = null; final static String monDirStr = "HTTPMonitor"; // NOI18N final static String currDirStr = "current"; // NOI18N final static String saveDirStr = "save"; // NOI18N final static String replayDirStr = "replay"; // NOI18N // Constant nodes etc we need to know about private transient NavigateNode root = null; private Children.SortedArray currTrans = null; private Children.SortedArray savedTrans = null; // These are the ones that should go. private Hashtable currBeans = null; private Hashtable saveBeans = null; private transient Comparator comp = null; /* private HtmlBrowser.BrowserComponent browser = null; private SettingsListener browserListener = null; private SystemOption settings = null; */ private boolean useBrowserCookie = true; private static Controller instance = null; private Controller() { currBeans = new Hashtable(); saveBeans = new Hashtable(); createNodeStructure(); /* registerBrowserListener(); */ } static Controller getInstance() { if(instance == null) instance = new Controller(); return instance; } /** * Invoked at startup, creates the root folder and the folder for * current and saved transactions (and their children arrays). */ private void createNodeStructure() { comp = new CompTime(true); currTrans = new Children.SortedArray(); currTrans.setComparator(comp); savedTrans = new Children.SortedArray(); savedTrans.setComparator(comp); CurrNode currNode = new CurrNode(currTrans); SavedNode savedNode = new SavedNode(savedTrans); Node[] kids = new Node[2]; kids[0] = currNode; kids[1] = savedNode; Children children = new Children.Array(); children.add(kids); root = new NavigateNode(children); } static void removeFiles() { if(instance == null) return; instance.cleanup(); } void cleanup() { deleteDirectory(currDirStr); /* removeBrowserListener(); */ } /** * Adds a transaction to the list of current transactions. */ void addTransaction(String id) { if(debug) log("Creating node for " + id); TransactionNode[] nodes = new TransactionNode[1]; MonitorData md = retrieveMonitorData(id, currDirStr); try { nodes[0] = createTransactionNode(md, true); currTrans.add(nodes); } catch(Exception ex) { // If there is some kind of parsing exception, do nothing } } /** * Adds a transaction to the list of current transactions. */ protected NavigateNode getRoot() { return root; } protected static FileObject getMonDir() throws FileNotFoundException { if(monDir == null || !monDir.isFolder()) { try { createDirectories(); } catch(FileNotFoundException ex) { throw ex; } } return monDir; } protected static FileObject getCurrDir() throws FileNotFoundException { if(currDir == null || !currDir.isFolder()) { try{ createDirectories(); } catch(FileNotFoundException ex) { throw ex; } } return currDir; } protected static FileObject getReplayDir() throws FileNotFoundException { if(replayDir == null || !replayDir.isFolder()) { try{ createDirectories(); } catch(FileNotFoundException ex) { throw ex; } } return replayDir; } protected static FileObject getSaveDir() throws FileNotFoundException { if(saveDir == null || !saveDir.isFolder()) { try{ createDirectories(); } catch(FileNotFoundException ex) { throw ex; } } return saveDir; } boolean haveDirectories() { if(currDir == null) { try { currDir = getCurrDir(); } catch(Exception ex) { return false; } } if(saveDir == null) { try { saveDir = getSaveDir(); } catch(Exception ex) { return false; } } return true; } private static void createDirectories() throws FileNotFoundException { if(debug) log("Now in createDirectories()"); // NOI18N FileObject rootdir = Repository.getDefault().getDefaultFileSystem().getRoot(); if(debug) { log("Root directory is " + rootdir.getName()); // NOI18N File rootF = FileUtil.toFile(rootdir); log("Root directory abs path " + // NOI18N rootF.getAbsolutePath()); } FileLock lock = null; if(monDir == null || !monDir.isFolder()) { try { monDir = rootdir.getFileObject(monDirStr); } catch(Exception ex) { } if(monDir == null || !monDir.isFolder()) { if(monDir != null) { try { lock = monDir.lock(); monDir.delete(lock); } catch(FileAlreadyLockedException falex) { throw new FileNotFoundException(); } catch(IOException ex) { throw new FileNotFoundException(); } finally { if(lock != null) lock.releaseLock(); } } try { monDir = rootdir.createFolder(monDirStr); } catch(IOException ioex) { if(debug) ioex.printStackTrace(); } } if(monDir == null || !monDir.isFolder()) throw new FileNotFoundException(); } if(debug) log("monitor directory is " + monDir.getName());// NOI18N // Current directory if(currDir == null || !currDir.isFolder()) { try { currDir = monDir.getFileObject(currDirStr); } catch(Exception ex) { } if(currDir == null || !currDir.isFolder()) { lock = null; if(currDir != null) { try { lock = currDir.lock(); currDir.delete(lock); } catch(FileAlreadyLockedException falex) { throw new FileNotFoundException(); } catch(IOException ex) { throw new FileNotFoundException(); } finally { if(lock != null) lock.releaseLock(); } } try { currDir = monDir.createFolder(currDirStr); } catch(IOException ex) { if(debug) ex.printStackTrace(); } } if(currDir == null || !currDir.isFolder()) throw new FileNotFoundException(); } if(debug) log("curr directory is " + currDir.getName()); // NOI18N // Save Directory if(saveDir == null || !saveDir.isFolder()) { try { saveDir = monDir.getFileObject(saveDirStr); } catch(Exception ex) { } if(saveDir == null || !saveDir.isFolder()) { if(saveDir != null) { lock = null; try { lock = saveDir.lock(); saveDir.delete(lock); } catch(FileAlreadyLockedException falex) { throw new FileNotFoundException(); } catch(IOException ex) { throw new FileNotFoundException(); } finally { if(lock != null) lock.releaseLock(); } } try { saveDir = monDir.createFolder(saveDirStr); } catch(IOException ex) { if(debug) ex.printStackTrace(); } } if(saveDir == null || !saveDir.isFolder()) throw new FileNotFoundException(); if(debug) log("save directory is " + saveDir.getName()); // NOI18N } // Replay Directory if(replayDir == null || !replayDir.isFolder()) { try { replayDir = monDir.getFileObject(replayDirStr); } catch(Exception ex) { } if(replayDir == null || !replayDir.isFolder()) { if(replayDir != null) { lock = null; try { lock = replayDir.lock(); replayDir.delete(lock); } catch(FileAlreadyLockedException falex) { throw new FileNotFoundException(); } catch(IOException ex) { throw new FileNotFoundException(); } finally { if(lock != null) lock.releaseLock(); } } try { replayDir = monDir.createFolder(replayDirStr); } catch(Exception ex) { if(debug) ex.printStackTrace(); } } if(replayDir == null || !replayDir.isFolder()) throw new FileNotFoundException(); if(debug) log("replay directory is " + replayDir.getName());// NOI18N } } /** * Invoked by ReplayAction. Replays the transaction corresponding to * the selected node. * * PENDING - it would be better if the nodes know which server * they were processed on. This would be the case if we listed the * nodes separately depending on the server that collected the * data. * */ void replayTransaction(Node node) { if(debug) log("replayTransaction(Node node) from node " + node.getName()); // NOI18N if(!checkServer(true)) return; TransactionNode tn = null; try { tn = (TransactionNode)node; } catch(ClassCastException cce) { if(debug) log("Selected node was not a transaction node");//NOI18N return; } MonitorData md = getMonitorData(tn, false, false); if(!useBrowserCookie) md.getRequestData().setReplaceSessionCookie(true); if(debug) { log("Replace is " + // NOI18N String.valueOf(md.getRequestData().getReplaceSessionCookie())); String fname = md.createTempFile("control-replay.xml"); // NOI18N log("Wrote replay data to " + fname);// NOI18N } String status; if(tn.isCurrent()) status = currDirStr; else status = saveDirStr; try { replayTransaction(md, status); } catch(UnknownHostException uhe) { // Notify the user that there is no host Object[] options = { NbBundle.getBundle(Controller.class).getString("MON_OK"), }; Object[] args = { md.getServerName(), }; MessageFormat msgFormat = new MessageFormat (NbBundle.getBundle(Controller.class).getString("MON_Exec_server_no_host")); NotifyDescriptor noServerDialog = new NotifyDescriptor(msgFormat.format(args), NbBundle.getBundle(Controller.class).getString("MON_Exec_server"), NotifyDescriptor.DEFAULT_OPTION, NotifyDescriptor.INFORMATION_MESSAGE, options, options[0]); DialogDisplayer.getDefault().notify(noServerDialog); } catch(IOException ioe) { if(debug) { log(ioe.getMessage()); ioe.printStackTrace(); } // Notify the user that the server is not running Object[] options = { NbBundle.getBundle(Controller.class).getString("MON_OK"), }; Object[] args = { md.getServerAndPort(), }; MessageFormat msgFormat = new MessageFormat (NbBundle.getBundle(Controller.class).getString("MON_Exec_server_start")); NotifyDescriptor noServerDialog = new NotifyDescriptor(msgFormat.format(args), NbBundle.getBundle(Controller.class).getString("MON_Exec_server"), NotifyDescriptor.DEFAULT_OPTION, NotifyDescriptor.INFORMATION_MESSAGE, options, options[0]); DialogDisplayer.getDefault().notify(noServerDialog); } } /** * Invoked by EditPanel. Replays the transaction corresponding to * the selected node. */ void replayTransaction(MonitorData md) throws UnknownHostException, IOException { // PENDING - can't make UI changes right now for Sierra // any exception thrown in this method indicates that we // couldn't even get to the monitor data, and we should add an // additional panel to that effect. Also, unreadable monitor // data should cause the transaction to be removed from the // pane. if(debug) log("replayTransaction(MonitorData md)"); //NOI18N FileObject fo; FileLock lock = null; OutputStream out = null; PrintWriter pw = null; if(debug) log("Creating record for replay"); //NOI18N String id = md.getAttributeValue("id"); // NOI18N try { // This will fail if the file already exists. This can // happen if the replay previously failed because the // server was not running. This is dealt with in the // catch clause below. fo = getReplayDir().createData(id, "xml"); //NOI18N if(debug) log(" Created file for replay data"); } catch(IOException ioex) { try { fo = getReplayDir().getFileObject(id, "xml"); } catch(IllegalArgumentException iaex) { // This is only thrown if getReplayDir() is not a // folder. This should not happen. throw new IOException("No replay dir"); } if(!fo.isData()) { throw new IOException("Can't create file, giving up"); } try { lock = fo.lock(); } catch(FileAlreadyLockedException falex) { throw new IOException("Old file exist, islocked"); } try { fo.delete(lock); } catch(IOException ioex2) { throw new IOException("Couldn't delete old file"); } finally { if(lock != null) lock.releaseLock(); } try { fo = getReplayDir().createData(id, "xml"); //NOI18N } catch(IOException ioex2) { if(debug) log(" Couldn't create file for replay data"); throw ioex2; } } try { lock = fo.lock(); } catch(FileAlreadyLockedException fale) { if(debug) log("Can't get a file lock for the replay file"); throw new IOException(); } try { out = fo.getOutputStream(lock); pw = new PrintWriter(out); md.write(pw); if(debug) log("...record complete"); //NOI18N if(debug) { String fname = md.createTempFile("control-record.xml"); // NOI18N log("Wrote replay data to " + fname); // NOI18N } } catch(IOException ioex) { throw ioex; } finally { if(lock != null) lock.releaseLock(); try { pw.close(); } catch(Throwable t) { } try { out.close(); } catch(Throwable t) { } } try { replayTransaction(md, replayDirStr); } catch(UnknownHostException uhe) { throw uhe; } catch(IOException ioe) { throw ioe; } } /** * */ void replayTransaction(MonitorData md, String status) throws UnknownHostException, IOException { if(debug) log("replayTransaction(MonitorData md, String status )"); //NOI18N URL url = null; try { String name = md.getServerName(); int port = md.getServerPort(); StringBuffer uriBuf = new StringBuffer(128); uriBuf.append(md.getRequestData().getAttributeValue("uri")); //NOI18N uriBuf.append("?"); //NOI18N uriBuf.append(REPLAY); uriBuf.append("="); //NOI18N uriBuf.append(md.getAttributeValue("id")); //NOI18N uriBuf.append("&"); //NOI18N uriBuf.append(REPLAYSTATUS); uriBuf.append("="); //NOI18N uriBuf.append(status); String portS = null; try { FileObject fo = Repository.getDefault().getDefaultFileSystem().getRoot(); URL u = getSampleHTTPServerURL(); portS = String.valueOf(u.getPort()/*HttpServer.getRepositoryRoot().getPort()*/); } catch(Exception ex) { // No internal HTTP server, do nothing } if(portS != null) { uriBuf.append("&"); //NOI18N uriBuf.append(PORT); uriBuf.append("="); //NOI18N uriBuf.append(portS); } if(md.getRequestData().getReplaceSessionCookie()) { uriBuf.append("&"); //NOI18N uriBuf.append(REPLAYSESSION); uriBuf.append("="); //NOI18N uriBuf.append(md.getRequestData().getSessionID()); } url = new URL("http", name, port, uriBuf.toString()); //NOI18N } catch(MalformedURLException me) { if(debug) log(me.getMessage()); } catch(NumberFormatException ne) { if(debug) log(ne.getMessage()); } // Send the url to the browser. try { showReplay(url); } catch(UnknownHostException uhe) { throw uhe; } catch(IOException ioe) { throw ioe; } } void saveTransaction(Node[] nodes) { if(!haveDirectories()) { // PENDING - report the error properly // This should not happen log("Couldn't get the directory"); //NOI18N return; } Node[] newNodes = new Node[nodes.length]; TransactionNode mvNode; String id; boolean error = false; for(int i=0; i < nodes.length; ++i) { mvNode = (TransactionNode)nodes[i]; id = mvNode.getID(); if(debug) log(" Saving " + id); //NOI18N if(currBeans.containsKey(id)) saveBeans.put(id, currBeans.remove(id)); // Note I didn't load the bean here yet. Will only do that // if the data is displayed. FileLock lock = null; try { FileObject fold = currDir.getFileObject(id, "xml"); //NOI18N lock = fold.lock(); fold.copy(saveDir, id, "xml"); //NOI18N if(debug) log(fold.getName()); fold.delete(lock); mvNode.setCurrent(false); newNodes[i] = mvNode; } catch(FileAlreadyLockedException falex) { error = true; // PENDING report properly } catch(IOException ioex) { error = true; // PENDING report properly } catch(Exception ex) { error = true; // PENDING report properly } finally { if(lock != null) lock.releaseLock(); } } if(!error) currTrans.remove(nodes); savedTrans.add(newNodes); } /** * Invoked by DeleteAction. Deletes a saved transaction */ void deleteTransaction(Node[] nodes) { if(!haveDirectories()) { // PENDING - report the error property // This should not happen log("Couldn't get the directory"); //NOI18N return; } // PENDING if((nodes == null) || (nodes.length == 0)) return; TransactionNode n = null; for(int i=0; i < nodes.length; ++i) { n = (TransactionNode)nodes[i]; if(debug) log("Deleting :" + n.toString()); //NOI18N if(n.isCurrent()) delete(n, currTrans, currBeans, true); else delete(n, savedTrans, saveBeans, false); } } private void delete(TransactionNode node, Children.SortedArray transactions, Hashtable beans, boolean current) { FileObject fold = null; FileLock lock = null; try { if(current) fold = currDir.getFileObject(node.getID(), "xml"); //NOI18N else fold = saveDir.getFileObject(node.getID(), "xml"); //NOI18N lock = fold.lock(); if(debug) log("Deleting: " + fold.getName()); //NOI18N fold.delete(lock); // We only do this if we could delete the file. Node[] nodes = { node }; transactions.remove(nodes); beans.remove(node.getID()); } catch(FileAlreadyLockedException ex) { // PENDING report properly if(debug) log("Couldn't lock file:" + node.getID()); //NOI18N } catch(IOException ex) { // PENDING report properly if(debug) log("Couldn't delete file:" + node.getID()); //NOI18N } finally { if(lock != null) lock.releaseLock(); } } void deleteDirectory(String dir) { if(!haveDirectories()) { // PENDING - report the error property // This should not happen log("Couldn't get the directory"); //NOI18N return; } FileObject directory = null; if(dir.equals(saveDirStr)) { directory = saveDir; savedTrans.remove(savedTrans.getNodes()); saveBeans.clear(); } else { directory = currDir; currTrans.remove(currTrans.getNodes()); currBeans.clear(); } FileLock lock = null; Enumeration e = directory.getData(false); while(e.hasMoreElements()) { FileObject fo = (FileObject) e.nextElement(); lock = null; try { lock = fo.lock(); fo.delete(lock); } catch(FileAlreadyLockedException falex) { // PENDING report properly } catch(IOException IOex) { // PENDING report properly } finally { if(lock != null) lock.releaseLock(); } } } void deleteTransactions() { deleteDirectory(Constants.Files.save); deleteDirectory(Constants.Files.current); savedTrans.remove(savedTrans.getNodes()); currTrans.remove(currTrans.getNodes()); } void getTransactions() { if(debug) log("getTransactions"); //NOI18N if(!haveDirectories()) { // PENDING - report the error property // This should not happen log("Couldn't get the directory"); //NOI18N return; } Enumeration e = null; Vector nodes = new Vector(); int numtns = 0; TransactionNode[] tns = null; FileObject fo = null; String id = null; MonitorData md = null; currTrans.remove(currTrans.getNodes()); if(debug) log("getTransactions removed old nodes"); //NOI18N e = currDir.getData(false); while(e.hasMoreElements()) { fo = (FileObject)e.nextElement(); id = fo.getName(); if(debug) log("getting current transaction: " + id); //NOI18N // Retrieve the monitordata md = retrieveMonitorData(id, currDir); nodes.add(createTransactionNode(md, true)); } numtns = nodes.size(); tns = new TransactionNode[numtns]; for(int i=0;i |
... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.