| 1 | /* |
| 2 | * Copyright 2006-2013 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 | @Override |
| 65 | public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { |
| 66 | parent = applicationContext; |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Stash the {@link BundleContext} for creating a job application context |
| 71 | * later. |
| 72 | * |
| 73 | * @see org.springframework.osgi.context.BundleContextAware#setBundleContext(org.osgi.framework.BundleContext) |
| 74 | */ |
| 75 | @Override |
| 76 | public void setBundleContext(BundleContext context) { |
| 77 | this.bundleContext = context; |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Create an application context from the provided path, using the current |
| 82 | * OSGi {@link BundleContext} and the enclosing Spring |
| 83 | * {@link ApplicationContext} as a parent context. |
| 84 | * |
| 85 | * @see ApplicationContextFactory#createApplicationContext() |
| 86 | */ |
| 87 | @Override |
| 88 | public ConfigurableApplicationContext createApplicationContext() { |
| 89 | OsgiBundleXmlApplicationContext context = new OsgiBundleXmlApplicationContext(new String[] { path }, parent); |
| 90 | String displayName = bundleContext.getBundle().getSymbolicName() + ":" + this.displayName; |
| 91 | context.setDisplayName(displayName); |
| 92 | context.setBundleContext(bundleContext); |
| 93 | context.refresh(); |
| 94 | return context; |
| 95 | } |
| 96 | |
| 97 | @Override |
| 98 | public String toString() { |
| 99 | String bundleId = bundleContext == null ? null : (bundleContext.getBundle() == null ? bundleContext.toString() |
| 100 | : "" + bundleContext.getBundle().getBundleId()); |
| 101 | return "OsgiBundleXmlApplicationContext [path=" + path + ", bundle=" + bundleId + "]"; |
| 102 | } |
| 103 | |
| 104 | @Override |
| 105 | public int hashCode() { |
| 106 | return toString().hashCode(); |
| 107 | } |
| 108 | |
| 109 | @Override |
| 110 | public boolean equals(Object obj) { |
| 111 | if (this == obj) { |
| 112 | return true; |
| 113 | } |
| 114 | if (obj == null) { |
| 115 | return false; |
| 116 | } |
| 117 | return toString().equals(obj.toString()); |
| 118 | } |
| 119 | |
| 120 | } |