Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
View | Details | Raw Unified | Return to bug 172064
Collapse All | Expand All

(-)src/org/eclipse/tptp/monitoring/auto/tests/utils/TestJUtils.java (+30 lines)
Lines 116-126 Link Here
116
	
116
	
117
	public  static void testBenchMarkCBEComparison(String benchMarkFile, String outputFile,boolean compareCreationTime)
117
	public  static void testBenchMarkCBEComparison(String benchMarkFile, String outputFile,boolean compareCreationTime)
118
	{
118
	{
119
		testBenchMarkCBEComparison(benchMarkFile,outputFile,compareCreationTime,false);
120
	}
121
	
122
	public  static void testBenchMarkCBEComparison(String benchMarkFile, String outputFile,boolean compareCreationTime,boolean GLAAsBenchmarkFolder)
123
	{
124
		testBenchMarkCBEComparison(benchMarkFile,outputFile,compareCreationTime,GLAAsBenchmarkFolder,false);
125
	}
126
	
127
	public  static void testBenchMarkCBEComparison(String benchMarkFile, String outputFile,boolean compareCreationTime,boolean GLAAsBenchmarkFolder,boolean narmalizeSapce)
128
	{
119
		try 
129
		try 
120
		{
130
		{
121
			System.out.println("Comparing...");
131
			System.out.println("Comparing...");
122
			CbeFileComparator comparator = new CbeFileComparator();
132
			CbeFileComparator comparator = new CbeFileComparator();
123
			comparator.setCompareCreationTime(compareCreationTime);
133
			comparator.setCompareCreationTime(compareCreationTime);
134
			comparator.setGLAAsBenchMarkFolder(GLAAsBenchmarkFolder);
135
			comparator.setNormalizeSpace(narmalizeSapce);
124
			comparator.compare(benchMarkFile,outputFile);
136
			comparator.compare(benchMarkFile,outputFile);
125
			String report = comparator.getReport();
137
			String report = comparator.getReport();
126
			assertNull(report,report);
138
			assertNull(report,report);
Lines 143-148 Link Here
143
			assertNull(report,report);
155
			assertNull(report,report);
144
		} catch (Exception e) 
156
		} catch (Exception e) 
145
		{
157
		{
158
			e.printStackTrace();
146
			fail(e.toString());
159
			fail(e.toString());
147
		}
160
		}
148
	}
161
	}
Lines 265-269 Link Here
265
			fail(e.toString());
278
			fail(e.toString());
266
		}
279
		}
267
	}
280
	}
281
	
282
	public static void testRunNoLogStandaloneAdapters(String adapter)
283
	{
284
		try
285
		{
286
			RunAdapter runAdap = new RunAdapter();
287
			runAdap.setNoLog(true);
288
			runAdap.run(adapter);
289
			String report = runAdap.getReport();
290
			assertNull(report,report);
291
			
292
		}
293
		catch(Exception e)
294
		{
295
			fail(e.toString());
296
		}
297
	}
268
298
269
}
299
}
(-)src/org/eclipse/tptp/monitoring/auto/tests/utils/RunAdapter.java (-3 / +51 lines)
Lines 66-71 Link Here
66
    
66
    
67
    private boolean simulateContOpp = false;
67
    private boolean simulateContOpp = false;
68
    private boolean noScript = false;
68
    private boolean noScript = false;
69
    private boolean noLog = false;
69
    private boolean filterTest = false;
70
    private boolean filterTest = false;
70
    
71
    
71
    private String componentName ="";
72
    private String componentName ="";
Lines 255-264 Link Here
255
    
256
    
256
	    junitReport = ex.runAdapter();
257
	    junitReport = ex.runAdapter();
257
		checkHGLALog(dir,adapterFile.getAbsolutePath());
258
		checkHGLALog(dir,adapterFile.getAbsolutePath());
258
        String genOutFileName = adapterDir.getAbsolutePath() + FILE_SEPARATOR + outputFileName;
259
		String outFile = ((outputFileName == null || outputFileName.equals(""))?"example.out":outputFileName);
260
        String genOutFileName = adapterDir.getAbsolutePath() + FILE_SEPARATOR + outFile;
259
        File genOutFile = new File(genOutFileName);
261
        File genOutFile = new File(genOutFileName);
260
        String toDelFileName = adapterFile.getName().substring(0,adapterFile.getName().lastIndexOf(".")) + ".out";
262
        String toDelFileName = adapterFile.getName().substring(0,adapterFile.getName().lastIndexOf(".")) + ".out";
261
		if(genOutFile.isFile() && !outputFileName.equals(toDelFileName) )
263
		if(genOutFile.isFile() && !outFile.equals(toDelFileName) )
262
		{
264
		{
263
			File toDelFile = new File(adapterDir.getAbsolutePath() + FILE_SEPARATOR + toDelFileName);
265
			File toDelFile = new File(adapterDir.getAbsolutePath() + FILE_SEPARATOR + toDelFileName);
264
			if(toDelFile.exists())
266
			if(toDelFile.exists())
Lines 356-362 Link Here
356
	    }
358
	    }
357
	    if(isAdapter)
359
	    if(isAdapter)
358
	    {
360
	    {
359
	    	runAdapterFile(dir);
361
	    	if(noLog)
362
	    		runNoLogAdapter(dir);
363
	    	else
364
	    		runAdapterFile(dir);
360
	    }
365
	    }
361
	    else
366
	    else
362
	    {
367
	    {
Lines 424-429 Link Here
424
		//removeEditedAdapter(adapter);
429
		//removeEditedAdapter(adapter);
425
    }
430
    }
