Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 819b12b25d687defd79f5c891a7dc885191703e2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/*******************************************************************************
 * Copyright (c) 2009, 2016 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.equinox.p2.internal.repository.tools.tasks;

import org.eclipse.equinox.p2.internal.repository.mirroring.IArtifactMirrorLog;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.equinox.internal.p2.artifact.repository.Messages;
import org.eclipse.equinox.p2.repository.artifact.IArtifactDescriptor;

public class AntMirrorLog implements IArtifactMirrorLog {

	private boolean consoleMessage = false;
	private Method log;
	private Object task;

	public AntMirrorLog(Object task) throws NoSuchMethodException {
		this.task = task;
		try {
			log = task.getClass().getMethod("log", new Class[] {String.class, int.class}); //$NON-NLS-1$
		} catch (SecurityException e) {
			exceptionOccurred(null, e);
		}
	}

	public void log(IArtifactDescriptor descriptor, IStatus status) {
		log(descriptor.toString(), status.getSeverity());
		log(status);
	}

	public void log(IStatus status) {
		int severity = status.getSeverity();
		// Log the status message
		log(status.getMessage(), severity);
		// Log the exception if applicable
		if (status.getException() != null)
			log(status.getException().getMessage(), severity);

		// Log any children of this status
		IStatus[] nestedStatus = status.getChildren();
		if (nestedStatus != null)
			for (int i = 0; i < nestedStatus.length; i++)
				log(nestedStatus[i]);
	}

	public void close() {
		// nothing to do here
	}

	/*
	 * Log a message to the Ant Task
	 */
	private void log(String message, int statusSeverity) {
		try {
			log.invoke(task, new Object[] {message, Integer.valueOf(mapLogLevels(statusSeverity))});
		} catch (IllegalArgumentException e) {
			exceptionOccurred(message, e);
		} catch (IllegalAccessException e) {
			exceptionOccurred(message, e);
		} catch (InvocationTargetException e) {
			exceptionOccurred(message, e);
		}
	}

	/*
	 * Show an error message if this the first time, and print status messages.
	 */
	private void exceptionOccurred(String message, Exception e) {
		if (!consoleMessage) {
			System.err.println(Messages.MirrorLog_Exception_Occurred);
			e.printStackTrace(System.err);
			System.err.println(Messages.MirrorLog_Console_Log);
			consoleMessage = true;
		}
		if (message != null)
			System.out.println(message);
	}

	/**
	 * Copied from AntLogAdapter in pde build.
	 */
	private int mapLogLevels(int iStatusLevel) {
		switch (iStatusLevel) {
			case IStatus.ERROR :
				return 0;
			case IStatus.OK :
				return 2;
			case IStatus.INFO :
				return 2;
			case IStatus.WARNING :
				return 1;
			default :
				return 1;
		}
	}
}

Back to the top