| 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 | public class OsgiBundleXmlApplicationContextFactory implements BundleContextAware, |
| 28 | ApplicationContextFactory, ApplicationContextAware { |
| 29 | |
| 30 | private BundleContext bundleContext; |
| 31 | |
| 32 | private ApplicationContext parent; |
| 33 | |
| 34 | private String path; |
| 35 | |
| 36 | private String displayName; |
| 37 | |
| 38 | /** |
| 39 | * @param path |
| 40 | * the resource path to the xml to load for the child context. |
| 41 | */ |
| 42 | public void setPath(String path) { |
| 43 | this.path = path; |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * @param displayName the display name for the application context created. |
| 48 | */ |
| 49 | public void setDisplayName(String displayName) { |
| 50 | this.displayName = displayName; |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * Setter for the parent application context. |
| 55 | * |
| 56 | * @see org.springframework.context.ApplicationContextAware#setApplicationContext(org.springframework.context.ApplicationContext) |
| 57 | */ |
| 58 | public void setApplicationContext(ApplicationContext applicationContext) |
| 59 | throws BeansException { |
| 60 | parent = applicationContext; |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Stash the {@link BundleContext} for creating a job application context |
| 65 | * later. |
| 66 | * |
| 67 | * @see org.springframework.osgi.context.BundleContextAware#setBundleContext(org.osgi.framework.BundleContext) |
| 68 | */ |
| 69 | public void setBundleContext(BundleContext context) { |
| 70 | this.bundleContext = context; |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Create an application context from the provided path, using the current |
| 75 | * OSGi {@link BundleContext} and the enclosing Spring |
| 76 | * {@link ApplicationContext} as a parent context. |
| 77 | * |
| 78 | * @see ApplicationContextFactory#createApplicationContext() |
| 79 | */ |
| 80 | public ConfigurableApplicationContext createApplicationContext() { |
| 81 | OsgiBundleXmlApplicationContext context = new OsgiBundleXmlApplicationContext( |
| 82 | new String[] { path }, parent); |
| 83 | String displayName = bundleContext.getBundle().getSymbolicName() + ":" + this.displayName; |
| 84 | context.setDisplayName(displayName); |
| 85 | context.setBundleContext(bundleContext); |
| 86 | context.refresh(); |
| 87 | return context; |
| 88 | } |
| 89 | |
| 90 | } |