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
;
38 /** The activator class controls the plug-in life cycle */
39 public class WorkbenchUiPlugin
extends AbstractUIPlugin
implements ILogListener
{
40 private final static Log log
= LogFactory
.getLog(WorkbenchUiPlugin
.class);
43 public final static String PLUGIN_ID
= "org.argeo.cms.ui.workbench"; //$NON-NLS-1$
45 private ResourceBundle messages
;
46 private static BundleContext bundleContext
;
48 public static InheritableThreadLocal
<Display
> display
= new InheritableThreadLocal
<Display
>() {
51 protected Display
initialValue() {
52 return Display
.getCurrent();
57 final static String CONTEXT_KEYRING
= "KEYRING";
59 private CallbackHandler defaultCallbackHandler
;
60 private ServiceRegistration
<CallbackHandler
> defaultCallbackHandlerReg
;
62 // The shared instance
63 private static WorkbenchUiPlugin plugin
;
65 public void start(BundleContext context
) throws Exception
{
67 bundleContext
= context
;
68 defaultCallbackHandler
= new DefaultCallbackHandler();
69 defaultCallbackHandlerReg
= context
.registerService(
70 CallbackHandler
.class, defaultCallbackHandler
, null);
73 messages
= ResourceBundle
.getBundle(PLUGIN_ID
+ ".messages");
74 Platform
.addLogListener(this);
75 if (log
.isTraceEnabled())
76 log
.trace("Eclipse logging now directed to standard logging");
79 public void stop(BundleContext context
) throws Exception
{
81 defaultCallbackHandlerReg
.unregister();
84 public static BundleContext
getBundleContext() {
89 * Returns the shared instance
91 * @return the shared instance
93 public static WorkbenchUiPlugin
getDefault() {
97 protected class DefaultCallbackHandler
implements CallbackHandler
{
98 public void handle(final Callback
[] callbacks
) throws IOException
,
99 UnsupportedCallbackException
{
101 // if (display != null) // RCP
102 Display displayToUse
= display
.get();
103 if (displayToUse
== null)// RCP
104 displayToUse
= Display
.getDefault();
105 displayToUse
.syncExec(new Runnable() {
107 DefaultLoginDialog dialog
= new DefaultLoginDialog(display
108 .get().getActiveShell());
110 dialog
.handle(callbacks
);
111 } catch (IOException e
) {
112 throw new CmsException("Cannot open dialog", e
);
117 // DefaultLoginDialog dialog = new DefaultLoginDialog();
118 // dialog.handle(callbacks);
124 public static ImageDescriptor
getImageDescriptor(String path
) {
125 return imageDescriptorFromPlugin(PLUGIN_ID
, path
);
128 /** Returns the internationalized label for the given key */
129 public static String
getMessage(String key
) {
131 return getDefault().messages
.getString(key
);
132 } catch (NullPointerException npe
) {
133 log
.warn(key
+ " not found.");
139 * Gives access to the internationalization message bundle. Returns null in
140 * case this UiPlugin is not started (for JUnit tests, by instance)
142 public static ResourceBundle
getMessagesBundle() {
143 if (getDefault() != null)
145 return getDefault().messages
;
150 public void logging(IStatus status
, String plugin
) {
151 Log pluginLog
= LogFactory
.getLog(plugin
);
152 Integer severity
= status
.getSeverity();
153 if (severity
== IStatus
.ERROR
)
154 pluginLog
.error(status
.getMessage(), status
.getException());
155 else if (severity
== IStatus
.WARNING
)
156 pluginLog
.warn(status
.getMessage(), status
.getException());
157 else if (severity
== IStatus
.INFO
)
158 pluginLog
.info(status
.getMessage(), status
.getException());
159 else if (severity
== IStatus
.CANCEL
)
160 if (pluginLog
.isDebugEnabled())
161 pluginLog
.debug(status
.getMessage(), status
.getException());