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