]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.security.ui/src/org/argeo/security/ui/SecurityUiPlugin.java
a19fec17d02cb9ad107e23eaba5768d7f3ccf11e
[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 private static BundleContext bundleContext;
47
48 public static InheritableThreadLocal<Display> display = new InheritableThreadLocal<Display>() {
49
50 @Override
51 protected Display initialValue() {
52 return Display.getCurrent();
53 }
54 };
55
56 public void start(BundleContext context) throws Exception {
57 super.start(context);
58 plugin = this;
59 bundleContext = context;
60
61 defaultCallbackHandler = new DefaultCallbackHandler();
62 defaultCallbackHandlerReg = context.registerService(
63 CallbackHandler.class.getName(), defaultCallbackHandler, null);
64 }
65
66 public void stop(BundleContext context) throws Exception {
67 plugin = null;
68 bundleContext = null;
69 defaultCallbackHandlerReg.unregister();
70 super.stop(context);
71 }
72
73 /**
74 * Returns the shared instance
75 *
76 * @return the shared instance
77 */
78 public static SecurityUiPlugin getDefault() {
79 return plugin;
80 }
81
82 public static BundleContext getBundleContext() {
83 return bundleContext;
84 }
85
86 public static ImageDescriptor getImageDescriptor(String path) {
87 return imageDescriptorFromPlugin(PLUGIN_ID, path);
88 }
89
90 protected class DefaultCallbackHandler implements CallbackHandler {
91 public void handle(final Callback[] callbacks) throws IOException,
92 UnsupportedCallbackException {
93
94 // if (display != null) // RCP
95 Display displayToUse = display.get();
96 if (displayToUse == null)// RCP
97 displayToUse = Display.getDefault();
98 displayToUse.syncExec(new Runnable() {
99 public void run() {
100 DefaultLoginDialog dialog = new DefaultLoginDialog(display
101 .get().getActiveShell());
102 try {
103 dialog.handle(callbacks);
104 } catch (IOException e) {
105 throw new ArgeoException("Cannot open dialog", e);
106 }
107 }
108 });
109 // else {// RAP
110 // DefaultLoginDialog dialog = new DefaultLoginDialog();
111 // dialog.handle(callbacks);
112 // }
113 }
114
115 }
116 }