|
Lines 1-5
Link Here
|
| 1 |
/********************************************************************** |
1 |
/********************************************************************** |
| 2 |
* Copyright (c) 2005, 2009 IBM Corporation and others. |
2 |
* Copyright (c) 2005, 2010 IBM Corporation and others. |
| 3 |
* All rights reserved. This program and the accompanying materials |
3 |
* All rights reserved. This program and the accompanying materials |
| 4 |
* are made available under the terms of the Eclipse Public License v1.0 |
4 |
* are made available under the terms of the Eclipse Public License v1.0 |
| 5 |
* which accompanies this distribution, and is available at |
5 |
* which accompanies this distribution, and is available at |
|
Lines 10-190
Link Here
|
| 10 |
* IBM Corporation - initial API and implementation |
10 |
* IBM Corporation - initial API and implementation |
| 11 |
**********************************************************************/ |
11 |
**********************************************************************/ |
| 12 |
package org.eclipse.hyades.internal.execution.recorder.utilities; |
12 |
package org.eclipse.hyades.internal.execution.recorder.utilities; |
| 13 |
import java.io.ByteArrayInputStream; |
13 |
|
| 14 |
import java.io.IOException; |
14 |
import java.io.File; |
| 15 |
import java.util.HashMap; |
|
|
| 16 |
import java.util.Map; |
| 17 |
|
15 |
|
| 18 |
import org.eclipse.core.resources.IFile; |
16 |
import org.eclipse.core.resources.IFile; |
|
|
17 |
import org.eclipse.core.resources.IResource; |
| 19 |
import org.eclipse.core.resources.ResourcesPlugin; |
18 |
import org.eclipse.core.resources.ResourcesPlugin; |
| 20 |
import org.eclipse.core.runtime.CoreException; |
19 |
import org.eclipse.core.runtime.IPath; |
| 21 |
import org.eclipse.core.runtime.NullProgressMonitor; |
20 |
import org.eclipse.core.runtime.NullProgressMonitor; |
| 22 |
import org.eclipse.core.runtime.Path; |
21 |
|
| 23 |
import org.eclipse.core.runtime.Platform; |
|
|
| 24 |
import org.eclipse.emf.common.util.URI; |
| 25 |
import org.eclipse.emf.ecore.EObject; |
| 26 |
import org.eclipse.emf.ecore.resource.Resource; |
| 27 |
import org.eclipse.emf.ecore.resource.ResourceSet; |
| 28 |
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl; |
| 29 |
import org.eclipse.emf.ecore.xmi.XMIResource; |
| 30 |
import org.eclipse.hyades.models.trace.TRCSourceInfo; |
| 31 |
import org.eclipse.hyades.models.trace.impl.TraceFactoryImpl; |
| 32 |
/** |
22 |
/** |
| 33 |
* @author amathur |
23 |
* <p>Utilities for recording files.</p> |
| 34 |
* @author Jerome Bozier |
24 |
* |
| 35 |
* @version November 17, 2009 |
25 |
* |
| 36 |
* |
26 |
* @author Ashish Mathur |
| 37 |
* To change the template for this generated type comment go to |
27 |
* @author Jerome Bozier |
| 38 |
* Window>Preferences>Java>Code Generation>Code and Comments |
28 |
* @author Paul Slauenwhite |
|
|
29 |
* @version March 25, 2010 |
| 30 |
* @since September 23, 2005 |
| 39 |
*/ |
31 |
*/ |
| 40 |
public class RecorderFileUtil { |
32 |
public class RecorderFileUtil { |
| 41 |
|
33 |
|
| 42 |
public static IFile createTraceFile(String filepath) throws CoreException |
34 |
/** |
| 43 |
{ |
35 |
* <p>Determines if the {@link IFile} exists in the workspace or local file system.</p> |
| 44 |
|
36 |
* |
| 45 |
String recFilePath = filepath; |
37 |
* @param iFile The {@link IFile} to be checked. |
| 46 |
IFile file = null; |
38 |
* @return <code>true</code> if the {@link IFile} exists in the workspace or local file system, otherwise <code>false</code>. |
| 47 |
try |
39 |
*/ |
| 48 |
{ |
40 |
public static boolean fileExists(IFile iFile){ |
| 49 |
if (!recFilePath.endsWith(".rec")) { //$NON-NLS-1$ |
41 |
|
| 50 |
recFilePath += ".rec"; //$NON-NLS-1$ |
42 |
//Workspace file check: |
| 51 |
} |
43 |
if (iFile.exists()){ |
| 52 |
|
44 |
return true; |
| 53 |
file = ResourcesPlugin.getWorkspace().getRoot().getFile(new Path(recFilePath)); |
|
|
| 54 |
|
| 55 |
if(file.exists()) |
| 56 |
{ |
| 57 |
file.delete(true,true,null); |
| 58 |
} |
| 59 |
|
| 60 |
file.create(new ByteArrayInputStream(new byte[] {}), true, new NullProgressMonitor()); |
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
} |
45 |
} |
| 65 |
catch (RuntimeException e) { |
46 |
|
| 66 |
e.printStackTrace(); |
47 |
//File system file check: |
|
|
48 |
else{ |
| 49 |
|
| 50 |
IPath iFilePath = iFile.getLocation(); |
| 51 |
|
| 52 |
if (iFilePath != null){ |
| 53 |
return (iFilePath.toFile().exists()); |
| 54 |
} |
| 67 |
} |
55 |
} |
| 68 |
|
56 |
|
| 69 |
return file; |
57 |
return false; |
| 70 |
} |
58 |
} |
| 71 |
|
59 |
|
| 72 |
public static IFile createTraceModel(String recModelPath, String recFilePath) { |
60 |
/** |
| 73 |
Map RESOURCE_OPTIONS = new HashMap(); |
61 |
* <p>Deletes the recording {@link IFile} if it exists in the workspace or local file system.</p> |
| 74 |
RESOURCE_OPTIONS.put(XMIResource.OPTION_DECLARE_XML, Boolean.TRUE); |
62 |
* |
| 75 |
RESOURCE_OPTIONS.put(XMIResource.OPTION_SKIP_ESCAPE, Boolean.FALSE); |
63 |
* <p>If the recording {@link IFile} was successfully deleted, the recording {@link IFile}'s parent container is refreshed.</p> |
| 76 |
RESOURCE_OPTIONS.put(XMIResource.OPTION_ENCODING, "UTF-8"); |
64 |
* |
| 77 |
TRCSourceInfo si = TraceFactoryImpl.eINSTANCE.createTRCSourceInfo(); |
65 |
* <p>If the recording {@link IFile} ends with the <code>.recmodel</code> file extension, the recording model {@link IFile} |
| 78 |
//URI testSuiteURI = EcoreUtil.getURI(tpfSuite); |
66 |
* (<code>*.recmodel</code>) is deleted and the recording {@link IFile}'s parent container is refreshed.</p> |
| 79 |
//URI uri = tpfSuite.eResource().getURI().trimFileExtension(); |
67 |
* |
| 80 |
|
68 |
* @param iFile The recording {@link IFile} to be deleted. |
| 81 |
if (recModelPath.endsWith(".rec")) { //$NON-NLS-1$ |
69 |
* @return <code>true</code> if the recording {@link IFile} and possible recording model {@link IFile} were successfully deleted, otherwise <code>false</code>. |
| 82 |
recModelPath += "model"; //$NON-NLS-1$ |
70 |
*/ |
| 83 |
} |
71 |
public static boolean deleteRecordingFile(IFile recordingFile){ |
| 84 |
else if (!recModelPath.endsWith(".recmodel")) { //$NON-NLS-1$ |
|
|
| 85 |
recModelPath += ".recmodel"; //$NON-NLS-1$ |
| 86 |
} |
| 87 |
|
| 88 |
|
72 |
|
| 89 |
URI modelURI = URI.createPlatformResourceURI(recModelPath,true); |
73 |
boolean isSuccessful = false; |
| 90 |
si.setLocation(recFilePath); |
|
|
| 91 |
ResourceSet rs = new ResourceSetImpl(); |
| 92 |
Resource resource = rs.createResource(modelURI); |
| 93 |
resource.getContents().add(si); |
| 94 |
|
| 95 |
//TODO handle situation where file already exists. |
| 96 |
try { |
| 97 |
resource.save(RESOURCE_OPTIONS); |
| 98 |
} |
| 99 |
catch (IOException e) { |
| 100 |
e.printStackTrace(); |
| 101 |
} |
| 102 |
|
| 103 |
return ResourcesPlugin.getWorkspace().getRoot().getFile(new Path(recModelPath)); |
| 104 |
|
74 |
|
| 105 |
|
75 |
if(recordingFile != null){ |
| 106 |
} |
76 |
|
| 107 |
public static EObject[] loadResource(IFile file) { |
77 |
isSuccessful = deleteFile(recordingFile); |
| 108 |
// Create a resource set before loading anything |
78 |
|
| 109 |
ResourceSet resourceSet = new ResourceSetImpl(); |
79 |
if(isSuccessful){ |
| 110 |
Resource resource = |
80 |
|
| 111 |
resourceSet.getResource( |
81 |
IPath iFilePath = recordingFile.getFullPath(); |
| 112 |
URI.createPlatformResourceURI(file.getFullPath().toString(),true), |
82 |
|
| 113 |
true); |
83 |
if("rec".equals(iFilePath.getFileExtension())){ //$NON-NLS-1$ |
| 114 |
if (resource == null) { |
84 |
isSuccessful = deleteFile(ResourcesPlugin.getWorkspace().getRoot().getFile(iFilePath.removeFileExtension().addFileExtension("recmodel"))); //$NON-NLS-1$ |
| 115 |
System.out.println("Failed to get the resource"); //$NON-NLS-1$ |
85 |
} |
| 116 |
return null; |
86 |
} |
| 117 |
} |
87 |
} |
| 118 |
// Retrieve an array of root elements in this file. In this case, there is really only |
88 |
|
| 119 |
// one TRCSourceInfo element. |
89 |
return isSuccessful; |
| 120 |
EObject[] elements = |
|
|
| 121 |
(EObject[]) resource.getContents().toArray( |
| 122 |
new EObject[resource.getContents().size()]); |
| 123 |
return elements; |
| 124 |
} |
90 |
} |
|
|
91 |
|
| 125 |
/** |
92 |
/** |
| 126 |
* @param file |
93 |
* <p>Deletes the {@link IFile} if it exists in the workspace or local file system.</p> |
|
|
94 |
* |
| 95 |
* <p>If the {@link IFile} was successfully deleted, the {@link IFile}'s parent container is refreshed.</p> |
| 96 |
* |
| 97 |
* @param iFile The {@link IFile} to be deleted. |
| 98 |
* @return <code>true</code> if the {@link IFile} was successfully deleted, otherwise <code>false</code>. |
| 127 |
*/ |
99 |
*/ |
| 128 |
public static IFile getTraceFile(IFile file) { |
100 |
public static boolean deleteFile(IFile iFile){ |
| 129 |
EObject[] elements = RecorderFileUtil.loadResource(file); |
101 |
|
| 130 |
TRCSourceInfo tsi = (TRCSourceInfo) elements[0]; |
102 |
boolean isSuccessful = false; |
| 131 |
// Get the location attribute |
103 |
|
| 132 |
String location = tsi.getLocation(); |
104 |
if(iFile != null){ |
| 133 |
System.out.println("Trace file: " + location); //$NON-NLS-1$ |
105 |
|
| 134 |
// Find and open the trace file |
106 |
//Workspace file check: |
| 135 |
IFile traceFile = |
107 |
if (iFile.exists()){ |
| 136 |
(IFile) ResourcesPlugin.getWorkspace().getRoot().findMember( |
108 |
|
| 137 |
location); |
109 |
try { |
| 138 |
return traceFile; |
110 |
|
| 139 |
} |
111 |
iFile.delete(true, new NullProgressMonitor()); |
| 140 |
|
112 |
|
| 141 |
public static boolean fileExists(IFile wsFile) |
113 |
isSuccessful = true; |
| 142 |
{ |
114 |
} |
| 143 |
if (wsFile.exists()) |
115 |
catch (Exception e) { |
| 144 |
return true; |
116 |
//Ignore and return false; |
| 145 |
if (Platform.getOS().equals("win32")) |
117 |
} |
| 146 |
{ //although the IFile does not exist in the workspace (it is case-sensitive) |
|
|
| 147 |
//it may still be impossible to create the file in case-insensitive |
| 148 |
java.net.URI uri = wsFile.getRawLocationURI(); |
| 149 |
if (uri == null) //probably because project doesn't exist |
| 150 |
return false; |
| 151 |
java.io.File file = new java.io.File(uri); |
| 152 |
return file.exists(); |
| 153 |
|
| 154 |
} |
| 155 |
return false; |
| 156 |
} |
| 157 |
|
| 158 |
public static boolean deleteFiles(IFile recFile, IFile tgFile) |
| 159 |
{ |
| 160 |
if (!(Platform.getOS().equals("win32"))) |
| 161 |
return false; //don't bother, since the case-insensitive delete is only required on Windows |
| 162 |
if (recFile != null) |
| 163 |
{ |
| 164 |
java.io.File javaFile = new java.io.File(recFile.getRawLocationURI()); |
| 165 |
javaFile.delete(); |
| 166 |
if (recFile.toString().endsWith(".rec")) |
| 167 |
{ |
| 168 |
java.io.File recmodel = new java.io.File(javaFile.toString()+ "model"); |
| 169 |
recmodel.delete(); |
| 170 |
//also remove recmodel paired file |
| 171 |
} |
| 172 |
try { |
| 173 |
recFile.getParent().refreshLocal(1, new NullProgressMonitor()); |
| 174 |
} |
118 |
} |
| 175 |
catch (Exception e) {} |
119 |
|
| 176 |
} |
120 |
//File system file check: |
| 177 |
if (tgFile != null) |
121 |
else{ |
| 178 |
{ |
122 |
|
| 179 |
java.io.File javaFile = new java.io.File(tgFile.getRawLocationURI()); |
123 |
IPath iFilePath = iFile.getLocation(); |
| 180 |
javaFile.delete(); |
124 |
|
| 181 |
try { |
125 |
if (iFilePath != null){ |
| 182 |
tgFile.getParent().refreshLocal(1, new NullProgressMonitor()); |
126 |
|
|
|
127 |
File file = iFilePath.toFile(); |
| 128 |
|
| 129 |
if(file.exists()){ |
| 130 |
isSuccessful = file.delete(); |
| 131 |
} |
| 132 |
} |
| 183 |
} |
133 |
} |
| 184 |
catch (Exception e) {} |
|
|
| 185 |
|
134 |
|
|
|
135 |
if(isSuccessful){ |
| 136 |
|
| 137 |
//Attempt to refresh the file's container: |
| 138 |
try { |
| 139 |
iFile.getParent().refreshLocal(IResource.DEPTH_ONE, new NullProgressMonitor()); |
| 140 |
} |
| 141 |
catch (Exception e) { |
| 142 |
//Ignore since the file's container could not be refreshed. |
| 143 |
} |
| 144 |
} |
| 186 |
} |
145 |
} |
| 187 |
return true; |
|
|
| 188 |
} |
| 189 |
|
146 |
|
|
|
147 |
return isSuccessful; |
| 148 |
} |
| 190 |
} |
149 |
} |