]> git.argeo.org Git - lgpl/argeo-commons.git/blob - eclipse/plugins/org.argeo.eclipse.ui/src/main/java/org/argeo/eclipse/spring/ApplicationContextTracker.java
+ Finalization of history request
[lgpl/argeo-commons.git] / eclipse / plugins / org.argeo.eclipse.ui / src / main / java / org / argeo / eclipse / spring / ApplicationContextTracker.java
1 /*
2 * Copyright (C) 2010 Mathieu Baudier <mbaudier@argeo.org>
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.argeo.eclipse.spring;
18
19 import static java.text.MessageFormat.format;
20
21 import org.apache.commons.logging.Log;
22 import org.apache.commons.logging.LogFactory;
23 import org.eclipse.core.runtime.Platform;
24 import org.osgi.framework.Bundle;
25 import org.osgi.framework.BundleContext;
26 import org.osgi.framework.BundleException;
27 import org.osgi.framework.FrameworkUtil;
28 import org.osgi.framework.InvalidSyntaxException;
29 import org.osgi.util.tracker.ServiceTracker;
30 import org.springframework.context.ApplicationContext;
31
32 /**
33 * @author Heiko Seeberger
34 * @author Mathieu Baudier
35 */
36 class ApplicationContextTracker {
37 private final static Log log = LogFactory
38 .getLog(ApplicationContextTracker.class);
39
40 private static final String FILTER = "(&(objectClass=org.springframework.context.ApplicationContext)" //$NON-NLS-1$
41 + "(org.springframework.context.service.name={0}))"; //$NON-NLS-1$
42
43 private ServiceTracker applicationContextServiceTracker;
44
45 /**
46 * @param contributorBundle
47 * OSGi bundle for which the Spring application context is to be
48 * tracked. Must not be null!
49 * @param factoryBundleContext
50 * BundleContext object which can be used to track services
51 * @throws IllegalArgumentException
52 * if the given bundle is null.
53 */
54 public ApplicationContextTracker(final Bundle contributorBundle,
55 final BundleContext factoryBundleContext) {
56 final String filter = format(FILTER,
57 contributorBundle.getSymbolicName());
58 try {
59 applicationContextServiceTracker = new ServiceTracker(
60 factoryBundleContext, FrameworkUtil.createFilter(filter),
61 null);
62 applicationContextServiceTracker.open();
63 } catch (final InvalidSyntaxException e) {
64 e.printStackTrace();
65 }
66 }
67
68 public void close() {
69 if (applicationContextServiceTracker != null) {
70 applicationContextServiceTracker.close();
71 }
72 }
73
74 public ApplicationContext getApplicationContext() {
75 ApplicationContext applicationContext = null;
76 if (applicationContextServiceTracker != null) {
77 try {
78 applicationContext = (ApplicationContext) applicationContextServiceTracker
79 .waitForService(5000);
80 } catch (InterruptedException e) {
81 e.printStackTrace();
82 }
83 }
84 return applicationContext;
85 }
86
87 @Override
88 protected void finalize() throws Throwable {
89 close();
90 super.finalize();
91 }
92
93 static ApplicationContext getApplicationContext(String bundleSymbolicName) {
94 Bundle contributorBundle = Platform.getBundle(bundleSymbolicName);
95 return getApplicationContext(contributorBundle);
96 }
97
98 static ApplicationContext getApplicationContext(Bundle contributorBundle) {
99 if (log.isTraceEnabled())
100 log.trace("Get application context for bundle " + contributorBundle);
101
102 if (contributorBundle.getState() != Bundle.ACTIVE
103 && contributorBundle.getState() != Bundle.STARTING) {
104 try {
105 log.info("Starting bundle: "
106 + contributorBundle.getSymbolicName());
107 contributorBundle.start();
108 } catch (BundleException e) {
109 e.printStackTrace();
110 }
111 }
112
113 final ApplicationContextTracker applicationContextTracker = new ApplicationContextTracker(
114 contributorBundle, contributorBundle.getBundleContext());
115 ApplicationContext applicationContext = null;
116 try {
117 applicationContext = applicationContextTracker
118 .getApplicationContext();
119 } finally {
120 applicationContextTracker.close();
121 }
122 return applicationContext;
123 }
124 }