KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > mountainminds > eclemma > internal > core > launching > CoverageLaunchInfo


1 /*******************************************************************************
2  * Copyright (c) 2006 Mountainminds GmbH & Co. KG
3  * This software is provided under the terms of the Eclipse Public License v1.0
4  * See http://www.eclipse.org/legal/epl-v10.html.
5  *
6  * $Id: CoverageLaunchInfo.java 399 2007-08-30 19:12:38Z mtnminds $
7  ******************************************************************************/

8 package com.mountainminds.eclemma.internal.core.launching;
9
10 import java.util.ArrayList JavaDoc;
11 import java.util.HashMap JavaDoc;
12 import java.util.List JavaDoc;
13 import java.util.Map JavaDoc;
14 import java.util.WeakHashMap JavaDoc;
15
16 import org.eclipse.core.runtime.CoreException;
17 import org.eclipse.core.runtime.IPath;
18 import org.eclipse.core.runtime.IProgressMonitor;
19 import org.eclipse.core.runtime.SubProgressMonitor;
20 import org.eclipse.debug.core.ILaunch;
21 import org.eclipse.debug.core.ILaunchConfiguration;
22 import org.eclipse.osgi.util.NLS;
23
24 import com.mountainminds.eclemma.core.CoverageTools;
25 import com.mountainminds.eclemma.core.IClassFiles;
26 import com.mountainminds.eclemma.core.IInstrumentation;
27 import com.mountainminds.eclemma.core.launching.ICoverageLaunchInfo;
28 import com.mountainminds.eclemma.internal.core.CoreMessages;
29 import com.mountainminds.eclemma.internal.core.EclEmmaCorePlugin;
30 import com.mountainminds.eclemma.internal.core.StateFiles;
31
32 /**
33  * Implementation of {@link ICoverageLaunchInfo}.
34  *
35  * @author Marc R. Hoffmann
36  * @version $Revision: 399 $
37  */

38 public class CoverageLaunchInfo implements ICoverageLaunchInfo {
39
40   private static int idcounter = (int) System.currentTimeMillis();
41   private static final Map JavaDoc instances = new WeakHashMap JavaDoc();
42
43   private final String JavaDoc id;
44   private final ILaunchConfiguration configuration;
45   private IPath coveragefile;
46   private IPath propertiesjarfile;
47   private final List JavaDoc instrumentations;
48   private final Map JavaDoc instrumentationpaths;
49
50   public CoverageLaunchInfo(ILaunch launch) {
51       id = Integer.toHexString(idcounter++);
52       instances.put(id, this);
53       configuration = launch.getLaunchConfiguration();
54       StateFiles statefiles = EclEmmaCorePlugin.getInstance().getStateFiles();
55       IPath base = statefiles.getLaunchDataFolder().append(id);
56       coveragefile = base.addFileExtension("ec"); //$NON-NLS-1$
57
statefiles.registerForCleanup(coveragefile);
58       propertiesjarfile = base.addFileExtension("jar"); //$NON-NLS-1$
59
statefiles.registerForCleanup(propertiesjarfile);
60       instrumentations = new ArrayList JavaDoc();
61       instrumentationpaths = new HashMap JavaDoc();
62       instances.put(launch, this);
63   }
64
65   /**
66    * Returns the coverage launch info that is assoziated with the given launch.
67    * If no info object is assoziated with the given launch <code>null</code>
68    * is returned.
69    *
70    * @param launch
71    * the launch object we need coverage data for
72    * @return the info data object or <code>null</code>
73    */

74   public static ICoverageLaunchInfo getInfo(ILaunch launch) {
75     return (ICoverageLaunchInfo) instances.get(launch);
76   }
77
78   // ICoverageLaunchInfo interface
79

80   public IPath getCoverageFile() {
81     return coveragefile;
82   }
83
84   public IPath getPropertiesJARFile() {
85     return propertiesjarfile;
86   }
87
88   public void instrument(IProgressMonitor monitor, boolean inplace) throws CoreException {
89     instrumentations.clear();
90     instrumentationpaths.clear();
91     IClassFiles[] classfiles = CoverageTools.getClassFilesForInstrumentation(
92         configuration, inplace);
93     monitor.beginTask(CoreMessages.InstrumentingClasses_task,
94         classfiles.length);
95     for (int i = 0; i < classfiles.length; i++) {
96       if (monitor.isCanceled()) {
97         return;
98       }
99       monitor.subTask(NLS.bind(CoreMessages.InstrumentingClassesIn_task, classfiles[i].getLocation()));
100       addInstrumentation(classfiles[i].instrument(inplace, new SubProgressMonitor(monitor, 1)));
101     }
102     monitor.done();
103   }
104
105   private void addInstrumentation(IInstrumentation instrumentation) {
106     instrumentations.add(instrumentation);
107     IPath orig = EclEmmaCorePlugin.getAbsolutePath(instrumentation.getClassFiles().getLocation());
108     instrumentationpaths.put(orig.toOSString(), instrumentation);
109   }
110
111   public IInstrumentation[] getInstrumentations() {
112     IInstrumentation[] a = new IInstrumentation[instrumentations.size()];
113     return (IInstrumentation[]) instrumentations.toArray(a);
114   }
115
116   public IInstrumentation getInstrumentation(String JavaDoc originalpath) {
117     return (IInstrumentation) instrumentationpaths.get(originalpath);
118   }
119
120   public void dispose() {
121     // TODO check why this is still necessary, someone seems to hold a reference
122
// to the launch objects.
123
coveragefile = null;
124     propertiesjarfile = null;
125   }
126
127 }
128
Popular Tags