1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.project.management;
14 import org.eclipse.core.runtime.IPath;
15 import org.eclipse.core.runtime.Platform;
16 import org.eclipse.equinox.internal.p2.core.DefaultAgentProvider;
17 import org.eclipse.equinox.p2.core.IProvisioningAgent;
18 import org.eclipse.equinox.p2.core.IProvisioningAgentProvider;
19 import org.eclipse.equinox.p2.core.ProvisionException;
20 import org.eclipse.equinox.p2.core.UIServices;
21 import org.osgi.framework.BundleContext;
22 import org.osgi.framework.ServiceReference;
24 @SuppressWarnings("restriction")
27 // private static void initializeServices() throws CoreException {
28 // BundleContext context = Activator.getContext();
29 // packageAdminRef = context.getServiceReference(PackageAdmin.class.getName());
30 // packageAdmin = (PackageAdmin) context.getService(packageAdminRef);
31 // ServiceReference agentProviderRef = context.getServiceReference(IProvisioningAgentProvider.SERVICE_NAME);
32 // IProvisioningAgentProvider provider = (IProvisioningAgentProvider) context.getService(agentProviderRef);
34 // if (destination != null || sharedLocation != null) {
35 // File dataAreaFile = sharedLocation == null ? new File(destination, "p2") : sharedLocation;//$NON-NLS-1$
36 // p2DataArea = dataAreaFile.toURI();
40 // targetAgent = provider.createAgent(p2DataArea);
41 // targetAgent.registerService(IProvisioningAgent.INSTALLER_AGENT, provider.createAgent(null));
43 // context.ungetService(agentProviderRef);
44 // if (profileId == null) {
45 // if (destination != null) {
46 // File configIni = new File(destination, "configuration/config.ini"); //$NON-NLS-1$
47 // InputStream in = null;
49 // Properties ciProps = new Properties();
50 // in = new BufferedInputStream(new FileInputStream(configIni));
52 // profileId = ciProps.getProperty(PROP_P2_PROFILE);
53 // } catch (IOException e) {
59 // } catch (IOException e) {
63 // if (profileId == null)
64 // profileId = destination.toString();
67 // if (profileId != null)
68 // targetAgent.registerService(PROP_P2_PROFILE, profileId);
70 // targetAgent.unregisterService(PROP_P2_PROFILE, null);
72 // IDirector director = (IDirector) targetAgent.getService(IDirector.SERVICE_NAME);
73 // if (director == null)
74 // throw new ProvisionException(Messages.Missing_director);
76 // planner = (IPlanner) targetAgent.getService(IPlanner.SERVICE_NAME);
77 // if (planner == null)
78 // throw new ProvisionException(Messages.Missing_planner);
80 // engine = (IEngine) targetAgent.getService(IEngine.SERVICE_NAME);
81 // if (engine == null)
82 // throw new ProvisionException(Messages.Missing_Engine);
84 // targetAgent.registerService(UIServices.SERVICE_NAME, new AvoidTrustPromptService());
86 public static BundleContext getBundleContext() {
87 return Platform.getBundle("org.simantics.project").getBundleContext();
90 public static IProvisioningAgentProvider getProvisioningAgentProvider() {
91 BundleContext context = getBundleContext();
92 IProvisioningAgentProvider provider = (IProvisioningAgentProvider) getService(context, IProvisioningAgentProvider.SERVICE_NAME);
94 DefaultAgentProvider p = new DefaultAgentProvider();
102 * Create an provisioning agent.
104 * @param agentLocation
106 * @throws ProvisionException
108 public static IProvisioningAgent createAgent(IPath agentLocation) throws ProvisionException {
110 IProvisioningAgentProvider provider = getProvisioningAgentProvider();
112 // Recruit Agent - Permission to provision
113 IProvisioningAgent agent = provider.createAgent(agentLocation == null ? null : agentLocation.toFile().toURI());
115 // Omit "This bundle is not certified" dialog.
116 UIServices serviceUI = new TrustAllCertificates();
117 agent.registerService("org.eclipse.equinox.p2.core.UIServices", serviceUI);
124 * Copied from ServiceHelper because we need to obtain services
125 * before p2 has been started.
127 public static Object getService(BundleContext context, String name) {
130 ServiceReference reference = context.getServiceReference(name);
131 if (reference == null)
133 Object result = context.getService(reference);
134 context.ungetService(reference);