KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > mountainminds > eclemma > internal > core > EclEmmaCorePlugin


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: EclEmmaCorePlugin.java 370 2007-07-18 11:14:27Z mtnminds $
7  ******************************************************************************/

8 package com.mountainminds.eclemma.internal.core;
9
10 import java.text.MessageFormat JavaDoc;
11 import java.util.Date JavaDoc;
12 import java.util.HashMap JavaDoc;
13 import java.util.Map JavaDoc;
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 /**
50  * Bundle activator for the EclEmma core.
51  *
52  * @author Marc R. Hoffmann
53  * @version $Revision: 370 $
54  */

55 public class EclEmmaCorePlugin extends Plugin {
56
57   public static final String JavaDoc ID = "com.mountainminds.eclemma.core"; //$NON-NLS-1$
58

59   /** Status used to trigger user prompts */
60   private static final IStatus PROMPT_STATUS = new Status(IStatus.INFO,
61       "org.eclipse.debug.ui", 200, "", null); //$NON-NLS-1$//$NON-NLS-2$
62

63   public static final IPath EMMA_JAR = new Path("/emma.jar"); //$NON-NLS-1$
64

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   // TODO synchronize access!
76
private Map JavaDoc 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 JavaDoc[] args = new Object JavaDoc[] {
103                   launch.getLaunchConfiguration().getName(), new Date JavaDoc() };
104               String JavaDoc 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 JavaDoc {
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 JavaDoc {
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   /**
185    * Issues an user prompt using the statis handler registered for the given
186    * status.
187    *
188    * @param status
189    * IStatus object to find prompter for
190    * @param info
191    * addional information passed to the handler
192    * @return boolean result returned by the status handler
193    * @throws CoreException
194    * if the status has severity error and no handler is available
195    */

196   public boolean showPrompt(IStatus status, Object JavaDoc 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 JavaDoc) prompter.handleStatus(status, info)).booleanValue();
207     }
208   }
209   
210   /**
211    * Tries to find the absolute path for the given workspace relative path.
212    *
213    * @param path
214    * workspace relative path to resolve
215    * @return absolute path
216    */

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   /**
228    * Calculates the list of IClassFiles for the given Java project. Basically
229    * for every separate output location a entry will be returned.
230    *
231    * @param project Java project to calculate IClassFiles for
232    * @return
233    * Array of IClassFiles objects
234    * @throws JavaModelException
235    * Thrown when a problem with the underlying Java model occures.
236    */

237   public synchronized IClassFiles[] getClassFiles(IJavaProject project) throws JavaModelException {
238     Map JavaDoc binpaths = new HashMap JavaDoc();
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 JavaDoc getClassFiles() throws CoreException {
254     // TODO use getClassFiles(IJavaProject)
255
if (instrumentedClasses == null) {
256       instrumentedClasses = new HashMap JavaDoc();
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 JavaDoc 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