KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > core > internal > runtime > auth > Activator


1 /*******************************************************************************
2  * Copyright (c) 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.core.internal.runtime.auth;
12
13 import java.util.ArrayList JavaDoc;
14 import org.eclipse.core.runtime.CoreException;
15 import org.eclipse.core.runtime.IStatus;
16 import org.eclipse.osgi.framework.log.FrameworkLog;
17 import org.eclipse.osgi.framework.log.FrameworkLogEntry;
18 import org.osgi.framework.BundleActivator;
19 import org.osgi.framework.BundleContext;
20 import org.osgi.util.tracker.ServiceTracker;
21
22 public class Activator implements BundleActivator {
23
24     private static BundleContext bundleContext;
25     private static ServiceTracker logTracker;
26
27     /*
28      * Return this activator's bundle context.
29      */

30     public static BundleContext getContext() {
31         return bundleContext;
32     }
33
34     /* (non-Javadoc)
35      * @see org.osgi.framework.BundleActivator#start(org.osgi.framework.BundleContext)
36      */

37     public void start(BundleContext context) throws Exception JavaDoc {
38         Activator.bundleContext = context;
39     }
40
41     /* (non-Javadoc)
42      * @see org.osgi.framework.BundleActivator#stop(org.osgi.framework.BundleContext)
43      */

44     public void stop(BundleContext context) throws Exception JavaDoc {
45         if (logTracker != null) {
46             logTracker.close();
47             logTracker = null;
48         }
49         Activator.bundleContext = null;
50     }
51
52     /*
53      * Log the given status in the framework log.
54      */

55     public static void log(IStatus status) {
56         if (logTracker == null) {
57             logTracker = new ServiceTracker(getContext(), FrameworkLog.class.getName(), null);
58             logTracker.open();
59         }
60         FrameworkLog log = (FrameworkLog) logTracker.getService();
61         log.log(getEntry(status));
62     }
63
64     /*
65      * Copied code from PlatformLogWriter to convert a status object into
66      * a FrameworkLogEntry.
67      */

68     private static FrameworkLogEntry getEntry(IStatus status) {
69         Throwable JavaDoc t = status.getException();
70         ArrayList JavaDoc childlist = new ArrayList JavaDoc();
71
72         int stackCode = t instanceof CoreException ? 1 : 0;
73         // ensure a sub-status inside a CoreException is properly logged
74
if (stackCode == 1) {
75             IStatus coreStatus = ((CoreException) t).getStatus();
76             if (coreStatus != null)
77                 childlist.add(getEntry(coreStatus));
78         }
79
80         if (status.isMultiStatus()) {
81             IStatus[] children = status.getChildren();
82             for (int i = 0; i < children.length; i++)
83                 childlist.add(getEntry(children[i]));
84         }
85
86         FrameworkLogEntry[] children = (FrameworkLogEntry[]) (childlist.size() == 0 ? null : childlist.toArray(new FrameworkLogEntry[childlist.size()]));
87
88         return new FrameworkLogEntry(status.getPlugin(), status.getSeverity(), status.getCode(), status.getMessage(), stackCode, t, children);
89     }
90
91 }
92
Popular Tags