426
    
431
    
432
    private void runNoLogAdapter(File adapter)
433
    throws Exception
434
    {
435
    	File dir = new File(USER_HOME);
436
    	markExistingHGLALogs(dir);
437
    	String fileName = adapter.getName().substring(0,adapter.getName().lastIndexOf("."));
438
		String adapterDir = adapter.getParent();
439
		String scriptKey = null;
440
	
441
		
442
		if((scriptKey = (String)adapterScripts.get(fileName))!= null)
443
		{
444
			File execFile = editAdapterScriptFile(adapter,adapterDir,scriptKey) ;
445
			if(execFile != null)
446
			{
447
				buildAdapterBasedCmdString(execFile) ;
448
				if(simulateContOpp)
449
					editAdapter(adapter);
450
			}
451
			else
452
			{
453
				writeErrorLog(adapter.getAbsolutePath(),"Could not find the adapter script file.Running the adapter with the GLA script.",30,true);
454
				editAdapter(adapter);
455
				buildGLABasedCmdString(adapter.getAbsolutePath());
456
			}
457
		}
458
		else
459
		{
460
			editAdapter(adapter);
461
			buildGLABasedCmdString(adapter.getAbsolutePath());
462
		}
463
		
464
		timeAndRun(adapter,fileName,"example.log");
465
466
		checkHGLALog(dir,adapter.getAbsolutePath());
467
		//removeEditedAdapter(adapter);
468
    }
469
    
427
    private void buildGLABasedCmdString(String filePath)
470
    private void buildGLABasedCmdString(String filePath)
428
	{    	    	
471
	{    	    	
429
		if(!(OPERATING_SYSTEM.indexOf("Win") >= 0))
472
		if(!(OPERATING_SYSTEM.indexOf("Win") >= 0))
Lines 796-801 Link Here
796
		noScript = b;
839
		noScript = b;
797
	}
840
	}
798
	
841
	
842
	public void setNoLog(boolean b) throws Exception
843
	{
844
		noLog = b;
845
	}
846
	
799
	public void editAdapter(File adapter)
847
	public void editAdapter(File adapter)
800
    throws Exception
848
    throws Exception
