Search in sources :

Example 1 with ProxyConfiguration

use of org.jboss.invocation.proxy.ProxyConfiguration in project wildfly by wildfly.

the class ExternalContextObjectFactory method createContext.

private Context createContext(final Hashtable<?, ?> environment, boolean useProxy) throws NamingException, ClassNotFoundException, NoSuchMethodException, InstantiationException, IllegalAccessException, InvocationTargetException, ModuleLoadException {
    String initialContextClassName = (String) environment.get(INITIAL_CONTEXT_CLASS);
    String initialContextModule = (String) environment.get(INITIAL_CONTEXT_MODULE);
    final boolean useStringLokup = useStringLookup(environment);
    final Hashtable<?, ?> newEnvironment = new Hashtable<>(environment);
    newEnvironment.remove(CACHE_CONTEXT);
    newEnvironment.remove(INITIAL_CONTEXT_CLASS);
    newEnvironment.remove(INITIAL_CONTEXT_MODULE);
    newEnvironment.remove(LOOKUP_BY_STRING);
    ClassLoader loader;
    if (!WildFlySecurityManager.isChecking()) {
        loader = getClass().getClassLoader();
    } else {
        loader = AccessController.doPrivileged(new PrivilegedAction<ClassLoader>() {

            @Override
            public ClassLoader run() {
                return getClass().getClassLoader();
            }
        });
    }
    Class initialContextClass = null;
    final Context loadedContext;
    if (initialContextModule == null) {
        initialContextClass = Class.forName(initialContextClassName);
        Constructor ctor = initialContextClass.getConstructor(Hashtable.class);
        loadedContext = (Context) ctor.newInstance(newEnvironment);
    } else {
        Module module = Module.getBootModuleLoader().loadModule(ModuleIdentifier.fromString(initialContextModule));
        loader = module.getClassLoader();
        final ClassLoader currentClassLoader = WildFlySecurityManager.getCurrentContextClassLoaderPrivileged();
        try {
            WildFlySecurityManager.setCurrentContextClassLoaderPrivileged(loader);
            initialContextClass = Class.forName(initialContextClassName, true, loader);
            Constructor ctor = initialContextClass.getConstructor(Hashtable.class);
            loadedContext = (Context) ctor.newInstance(newEnvironment);
        } finally {
            WildFlySecurityManager.setCurrentContextClassLoaderPrivileged(currentClassLoader);
        }
    }
    final Context context;
    if (useStringLokup) {
        context = new LookupByStringContext(loadedContext);
    } else {
        context = loadedContext;
    }
    if (!useProxy) {
        return context;
    }
    ProxyConfiguration config = new ProxyConfiguration();
    config.setClassLoader(loader);
    config.setSuperClass(initialContextClass);
    config.setProxyName(initialContextClassName + "$$$$Proxy" + PROXY_ID.incrementAndGet());
    config.setProtectionDomain(context.getClass().getProtectionDomain());
    ProxyFactory<?> factory = new ProxyFactory<Object>(config);
    return (Context) factory.newInstance(new CachedContext(context));
}
Also used : Context(javax.naming.Context) ProxyFactory(org.jboss.invocation.proxy.ProxyFactory) Hashtable(java.util.Hashtable) Constructor(java.lang.reflect.Constructor) ProxyConfiguration(org.jboss.invocation.proxy.ProxyConfiguration) PrivilegedAction(java.security.PrivilegedAction) Module(org.jboss.modules.Module)

Example 2 with ProxyConfiguration

use of org.jboss.invocation.proxy.ProxyConfiguration in project wildfly by wildfly.

the class DefaultComponentViewConfigurator method configure.

