001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.insane.model;
043:
044: import java.io.*;
045: import java.util.*;
046:
047: /**
048: * A support class containing HeapModel factories and helper methods
049: * for analysing HeapModels.
050: *
051: * @author Nenik
052: */
053: public final class Support {
054:
055: private Support() {
056: }
057:
058: /**
059: * Parses a XML dump of the heap created by the SimpleXmlVisitor.
060: *
061: * @param file the file with the heap dump.
062: * @return The HeapModel representing the heap dump.
063: * @throws Exception if there is any error during processing the dump.
064: */
065: public static HeapModel parseSimpleXMLDump(File file)
066: throws Exception {
067: return XmlHeapModel.parse(file);
068: }
069:
070: /**
071: * Opens a binary dump of the heap created by conversion from the XML
072: * dump produced by SimpleXmlVisitor.
073: *
074: * @param file the file with the heap dump.
075: * @return The HeapModel representing the heap dump.
076: * @throws Exception if there is any error during processing the dump.
077: */
078: public static HeapModel openSimpleBinaryDump(File file)
079: throws Exception {
080: return BinaryHeapModel.open(file);
081: }
082:
083: /*
084: * Converts the XML dump format to throws ebinary file format.
085: */
086: public static void convertSimpleDump(File from, File to)
087: throws Exception {
088: InsaneConverter.convert(from, to);
089: }
090:
091: /*
092: */
093: public static void findRoots(HeapModel model, Item itm, boolean weak) {
094: LinkedList<PathElement> queue = new LinkedList<PathElement>();
095: queue.add(new PathElement(itm, null));
096: Set<Object> visited = new HashSet<Object>(queue);
097: while (!queue.isEmpty()) {
098: PathElement act = queue.remove(0);
099: Enumeration en = act.getItem().incomming();
100: while (en.hasMoreElements()) {
101: Object o = en.nextElement();
102: if (o instanceof String) {
103: System.out.println(o + "->\n" + act);
104: return;
105: } else { // add real support for weak references to the model.
106: Item ref = (Item) o;
107: if (!weak
108: && ("java.lang.ref.WeakReference"
109: .equals(ref.getType())
110: || "javax.swing.AbstractActionPropertyChangeListener$OwnedWeakReference"
111: .equals(ref.getType())
112: || "org.openide.util.WeakListener$ListenerReference"
113: .equals(ref.getType())
114: || "org.openide.util.WeakListenerImpl$ListenerReference"
115: .equals(ref.getType())
116: || "org.openide.util.WeakSet$Entry"
117: .equals(ref.getType())
118: || "java.lang.ref.SoftReference"
119: .equals(ref.getType())
120: ||
121: // "sun.misc.Cleaner".equals(ref.getType()) ||
122: "org.netbeans.modules.javacore.classpath.MergedClassPathImplementation$ClassPathMap$WeakPair"
123: .equals(ref.getType())
124: || "java.util.WeakHashMap$Entry"
125: .equals(ref.getType())
126: || "org.netbeans.modules.project.ant.FileChangeSupport$Holder"
127: .equals(ref.getType())
128: | "org.netbeans.api.nodes2looks.LookNode$FirerImpl"
129: .equals(ref.getType())
130: || "org.openide.loaders.DataObjectPool$ItemReference"
131: .equals(ref.getType())
132: || "org.netbeans.mdr.NBMDRepositoryImpl$FacilityCache$HandlerReference"
133: .equals(ref.getType())
134: || "org.netbeans.mdr.storagemodel.MdrStorage$InstanceMap$InstanceReference"
135: .equals(ref.getType()) || "org.openide.util.IconManager$ActiveRef"
136: .equals(ref.getType()))) {
137: // skip
138: } else {
139: // add to the queue if not new
140: if (visited.add(ref))
141: queue.add(new PathElement(ref, act));
142: }
143: }
144: }
145: }
146: }
147:
148: private static class PathElement {
149: private Item item;
150: private PathElement next;
151:
152: public PathElement(Item item, PathElement next) {
153: this .item = item;
154: this .next = next;
155: }
156:
157: public Item getItem() {
158: return item;
159: }
160:
161: public String toString() {
162: if (next == null) {
163: return item.toString();
164: } else {
165: return item.toString() + "->\n" + next.toString();
166: }
167: }
168: }
169:
170: }
|