org.apache.cxf.bus.extension.ExtensionManagerImpl Maven / Gradle / Ivy
/**
* 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.cxf.bus.extension;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Method;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import org.apache.cxf.common.injection.ResourceInjector;
import org.apache.cxf.configuration.Configurer;
import org.apache.cxf.resource.ObjectTypeResolver;
import org.apache.cxf.resource.ResourceManager;
import org.apache.cxf.resource.ResourceResolver;
import org.apache.cxf.resource.SinglePropertyResolver;
public class ExtensionManagerImpl implements ExtensionManager {
public static final String EXTENSIONMANAGER_PROPERTY_NAME = "extensionManager";
public static final String ACTIVATION_NAMESPACES_PROPERTY_NAME = "activationNamespaces";
public static final String ACTIVATION_NAMESPACES_SETTER_METHOD_NAME = "setActivationNamespaces";
public static final String BUS_EXTENSION_RESOURCE_COMPAT = "META-INF/bus-extensions.xml";
public static final String BUS_EXTENSION_RESOURCE = "META-INF/cxf/bus-extensions.xml";
private final ClassLoader loader;
private ResourceManager resourceManager;
private Map> deferred;
private final Map activated;
private final Map> namespaced =
new ConcurrentHashMap>();
public ExtensionManagerImpl(ClassLoader cl, Map initialExtensions,
ResourceManager rm) {
this(new String[] {BUS_EXTENSION_RESOURCE, BUS_EXTENSION_RESOURCE_COMPAT}, cl, initialExtensions, rm);
}
public ExtensionManagerImpl(String resource,
ClassLoader cl,
Map initialExtensions,
ResourceManager rm) {
this(new String[] {resource}, cl, initialExtensions, rm);
}
public ExtensionManagerImpl(String resources[],
ClassLoader cl,
Map initialExtensions,
ResourceManager rm) {
loader = cl;
activated = initialExtensions;
resourceManager = rm;
ResourceResolver extensionManagerResolver =
new SinglePropertyResolver(EXTENSIONMANAGER_PROPERTY_NAME, this);
resourceManager.addResourceResolver(extensionManagerResolver);
resourceManager.addResourceResolver(new ObjectTypeResolver(this));
deferred = new ConcurrentHashMap>();
try {
for (String resource : resources) {
load(resource);
}
} catch (IOException ex) {
throw new ExtensionException(ex);
}
}
public synchronized void activateViaNS(String namespaceURI) {
Collection extensions = deferred.get(namespaceURI);
if (null == extensions) {
return;
}
for (Extension e : extensions) {
loadAndRegister(e);
}
extensions.clear();
deferred.remove(namespaceURI);
}
public synchronized void activateAll() {
while (!deferred.isEmpty()) {
activateViaNS(deferred.keySet().iterator().next());
}
}
public void activateAllByType(Class type) {
for (Map.Entry> e : deferred.entrySet()) {
Iterator it = e.getValue().iterator();
while (it.hasNext()) {
Extension ex = it.next();
if (type.isAssignableFrom(ex.getClassObject(loader))) {
loadAndRegister(ex);
it.remove();
}
}
}
}
final void load(String resource) throws IOException {
Enumeration urls = Thread.currentThread().getContextClassLoader().getResources(resource);
while (urls.hasMoreElements()) {
URL url = urls.nextElement();
InputStream is = url.openStream();
loadFragment(is);
}
}
final void loadFragment(InputStream is) {
List extensions = new ExtensionFragmentParser().getExtensions(is);
for (Extension e : extensions) {
processExtension(e);
}
}
final void processExtension(Extension e) {
if (!e.isDeferred()) {
loadAndRegister(e);
}
Collection namespaces = e.getNamespaces();
for (String ns : namespaces) {
Collection extensions = deferred.get(ns);
if (null == extensions) {
extensions = new ArrayList();
deferred.put(ns, extensions);
}
extensions.add(e);
}
}
final void loadAndRegister(Extension e) {
Class> cls = null;
if (null != e.getInterfaceName() && !"".equals(e.getInterfaceName())) {
cls = e.loadInterface(loader);
}
if (null != activated && null != cls && null != activated.get(cls)) {
return;
}
Object obj = e.load(loader);
Configurer configurer = (Configurer)(activated.get(Configurer.class));
if (null != configurer) {
configurer.configureBean(obj);
}
// let the object know for which namespaces it has been activated
ResourceResolver namespacesResolver = null;
if (null != e.getNamespaces()) {
namespacesResolver = new SinglePropertyResolver(ACTIVATION_NAMESPACES_PROPERTY_NAME,
e.getNamespaces());
resourceManager.addResourceResolver(namespacesResolver);
}
// Since we need to support spring2.5 by removing @Resource("activationNamespaces")
// Now we call the setActivationNamespaces method directly here
invokeSetterActivationNSMethod(obj, e.getNamespaces());
ResourceInjector injector = new ResourceInjector(resourceManager);
try {
injector.inject(obj);
injector.construct(obj);
} finally {
if (null != namespacesResolver) {
resourceManager.removeResourceResolver(namespacesResolver);
}
}
if (null != activated) {
if (cls == null) {
cls = obj.getClass();
}
activated.put(cls, obj);
}
for (String ns : e.getNamespaces()) {
Collection
© 2015 - 2025 Weber Informatics LLC | Privacy Policy