801
	{
849
	{
(-)src/org/eclipse/tptp/monitoring/auto/tests/utils/CbeFileComparator.java (-44 / +63 lines)
Lines 13-20 Link Here
13
 
13
 
14
package org.eclipse.tptp.monitoring.auto.tests.utils;
14
package org.eclipse.tptp.monitoring.auto.tests.utils;
15
15
16
import java.io.BufferedReader;
16
import java.io.File;
17
import java.io.File;
18
import java.io.FileOutputStream;
19
import java.io.FileReader;
17
import java.io.IOException;
20
import java.io.IOException;
21
import java.io.OutputStreamWriter;
22
import java.io.Writer;
18
import java.util.Iterator;
23
import java.util.Iterator;
19
import java.util.LinkedList;
24
import java.util.LinkedList;
20
25
Lines 37-42 Link Here
37
	public final static String LINE_SEPARATOR;
42
	public final static String LINE_SEPARATOR;
38
	public final static String FILE_SEPARATOR;
43
	public final static String FILE_SEPARATOR;
39
	private boolean compareCreationTime = false;
44
	private boolean compareCreationTime = false;
45
	private boolean compareGLALogs = false;
46
	private boolean replaceSpaceChars = false;
40
    
47
    
41
    static
48
    static
42
	{
49
	{
Lines 54-109 Link Here
54
	{
61
	{
55
		simulateContOpp = contOppVal;
62
		simulateContOpp = contOppVal;
56
	}
63
	}
64
	/**
65
	* Required when the comparison needs to be done against an out file
66
	* that is also generated by the GLA
67
	*/
68
	public void setGLAAsBenchMarkFolder(boolean b)
69
	{
70
		compareGLALogs = b;
71
	}
72
	
73
	public void setNormalizeSpace(boolean b)
74
	{
75
		replaceSpaceChars = b;
76
	}
57
    
77
    
58
    public CbeFileComparator() throws IOException
78
    public CbeFileComparator() throws IOException
59
	{
79
	{
60
    	fileSuffix = PropertiesWrapper.get("comparator.filesuffix");
80
    	fileSuffix = PropertiesWrapper.get("comparator.filesuffix");
61
    	if(fileSuffix == null) fileSuffix = ".out";
81
    	if(fileSuffix == null || fileSuffix.trim().equals("")) fileSuffix = ".out";
62
	}
82
	}
63
    
83
    
64
    
84
    
65
    public boolean compare(String firstFileName)
85
    public boolean compare(String firstFileName)
66
    throws Exception
86
    throws Exception
67
    {
87
    {
68
    	if(firstFileName.endsWith(".adapter")) firstFileName = firstFileName.substring(0,firstFileName.lastIndexOf(".")) + fileSuffix;
88
    	if(firstFileName.endsWith(".adapter")) 
69
70
    	String ADAPTER_HOME= PropertiesWrapper.getProperty("ADAPTER_CONFIG_HOME");
71
    	if(ADAPTER_HOME == null || !(new File(ADAPTER_HOME)).isDirectory())
72
		{
73
			ADAPTER_HOME = PropertiesWrapper.getProperty("GLA_HOME") + FILE_SEPARATOR + "config";
74
		}
75
    	String secondFileName = ADAPTER_HOME + FILE_SEPARATOR + firstFileName;
76
    	if(simulateContOpp)
77
    	{
78
    		firstFileName = ADAPTER_HOME + FILE_SEPARATOR + firstFileName;
79
    	}
80
    	else
81
    	{
82
    		String BENCHMARK_HOME = PropertiesWrapper.getProperty("BENCHMARK_HOME");
83
    		firstFileName = BENCHMARK_HOME + FILE_SEPARATOR + firstFileName;
84
    	}
85
    	
86
87
    	File dir1 = new File(firstFileName);
88
    	File dir2 = new File(secondFileName);
89
90
    	if(dir1.isDirectory() && dir2.isDirectory())
91
    	{
92
    		frstDir = firstFileName;
93
    		scndDir = secondFileName;
94
    		list(dir1);
95
    	}
96
    	else if(dir2.isFile() && dir1.isFile())
97
    	{
98
    		compareXmlFiles(firstFileName,secondFileName);
99
    	}
100
    	else
101
    	{
89
    	{
102
    		System.out.println("The files \"" + firstFileName + "\" and \"" + secondFileName + "\" seem to have a problem and comparison cannot be carried out.");
90
    		firstFileName = firstFileName.substring(0,firstFileName.lastIndexOf(".")) + fileSuffix;
103
    		throw new Exception("The files \"" + firstFileName + "\" and \"" + secondFileName + "\" seem to have a problem and comparison cannot be carried out.");
91
    	}    	
104
    	}
92
    	return compare(firstFileName,firstFileName);    
105
    	return true;
106
    
107
    }
93
    }
108
    
94
    
109
    public boolean compare(String benchMarkFileName, String outputFileName)
95
    public boolean compare(String benchMarkFileName, String outputFileName)
Lines 117-126 Link Here
117
		{
103
		{
118
			ADAPTER_HOME = PropertiesWrapper.getProperty("GLA_HOME") + FILE_SEPARATOR + "config";
104
			ADAPTER_HOME = PropertiesWrapper.getProperty("GLA_HOME") + FILE_SEPARATOR + "config";
119
		}
105
		}
106
    	if(compareGLALogs || simulateContOpp) 
107
    	{
108
    		BENCHMARK_HOME = ADAPTER_HOME;
109
    	}
120
    	String secondFileName = ADAPTER_HOME + FILE_SEPARATOR + outputFileName;
110
    	String secondFileName = ADAPTER_HOME + FILE_SEPARATOR + outputFileName;
121
    	String firstFileName = BENCHMARK_HOME + FILE_SEPARATOR + benchMarkFileName;
111
    	String firstFileName = BENCHMARK_HOME + FILE_SEPARATOR + benchMarkFileName;
122
    	
123
    	
124
112
125
    	File dir1 = new File(firstFileName);
113
    	File dir1 = new File(firstFileName);
126
    	File dir2 = new File(secondFileName);
114
    	File dir2 = new File(secondFileName);
Lines 133-139 Link Here
133
    	}
121
    	}
134
    	else if(dir2.isFile() && dir1.isFile())
122
    	else if(dir2.isFile() && dir1.isFile())
135
    	{
123
    	{
136
    		compareXmlFiles(firstFileName,secondFileName);
124
    		compareXmlFiles(dir1.getAbsolutePath(),dir2.getAbsolutePath());
137
    	}
125
    	}
138
    	else
126
    	else
139
    	{
127
    	{
Lines 143-148 Link Here
143
    	return true;
131
    	return true;
144
    
132
    
145
    }
133
    }
134
    
135
    private String convertToAscii(String file) throws IOException
136
    {
137
    	String newFile = file + "_ascii";
138
    	File nFile = new File(newFile);
139
    	if(nFile.exists()) nFile.delete();
140
    	
141
    	BufferedReader source = new BufferedReader(new FileReader(file));
142
    	FileOutputStream fos = new FileOutputStream(newFile);
143
    	Writer out = new OutputStreamWriter(fos, "ascii");
144
    	int len;		
145
		char[] buffer = new char[2 * 1024 * 1024];		
146
		while ((len = source.read(buffer)) >= 0)
147
			out.write(buffer, 0, len);
148
		out.close();
149
		
150
		//nFile.deleteOnExit();
151
		return newFile;
152
    }
153
    
154
    /*
155
	* The outfiles on zOS are generated in the local encoding and comparison fails
156
	* To avoid this the files can be converted to ascii and then compared.
157
	*/    
158
    private String noramlizeOutFileOnZOS(String file,boolean isBenchmark) throws IOException
159
    {
160
    	if(!PropertiesWrapper.OPERATING_SYSTEM.startsWith("z/OS") || (isBenchmark && !(compareGLALogs || simulateContOpp))) return file;
161
    	return convertToAscii(file);
162
    }
146
    private boolean compareXmlFiles(String firstFileName,String secondFileName)
163
    private boolean compareXmlFiles(String firstFileName,String secondFileName)
147
    	throws Exception
164
    	throws Exception
148
    {
165
    {
Lines 160-167 Link Here
160
    			secondFileName = firstFileName.substring(0,firstFileName.lastIndexOf(FILE_SEPARATOR)) + FILE_SEPARATOR + "CON_" + firstFileName.substring(firstFileName.lastIndexOf(FILE_SEPARATOR) + 1,firstFileName.length());
177
    			secondFileName = firstFileName.substring(0,firstFileName.lastIndexOf(FILE_SEPARATOR)) + FILE_SEPARATOR + "CON_" + firstFileName.substring(firstFileName.lastIndexOf(FILE_SEPARATOR) + 1,firstFileName.length());
161
    		}
178
    		}
162
    	}
179
    	}
