P2 update implementation and editor version extraction
[taxeditor.git] / eu.etaxonomy.taxeditor.application / src / main / java / eu / etaxonomy / taxeditor / TaxonomicEditorPlugin.java
1 package eu.etaxonomy.taxeditor;
2
3 import org.apache.log4j.Logger;
4 import org.eclipse.equinox.p2.core.IProvisioningAgent;
5 import org.eclipse.equinox.p2.engine.IProfileRegistry;
6 import org.eclipse.equinox.p2.operations.ProvisioningSession;
7 import org.eclipse.equinox.p2.ui.Policy;
8 import org.eclipse.equinox.p2.ui.ProvisioningUI;
9 import org.eclipse.ui.plugin.AbstractUIPlugin;
10 import org.osgi.framework.Bundle;
11 import org.osgi.framework.BundleContext;
12 import org.osgi.framework.ServiceReference;
13 import org.osgi.framework.ServiceRegistration;
14 import org.osgi.service.packageadmin.PackageAdmin;
15
16 import eu.etaxonomy.taxeditor.model.MessagingUtils;
17
18 /**
19 * The activator class controls the plug-in life cycle
20 *
21 * @author n.hoffmann
22 * @version $Id: $
23 */
24 public class TaxonomicEditorPlugin extends AbstractUIPlugin {
25 private static final Logger logger = Logger
26 .getLogger(TaxonomicEditorPlugin.class);
27
28 // The plug-in ID
29 /** Constant <code>PLUGIN_ID="eu.etaxonomy.taxeditor.application"</code> */
30 public static final String PLUGIN_ID = "eu.etaxonomy.taxeditor.application";
31
32 // The shared instance
33 private static TaxonomicEditorPlugin plugin;
34
35 private PackageAdmin packageAdmin;
36
37 private ServiceReference packageAdminRef;
38
39 /** Constant <code>bundleContext</code> */
40 protected static BundleContext bundleContext;
41
42
43 private ServiceRegistration policyRegistration;
44
45 private ProvisioningSession session;
46 private ProvisioningUI ui;
47
48 // private CloudPolicy policy;
49
50 /**
51 * The constructor
52 */
53 public TaxonomicEditorPlugin() {
54 }
55
56 /*
57 * (non-Javadoc)
58 * @see org.eclipse.ui.plugin.AbstractUIPlugin#start(org.osgi.framework.BundleContext)
59 */
60 /** {@inheritDoc} */
61 @Override
62 public void start(BundleContext context) throws Exception {
63 super.start(context);
64
65 plugin = this;
66 bundleContext = context;
67
68 packageAdminRef = bundleContext.getServiceReference(PackageAdmin.class.getName());
69 packageAdmin = (PackageAdmin) bundleContext.getService(packageAdminRef);
70
71 logger.debug("Taxonomic Editor started.");
72 }
73
74 private Bundle getBundle(String symbolicName) {
75 if (packageAdmin == null) {
76 return null;
77 }
78 Bundle[] bundles = packageAdmin.getBundles(symbolicName, null);
79 if (bundles == null) {
80 return null;
81 }
82 // Return the first bundle that is not installed or uninstalled
83 for (int i = 0; i < bundles.length; i++) {
84 if ((bundles[i].getState() & (Bundle.INSTALLED | Bundle.UNINSTALLED)) == 0) {
85 return bundles[i];
86 }
87 }
88 return null;
89 }
90
91 /*
92 * (non-Javadoc)
93 * @see org.eclipse.ui.plugin.AbstractUIPlugin#stop(org.osgi.framework.BundleContext)
94 */
95 /** {@inheritDoc} */
96 @Override
97 public void stop(BundleContext context) throws Exception {
98 plugin = null;
99 super.stop(context);
100 MessagingUtils.info("Taxonomic Editor stopped.");
101 }
102
103 /**
104 * Returns the shared instance
105 *
106 * @return the shared instance
107 */
108 public static TaxonomicEditorPlugin getDefault() {
109 return plugin;
110 }
111
112 public static BundleContext getContext() {
113 return bundleContext;
114 }
115
116 public ProvisioningUI getProvisioningUI() {
117
118 if (ui == null) {
119 ServiceReference reference = bundleContext.getServiceReference(IProvisioningAgent.SERVICE_NAME);
120 IProvisioningAgent agent = (IProvisioningAgent) bundleContext.getService(reference);
121 session = new ProvisioningSession(agent);
122 Policy policy = null;// = (Policy) ServiceHelper.getService(ProvUIActivator.getContext(), Policy.class.getName());
123 if (policy == null) {
124 policy = new Policy();
125 }
126 ui = new ProvisioningUI(session, IProfileRegistry.SELF, policy);
127 }
128 return ui;
129 }
130
131
132 }