]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.project/src/org/simantics/project/management/P2Util.java
Added equations to exported packages
[simantics/platform.git] / bundles / org.simantics.project / src / org / simantics / project / management / P2Util.java
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
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.project.management;\r
13 \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
23 \r
24 @SuppressWarnings("restriction")\r
25 public class P2Util {\r
26         \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
33 //              URI p2DataArea;\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
37 //              } else {\r
38 //                      p2DataArea = null;\r
39 //              }\r
40 //              targetAgent = provider.createAgent(p2DataArea);\r
41 //              targetAgent.registerService(IProvisioningAgent.INSTALLER_AGENT, provider.createAgent(null));\r
42 //\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
48 //                              try {\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
54 //                                      // Ignore\r
55 //                              } finally {\r
56 //                                      if (in != null)\r
57 //                                              try {\r
58 //                                                      in.close();\r
59 //                                              } catch (IOException e) {\r
60 //                                                      // Ignore;\r
61 //                                              }\r
62 //                              }\r
63 //                              if (profileId == null)\r
64 //                                      profileId = destination.toString();\r
65 //                      }\r
66 //              }\r
67 //              if (profileId != null)\r
68 //                      targetAgent.registerService(PROP_P2_PROFILE, profileId);\r
69 //              else\r
70 //                      targetAgent.unregisterService(PROP_P2_PROFILE, null);\r
71 //\r
72 //              IDirector director = (IDirector) targetAgent.getService(IDirector.SERVICE_NAME);\r
73 //              if (director == null)\r
74 //                      throw new ProvisionException(Messages.Missing_director);\r
75 //\r
76 //              planner = (IPlanner) targetAgent.getService(IPlanner.SERVICE_NAME);\r
77 //              if (planner == null)\r
78 //                      throw new ProvisionException(Messages.Missing_planner);\r
79 //\r
80 //              engine = (IEngine) targetAgent.getService(IEngine.SERVICE_NAME);\r
81 //              if (engine == null)\r
82 //                      throw new ProvisionException(Messages.Missing_Engine);\r
83 //\r
84 //              targetAgent.registerService(UIServices.SERVICE_NAME, new AvoidTrustPromptService());\r
85 //      }       \r
86         public static BundleContext getBundleContext() {\r
87                 return Platform.getBundle("org.simantics.project").getBundleContext();\r
88         }\r
89         \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
96                         provider = p;\r
97                 }\r
98                 return provider;                \r
99         }\r
100         \r
101         /**\r
102          * Create an provisioning agent.\r
103          * \r
104          * @param agentLocation\r
105          * @return\r
106          * @throws ProvisionException \r
107          */\r
108         public static IProvisioningAgent createAgent(IPath agentLocation) throws ProvisionException {\r
109                 // Create recruiter\r
110                 IProvisioningAgentProvider provider = getProvisioningAgentProvider();\r
111 \r
112                 // Recruit Agent - Permission to provision\r
113                 IProvisioningAgent agent = provider.createAgent(agentLocation == null ? null : agentLocation.toFile().toURI());\r
114                         \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
118                         \r
119                 return agent;\r
120         }       \r
121         \r
122         \r
123         /**\r
124          * Copied from ServiceHelper because we need to obtain services\r
125          * before p2 has been started.\r
126          */\r
127         public static Object getService(BundleContext context, String name) {\r
128                 if (context == null)\r
129                         return null;\r
130                 ServiceReference reference = context.getServiceReference(name);\r
131                 if (reference == null)\r
132                         return null;\r
133                 Object result = context.getService(reference);\r
134                 context.ungetService(reference);\r
135                 return result;\r
136         }       \r
137 \r
138 }\r
139 \r