Java tutorial
/* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.apache.servicemix.nmr.spring; import org.springframework.beans.factory.config.BeanFactoryPostProcessor; import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; import org.springframework.beans.factory.config.BeanDefinitionVisitor; import org.springframework.beans.factory.config.BeanDefinition; import org.springframework.beans.factory.BeanNameAware; import org.springframework.beans.factory.BeanFactoryAware; import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.BeanDefinitionStoreException; import org.springframework.beans.BeansException; import org.springframework.core.Ordered; import org.springframework.osgi.context.BundleContextAware; import org.osgi.framework.BundleContext; /** * */ public class BundleExtUrlPostProcessor implements BeanFactoryPostProcessor, Ordered, BundleContextAware, BeanNameAware, BeanFactoryAware { private int order; private String beanName; private BeanFactory beanFactory; private BundleContext bundleContext; public int getOrder() { return order; } public void setOrder(int order) { this.order = order; } public void setBundleContext(BundleContext bundleContext) { this.bundleContext = bundleContext; } public void setBeanName(String string) { this.beanName = string; } public void setBeanFactory(BeanFactory beanFactory) throws BeansException { this.beanFactory = beanFactory; } public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactoryToProcess) throws BeansException { BeanDefinitionVisitor visitor = new BundleExtUrlBeanDefinitionVisitor(); String[] beanNames = beanFactoryToProcess.getBeanDefinitionNames(); for (int i = 0; i < beanNames.length; i++) { // Check that we're not parsing our own bean definition, // to avoid failing on unresolvable placeholders in properties file locations. if (!(beanNames[i].equals(this.beanName) && beanFactoryToProcess.equals(this.beanFactory))) { BeanDefinition bd = beanFactoryToProcess.getBeanDefinition(beanNames[i]); try { visitor.visitBeanDefinition(bd); } catch (BeanDefinitionStoreException ex) { throw new BeanDefinitionStoreException(bd.getResourceDescription(), beanNames[i], ex.getMessage()); } } } } private class BundleExtUrlBeanDefinitionVisitor extends BeanDefinitionVisitor { protected String resolveStringValue(String string) { if (string.startsWith("bundle-ext:")) { string = "bundle://" + bundleContext.getBundle().getBundleId() + "/" + string.substring("bundle-ext:".length()); } return string; } } }