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 |
* |
8 |
* Contributors: |
9 |
* IBM Corporation - initial API and implementation |
10 |
*******************************************************************************/ |
11 |
package org.eclipse.core.internal.runtime; |
12 |
|
13 |
import org.osgi.framework.FrameworkListener; |
14 |
|
15 |
import org.osgi.framework.Bundle; |
16 |
|
17 |
import org.osgi.framework.ExtendedFrameworkEvent; |
18 |
|
19 |
import org.eclipse.core.runtime.IStatus2; |
20 |
|
21 |
import org.osgi.framework.FrameworkEvent; |
22 |
|
23 |
import java.util.ArrayList; |
24 |
import org.eclipse.core.runtime.CoreException; |
25 |
import org.eclipse.osgi.framework.log.FrameworkLogEntry; |
26 |
|
27 |
import org.eclipse.core.runtime.IStatus; |
28 |
|
29 |
import org.eclipse.core.runtime.ILogListener; |
30 |
|
31 |
import org.eclipse.osgi.framework.log.FrameworkEventHandler; |
32 |
|
33 |
/** |
34 |
* Should be promoted to API. |
35 |
*/ |
36 |
public class PlatformStatusHandler implements ILogListener { |
37 |
|
38 |
FrameworkEventHandler eventHandler; |
39 |
|
40 |
public PlatformStatusHandler(FrameworkEventHandler eventHandler) { |
41 |
this.eventHandler = eventHandler; |
42 |
eventHandler.addListener(new FrameworkListener() { |
43 |
|
44 |
public void frameworkEvent(FrameworkEvent event) { |
45 |
ExtendedFrameworkEvent event2; |
46 |
if (event instanceof ExtendedFrameworkEvent) { |
47 |
event2 = (ExtendedFrameworkEvent) event; |
48 |
} else { |
49 |
event2 = new ExtendedFrameworkEvent(event); |
50 |
} |
51 |
if (event2.getProperty(IStatus.class) != null) { |
52 |
return; |
53 |
} |
54 |
event2.setProperty(IStatus.class, eventToStatus(event2)); |
55 |
} |
56 |
|
57 |
}, true); |
58 |
} |
59 |
|
60 |
public void logging(IStatus status, String plugin) { |
61 |
eventHandler.handle(statusToEvent(status), FrameworkEventHandler.LOG); |
62 |
} |
63 |
|
64 |
public void handle(IStatus status, int style) { |
65 |
eventHandler.handle(statusToEvent(status), style); |
66 |
} |
67 |
|
68 |
protected FrameworkLogEntry getLog(IStatus status) { |
69 |
Throwable t = status.getException(); |
70 |
ArrayList childlist = new ArrayList(); |
71 |
|
72 |
int stackCode = t instanceof CoreException ? 1 : 0; |
73 |
// ensure a substatus inside a CoreException is properly logged |
74 |
if (stackCode == 1) { |
75 |
IStatus coreStatus = ((CoreException) t).getStatus(); |
76 |
if (coreStatus != null) { |
77 |
childlist.add(getLog(coreStatus)); |
78 |
} |
79 |
} |
80 |
|
81 |
if (status.isMultiStatus()) { |
82 |
IStatus[] children = status.getChildren(); |
83 |
for (int i = 0; i < children.length; i++) { |
84 |
childlist.add(getLog(children[i])); |
85 |
} |
86 |
} |
87 |
|
88 |
FrameworkLogEntry[] children = (FrameworkLogEntry[]) (childlist.size() == 0 ? null : childlist.toArray(new FrameworkLogEntry[childlist.size()])); |
89 |
|
90 |
return new FrameworkLogEntry(status.getPlugin(), status.getSeverity(), status.getCode(), status.getMessage(), stackCode, t, children); |
91 |
} |
92 |
|
93 |
protected FrameworkEvent statusToEvent(IStatus status) { |
94 |
if (status instanceof IStatus2) { |
95 |
IStatus2 status2 = (IStatus2) status; |
96 |
if (status2.getAdapter(FrameworkEvent.class) != null) { |
97 |
return (FrameworkEvent) status2.getAdapter(FrameworkEvent.class); |
98 |
} |
99 |
ExtendedFrameworkEvent efe = new ExtendedFrameworkEvent(status.getSeverity(), idToBundle(status.getPlugin()), status.getException()); |
100 |
//TODO: do not allow for loosing any information here. Add everything as ExtendedFrameworkEvent properties. |
101 |
efe.setProperty(IStatus.class, status); |
102 |
return efe; |
103 |
} |
104 |
if (status instanceof IStatus) { |
105 |
ExtendedFrameworkEvent efe = new ExtendedFrameworkEvent(status.getSeverity(), idToBundle(status.getPlugin()), status.getException()); |
106 |
//TODO: do not allow for loosing any information here. Add everything as ExtendedFrameworkEvent properties. |
107 |
efe.setProperty(IStatus.class, status); |
108 |
return efe; |
109 |
} |
110 |
return null; |
111 |
} |
112 |
|
113 |
protected IStatus eventToStatus(FrameworkEvent event){ |
114 |
//TODO: implement this |
115 |
return null; |
116 |
} |
117 |
|
118 |
protected Bundle idToBundle(String id){ |
119 |
//TODO: implement this |
120 |
return null; |
121 |
} |
122 |
|
123 |
public void addListener(FrameworkListener listener){ |
124 |
eventHandler.addListener(listener, false); |
125 |
} |
126 |
|
127 |
} |