KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > debugger > jpda > ant > JPDAReload


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.debugger.jpda.ant;
21
22 import java.io.File JavaDoc;
23 import java.io.FileNotFoundException JavaDoc;
24 import java.io.IOException JavaDoc;
25 import java.io.InputStream JavaDoc;
26 import java.util.ArrayList JavaDoc;
27 import java.util.HashMap JavaDoc;
28 import java.util.Iterator JavaDoc;
29 import java.util.List JavaDoc;
30 import java.util.Map JavaDoc;
31 import java.util.logging.Level JavaDoc;
32 import java.util.logging.Logger JavaDoc;
33
34 import org.apache.tools.ant.BuildException;
35 import org.apache.tools.ant.DirectoryScanner;
36 import org.apache.tools.ant.Project;
37 import org.apache.tools.ant.Task;
38 import org.apache.tools.ant.types.FileSet;
39 import org.apache.tools.ant.util.FileUtils;
40
41 import org.openide.filesystems.FileObject;
42 import org.openide.filesystems.FileStateInvalidException;
43 import org.openide.filesystems.FileUtil;
44
45 import org.netbeans.api.debugger.DebuggerEngine;
46 import org.netbeans.api.debugger.DebuggerManager;
47 import org.netbeans.api.debugger.jpda.JPDADebugger;
48 import org.netbeans.api.java.classpath.ClassPath;
49 import org.netbeans.api.java.queries.SourceForBinaryQuery;
50 import org.netbeans.spi.java.classpath.support.ClassPathSupport;
51 import org.netbeans.spi.debugger.jpda.EditorContext;
52
53
54 /**
55  * Ant task to reload classes in VM for running debugging session.
56  *
57  * @author David Konecny
58  */

59 public class JPDAReload extends Task {
60
61     private static final Logger JavaDoc logger = Logger.getLogger("org.netbeans.modules.debugger.jpda.ant"); // NOI18N
62

63     private List JavaDoc filesets = new ArrayList JavaDoc ();
64  
65     /**
66      * FileSet with .class files to reload. The base dir of the fileset is expected
67      * to be classpath root for these classes.
68      */

69     public void addFileset (FileSet fileset) {
70         filesets.add (fileset);
71     }
72     
73     public void execute() throws BuildException {
74         if (logger.isLoggable(Level.FINE)) {
75             logger.fine("JPDAReload.execute(), filesets = "+filesets);
76         }
77         if (filesets.size() == 0) {
78             throw new BuildException ("A nested fileset with class to refresh in VM must be specified.");
79         }
80         
81         // check debugger state
82
DebuggerEngine debuggerEngine = DebuggerManager.getDebuggerManager ().
83             getCurrentEngine ();
84         if (debuggerEngine == null) {
85             throw new BuildException ("No debugging sessions was found.");
86         }
87         JPDADebugger debugger = (JPDADebugger) debuggerEngine.lookupFirst
88             (null, JPDADebugger.class);
89         if (debugger == null) {
90             throw new BuildException("Current debugger is not JPDA one.");
91         }
92         if (!debugger.canFixClasses ()) {
93             throw new BuildException("The debugger does not support Fix action.");
94         }
95         if (debugger.getState () == JPDADebugger.STATE_DISCONNECTED) {
96             throw new BuildException ("The debugger is not running");
97         }
98         
99         System.out.println ("Classes to be reloaded:");
100         
101         FileUtils fileUtils = FileUtils.newFileUtils ();
102         Map JavaDoc map = new HashMap JavaDoc ();
103         EditorContext editorContext = (EditorContext) DebuggerManager.
104             getDebuggerManager ().lookupFirst (null, EditorContext.class);
105
106         Iterator JavaDoc it = filesets.iterator ();
107         while (it.hasNext ()) {
108             FileSet fs = (FileSet) it.next ();
109             DirectoryScanner ds = fs.getDirectoryScanner (getProject ());
110             String JavaDoc fileNames[] = ds.getIncludedFiles ();
111             File JavaDoc baseDir = fs.getDir (getProject ());
112             int i, k = fileNames.length;
113             for (i = 0; i < k; i++) {
114                 File JavaDoc f = fileUtils.resolveFile (baseDir, fileNames [i]);
115                 if (f != null) {
116                     FileObject fo = FileUtil.toFileObject(f);
117                     if (fo != null) {
118                         try {
119                             String JavaDoc url = classToSourceURL (fo);
120                             if (url != null)
121                                 editorContext.updateTimeStamp (debugger, url);
122                             InputStream JavaDoc is = fo.getInputStream ();
123                             long fileSize = fo.getSize ();
124                             byte[] bytecode = new byte [(int) fileSize];
125                             is.read (bytecode);
126                             // remove ".class" from and use dots for for separator
127
String JavaDoc className = fileNames [i].substring (
128                                     0,
129                                     fileNames [i].length () - 6
130                                 ).replace (File.separatorChar, '.');
131                             map.put (
132                                 className,
133                                 bytecode
134                             );
135                             System.out.println (" " + className);
136                         } catch (IOException JavaDoc ex) {
137                             ex.printStackTrace ();
138                         }
139                     }
140                 }
141             }
142         }
143         if (logger.isLoggable(Level.FINE)) {
144             logger.fine("Reloaded classes: "+map.keySet());
145         }
146         if (map.size () == 0) {
147             System.out.println (" No class to reload");
148             return;
149         }
150         String JavaDoc error = null;
151         try {
152             debugger.fixClasses (map);
153         } catch (UnsupportedOperationException JavaDoc uoex) {
154             error = "The virtual machine does not support this operation: "+uoex.getLocalizedMessage();
155         } catch (NoClassDefFoundError JavaDoc ncdfex) {
156             error = "The bytes don't correspond to the class type (the names don't match): "+ncdfex.getLocalizedMessage();
157         } catch (VerifyError JavaDoc ver) {
158             error = "A \"verifier\" detects that a class, though well formed, contains an internal inconsistency or security problem: "+ver.getLocalizedMessage();
159         } catch (UnsupportedClassVersionError JavaDoc ucver) {
160             error = "The major and minor version numbers in bytes are not supported by the VM. "+ucver.getLocalizedMessage();
161         } catch (ClassFormatError JavaDoc cfer) {
162             error = "The bytes do not represent a valid class. "+cfer.getLocalizedMessage();
163         } catch (ClassCircularityError JavaDoc ccer) {
164             error = "A circularity has been detected while initializing a class: "+ccer.getLocalizedMessage();
165         }
166         if (error != null) {
167             getProject().log(error, Project.MSG_ERR);
168             throw new BuildException(error);
169         }
170     }
171     
172     private String JavaDoc classToSourceURL (FileObject fo) {
173         try {
174             ClassPath cp = ClassPath.getClassPath (fo, ClassPath.EXECUTE);
175             FileObject root = cp.findOwnerRoot (fo);
176             String JavaDoc resourceName = cp.getResourceName (fo, '/', false);
177             int i = resourceName.indexOf ('$');
178             if (i > 0)
179                 resourceName = resourceName.substring (0, i);
180             FileObject[] sRoots = SourceForBinaryQuery.findSourceRoots
181                 (root.getURL ()).getRoots ();
182             ClassPath sourcePath = ClassPathSupport.createClassPath (sRoots);
183             FileObject rfo = sourcePath.findResource (resourceName + ".java");
184             if (rfo == null) return null;
185             return rfo.getURL ().toExternalForm ();
186         } catch (FileStateInvalidException ex) {
187             ex.printStackTrace ();
188             return null;
189         }
190     }
191 }
192
Popular Tags