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