2 * Copyright (C) 2007-2012 Argeo GmbH
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package org
.argeo
.cms
.ui
.workbench
;
18 import java
.io
.IOException
;
19 import java
.util
.ResourceBundle
;
21 import javax
.security
.auth
.callback
.Callback
;
22 import javax
.security
.auth
.callback
.CallbackHandler
;
23 import javax
.security
.auth
.callback
.UnsupportedCallbackException
;
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
;
39 * The activator class controls the plug-in life cycle
41 public class WorkbenchUiPlugin
extends AbstractUIPlugin
implements ILogListener
{
43 private final static Log log
= LogFactory
.getLog(WorkbenchUiPlugin
.class);
45 private ResourceBundle messages
;
47 private static BundleContext bundleContext
;
48 public static InheritableThreadLocal
<Display
> display
= new InheritableThreadLocal
<Display
>() {
51 protected Display
initialValue() {
52 return Display
.getCurrent();
57 // public final static String PLUGIN_ID = "org.argeo.security.ui"; //$NON-NLS-1$
58 public final static String PLUGIN_ID
= "org.argeo.cms.ui.workbench"; //$NON-NLS-1$
60 final static String CONTEXT_KEYRING
= "KEYRING";
62 private CallbackHandler defaultCallbackHandler
;
63 private ServiceRegistration
<CallbackHandler
> defaultCallbackHandlerReg
;
65 // The shared instance
66 private static WorkbenchUiPlugin plugin
;
68 public void start(BundleContext context
) throws Exception
{
71 bundleContext
= context
;
73 defaultCallbackHandler
= new DefaultCallbackHandler();
74 defaultCallbackHandlerReg
= context
.registerService(
75 CallbackHandler
.class, defaultCallbackHandler
, null);
78 messages
= ResourceBundle
.getBundle(PLUGIN_ID
+ ".messages");
79 Platform
.addLogListener(this);
80 if (log
.isTraceEnabled())
81 log
.trace("Eclipse logging now directed to standard logging");
85 public void stop(BundleContext context
) throws Exception
{
87 defaultCallbackHandlerReg
.unregister();
90 public static BundleContext
getBundleContext() {
95 * Returns the shared instance
97 * @return the shared instance
99 public static WorkbenchUiPlugin
getDefault() {
103 protected class DefaultCallbackHandler
implements CallbackHandler
{
104 public void handle(final Callback
[] callbacks
) throws IOException
,
105 UnsupportedCallbackException
{
107 // if (display != null) // RCP
108 Display displayToUse
= display
.get();
109 if (displayToUse
== null)// RCP
110 displayToUse
= Display
.getDefault();
111 displayToUse
.syncExec(new Runnable() {
113 DefaultLoginDialog dialog
= new DefaultLoginDialog(display
114 .get().getActiveShell());
116 dialog
.handle(callbacks
);
117 } catch (IOException e
) {
118 throw new CmsException("Cannot open dialog", e
);
123 // DefaultLoginDialog dialog = new DefaultLoginDialog();
124 // dialog.handle(callbacks);
130 public static ImageDescriptor
getImageDescriptor(String path
) {
131 return imageDescriptorFromPlugin(PLUGIN_ID
, path
);
134 /** Returns the internationalized label for the given key */
135 public static String
getMessage(String key
) {
137 return getDefault().messages
.getString(key
);
138 } catch (NullPointerException npe
) {
139 log
.warn(key
+ " not found.");
145 * Gives access to the internationalization message bundle. Returns null in
146 * case this UiPlugin is not started (for JUnit tests, by instance)
148 public static ResourceBundle
getMessagesBundle() {
149 if (getDefault() != null)
151 return getDefault().messages
;
156 public void logging(IStatus status
, String plugin
) {
157 Log pluginLog
= LogFactory
.getLog(plugin
);
158 Integer severity
= status
.getSeverity();
159 if (severity
== IStatus
.ERROR
)
160 pluginLog
.error(status
.getMessage(), status
.getException());
161 else if (severity
== IStatus
.WARNING
)
162 pluginLog
.warn(status
.getMessage(), status
.getException());
163 else if (severity
== IStatus
.INFO
)
164 pluginLog
.info(status
.getMessage(), status
.getException());
165 else if (severity
== IStatus
.CANCEL
)
166 if (pluginLog
.isDebugEnabled())
167 pluginLog
.debug(status
.getMessage(), status
.getException());