--- /dev/null
+/*******************************************************************************\r
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
+ * in Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ * VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.project.management;\r
+\r
+import org.eclipse.core.runtime.IPath;\r
+import org.eclipse.core.runtime.Platform;\r
+import org.eclipse.equinox.internal.p2.core.DefaultAgentProvider;\r
+import org.eclipse.equinox.p2.core.IProvisioningAgent;\r
+import org.eclipse.equinox.p2.core.IProvisioningAgentProvider;\r
+import org.eclipse.equinox.p2.core.ProvisionException;\r
+import org.eclipse.equinox.p2.core.UIServices;\r
+import org.osgi.framework.BundleContext;\r
+import org.osgi.framework.ServiceReference;\r
+\r
+@SuppressWarnings("restriction")\r
+public class P2Util {\r
+ \r
+// private static void initializeServices() throws CoreException {\r
+// BundleContext context = Activator.getContext();\r
+// packageAdminRef = context.getServiceReference(PackageAdmin.class.getName());\r
+// packageAdmin = (PackageAdmin) context.getService(packageAdminRef);\r
+// ServiceReference agentProviderRef = context.getServiceReference(IProvisioningAgentProvider.SERVICE_NAME);\r
+// IProvisioningAgentProvider provider = (IProvisioningAgentProvider) context.getService(agentProviderRef);\r
+// URI p2DataArea;\r
+// if (destination != null || sharedLocation != null) {\r
+// File dataAreaFile = sharedLocation == null ? new File(destination, "p2") : sharedLocation;//$NON-NLS-1$\r
+// p2DataArea = dataAreaFile.toURI();\r
+// } else {\r
+// p2DataArea = null;\r
+// }\r
+// targetAgent = provider.createAgent(p2DataArea);\r
+// targetAgent.registerService(IProvisioningAgent.INSTALLER_AGENT, provider.createAgent(null));\r
+//\r
+// context.ungetService(agentProviderRef);\r
+// if (profileId == null) {\r
+// if (destination != null) {\r
+// File configIni = new File(destination, "configuration/config.ini"); //$NON-NLS-1$\r
+// InputStream in = null;\r
+// try {\r
+// Properties ciProps = new Properties();\r
+// in = new BufferedInputStream(new FileInputStream(configIni));\r
+// ciProps.load(in);\r
+// profileId = ciProps.getProperty(PROP_P2_PROFILE);\r
+// } catch (IOException e) {\r
+// // Ignore\r
+// } finally {\r
+// if (in != null)\r
+// try {\r
+// in.close();\r
+// } catch (IOException e) {\r
+// // Ignore;\r
+// }\r
+// }\r
+// if (profileId == null)\r
+// profileId = destination.toString();\r
+// }\r
+// }\r
+// if (profileId != null)\r
+// targetAgent.registerService(PROP_P2_PROFILE, profileId);\r
+// else\r
+// targetAgent.unregisterService(PROP_P2_PROFILE, null);\r
+//\r
+// IDirector director = (IDirector) targetAgent.getService(IDirector.SERVICE_NAME);\r
+// if (director == null)\r
+// throw new ProvisionException(Messages.Missing_director);\r
+//\r
+// planner = (IPlanner) targetAgent.getService(IPlanner.SERVICE_NAME);\r
+// if (planner == null)\r
+// throw new ProvisionException(Messages.Missing_planner);\r
+//\r
+// engine = (IEngine) targetAgent.getService(IEngine.SERVICE_NAME);\r
+// if (engine == null)\r
+// throw new ProvisionException(Messages.Missing_Engine);\r
+//\r
+// targetAgent.registerService(UIServices.SERVICE_NAME, new AvoidTrustPromptService());\r
+// } \r
+ public static BundleContext getBundleContext() {\r
+ return Platform.getBundle("org.simantics.project").getBundleContext();\r
+ }\r
+ \r
+ public static IProvisioningAgentProvider getProvisioningAgentProvider() {\r
+ BundleContext context = getBundleContext(); \r
+ IProvisioningAgentProvider provider = (IProvisioningAgentProvider) getService(context, IProvisioningAgentProvider.SERVICE_NAME);\r
+ if (provider==null) {\r
+ DefaultAgentProvider p = new DefaultAgentProvider();\r
+ p.activate(context);\r
+ provider = p;\r
+ }\r
+ return provider; \r
+ }\r
+ \r
+ /**\r
+ * Create an provisioning agent.\r
+ * \r
+ * @param agentLocation\r
+ * @return\r
+ * @throws ProvisionException \r
+ */\r
+ public static IProvisioningAgent createAgent(IPath agentLocation) throws ProvisionException {\r
+ // Create recruiter\r
+ IProvisioningAgentProvider provider = getProvisioningAgentProvider();\r
+\r
+ // Recruit Agent - Permission to provision\r
+ IProvisioningAgent agent = provider.createAgent(agentLocation == null ? null : agentLocation.toFile().toURI());\r
+ \r
+ // Omit "This bundle is not certified" dialog. \r
+ UIServices serviceUI = new TrustAllCertificates();\r
+ agent.registerService("org.eclipse.equinox.p2.core.UIServices", serviceUI); \r
+ \r
+ return agent;\r
+ } \r
+ \r
+ \r
+ /**\r
+ * Copied from ServiceHelper because we need to obtain services\r
+ * before p2 has been started.\r
+ */\r
+ public static Object getService(BundleContext context, String name) {\r
+ if (context == null)\r
+ return null;\r
+ ServiceReference reference = context.getServiceReference(name);\r
+ if (reference == null)\r
+ return null;\r
+ Object result = context.getService(reference);\r
+ context.ungetService(reference);\r
+ return result;\r
+ } \r
+\r
+}\r
+\r