1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.argeo.cms.ui.workbench;
17
18 import java.io.IOException;
19 import java.util.ResourceBundle;
20
21 import javax.security.auth.callback.Callback;
22 import javax.security.auth.callback.CallbackHandler;
23 import javax.security.auth.callback.UnsupportedCallbackException;
24
25 import org.apache.commons.logging.Log;
26 import org.apache.commons.logging.LogFactory;
27 import org.argeo.cms.CmsException;
28 import org.argeo.cms.widgets.auth.DefaultLoginDialog;
29 import org.eclipse.core.runtime.ILogListener;
30 import org.eclipse.core.runtime.IStatus;
31 import org.eclipse.core.runtime.Platform;
32 import org.eclipse.jface.resource.ImageDescriptor;
33 import org.eclipse.swt.widgets.Display;
34 import org.eclipse.ui.plugin.AbstractUIPlugin;
35 import org.osgi.framework.BundleContext;
36 import org.osgi.framework.ServiceRegistration;
37
38
39 public class WorkbenchUiPlugin extends AbstractUIPlugin implements ILogListener {
40 private final static Log log = LogFactory.getLog(WorkbenchUiPlugin.class);
41
42
43 public final static String PLUGIN_ID = "org.argeo.cms.ui.workbench";
44 public final static String THEME_PLUGIN_ID = "org.argeo.cms.ui.theme";
45
46 private ResourceBundle messages;
47 private static BundleContext bundleContext;
48
49 public static InheritableThreadLocal<Display> display = new InheritableThreadLocal<Display>() {
50
51 @Override
52 protected Display initialValue() {
53 return Display.getCurrent();
54 }
55 };
56
57 final static String CONTEXT_KEYRING = "KEYRING";
58
59 private CallbackHandler defaultCallbackHandler;
60 private ServiceRegistration<CallbackHandler> defaultCallbackHandlerReg;
61
62
63 private static WorkbenchUiPlugin plugin;
64
65 public void start(BundleContext context) throws Exception {
66 super.start(context);
67 bundleContext = context;
68 defaultCallbackHandler = new DefaultCallbackHandler();
69 defaultCallbackHandlerReg = context.registerService(CallbackHandler.class, defaultCallbackHandler, null);
70
71 plugin = this;
72 messages = ResourceBundle.getBundle(PLUGIN_ID + ".messages");
73 Platform.addLogListener(this);
74 if (log.isTraceEnabled())
75 log.trace("Eclipse logging now directed to standard logging");
76 }
77
78 public void stop(BundleContext context) throws Exception {
79 bundleContext = null;
80 defaultCallbackHandlerReg.unregister();
81 }
82
83 public static BundleContext getBundleContext() {
84 return bundleContext;
85 }
86
87
88
89
90
91
92 public static WorkbenchUiPlugin getDefault() {
93 return plugin;
94 }
95
96 protected class DefaultCallbackHandler implements CallbackHandler {
97 public void handle(final Callback[] callbacks) throws IOException, UnsupportedCallbackException {
98
99
100 Display displayToUse = display.get();
101 if (displayToUse == null)
102 displayToUse = Display.getDefault();
103 displayToUse.syncExec(new Runnable() {
104 public void run() {
105 DefaultLoginDialog dialog = new DefaultLoginDialog(display.get().getActiveShell());
106 try {
107 dialog.handle(callbacks);
108 } catch (IOException e) {
109 throw new CmsException("Cannot open dialog", e);
110 }
111 }
112 });
113
114
115
116
117 }
118
119 }
120
121 public static ImageDescriptor getImageDescriptor(String path) {
122 return imageDescriptorFromPlugin(THEME_PLUGIN_ID, path);
123 }
124
125
126 public static String getMessage(String key) {
127 try {
128 return getDefault().messages.getString(key);
129 } catch (NullPointerException npe) {
130 log.warn(key + " not found.");
131 return key;
132 }
133 }
134
135
136
137
138
139 public static ResourceBundle getMessagesBundle() {
140 if (getDefault() != null)
141
142 return getDefault().messages;
143 else
144 return null;
145 }
146
147 public void logging(IStatus status, String plugin) {
148 Log pluginLog = LogFactory.getLog(plugin);
149 Integer severity = status.getSeverity();
150 if (severity == IStatus.ERROR)
151 pluginLog.error(status.getMessage(), status.getException());
152 else if (severity == IStatus.WARNING)
153 pluginLog.warn(status.getMessage(), status.getException());
154 else if (severity == IStatus.INFO)
155 pluginLog.info(status.getMessage(), status.getException());
156 else if (severity == IStatus.CANCEL)
157 if (pluginLog.isDebugEnabled())
158 pluginLog.debug(status.getMessage(), status.getException());
159 }
160 }