Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   *     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.openejb.server.cxf.transport.util;
 
 
 import java.util.List;
 import java.util.Map;
 
 public final class CxfUtil {
     public static final String ENDPOINT_PROPERTIES = "properties";
     public static final String FEATURES = "features";
     public static final String IN_INTERCEPTORS = "in-interceptors";
     public static final String IN_FAULT_INTERCEPTORS = "in-fault-interceptors";
     public static final String OUT_INTERCEPTORS = "out-interceptors";
     public static final String OUT_FAULT_INTERCEPTORS = "out-fault-interceptors";
     public static final String DATABINDING = "databinding";
     public static final String ADDRESS = "address";
     public static final String DEBUG = "debug";
     public static final String BUS_PREFIX = "org.apache.openejb.cxf.bus.";
     public static final String BUS_CONFIGURED_FLAG = "openejb.cxf.bus.configured";
     private static final Map<StringBindingFactorybindingFactoryMap = new ConcurrentHashMap<StringBindingFactory>(8, 0.75f, 4);
     private static final Bus DEFAULT_BUS = initDefaultBus(); // has to be initializd after bindingFactoryMap
     private static volatile boolean usingBindingFactoryMap = false;
 
     private CxfUtil() {
         // no-op
     }
 
     public static boolean hasService(final String name) {
         return  && .containsKey(name);
     }
 
     private static Bus initDefaultBus() {
         final ClassLoader cl = Thread.currentThread().getContextClassLoader();
         Thread.currentThread().setContextClassLoader(CxfUtil.class.getClassLoader());
         try { // create the bus reusing cxf logic but skipping factory lookup
             final Bus bus = BusFactory.newInstance(CXFBusFactory.class.getName()).createBus();
             final BindingFactoryManager bfm = bus.getExtension(BindingFactoryManager.class);
 
             if (BindingFactoryManagerImpl.class.isInstance(bfm) && !) {
                 BindingFactoryManagerImpl.class.cast(bfm).setMapProvider(new MapProvider<StringBindingFactory>() {
                     @Override
                     public Map<StringBindingFactorycreateMap() {
                          = true;
                         return ;
                     }
                 });
             }
 
             // ensure client proxies can use app classes
             CXFBusFactory.setDefaultBus(Bus.class.cast(Proxy.newProxyInstance(CxfUtil.class.getClassLoader(), new Class<?>[]{ Bus.class }, new ClientAwareBusHandler())));
 
             return bus// we keep as internal the real bus and just expose to cxf the client aware bus to be able to cast it easily
         } finally {
             Thread.currentThread().setContextClassLoader(cl);
         }
     }
 
    public static Bus getBus() {
        return ;
    }
    @Deprecated // no more useful since we create it once
    public static Bus getDefaultBus() {
        return getBus();
    }
    public static ClassLoader initBusLoader() {
        final ClassLoader loader = CxfUtil.getBus().getExtension(ClassLoader.class);
        if (loader != null) {
            if (CxfContainerClassLoader.class.isInstance(loader)) {
                CxfContainerClassLoader.class.cast(loader).tccl(Thread.currentThread().getContextClassLoader());
            }
            return loader;
        }
        return Thread.currentThread().getContextClassLoader();
    }
    public static void clearBusLoader(final ClassLoader old) {
        final ClassLoader loader = CxfUtil.getBus().getExtension(ClassLoader.class);
        if (loader != null && CxfContainerClassLoader.class.isInstance(loader)
                && (old == null || !CxfContainerClassLoader.class.isInstance(old))) {
            CxfContainerClassLoader.class.cast(loader).clear();
        }
        Thread.currentThread().setContextClassLoader(old);
    }
    public static void configureEndpoint(final AbstractEndpointFactory svrFactoryfinal ServiceConfiguration configurationfinal String prefix) {
        final Properties beanConfig = configuration.getProperties();
        if (beanConfig == null || beanConfig.isEmpty()) {
            return;
        }
        final Collection<ServiceInfoavailableServices = configuration.getAvailableServices();
        // endpoint properties
        final Properties properties = ServiceInfos.serviceProperties(availableServicesbeanConfig.getProperty(prefix + ));
        if (properties != null) {
            svrFactory.setProperties(PropertiesHelper.map(properties));
        }
        final String debugKey = prefix + ;
        if ("true".equalsIgnoreCase(beanConfig.getProperty(debugKey, SystemInstance.get().getOptions().get(debugKey"false")))) {
            svrFactory.getProperties(true).put("faultStackTraceEnabled""true");
        }
        // endpoint features
        final String featuresIds = beanConfig.getProperty(prefix + );
        if (featuresIds != null) {
            final List<?> features = createFeatures(availableServicesfeaturesIds);
            svrFactory.setFeatures((List<AbstractFeature>) features);
        }
        configureInterceptors(svrFactoryprefixavailableServicesbeanConfig);
        // databinding
        final String databinding = beanConfig.getProperty(prefix + );
        if (databinding != null && !databinding.trim().isEmpty()) {
            Object instance = ServiceInfos.resolve(availableServicesdatabinding);
            if (instance == null) {  // maybe id == classname
                try {
                    instance = Thread.currentThread().getContextClassLoader().loadClass(databinding).newInstance();
                } catch (Exception e) {
                    // ignore
                }
            }
            if (!DataBinding.class.isInstance(instance)) {
                throw new OpenEJBRuntimeException(instance + " is not a " + DataBinding.class.getName()
                        + ", please check configuration of service [id=" + databinding + "]");
            }
            svrFactory.setDataBinding((DataBindinginstance);
        }
        // address: easier than using openejb-jar.xml
        final String changedAddress = beanConfig.getProperty(prefix + );
        if (changedAddress != null && !changedAddress.trim().isEmpty()) {
            svrFactory.setAddress(changedAddress);
        }
    }
    public static void configureInterceptors(final AbstractBasicInterceptorProvider abipfinal String prefixfinal Collection<ServiceInfoavailableServicesfinal Properties beanConfig) {
        // interceptors
        final String inInterceptorsIds = beanConfig.getProperty(prefix + );
        if (inInterceptorsIds != null && !inInterceptorsIds.trim().isEmpty()) {
            abip.getInInterceptors().addAll(createInterceptors(availableServicesinInterceptorsIds));
        }
        final String inFaultInterceptorsIds = beanConfig.getProperty(prefix + );
        if (inFaultInterceptorsIds != null && !inFaultInterceptorsIds.trim().isEmpty()) {
            abip.getInFaultInterceptors().addAll(createInterceptors(availableServicesinFaultInterceptorsIds));
        }
        final String outInterceptorsIds = beanConfig.getProperty(prefix + );
        if (outInterceptorsIds != null && !outInterceptorsIds.trim().isEmpty()) {
            abip.getOutInterceptors().addAll(createInterceptors(availableServicesoutInterceptorsIds));
        }
        final String outFaultInterceptorsIds = beanConfig.getProperty(prefix + );
        if (outFaultInterceptorsIds != null && !outFaultInterceptorsIds.trim().isEmpty()) {
            abip.getOutFaultInterceptors().addAll(createInterceptors(availableServicesoutFaultInterceptorsIds));
        }
    }
    public static List<AbstractFeaturecreateFeatures(final Collection<ServiceInfoavailableServicesfinal String featuresIds) {
        final List<?> features = ServiceInfos.resolve(availableServicesfeaturesIds.split(","));
        for (Object instance : features) {
            if (!AbstractFeature.class.isInstance(instance)) {
                throw new OpenEJBRuntimeException("feature should inherit from " + AbstractFeature.class.getName());
            }
        }
        return (List<AbstractFeature>) features;
    }
    public static List<Interceptor<? extends Message>> createInterceptors(final Collection<ServiceInfoavailableServicesfinal String ids) {
        final List<?> instances = ServiceInfos.resolve(availableServicesids.split(","));
        for (Object instance : instances) {
            if (!Interceptor.class.isInstance(instance)) {
                throw new OpenEJBRuntimeException("interceptors should implement " + Interceptor.class.getName());
            }
        }
        return (List<Interceptor<? extends Message>>) instances;
    }
    public static void configureBus() {
        if (SystemInstance.get().getProperties().containsKey()) { // jaxws and jaxrs for instance
            return;
        }
        final Bus bus = getBus();
        // ensure cxf classes are loaded from container to avoid conflicts with app
        bus.setExtension(new CxfContainerClassLoader(), ClassLoader.class);
        if (bus instanceof CXFBusImpl) {
            final ServiceConfiguration configuration = new ServiceConfiguration(SystemInstance.get().getProperties(),
                    SystemInstance.get().getComponent(OpenEjbConfiguration.class)..);
            final CXFBusImpl busImpl = (CXFBusImplbus;
            final Collection<ServiceInfoserviceInfos = configuration.getAvailableServices();
            final Properties properties = configuration.getProperties();
            if (properties == null || properties.isEmpty()) {
                return;
            }
            final String featuresIds = properties.getProperty( + );
            if (featuresIds != null) {
                final List<AbstractFeaturefeatures = createFeatures(serviceInfosfeaturesIds);
                if (features != null) {
                    features.addAll(busImpl.getFeatures());
                    busImpl.setFeatures(features);
                }
            }
            final Properties busProperties = ServiceInfos.serviceProperties(serviceInfosproperties.getProperty( + ));
            if (busProperties != null) {
                busImpl.getProperties().putAll(PropertiesHelper.map(busProperties));
            }
            configureInterceptors(busImplserviceInfosconfiguration.getProperties());
            SystemInstance.get().getProperties().setProperty("true");
        }
    }
    private static class ClientAwareBusHandler implements InvocationHandler {
        @Override
        public Object invoke(final Object proxyfinal Method methodfinal Object[] argsthrows Throwable {
            final Bus bus = getBus();
            // when creating a client it is important to use the application loader to be able to load application classes
            // it is the default case but using our own CxfClassLoader we make it wrong so simply skip it when calling a client
            // and no app classloader is registered
            if ("getExtension".equals(method.getName()) && args != null && args.length == 1 && ClassLoader.class.equals(args[0])) {
                final ClassLoader extensionLoader = ClassLoader.class.cast(method.invoke(busargs));
                if (CxfContainerClassLoader.class.isInstance(extensionLoader) && !CxfContainerClassLoader.class.cast(extensionLoader).hasTccl()) {
                    return null;
                }
                return extensionLoader;
            }
            return method.invoke(busargs);
        }
    }
New to GrepCode? Check out our FAQ X