163
    	XMLFileReader fileOne = new XMLFileReader(firstFileName,"CommonBaseEvent");
180
    	XMLFileReader fileOne = new XMLFileReader(noramlizeOutFileOnZOS(firstFileName,true),"CommonBaseEvent");
164
        XMLFileReader fileTwo = new XMLFileReader(secondFileName,"CommonBaseEvent");
181
        XMLFileReader fileTwo = new XMLFileReader(noramlizeOutFileOnZOS(secondFileName,false),"CommonBaseEvent");
165
        LinkedList fileOneList = new LinkedList();
182
        LinkedList fileOneList = new LinkedList();
166
        LinkedList fileTwoList = new LinkedList();
183
        LinkedList fileTwoList = new LinkedList();
167
        long fileOne_lastIndex = fileOne.createIndexes();
184
        long fileOne_lastIndex = fileOne.createIndexes();
Lines 332-337 Link Here
332
    
349
    
333
    private String cleanStrings(String vals)
350
    private String cleanStrings(String vals)
334
    {
351
    {
352
    	if(replaceSpaceChars)
353
    		vals = vals.replaceAll("\\s+", " ");
335
    	return vals.replaceAll("&#x[DA9];","").trim();
354
    	return vals.replaceAll("&#x[DA9];","").trim();
336
    }
355
    }
337
    
356
    
(-)src/org/eclipse/tptp/monitoring/auto/tests/utils/DeploymentUtil.java (+238 lines)
Added Link Here
1
package org.eclipse.tptp.monitoring.auto.tests.utils;
2
/**********************************************************************
3
 * Copyright (c) 2006 IBM Corporation and others.
4
 * All rights reserved.   This program and the accompanying materials
5
 * are made available under the terms of the Eclipse Public License v1.0
6
 * which accompanies this distribution, and is available at
7
 * http://www.eclipse.org/legal/epl-v10.html
8
 * $Id: DeploymentUtil.java,v 1.1 2007/02/14 07:15:40 roshetty Exp $
9
 * 
10
 * Contributors: 
11
 * IBM - Initial API and implementation
12
 **********************************************************************/
