]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.security.ui/src/org/argeo/security/ui/SecurityUiPlugin.java
03584185bf06ad73193068ec1b2d717543262459
[lgpl/argeo-commons.git] / org.argeo.security.ui / src / org / argeo / security / ui / SecurityUiPlugin.java
1 /*
2 * Copyright (C) 2007-2012 Argeo GmbH
3 *
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
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16 package org.argeo.security.ui;
17
18 import java.io.IOException;
19
20 import javax.security.auth.callback.Callback;
21 import javax.security.auth.callback.CallbackHandler;
22 import javax.security.auth.callback.UnsupportedCallbackException;
23
24 import org.argeo.ArgeoException;
25 import org.argeo.security.ui.dialogs.DefaultLoginDialog;
26 import org.eclipse.jface.resource.ImageDescriptor;
27 import org.eclipse.swt.widgets.Display;
28 import org.eclipse.ui.plugin.AbstractUIPlugin;
29 import org.osgi.framework.BundleContext;
30 import org.osgi.framework.ServiceRegistration;
31
32 /**
33 * The activator class controls the plug-in life cycle
34 */
35 public class SecurityUiPlugin extends AbstractUIPlugin {
36
37 // The plug-in ID
38 public static final String PLUGIN_ID = "org.argeo.security.ui"; //$NON-NLS-1$
39
40 public final static String CONTEXT_KEYRING = "KEYRING";
41
42 private CallbackHandler defaultCallbackHandler;
43 private ServiceRegistration defaultCallbackHandlerReg;
44
45 private static SecurityUiPlugin plugin;
46
47 public static InheritableThreadLocal<Display> display = new InheritableThreadLocal<Display>() {
48
49 @Override
50 protected Display initialValue() {
51 return Display.getCurrent();
52 }
53 };
54
55 public void start(BundleContext context) throws Exception {
56 super.start(context);
57 plugin = this;
58
59 defaultCallbackHandler = new DefaultCallbackHandler();
60 defaultCallbackHandlerReg = context.registerService(
61 CallbackHandler.class.getName(), defaultCallbackHandler, null);
62 }
63
64 public void stop(BundleContext context) throws Exception {
65 plugin = null;
66 defaultCallbackHandlerReg.unregister();
67 super.stop(context);
68 }
69
70 /**
71 * Returns the shared instance
72 *
73 * @return the shared instance
74 */
75 public static SecurityUiPlugin getDefault() {
76 return plugin;
77 }
78
79 public static ImageDescriptor getImageDescriptor(String path) {
80 return imageDescriptorFromPlugin(PLUGIN_ID, path);
81 }
82
83 protected class DefaultCallbackHandler implements CallbackHandler {
84 public void handle(final Callback[] callbacks) throws IOException,
85 UnsupportedCallbackException {
86
87 // if (display != null) // RCP
88 Display displayToUse = display.get();
89 if (displayToUse == null)// RCP
90 displayToUse = Display.getDefault();
91 displayToUse.syncExec(new Runnable() {
92 public void run() {
93 DefaultLoginDialog dialog = new DefaultLoginDialog(display
94 .get().getActiveShell());
95 try {
96 dialog.handle(callbacks);
97 } catch (IOException e) {
98 throw new ArgeoException("Cannot open dialog", e);
99 }
100 }
101 });
102 // else {// RAP
103 // DefaultLoginDialog dialog = new DefaultLoginDialog();
104 // dialog.handle(callbacks);
105 // }
106 }
107
108 }
109 }