View Javadoc
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.cms.ui.workbench;
17  
18  import java.io.IOException;
19  import java.util.ResourceBundle;
20  
21  import javax.security.auth.callback.Callback;
22  import javax.security.auth.callback.CallbackHandler;
23  import javax.security.auth.callback.UnsupportedCallbackException;
24  
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;
37  
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);
41  
42  	// The plug-in ID
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$
45  
46  	private ResourceBundle messages;
47  	private static BundleContext bundleContext;
48  
49  	public static InheritableThreadLocal<Display> display = new InheritableThreadLocal<Display>() {
50  
51  		@Override
52  		protected Display initialValue() {
53  			return Display.getCurrent();
54  		}
55  	};
56  
57  	final static String CONTEXT_KEYRING = "KEYRING";
58  
59  	private CallbackHandler defaultCallbackHandler;
60  	private ServiceRegistration<CallbackHandler> defaultCallbackHandlerReg;
61  
62  	// The shared instance
63  	private static WorkbenchUiPlugin plugin;
64  
65  	public void start(BundleContext context) throws Exception {
66  		super.start(context);
67  		bundleContext = context;
68  		defaultCallbackHandler = new DefaultCallbackHandler();
69  		defaultCallbackHandlerReg = context.registerService(CallbackHandler.class, defaultCallbackHandler, null);
70  
71  		plugin = this;
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");
76  	}
77  
78  	public void stop(BundleContext context) throws Exception {
79  		bundleContext = null;
80  		defaultCallbackHandlerReg.unregister();
81  	}
82  
83  	public static BundleContext getBundleContext() {
84  		return bundleContext;
85  	}
86  
87  	/*
88  	 * Returns the shared instance
89  	 * 
90  	 * @return the shared instance
91  	 */
92  	public static WorkbenchUiPlugin getDefault() {
93  		return plugin;
94  	}
95  
96  	protected class DefaultCallbackHandler implements CallbackHandler {
97  		public void handle(final Callback[] callbacks) throws IOException, UnsupportedCallbackException {
98  
99  			// if (display != null) // RCP
100 			Display displayToUse = display.get();
101 			if (displayToUse == null)// RCP
102 				displayToUse = Display.getDefault();
103 			displayToUse.syncExec(new Runnable() {
104 				public void run() {
105 					DefaultLoginDialog dialog = new DefaultLoginDialog(display.get().getActiveShell());
106 					try {
107 						dialog.handle(callbacks);
108 					} catch (IOException e) {
109 						throw new CmsException("Cannot open dialog", e);
110 					}
111 				}
112 			});
113 			// else {// RAP
114 			// DefaultLoginDialog dialog = new DefaultLoginDialog();
115 			// dialog.handle(callbacks);
116 			// }
117 		}
118 
119 	}
120 
121 	public static ImageDescriptor getImageDescriptor(String path) {
122 		return imageDescriptorFromPlugin(THEME_PLUGIN_ID, path);
123 	}
124 
125 	/** Returns the internationalized label for the given key */
126 	public static String getMessage(String key) {
127 		try {
128 			return getDefault().messages.getString(key);
129 		} catch (NullPointerException npe) {
130 			log.warn(key + " not found.");
131 			return key;
132 		}
133 	}
134 
135 	/**
136 	 * Gives access to the internationalization message bundle. Returns null in case
137 	 * this UiPlugin is not started (for JUnit tests, by instance)
138 	 */
139 	public static ResourceBundle getMessagesBundle() {
140 		if (getDefault() != null)
141 			// To avoid NPE
142 			return getDefault().messages;
143 		else
144 			return null;
145 	}
146 
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());
159 	}
160 }