13
14
import java.io.BufferedInputStream;
15
import java.io.BufferedOutputStream;
16
import java.io.BufferedWriter;
17
import java.io.File;
18
import java.io.FileInputStream;
19
import java.io.FileOutputStream;
20
import java.io.FileWriter;
21
import java.io.IOException;
22
import java.io.InputStream;
23
import java.io.OutputStream;
24
import java.lang.reflect.Method;
25
import java.net.URL;
26
import java.net.URLClassLoader;
27
import java.util.zip.ZipEntry;
28
import java.util.zip.ZipInputStream;
29
30
public class DeploymentUtil
31
{
32
	private static boolean _deployed = false;
33
	
34
	public static void InstallGLA()
35
	{
36
		if(!_deployed)
37
		{
38
			_deployed = true;
39
			try {
40
				deploy();
41
				// Add the GLA jars to the classpath dynamically
42
				addAllFilesToClasspath(new File(PropertiesWrapper.GLA_HOME + PropertiesWrapper.FILE_SEPARATOR + "lib"));
43
			} catch (Throwable e) {
44
				// TODO Auto-generated catch block
45
				System.err.println(e);
46
			}			
47
		}
48
	}
49
	
50
	public static void deploy(String deploymentHome, String imageFolder) throws IOException
51
	{
52
		File depRoot = new File(imageFolder);
53
		File[] files = depRoot.listFiles();
54
		
55
		Processor processor = new Processor();
56
		String zOSScript = createZOSUnzipScript(processor);
57
		
58
		for(int i = 0; i < files.length; i ++)
59
		{
60
			String depFile = files[i].getAbsolutePath();
61
			
62
			if(isZipFile(depFile))
63
			{
64
				System.out.println("Deploying file: " + depFile);
65
				if(PropertiesWrapper.OPERATING_SYSTEM.startsWith("z/OS"))
66
				{
67
					try
68
					{
69
						//On zOS there are specific commands that need to be run, use the generated script for this
70
						processor.executeProcess("/bin/sh " + zOSScript + " " + depFile,deploymentHome);
71
					}
72
					catch (Throwable e) 
73
					{
74
						// TODO Auto-generated catch block
75
						System.err.println(e.getMessage());
76
					}	
77
				}
78
				else
79
				{
80
					// GLA Install guide says - Extract the zip file, So we have code here to unzip.
81
					extractZipFile(depFile,deploymentHome);
82
				}
83
			}		
84
		}
85
	}
86
	
87
	//Create the shell script in the local encoding to unzip the GLA
88
	private static String createZOSUnzipScript(Processor processor) throws IOException
89
	{
90
		if(PropertiesWrapper.OPERATING_SYSTEM.startsWith("z/OS"))
91
		{
92
			File newFileName = new File(PropertiesWrapper.DEPLOYMENT_HOME + "/unzipzos.sh");
93
			if(newFileName.exists()) newFileName.delete();
94
			BufferedWriter newExecFile = new BufferedWriter(new FileWriter(newFileName)) ;
95
			newExecFile.write("#!/bin/sh" 
96
					+ PropertiesWrapper.LINE_SEPARATOR + "/c390/build/bin/unzip -aoq $1" 
97
					+ PropertiesWrapper.LINE_SEPARATOR + "/c390/build/bin/unzip -oq $1 *.adapter" 
98
					+ PropertiesWrapper.LINE_SEPARATOR + "/c390/build/bin/unzip -oq $1 *.xsd" 
99
					+ PropertiesWrapper.LINE_SEPARATOR + "/c390/build/bin/unzip -aaoq $1 *.sh" 
100
					+ PropertiesWrapper.LINE_SEPARATOR + "/c390/build/bin/unzip -aaoq $1 *.log" 
101
					+ PropertiesWrapper.LINE_SEPARATOR);
102
			newExecFile.close();
103
			newFileName.deleteOnExit();
104
			processor.executeProcess("/bin/chmod 777 " + newFileName.getAbsolutePath(),PropertiesWrapper.DEPLOYMENT_HOME);
105
			return newFileName.getAbsolutePath();
106
		}
107
		return null;
108
	}
109
	
110
	public static void deploy(String imageFolder) throws IOException
111
	{
112
		deploy(PropertiesWrapper.DEPLOYMENT_HOME,imageFolder);
113
	}
114
	
115
	public static void deploy() throws IOException
116
	{
117
		deploy(PropertiesWrapper.DEPLOYMENT_HOME + PropertiesWrapper.FILE_SEPARATOR + "ZipFiles");
118
	}
119
	
120
	private static final Class[] parameters = new Class[]{URL.class};
121
	
122
	public static void addAllFilesToClasspath(File root) throws IOException
123
	{
124
		File[] files = root.listFiles();
125
		for(int i = 0; i < files.length; i ++)
126
		{
127
			addFileToClasspath(files[i]);
128
		}
129
	}
130
	 
131
	public static void addFileToClasspath(File f) throws IOException {
132
		addURL(f.toURL());
133
	}		 
134
	 
135
	private static void addURL(URL u) throws IOException 
136
	{				
137
		URLClassLoader sysloader = (URLClassLoader)ClassLoader.getSystemClassLoader();
138
		Class sysclass = URLClassLoader.class;		 
139
		try 
140
		{
141
			Method method = sysclass.getDeclaredMethod("addURL",parameters);
142
			method.setAccessible(true);
143
			method.invoke(sysloader,new Object[]{ u });
144
		} 
145
		catch (Throwable t) 
146
		{
147
			throw new IOException("Error, could not add URL to system classloader");
148
		}				
149
	}
150
	
151
	 /**
152
     *  Check if a file is a zip file
153
     * @param file
154
     * @return
155
     * @throws IOException
156
     */
157
	public static boolean isZipFile(String file) throws IOException
158
	{
159
		if(!(new File(file)).isFile()) return false;
160
		InputStream inputStream = new BufferedInputStream(new FileInputStream(file), 64*1024);
161
		inputStream.mark(5);
162
		return isZipFile(inputStream);			
163
	}
164
165
    /**
166
     * Check if a stream is from a zip file
167
     * @param inputStream
168
     * @return
169
     * @throws IOException
170
     */
171
	public static boolean isZipFile(InputStream inputStream) throws IOException{
172
		byte[] headerSignature = new byte[4];
173
		
174
		if (inputStream.read(headerSignature) > 1) {
175
			if (headerSignature[0] == 0x50 &&
176
				headerSignature[1] == 0x4b &&
177
				headerSignature[2] == 0x03 &&
178
				headerSignature[3] == 0x04) {
179
				return true;
180
			}
181
		}
182
		return false;			
183
	}
184
	
185
	public static String extractZipFile(String file,String parentDir) throws IOException
186
	{
187
		return extractZipFile(new BufferedInputStream(new FileInputStream(file), 64*1024),parentDir);
188
	}
189
	
190
	public static String extractZipFile(InputStream inputStream,String parentDir) throws IOException
191
	{
192
		File parent = new File(parentDir);
193
		
194
		if (parent != null && !parent.exists())
195
			parent.mkdirs();
196
197
		ZipInputStream zipInputStream = new ZipInputStream(inputStream);
198
		String fileName;
199
		
200
		while (zipInputStream.available() > -1) 
201
		{
202
			ZipEntry entry = (ZipEntry) zipInputStream.getNextEntry();
203
			if (entry == null) 
204
			{
205
				break;
206
			}
207
			
208
			fileName = entry.getName();
209
			
210
			File file = new File(parentDir + "/" + fileName);
211
			
212
			if(file.exists() && file.isFile()) file.delete();
213
			if (entry.isDirectory()) {
214
				if(!file.exists())
215
					file.mkdir();
216
				continue;
217
			}
218
			file.deleteOnExit();
219
220
			copyInputStream(zipInputStream, new BufferedOutputStream(new FileOutputStream(file)));
221
			
222
			zipInputStream.closeEntry();
223
		}
224
225
		zipInputStream.close();
226
		return parent+"/";
227
	}
228
	
229
	public static void copyInputStream(InputStream in, OutputStream out) throws IOException 
230
	{
231
		int len;		
232
		byte[] buffer = new byte[2 * 1024 * 1024];
233
		
234
		while ((len = in.read(buffer)) >= 0)
235
			out.write(buffer, 0, len);
236
		out.close();
237
	}
238
}
(-)src/org/eclipse/tptp/monitoring/auto/tests/utils/WindowsLogParser.java (+420 lines)
Added Link Here
1
/**********************************************************************
2
 * Copyright (c) 2006 IBM Corporation and others.
3
 * All rights reserved.   This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 * $Id: WindowsLogParser.java,v 1.1 2006/02/14 07:15:40 roshetty Exp $
8
 * 
9
 * Contributors: 
10
 * IBM - Initial API and implementation
11
 **********************************************************************/