public void configure(final DeploymentPhaseContext context, final ComponentDescription description, final ComponentConfiguration configuration) throws DeploymentUnitProcessingException {
    final DeploymentUnit deploymentUnit = context.getDeploymentUnit();
    final Module module = deploymentUnit.getAttachment(org.jboss.as.server.deployment.Attachments.MODULE);
    final ProxyMetadataSource proxyReflectionIndex = deploymentUnit.getAttachment(org.jboss.as.server.deployment.Attachments.PROXY_REFLECTION_INDEX);
    //views
    for (ViewDescription view : description.getViews()) {
        Class<?> viewClass;
        try {
            viewClass = module.getClassLoader().loadClass(view.getViewClassName());
        } catch (ClassNotFoundException e) {
            throw EeLogger.ROOT_LOGGER.cannotLoadViewClass(e, view.getViewClassName(), configuration);
        }
        final ViewConfiguration viewConfiguration;
        final ProxyConfiguration proxyConfiguration = new ProxyConfiguration();
        if (viewClass.getName().startsWith("java.")) {
            proxyConfiguration.setProxyName("org.jboss.proxy.java.lang." + viewClass.getSimpleName() + "$$$view" + PROXY_ID.incrementAndGet());
        } else {
            proxyConfiguration.setProxyName(viewClass.getName() + "$$$view" + PROXY_ID.incrementAndGet());
        }
        proxyConfiguration.setClassLoader(module.getClassLoader());
        proxyConfiguration.setProtectionDomain(viewClass.getProtectionDomain());
        proxyConfiguration.setMetadataSource(proxyReflectionIndex);
        if (view.isSerializable()) {
            proxyConfiguration.addAdditionalInterface(Serializable.class);
            if (view.isUseWriteReplace()) {
                proxyConfiguration.addAdditionalInterface(WriteReplaceInterface.class);
            }
        }
        //we define it in the modules class loader to prevent permgen leaks
        if (viewClass.isInterface()) {
            proxyConfiguration.setSuperClass(Object.class);
            proxyConfiguration.addAdditionalInterface(viewClass);
            viewConfiguration = view.createViewConfiguration(viewClass, configuration, new ProxyFactory(proxyConfiguration));
        } else {
            proxyConfiguration.setSuperClass(viewClass);
            viewConfiguration = view.createViewConfiguration(viewClass, configuration, new ProxyFactory(proxyConfiguration));
        }
        for (final ViewConfigurator configurator : view.getConfigurators()) {
            configurator.configure(context, configuration, view, viewConfiguration);
        }
        configuration.getViews().add(viewConfiguration);
    }
    configuration.getStartDependencies().add(new DependencyConfigurator<ComponentStartService>() {

        @Override
        public void configureDependency(final ServiceBuilder<?> serviceBuilder, ComponentStartService service) throws DeploymentUnitProcessingException {
            for (final Map.Entry<ServiceName, ServiceBuilder.DependencyType> entry : description.getDependencies().entrySet()) {
                serviceBuilder.addDependency(entry.getValue(), entry.getKey());
            }
        }
    });
}
Also used : DeploymentUnitProcessingException(org.jboss.as.server.deployment.DeploymentUnitProcessingException) ProxyFactory(org.jboss.invocation.proxy.ProxyFactory) ProxyMetadataSource(org.jboss.as.server.deployment.reflect.ProxyMetadataSource) ServiceBuilder(org.jboss.msc.service.ServiceBuilder) ProxyConfiguration(org.jboss.invocation.proxy.ProxyConfiguration) Module(org.jboss.modules.Module) DeploymentUnit(org.jboss.as.server.deployment.DeploymentUnit)

Aggregations

ProxyConfiguration (org.jboss.invocation.proxy.ProxyConfiguration)2 ProxyFactory (org.jboss.invocation.proxy.ProxyFactory)2 Module (org.jboss.modules.Module)2 Constructor (java.lang.reflect.Constructor)1 PrivilegedAction (java.security.PrivilegedAction)1 Hashtable (java.util.Hashtable)1 Context (javax.naming.Context)1 DeploymentUnit (org.jboss.as.server.deployment.DeploymentUnit)1 DeploymentUnitProcessingException (org.jboss.as.server.deployment.DeploymentUnitProcessingException)1 ProxyMetadataSource (org.jboss.as.server.deployment.reflect.ProxyMetadataSource)1 ServiceBuilder (org.jboss.msc.service.ServiceBuilder)1