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$
44 public final static String THEME_PLUGIN_ID
= "org.argeo.cms.ui.theme"; //$NON-NLS-1$
46 private ResourceBundle messages
;
47 private static BundleContext bundleContext
;
49 public static InheritableThreadLocal
<Display
> display
= new InheritableThreadLocal
<Display
>() {
52 protected Display
initialValue() {
53 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(CallbackHandler
.class, defaultCallbackHandler
, null);
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");
78 public void stop(BundleContext context
) throws Exception
{
80 defaultCallbackHandlerReg
.unregister();
83 public static BundleContext
getBundleContext() {
88 * Returns the shared instance
90 * @return the shared instance
92 public static WorkbenchUiPlugin
getDefault() {
96 protected class DefaultCallbackHandler
implements CallbackHandler
{
97 public void handle(final Callback
[] callbacks
) throws IOException
, UnsupportedCallbackException
{
99 // if (display != null) // RCP
100 Display displayToUse
= display
.get();
101 if (displayToUse
== null)// RCP
102 displayToUse
= Display
.getDefault();
103 displayToUse
.syncExec(new Runnable() {
105 DefaultLoginDialog dialog
= new DefaultLoginDialog(display
.get().getActiveShell());
107 dialog
.handle(callbacks
);
108 } catch (IOException e
) {
109 throw new CmsException("Cannot open dialog", e
);
114 // DefaultLoginDialog dialog = new DefaultLoginDialog();
115 // dialog.handle(callbacks);
121 public static ImageDescriptor
getImageDescriptor(String path
) {
122 return imageDescriptorFromPlugin(THEME_PLUGIN_ID
, path
);
125 /** Returns the internationalized label for the given key */
126 public static String
getMessage(String key
) {
128 return getDefault().messages
.getString(key
);
129 } catch (NullPointerException npe
) {
130 log
.warn(key
+ " not found.");
136 * Gives access to the internationalization message bundle. Returns null in case
137 * this UiPlugin is not started (for JUnit tests, by instance)
139 public static ResourceBundle
getMessagesBundle() {
140 if (getDefault() != null)
142 return getDefault().messages
;
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());