1 | /* |
2 | * Copyright 2006-2007 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.batch.core.configuration.support; |
18 | |
19 | import org.osgi.framework.BundleContext; |
20 | import org.springframework.beans.BeansException; |
21 | import org.springframework.context.ApplicationContext; |
22 | import org.springframework.context.ApplicationContextAware; |
23 | import org.springframework.context.ConfigurableApplicationContext; |
24 | import org.springframework.osgi.context.BundleContextAware; |
25 | import org.springframework.osgi.context.support.OsgiBundleXmlApplicationContext; |
26 | |
27 | /** |
28 | * {@link ApplicationContextFactory} that can be used to load a context from an |
29 | * XML location in a bundle. |
30 | * |
31 | * @author Dave Syer |
32 | * |
33 | */ |
34 | public class OsgiBundleXmlApplicationContextFactory implements BundleContextAware, ApplicationContextFactory, |
35 | ApplicationContextAware { |
36 | |
37 | private BundleContext bundleContext; |
38 | |
39 | private ApplicationContext parent; |
40 | |
41 | private String path; |
42 | |
43 | private String displayName; |
44 | |
45 | /** |
46 | * @param path the resource path to the xml to load for the child context. |
47 | */ |
48 | public void setPath(String path) { |
49 | this.path = path; |
50 | } |
51 | |
52 | /** |
53 | * @param displayName the display name for the application context created. |
54 | */ |
55 | public void setDisplayName(String displayName) { |
56 | this.displayName = displayName; |
57 | } |
58 | |
59 | /** |
60 | * Setter for the parent application context. |
61 | * |
62 | * @see org.springframework.context.ApplicationContextAware#setApplicationContext(org.springframework.context.ApplicationContext) |
63 | */ |
64 | public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { |
65 | parent = applicationContext; |
66 | } |
67 | |
68 | /** |
69 | * Stash the {@link BundleContext} for creating a job application context |
70 | * later. |
71 | * |
72 | * @see org.springframework.osgi.context.BundleContextAware#setBundleContext(org.osgi.framework.BundleContext) |
73 | */ |
74 | public void setBundleContext(BundleContext context) { |
75 | this.bundleContext = context; |
76 | } |
77 | |
78 | /** |
79 | * Create an application context from the provided path, using the current |
80 | * OSGi {@link BundleContext} and the enclosing Spring |
81 | * {@link ApplicationContext} as a parent context. |
82 | * |
83 | * @see ApplicationContextFactory#createApplicationContext() |
84 | */ |
85 | public ConfigurableApplicationContext createApplicationContext() { |
86 | OsgiBundleXmlApplicationContext context = new OsgiBundleXmlApplicationContext(new String[] { path }, parent); |
87 | String displayName = bundleContext.getBundle().getSymbolicName() + ":" + this.displayName; |
88 | context.setDisplayName(displayName); |
89 | context.setBundleContext(bundleContext); |
90 | context.refresh(); |
91 | return context; |
92 | } |
93 | |
94 | @Override |
95 | public String toString() { |
96 | String bundleId = bundleContext == null ? null : (bundleContext.getBundle() == null ? bundleContext.toString() |
97 | : "" + bundleContext.getBundle().getBundleId()); |
98 | return "OsgiBundleXmlApplicationContext [path=" + path + ", bundle=" + bundleId + "]"; |
99 | } |
100 | |
101 | @Override |
102 | public int hashCode() { |
103 | return toString().hashCode(); |
104 | } |
105 | |
106 | @Override |
107 | public boolean equals(Object obj) { |
108 | if (this == obj) |
109 | return true; |
110 | if (obj == null) |
111 | return false; |
112 | return toString().equals(obj.toString()); |
113 | } |
114 | |
115 | } |