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