12
package org.eclipse.tptp.monitoring.auto.tests.utils;
13
//org.eclipse.tptp.monitoring.auto.tests.utils;
14
15
import java.text.ParsePosition;
16
import java.text.SimpleDateFormat;
17
import java.util.Calendar;
18
import java.util.Hashtable;
19
20
import org.eclipse.hyades.logging.core.Guid;
21
import org.eclipse.hyades.logging.events.cbe.CommonBaseEvent;
22
import org.eclipse.hyades.logging.events.cbe.ComponentIdentification;
23
import org.eclipse.hyades.logging.events.cbe.ReportSituation;
24
import org.eclipse.hyades.logging.events.cbe.Situation;
25
import org.eclipse.hyades.logging.parsers.LogParserException;
26
import org.eclipse.hyades.logging.parsers.MonitoringParser;
27
import org.eclipse.hyades.logging.parsers.ParserConstants;
28
29
public class WindowsLogParser extends MonitoringParser
30
{
31
    //Variable to hold the system timezone value
32
    private String timezone;  
33
    
34
    public static final String WINDOWS_DATE_FORMAT_1 = "dd/MM/yyyy,hh:mm:ss aa";
35
    public static final String WINDOWS_DATE_FORMAT_2 = "dd/MM/yyyy,HH:mm:ss";
36
    
37
    // protected static Locale parseLocale = new Locale("en", "US");
38
    // protected static SimpleDateFormat dateFormat = new SimpleDateFormat(LogParserConstants.WAS_INSTALL_LOG_DATE_FORMAT1,parseLocale);
39
    //protected static SimpleDateFormat cbeDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss",parseLocale);
40
    
41
    protected static SimpleDateFormat dateFormat_1 = new SimpleDateFormat(WINDOWS_DATE_FORMAT_1);
42
    protected static SimpleDateFormat dateFormat_2 = new SimpleDateFormat(WINDOWS_DATE_FORMAT_2);
43
	protected static SimpleDateFormat cbeDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
44
   
45
    //  Default Constructor, also used to initialize all member variables
46
    public WindowsLogParser() {  	
47
       //intialize the other variables     
48
        timezone = "";
49
        inittimezone();
50
    }
51
52
    /*
53
     * this is an renamed function of setUserInput, (non-Javadoc)
54
     * @see org.eclipse.hyades.logging.parsers.IParser#setConfiguration(java.util.Hashtable)
55
     */
56
    public void setConfiguration(Hashtable table) throws LogParserException {       
57
        super.setConfiguration(table);
58
    }
59
    
60
      
61
    /**
62
     * Determine the situation based on the situation code value
63
     */
64
    private Situation createSituation() 
65
    { 
66
    	Situation cbeSituation = eventFactory.createSituation(); 
67
        ReportSituation cbeReportSituation = eventFactory.createReportSituation();
68
        cbeReportSituation.setReasoningScope(ParserConstants.INTERNAL_REASONING_SCOPE);
69
        cbeReportSituation.setReportCategory(ParserConstants.LOG_REPORT_CATEGORY);
70
        cbeSituation.setCategoryName(ParserConstants.REPORT_SITUATION_CATEGORY_NAME);
71
        cbeSituation.setSituationType(cbeReportSituation);
72
        return cbeSituation;
73
    }
74
    
75
    private void parseRecord(String line,CommonBaseEvent cbe) throws LogParserException
76
    {
77
    	// Initialize the CBE
78
        cbe.init();
79
        
80
        // Set the event's globalInstanceId property with a new GUID
81
        cbe.setGlobalInstanceId(Guid.generate());
82
       
83
        // Set the ExtensionName
84
        cbe.setExtensionName("CommonBaseEvent");
85
        
86
        //Set the Situation value
87
        cbe.setSituation(createSituation()); 
88
        
89
    	for(int i=0;i<=7;i++)
90
    	{
91
    		int idx = line.indexOf(',');
92
    		String snippet = (idx >-1 && i < 7)?line.substring(0,idx):line;
93
    		line = line.substring(idx+1);
94
    		switch(i)
95
    		{
96
    			case 0:
97
    				//parse time
98
    				idx = line.indexOf(',');
99
    				snippet = snippet + "," + line.substring(0,idx);
100
    	    		line = line.substring(idx+1);
101
    	    		String cbetime = getCBEDate(snippet,(snippet.endsWith("M")?dateFormat_1:dateFormat_2));
102
    	        	cbetime = cbetime.concat(".000");
103
    	        	cbetime = cbetime.concat(timezone);
104
    	        	cbe.setCreationTime(cbetime);
105
    				break;
106
    			case 1:
107
    				//Create source component id and set the values 
108
    	    		ComponentIdentification srcComponent = eventFactory.createComponentIdentification() ;
109
    	    		srcComponent.init();
110
    	    		srcComponent.setComponent("Windows");
111
    	    		srcComponent.setComponentIdType("ProductName");
112
    	    		srcComponent.setSubComponent(snippet);
113
    	    		srcComponent.setComponentType("WindowsOperatingSystem");
114
    	    		cbe.setSourceComponentId(srcComponent);
115
    				break;
116
    			case 2:
117
    				//Set the severity value
118
    				cbe.setSeverity(getSeverity(snippet));
119
    				break;
120
    			case 3:
121
    				break;
122
    			case 4:
123
    				// code to conver to Hexadecimal as the eventReader.exe displays this is hex
124
    				// Remove the below try-catch if the exe is change to output decimal
125
    				/*try
126
    				{
127
	    				snippet = Long.toHexString(Long.parseLong(snippet));
128
	    				snippet = snippet.toUpperCase();
129
	    				
130
	    				for(int len = snippet.length();len < 8; len ++)
131
	    				{
132
	    					snippet = 0 + snippet;
133
	    				}
134
    				}
135
    				catch(Exception e){}*/
136
    				//Uncomment the above code and comment the line below once bugzilla 176476 has been fixed.
137
    				snippet = (recordCount + 1) + "";
138
    				cbe.setMsgDataElement(eventFactory.createMsgDataElement());
139
	     			cbe.getMsgDataElement().init();
140
	     			cbe.getMsgDataElement().setMsgId(snippet);
141
	     			cbe.getMsgDataElement().setMsgIdType("UNKNOWN");
142
    				break;
143
    			case 5:
144
    				if(!(snippet.trim().equals(""))) 
145
    				{
146
    	            	cbe.addExtendedDataElement(createStringEDE("Username",snippet.substring(snippet.indexOf('\\')+1)));
147
    	            }
148
    				break;
149
    			case 6:
150
    				// Set the ReporterComponent values
151
    	    		ComponentIdentification reporterComponent = eventFactory.createComponentIdentification() ;
152
    	    		reporterComponent.init(); 
153
    	    		reporterComponent.setLocationType("Hostname");	
154
    	    		reporterComponent.setComponent("ACAdapter");
155
    	    		reporterComponent.setComponentType("WindowsOperatingSystem");
156
    	    		reporterComponent.setComponentIdType("APPLICATION");            
157
    	    		reporterComponent.setSubComponent("NTEventLogSensor");
158
    	    		cbe.setReporterComponentId(reporterComponent); 
159
    	    		
160
    				if(!(snippet.trim().equals("")))
161
    				{
162
    					cbe.getReporterComponentId().setLocation(snippet);
163
    					cbe.getReporterComponentId().setLocationType("Hostname");
164
    					cbe.getSourceComponentId().setLocation(snippet);
165
    					cbe.getSourceComponentId().setLocationType("Hostname");
166
    				}
167
    	            else
168
    	            {
169
    	            	cbe.getReporterComponentId().setLocation(localHostId);
170
    					cbe.getReporterComponentId().setLocationType(localHostIdFormat);
171
    	            	cbe.getSourceComponentId().setLocation(localHostId);
172
    	            	cbe.getSourceComponentId().setLocationType(localHostIdFormat);
173
    	            }    				
174
    				break;
175
    			case 7:
176
    				////System.out.println("*****messageString******   "+snippet);
177
    				String messageString = undoCVSFormating(snippet);
178
    				////System.out.println("*****messageString******   "+messageString);
179
    				if(messageString.equals("")) break;
180
    				
181
    				if (messageString.length() > 1024) 
182
    				{
183
    	                cbe.setMsg(messageString.substring(0, 1024));
184
    	                cbe.addExtendedDataElement(createStringEDE(ParserConstants.MESSAGE_EXTENDED_DATA_ELEMENT,messageString.toString()));
185
    	            } 
186
    				else
187
    				{
188
    					cbe.setMsg(messageString.toString());
189
    				}
190
    				break;
191
     			default:
192
    				break;
193
    		}
194
    	}
195
    }
196
197
    private String undoCVSFormating(String msgString)  
198
    {
199
    	msgString = msgString.replaceAll("\"\"", "\"");
200
    	msgString = msgString.replaceAll("\n", " ");
201
    	msgString = msgString.replaceAll("\r", " ");
202
    	
203
    	if(msgString.startsWith("\"") && msgString.endsWith("\""))
204
    	{
205
    		msgString = msgString.substring(1,msgString.length()-1);
206
    	}
207
    	if(msgString.matches("The description for Event ID .* in Source .* cannot be found. The local computer may not have the necessary registry information or message DLL files to display messages from a remote computer. You may be able to use the .* flag to retrieve this description; see Help and Support for details. The following information is part of the event:.*"))
208
    	{
209
    		msgString = msgString.replaceAll("The description for Event ID .* in Source .* cannot be found. The local computer may not have the necessary registry information or message DLL files to display messages from a remote computer. You may be able to use the /AUXSOURCE= flag to retrieve this description; see Help and Support for details. The following information is part of the event:", "");
210
    		msgString = msgString.trim();
211
    		msgString = msgString.substring(0, msgString.length()-1);
212
    	}
213
    	if(msgString.equals("The event log file is corrupt."))
214
    	{
215
    		msgString = "";
216
    	}
217
    	return msgString.trim();
218
    }
219
    
220
    private String nextLine = null;
221
    
222
    private String getLogRecord() throws LogParserException
223
    {
224
    	String record = nextLine;
225
    	nextLine = null;
226
    	String line=null;
227
    	
228
    	while ((line=readALine()) != null)
229
   	 	{
230
    		if(line.length() == 0) line = " ";
231
    		
232
    		 try
233
	       	 {
234
    				 if(line.matches("\\d{2}/\\d{2}/\\d{4},\\d{2}:\\d{2}:\\d{2}[^,]*,[^,]*,[^,]*,[^,]*,[^,]*,.*"))
235
	  	              {
236
	  	            	  if(record == null)
237
	  	            	  {
238
	  	            		  record = line;	
239
	  	            	  }
240
	  	            	  else
241
	  	            	  {
242
	  	            		  nextLine = line;
243
	  	            		  break;
244
	  	            	  }
245
	  	              } 
246
	  	              else if(record != null)
247
	  	              {
248
	  	            	  record = record + ParserConstants.LINE_SEPARATOR + line;	
249
	  	              }
250
	       	 }
251
	       	 catch(Exception e)
252
	       	 {
253
	       		 if(record != null)
254
	  	         {
255
	       			 record = record + ParserConstants.LINE_SEPARATOR + line;	
256
	  	         }
257
	       	 }
258
        } 
259
    	
260
    	return record;
261
    }
262
    /**
263
     * to parse Severity of the record
264
     * @param message id
265
     * @return
266
     */
267
    private short getSeverity(String severString)  
268
    {
269
    	if(severString.endsWith("Success Audit"))
270
    		return 15;
271
    	else if(severString.endsWith("Failure Audit"))
272
    		return 16;
273
    	else if(severString.endsWith("Information"))
274
        	return 20;
275
        else if(severString.endsWith("Warning"))
276
        	return 30;
277
        else if(severString.endsWith("Error"))
278
        	return 40;
279
        else 
280
        	return 17;
281
    }
282
    
283
    /*
284
     * Intialize the timezone value to the system default timezone value 
285
     */
286
    private void inittimezone()
287
    {
288
		//intialize the timezone value
289
		Calendar localCalendar = Calendar.getInstance();
290
	    int utcOffset = ((localCalendar.get(Calendar.ZONE_OFFSET) + localCalendar.get(Calendar.DST_OFFSET)) / 60000);                
291
	    if (utcOffset < 0) {
292
	    	timezone = timezone.concat("-");
293
	    }
294
	    else {
295
	    	timezone = timezone.concat("+");
296
	    }
297
	    utcOffset = Math.abs(utcOffset);
298
	    // Compute the Time Zone value (+/-hh:mm)
299
	    String numberHours = String.valueOf(utcOffset / 60);
300
	    if (numberHours.length() == 1) {
301
	    	timezone = timezone.concat("0");
302
	    }
303
	    timezone = timezone.concat(numberHours);
304
	    timezone = timezone.concat(":");
305
	    String numberMinutes = String.valueOf(utcOffset % 60);
306
	    if (numberMinutes.length() == 1) {
307
	    	timezone = timezone.concat("0");
308
	    }
309
	    timezone = timezone.concat(numberMinutes);
310
    }
311
    
312
    
313
	/**
314
	 * Parses the log file, generates a CBE for each parsed log record and 
315
	 * returns the array of CBE's.
316
	 * @return CommonBaseEvent[] The array of generated CBE objects.
317
	 * @exception LogParserException thrown if an exception is encountered during parsing
318
	 */
319
	public CommonBaseEvent[] parseNext() throws LogParserException 
320
	{
321
		try	
322
		{
323
			// counter for the number of CBE events converted
324
			arrayIndex = 0;
325
			String currentLine = null;
326
			while((currentLine = getLogRecord()) != null)
327
			{
328
				currentLine = currentLine.trim();
329
				//System.out.println("*****currentLine****  "+currentLine);
330
				if(messages[arrayIndex] == null) 
331
		       	{
332
					messages[arrayIndex] = eventFactory.createCommonBaseEvent();
333
				}
334
				
335
				if(currentLine.length() == 0) {
336
					continue;
337
				}
338
				if (currentLine.length()> 0) 
339
				{
340
					parseRecord(currentLine,messages[arrayIndex]);
341
342
                    arrayIndex++;
343
    				recordCount++;
344
                 } //if condition block
345
				 if(arrayIndex == MessageArraySize) 
346
				 {
347
					arrayIndex = 0;
348
					return messages;
349
				 }
350
			}
351
			if (arrayIndex == 0) {
352
				setEndOfFile();
353
				return null;
354
			}
355
			else {
356
				for (int index = arrayIndex; index < MessageArraySize; index++) 
357
				{
358
					messages[index] = null;
359
				}
360
			}	
361
		}
362
		catch(Exception e) {
363
			throw new LogParserException("Internal Error occurred in Windows CSV parser - " + e.getMessage());
364
		}		
365
		if(recordCount == 0) {
366
			throw new LogParserException("The Windows CSV log does not have any records");
367
		}
368
		return messages;
369
	}
370
	
371
    /**
372
 	 * This method converts the given date into CBE Date format
373
 	 * @param dateString
374
 	 * @param dateFormat
375
 	 * @return
376
 	 */
377
 	private String getCBEDate(String dateString,SimpleDateFormat dateFormat) 
378
 	{
379
 		try
380
 		{
381
 			//Date creationDateObj = dateFormat.parse(dateString);
382
 			ParsePosition parseLoc = new ParsePosition(0);
383
			java.util.Date creationDateObj = dateFormat.parse(dateString, parseLoc);
384
 			if(creationDateObj != null)
385
 			{
386
 				StringBuffer cbeCreationDate = new StringBuffer(cbeDateFormat.format(creationDateObj));
387
 				return cbeCreationDate.toString();
388
 			}
389
 		}
390
 		catch(Exception pe)
391
 		{
392
 			//pe.printStackTrace();
393
 			// do nothing
394
 		}
395
 		return null;
396
 	} 
397
    
398
	/**
399
     * Initialize this parser.
400
     */
401
    public void preParse() throws LogParserException {        
402
        super.preParse();
403
    }
404
    
405
    /**
406
     * Returns the name of the parser (non-Javadoc)
407
     * @see org.eclipse.hyades.logging.parsers.Parser#getName()
408
     */
409
    public String getName() {
410
        return "Windows application log";
411
    }
412
413
    /**
414
     * Returns current Version
415
     */
416
    public String getVersion() {
417
        return "windows";
418
    }
419
420
}

Return to bug 172064