]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.project/src/org/simantics/project/management/P2Util.java
Fixed race condition in getValues/Relations/EntityTypes
[simantics/platform.git] / bundles / org.simantics.project / src / org / simantics / project / management / P2Util.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
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
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.project.management;
13
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;
23
24 @SuppressWarnings("restriction")
25 public class P2Util {
26         
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);
33 //              URI p2DataArea;
34 //              if (destination != null || sharedLocation != null) {
35 //                      File dataAreaFile = sharedLocation == null ? new File(destination, "p2") : sharedLocation;//$NON-NLS-1$
36 //                      p2DataArea = dataAreaFile.toURI();
37 //              } else {
38 //                      p2DataArea = null;
39 //              }
40 //              targetAgent = provider.createAgent(p2DataArea);
41 //              targetAgent.registerService(IProvisioningAgent.INSTALLER_AGENT, provider.createAgent(null));
42 //
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;
48 //                              try {
49 //                                      Properties ciProps = new Properties();
50 //                                      in = new BufferedInputStream(new FileInputStream(configIni));
51 //                                      ciProps.load(in);
52 //                                      profileId = ciProps.getProperty(PROP_P2_PROFILE);
53 //                              } catch (IOException e) {
54 //                                      // Ignore
55 //                              } finally {
56 //                                      if (in != null)
57 //                                              try {
58 //                                                      in.close();
59 //                                              } catch (IOException e) {
60 //                                                      // Ignore;
61 //                                              }
62 //                              }
63 //                              if (profileId == null)
64 //                                      profileId = destination.toString();
65 //                      }
66 //              }
67 //              if (profileId != null)
68 //                      targetAgent.registerService(PROP_P2_PROFILE, profileId);
69 //              else
70 //                      targetAgent.unregisterService(PROP_P2_PROFILE, null);
71 //
72 //              IDirector director = (IDirector) targetAgent.getService(IDirector.SERVICE_NAME);
73 //              if (director == null)
74 //                      throw new ProvisionException(Messages.Missing_director);
75 //
76 //              planner = (IPlanner) targetAgent.getService(IPlanner.SERVICE_NAME);
77 //              if (planner == null)
78 //                      throw new ProvisionException(Messages.Missing_planner);
79 //
80 //              engine = (IEngine) targetAgent.getService(IEngine.SERVICE_NAME);
81 //              if (engine == null)
82 //                      throw new ProvisionException(Messages.Missing_Engine);
83 //
84 //              targetAgent.registerService(UIServices.SERVICE_NAME, new AvoidTrustPromptService());
85 //      }       
86         public static BundleContext getBundleContext() {
87                 return Platform.getBundle("org.simantics.project").getBundleContext();
88         }
89         
90         public static IProvisioningAgentProvider getProvisioningAgentProvider() {
91                 BundleContext context = getBundleContext();             
92                 IProvisioningAgentProvider provider = (IProvisioningAgentProvider) getService(context, IProvisioningAgentProvider.SERVICE_NAME);
93                 if (provider==null) {
94                         DefaultAgentProvider p = new DefaultAgentProvider();
95                         p.activate(context);
96                         provider = p;
97                 }
98                 return provider;                
99         }
100         
101         /**
102          * Create an provisioning agent.
103          * 
104          * @param agentLocation
105          * @return
106          * @throws ProvisionException 
107          */
108         public static IProvisioningAgent createAgent(IPath agentLocation) throws ProvisionException {
109                 // Create recruiter
110                 IProvisioningAgentProvider provider = getProvisioningAgentProvider();
111
112                 // Recruit Agent - Permission to provision
113                 IProvisioningAgent agent = provider.createAgent(agentLocation == null ? null : agentLocation.toFile().toURI());
114                         
115                 // Omit "This bundle is not certified" dialog.                  
116                 UIServices serviceUI = new TrustAllCertificates();
117                 agent.registerService("org.eclipse.equinox.p2.core.UIServices", serviceUI);                     
118                         
119                 return agent;
120         }       
121         
122         
123         /**
124          * Copied from ServiceHelper because we need to obtain services
125          * before p2 has been started.
126          */
127         public static Object getService(BundleContext context, String name) {
128                 if (context == null)
129                         return null;
130                 ServiceReference reference = context.getServiceReference(name);
131                 if (reference == null)
132                         return null;
133                 Object result = context.getService(reference);
134                 context.ungetService(reference);
135                 return result;
136         }       
137
138 }
139