]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.security.ui/src/org/argeo/security/ui/SecurityUiPlugin.java
Work on authentication
[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.cms.widgets.auth.DefaultLoginDialog;
26 import org.eclipse.swt.widgets.Display;
27 import org.osgi.framework.BundleActivator;
28 import org.osgi.framework.BundleContext;
29 import org.osgi.framework.ServiceRegistration;
30
31 /**
32 * The activator class controls the plug-in life cycle
33 */
34 public class SecurityUiPlugin implements BundleActivator {
35 private static BundleContext bundleContext;
36 public static InheritableThreadLocal<Display> display = new InheritableThreadLocal<Display>() {
37
38 @Override
39 protected Display initialValue() {
40 return Display.getCurrent();
41 }
42 };
43
44 // The plug-in ID
45 public final static String PLUGIN_ID = "org.argeo.security.ui"; //$NON-NLS-1$
46
47 final static String CONTEXT_KEYRING = "KEYRING";
48
49 private CallbackHandler defaultCallbackHandler;
50 private ServiceRegistration<CallbackHandler> defaultCallbackHandlerReg;
51
52 public void start(BundleContext context) throws Exception {
53 if (bundleContext != null)
54 if (!bundleContext.equals(bundleContext))
55 throw new ArgeoException(
56 "Bundle context already set with a different value");
57 else
58 return;
59
60 bundleContext = context;
61
62 defaultCallbackHandler = new DefaultCallbackHandler();
63 defaultCallbackHandlerReg = context.registerService(
64 CallbackHandler.class, defaultCallbackHandler, null);
65 }
66
67 public void stop(BundleContext context) throws Exception {
68 bundleContext = null;
69 defaultCallbackHandlerReg.unregister();
70 }
71
72 public static BundleContext getBundleContext() {
73 return bundleContext;
74 }
75
76 protected class DefaultCallbackHandler implements CallbackHandler {
77 public void handle(final Callback[] callbacks) throws IOException,
78 UnsupportedCallbackException {
79
80 // if (display != null) // RCP
81 Display displayToUse = display.get();
82 if (displayToUse == null)// RCP
83 displayToUse = Display.getDefault();
84 displayToUse.syncExec(new Runnable() {
85 public void run() {
86 DefaultLoginDialog dialog = new DefaultLoginDialog(display
87 .get().getActiveShell());
88 try {
89 dialog.handle(callbacks);
90 } catch (IOException e) {
91 throw new ArgeoException("Cannot open dialog", e);
92 }
93 }
94 });
95 // else {// RAP
96 // DefaultLoginDialog dialog = new DefaultLoginDialog();
97 // dialog.handle(callbacks);
98 // }
99 }
100
101 }
102 }