KickJava   Java API By Example, From Geeks To Geeks.

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


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: SessionImporter.java 89 2006-09-18 09:35:10Z mho $
7  ******************************************************************************/

8 package com.mountainminds.eclemma.internal.core;
9
10 import java.io.File JavaDoc;
11 import java.io.FileInputStream JavaDoc;
12 import java.io.FileOutputStream JavaDoc;
13 import java.io.IOException JavaDoc;
14 import java.io.InputStream JavaDoc;
15 import java.io.OutputStream JavaDoc;
16
17 import org.eclipse.core.runtime.CoreException;
18 import org.eclipse.core.runtime.IPath;
19 import org.eclipse.core.runtime.IProgressMonitor;
20 import org.eclipse.core.runtime.Path;
21 import org.eclipse.core.runtime.SubProgressMonitor;
22
23 import com.mountainminds.eclemma.core.CoverageTools;
24 import com.mountainminds.eclemma.core.EclEmmaStatus;
25 import com.mountainminds.eclemma.core.IClassFiles;
26 import com.mountainminds.eclemma.core.ICoverageSession;
27 import com.mountainminds.eclemma.core.IInstrumentation;
28 import com.mountainminds.eclemma.core.ISessionImporter;
29
30 /**
31  * Implementation of ISessionImporter.
32  *
33  * @author Marc R. Hoffmann
34  * @version $Revision: 89 $
35  */

36 public class SessionImporter implements ISessionImporter {
37   
38   private String JavaDoc description;
39   private String JavaDoc coveragefile;
40   private IClassFiles[] classfiles;
41   private boolean copy;
42
43   public void setDescription(String JavaDoc description) {
44     this.description = description;
45   }
46
47   public void setCoverageFile(String JavaDoc file) {
48     this.coveragefile = file;
49   }
50
51   public void setClassFiles(IClassFiles[] classfiles) {
52     this.classfiles = classfiles;
53   }
54
55   public void setCopy(boolean copy) {
56     this.copy = copy;
57   }
58
59   public void importSession(IProgressMonitor monitor) throws CoreException {
60     monitor.beginTask(CoreMessages.ImportingSession_task, 2);
61     IInstrumentation[] instr = instrument(new SubProgressMonitor(monitor, 1));
62     IPath[] cfiles = new IPath[1];
63     cfiles[0] = createCopy(new SubProgressMonitor(monitor, 1));
64     ICoverageSession s = CoverageTools.createCoverageSession(description, instr, cfiles, null);
65     CoverageTools.getSessionManager().addSession(s, true, null);
66     monitor.done();
67   }
68   
69   private IInstrumentation[] instrument(IProgressMonitor monitor) throws CoreException {
70     monitor.beginTask("", classfiles.length); //$NON-NLS-1$
71
IInstrumentation[] instr = new IInstrumentation[classfiles.length];
72     for (int i = 0; i < classfiles.length; i++) {
73       instr[i] = classfiles[i].instrument(false, new SubProgressMonitor(monitor, 1));
74       monitor.worked(1);
75     }
76     monitor.done();
77     return instr;
78   }
79   
80   private IPath createCopy(IProgressMonitor monitor) throws CoreException {
81     IPath file = new Path(coveragefile);
82     if (copy) {
83       file = EclEmmaCorePlugin.getInstance().getStateFiles().getImportSessionFile(file);
84       File JavaDoc source = new File JavaDoc(coveragefile);
85       monitor.beginTask("", (int) source.length()); //$NON-NLS-1$
86
byte[] buffer = new byte[0x1000];
87       try {
88         InputStream JavaDoc in = new FileInputStream JavaDoc(source);
89         OutputStream JavaDoc out = new FileOutputStream JavaDoc(file.toFile());
90         int l;
91         while ((l = in.read(buffer)) != -1) {
92           out.write(buffer, 0, l);
93           monitor.worked(l);
94         }
95         in.close();
96         out.close();
97       } catch (IOException JavaDoc e) {
98         throw new CoreException(EclEmmaStatus.IMPORT_ERROR.getStatus(e));
99       }
100     }
101     monitor.done();
102     return file;
103   }
104
105 }
106
Popular Tags