View Javadoc

1   /*
2    * Copyright 2006-2008 the original author or authors.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.springframework.osgi.test.platform;
18  
19  import java.lang.reflect.Field;
20  import java.security.AccessController;
21  import java.security.PrivilegedAction;
22  import java.util.Properties;
23  
24  import org.eclipse.core.runtime.adaptor.EclipseStarter;
25  import org.osgi.framework.BundleContext;
26  
27  /**
28   * Equinox (3.2.x) OSGi platform.
29   * 
30   * @author Costin Leau
31   * 
32   */
33  public class EquinoxPlatform extends AbstractOsgiPlatform {
34  
35  	private BundleContext context;
36  
37  
38  	public EquinoxPlatform() {
39  		toString = "Equinox OSGi Platform";
40  	}
41  
42  	Properties getPlatformProperties() {
43  		// default properties
44  		Properties props = new Properties();
45  		props.setProperty("eclipse.ignoreApp", "true");
46  		props.setProperty("osgi.clean", "true");
47  		props.setProperty("osgi.noShutdown", "true");
48  
49  		// local temporary folder for running tests
50  		// prevents accidental rewrites
51  		props.setProperty("osgi.configuration.area", "eclipse_config");
52  		props.setProperty("osgi.instance.area", "eclipse_config");
53  		props.setProperty("osgi.user.area", "eclipse_config");
54  
55  		// props.setProperty("osgi.java.profile.bootdelegation", "ignore");
56  
57  		// props.setProperty("eclipse.consoleLog", "true");
58  		// props.setProperty("osgi.debug", "");
59  
60  		return props;
61  	}
62  
63  	public BundleContext getBundleContext() {
64  		return context;
65  	}
66  
67  	public void start() throws Exception {
68  
69  		if (context == null) {
70  			// copy configuration properties to sys properties
71  			System.getProperties().putAll(getConfigurationProperties());
72  
73  			// Equinox 3.1.x returns void - use of reflection is required
74  			// use main since in 3.1.x it sets up some system properties
75  			EclipseStarter.main(new String[0]);
76  
77  			final Field field = EclipseStarter.class.getDeclaredField("context");
78  
79  			AccessController.doPrivileged(new PrivilegedAction() {
80  
81  				public Object run() {
82  					field.setAccessible(true);
83  					return null;
84  				}
85  			});
86  			context = (BundleContext) field.get(null);
87  		}
88  	}
89  
90  	public void stop() throws Exception {
91  		if (context != null) {
92  			context = null;
93  			EclipseStarter.shutdown();
94  		}
95  	}
96  }