1 8 package com.mountainminds.eclemma.internal.core; 9 10 import java.text.MessageFormat ; 11 import java.util.Date ; 12 import java.util.HashMap ; 13 import java.util.Map ; 14 15 import org.eclipse.core.resources.IResource; 16 import org.eclipse.core.resources.ResourcesPlugin; 17 import org.eclipse.core.runtime.CoreException; 18 import org.eclipse.core.runtime.IPath; 19 import org.eclipse.core.runtime.IStatus; 20 import org.eclipse.core.runtime.Path; 21 import org.eclipse.core.runtime.Plugin; 22 import org.eclipse.core.runtime.Status; 23 import org.eclipse.debug.core.DebugEvent; 24 import org.eclipse.debug.core.DebugPlugin; 25 import org.eclipse.debug.core.IDebugEventSetListener; 26 import org.eclipse.debug.core.ILaunch; 27 import org.eclipse.debug.core.ILaunchListener; 28 import org.eclipse.debug.core.IStatusHandler; 29 import org.eclipse.debug.core.model.IProcess; 30 import org.eclipse.jdt.core.ElementChangedEvent; 31 import org.eclipse.jdt.core.IClasspathEntry; 32 import org.eclipse.jdt.core.IElementChangedListener; 33 import org.eclipse.jdt.core.IJavaModel; 34 import org.eclipse.jdt.core.IJavaProject; 35 import org.eclipse.jdt.core.IPackageFragmentRoot; 36 import org.eclipse.jdt.core.JavaCore; 37 import org.eclipse.jdt.core.JavaModelException; 38 import org.osgi.framework.BundleContext; 39 40 import com.mountainminds.eclemma.core.CoverageTools; 41 import com.mountainminds.eclemma.core.EclEmmaStatus; 42 import com.mountainminds.eclemma.core.IClassFiles; 43 import com.mountainminds.eclemma.core.ICorePreferences; 44 import com.mountainminds.eclemma.core.ICoverageSession; 45 import com.mountainminds.eclemma.core.ISessionManager; 46 import com.mountainminds.eclemma.core.launching.ICoverageLaunchInfo; 47 import com.mountainminds.eclemma.internal.core.instr.ClassFiles; 48 49 55 public class EclEmmaCorePlugin extends Plugin { 56 57 public static final String ID = "com.mountainminds.eclemma.core"; 59 60 private static final IStatus PROMPT_STATUS = new Status(IStatus.INFO, 61 "org.eclipse.debug.ui", 200, "", null); 63 public static final IPath EMMA_JAR = new Path("/emma.jar"); 65 private static EclEmmaCorePlugin instance; 66 67 private ICorePreferences preferences = ICorePreferences.DEFAULT; 68 69 private ISessionManager sessionManager; 70 71 private JavaCoverageLoader coverageLoader; 72 73 private StateFiles stateFiles; 74 75 private Map instrumentedClasses = null; 77 78 private ILaunchListener launchListener = new ILaunchListener() { 79 public void launchRemoved(ILaunch launch) { 80 if (preferences.getAutoRemoveSessions()) { 81 sessionManager.removeSession(launch); 82 } 83 } 84 public void launchAdded(ILaunch launch) { 85 } 86 public void launchChanged(ILaunch launch) { 87 } 88 }; 89 90 private IDebugEventSetListener debugListener = new IDebugEventSetListener() { 91 public void handleDebugEvents(DebugEvent[] events) { 92 for (int i = 0; i < events.length; i++) { 93 DebugEvent e = events[i]; 94 if (e.getSource() instanceof IProcess 95 && e.getKind() == DebugEvent.TERMINATE) { 96 IProcess proc = (IProcess) e.getSource(); 97 final ILaunch launch = proc.getLaunch(); 98 ICoverageLaunchInfo info = CoverageTools.getLaunchInfo(launch); 99 if (info != null) { 100 IPath coveragedatafile = info.getCoverageFile(); 101 if (checkCoverageDataFile(coveragedatafile)) { 102 Object [] args = new Object [] { 103 launch.getLaunchConfiguration().getName(), new Date () }; 104 String description = MessageFormat.format( 105 CoreMessages.LaunchSessionDescription_value, args); 106 ICoverageSession session = new CoverageSession(description, info 107 .getInstrumentations(), new IPath[] { coveragedatafile }, 108 launch.getLaunchConfiguration()); 109 sessionManager.addSession(session, preferences.getActivateNewSessions(), launch); 110 } 111 info.dispose(); 112 } 113 } 114 } 115 } 116 private boolean checkCoverageDataFile(IPath path) { 117 boolean ok = path.toFile().exists(); 118 if (!ok) { 119 try { 120 showPrompt(EclEmmaStatus.NO_COVERAGE_DATA_ERROR.getStatus(), path); 121 } catch (CoreException e) { 122 getLog().log(e.getStatus()); 123 } 124 } 125 return ok; 126 } 127 }; 128 129 private IElementChangedListener elementListener = new IElementChangedListener() { 130 public void elementChanged(ElementChangedEvent event) { 131 synchronized (EclEmmaCorePlugin.this) { 132 instrumentedClasses = null; 133 } 134 } 135 }; 136 137 public void start(BundleContext context) throws Exception { 138 super.start(context); 139 sessionManager = new SessionManager(); 140 coverageLoader = new JavaCoverageLoader(sessionManager); 141 stateFiles = new StateFiles(getStateLocation()); 142 stateFiles.deleteTemporaryFiles(); 143 DebugPlugin.getDefault().getLaunchManager().addLaunchListener( 144 launchListener); 145 DebugPlugin.getDefault().addDebugEventListener(debugListener); 146 JavaCore.addElementChangedListener(elementListener); 147 instance = this; 148 } 149 150 public void stop(BundleContext context) throws Exception { 151 instance = null; 152 stateFiles.deleteTemporaryFiles(); 153 JavaCore.removeElementChangedListener(elementListener); 154 DebugPlugin.getDefault().removeDebugEventListener(debugListener); 155 DebugPlugin.getDefault().getLaunchManager().removeLaunchListener( 156 launchListener); 157 stateFiles = null; 158 coverageLoader.dispose(); 159 coverageLoader = null; 160 sessionManager = null; 161 super.stop(context); 162 } 163 164 public static EclEmmaCorePlugin getInstance() { 165 return instance; 166 } 167 168 public void setPreferences(ICorePreferences preferences) { 169 this.preferences = preferences; 170 } 171 172 public ISessionManager getSessionManager() { 173 return sessionManager; 174 } 175 176 public JavaCoverageLoader getJavaCoverageLoader() { 177 return coverageLoader; 178 } 179 180 public StateFiles getStateFiles() { 181 return stateFiles; 182 } 183 184 196 public boolean showPrompt(IStatus status, Object info) throws CoreException { 197 IStatusHandler prompter = DebugPlugin.getDefault().getStatusHandler( 198 PROMPT_STATUS); 199 if (prompter == null) { 200 if (status.getSeverity() == IStatus.ERROR) { 201 throw new CoreException(status); 202 } else { 203 return true; 204 } 205 } else { 206 return ((Boolean ) prompter.handleStatus(status, info)).booleanValue(); 207 } 208 } 209 210 217 public static IPath getAbsolutePath(IPath path) { 218 if (path.getDevice() == null) { 219 IResource res = ResourcesPlugin.getWorkspace().getRoot().findMember(path); 220 if (res != null) { 221 return res.getLocation(); 222 } 223 } 224 return path; 225 } 226 227 237 public synchronized IClassFiles[] getClassFiles(IJavaProject project) throws JavaModelException { 238 Map binpaths = new HashMap (); 239 IPackageFragmentRoot[] roots = project.getPackageFragmentRoots(); 240 for (int j = 0; j < roots.length; j++) { 241 IPath binpath = getClassFileLocation(roots[j]); 242 ClassFiles classfiles = (ClassFiles) binpaths.get(binpath); 243 if (classfiles == null) { 244 IPackageFragmentRoot[] element = new IPackageFragmentRoot[] { roots[j] }; 245 binpaths.put(binpath, new ClassFiles(element, binpath)); 246 } else { 247 binpaths.put(binpath, classfiles.addRoot(roots[j])); 248 } 249 } 250 return (IClassFiles[]) binpaths.values().toArray(new IClassFiles[0]); 251 } 252 253 public synchronized Map getClassFiles() throws CoreException { 254 if (instrumentedClasses == null) { 256 instrumentedClasses = new HashMap (); 257 IJavaModel model = JavaCore.create(ResourcesPlugin.getWorkspace() 258 .getRoot()); 259 IJavaProject[] projects = model.getJavaProjects(); 260 for (int i = 0; i < projects.length; i++) { 261 { 262 IPackageFragmentRoot[] roots = projects[i].getPackageFragmentRoots(); 263 for (int j = 0; j < roots.length; j++) { 264 IPath location = getClassFileLocation(roots[j]); 265 String ospath = getAbsolutePath(location).toOSString(); 266 ClassFiles classfiles = (ClassFiles) instrumentedClasses 267 .get(ospath); 268 if (classfiles == null) { 269 instrumentedClasses.put(ospath, new ClassFiles( 270 new IPackageFragmentRoot[] { roots[j] }, location)); 271 } else { 272 instrumentedClasses.put(ospath, classfiles.addRoot(roots[j])); 273 } 274 } 275 } 276 } 277 } 278 return instrumentedClasses; 279 } 280 281 private static IPath getClassFileLocation(IPackageFragmentRoot root) 282 throws JavaModelException { 283 IPath path; 284 if (root.getKind() == IPackageFragmentRoot.K_SOURCE) { 285 IClasspathEntry entry = root.getRawClasspathEntry(); 286 path = entry.getOutputLocation(); 287 if (path == null) { 288 path = root.getJavaProject().getOutputLocation(); 289 } 290 } else { 291 path = root.getPath(); 292 } 293 return path; 294 } 295 296 } 297 | Popular Tags |