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