Skip to content

Commit

Permalink
[WELD-745]; allow for exact resources listing in web listener.
Browse files Browse the repository at this point in the history
  • Loading branch information
alesj committed Nov 1, 2010
1 parent 80ce691 commit 68e172a
Show file tree
Hide file tree
Showing 4 changed files with 182 additions and 45 deletions.
@@ -0,0 +1,116 @@
/*
* JBoss, Home of Professional Open Source
* Copyright 2008, Red Hat Middleware LLC, and individual contributors
* by the @authors tag. See the copyright.txt in the distribution for a
* full listing of individual contributors.
*
* Licensed 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.jboss.weld.environment.servlet;

import javax.servlet.ServletContext;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.List;

import org.jboss.weld.bootstrap.api.Bootstrap;
import org.jboss.weld.environment.servlet.deployment.ServletDeployment;
import org.jboss.weld.environment.servlet.deployment.URLScanner;
import org.jboss.weld.environment.servlet.deployment.WebAppBeanDeploymentArchive;

/**
* Exact listener.
*
* Explicitly list all the bean classes in beans.txt file.
* This will reduce scanning; e.g. useful for restricted env like GAE
*
* @author Ales Justin
*/
public class ExactListener extends Listener
{
protected ServletDeployment createServletDeployment(ServletContext context, Bootstrap bootstrap)
{
return new ExactServletDeployment(context, bootstrap);
}

private static class ExactServletDeployment extends ServletDeployment
{
private ExactServletDeployment(ServletContext servletContext, Bootstrap bootstrap)
{
super(servletContext, bootstrap);
}

protected WebAppBeanDeploymentArchive createWebAppBeanDeploymentArchive(ServletContext servletContext, Bootstrap bootstrap)
{
return new ExactWebAppBeanDeploymentArchive(servletContext, bootstrap);
}
}

private static class ExactWebAppBeanDeploymentArchive extends WebAppBeanDeploymentArchive
{
private ExactWebAppBeanDeploymentArchive(ServletContext servletContext, Bootstrap bootstrap)
{
super(servletContext, bootstrap);
}

protected URLScanner createScanner(ClassLoader classLoader)
{
return new ExactScanner(classLoader);
}
}

private static class ExactScanner extends URLScanner
{
private ExactScanner(ClassLoader classLoader)
{
super(classLoader);
}

public void scanResources(String[] resources, List<String> classes, List<URL> urls)
{
URL url = getClassLoader().getResource("beans.txt");
if (url == null)
throw new IllegalArgumentException("Missing beans.txt");

try
{
InputStream is = url.openStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
try
{
String line;
while((line = reader.readLine()) != null)
{
classes.add(line);
}
}
finally
{
try
{
is.close();
}
catch (IOException ignore)
{
}
}
}
catch (Exception e)
{
throw new RuntimeException(e);
}
}
}
}
Expand Up @@ -21,6 +21,7 @@

import javax.el.ELContextListener;
import javax.enterprise.inject.spi.BeanManager;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.jsp.JspApplicationContext;
import javax.servlet.jsp.JspFactory;
Expand All @@ -42,11 +43,12 @@

/**
* @author Pete Muir
* @author Ales Justin
*/
public class Listener extends ForwardingServletListener
{
private static final Logger log = LoggerFactory.getLogger(Listener.class);

private static final String BOOTSTRAP_IMPL_CLASS_NAME = "org.jboss.weld.bootstrap.WeldBootstrap";
private static final String WELD_LISTENER_CLASS_NAME = "org.jboss.weld.servlet.WeldListener";
private static final String EXPRESSION_FACTORY_NAME = "org.jboss.weld.el.ExpressionFactory";
Expand All @@ -56,7 +58,6 @@ public class Listener extends ForwardingServletListener

private final transient Bootstrap bootstrap;
private final transient ServletListener weldListener;
private WeldManager manager;

public Listener()
{
Expand Down Expand Up @@ -97,21 +98,36 @@ public void contextDestroyed(ServletContextEvent sce)
super.contextDestroyed(sce);
}

/**
* Create server deployment.
*
* Can be overridden with custom servlet deployment.
* e.g. exact resources listing in ristricted wnv like GAE
*
* @param context the servlet context
* @param bootstrap the bootstrap
* @return new servlet deployment
*/
protected ServletDeployment createServletDeployment(ServletContext context, Bootstrap bootstrap)
{
return new ServletDeployment(context, bootstrap);
}

@Override
public void contextInitialized(ServletContextEvent sce)
{
// Make Javassist always use the TCCL to load classes
ProxyFactory.classLoaderProvider = new ClassLoaderProvider()
{

public ClassLoader get(ProxyFactory pf)
{
return Thread.currentThread().getContextClassLoader();
}

};
ServletDeployment deployment = new ServletDeployment(sce.getServletContext(), bootstrap);

ServletDeployment deployment = createServletDeployment(sce.getServletContext(), bootstrap);
try
{
deployment.getWebAppBeanDeploymentArchive().getServices().add(
Expand All @@ -122,12 +138,12 @@ public ClassLoader get(ProxyFactory pf)
// Support GAE
log.warn("@Resource injection not available in simple beans");
}

bootstrap.startContainer(Environments.SERVLET, deployment).startInitialization();
manager = bootstrap.getManager(deployment.getWebAppBeanDeploymentArchive());
WeldManager manager = bootstrap.getManager(deployment.getWebAppBeanDeploymentArchive());

boolean tomcat = true;
boolean tomcat7 = false;
boolean tomcat7;
try
{
Reflections.classForName("org.apache.AnnotationProcessor");
Expand Down Expand Up @@ -202,23 +218,23 @@ public ClassLoader get(ProxyFactory pf)

// Push the manager into the servlet context so we can access in JSF
sce.getServletContext().setAttribute(BEAN_MANAGER_ATTRIBUTE_NAME, manager);

if (JspFactory.getDefaultFactory() != null)
{
JspApplicationContext jspApplicationContext = JspFactory.getDefaultFactory().getJspApplicationContext(sce.getServletContext());

// Register the ELResolver with JSP
jspApplicationContext.addELResolver(manager.getELResolver());

// Register ELContextListener with JSP
jspApplicationContext.addELContextListener(Reflections.<ELContextListener>
newInstance("org.jboss.weld.el.WeldELContextListener"));

// Push the wrapped expression factory into the servlet context so that Tomcat or Jetty can hook it in using a container code
sce.getServletContext().setAttribute(EXPRESSION_FACTORY_NAME,
manager.wrapExpressionFactory(jspApplicationContext.getExpressionFactory()));
}

bootstrap.deployBeans().validateBeans().endInitialization();
super.contextInitialized(sce);
}
Expand All @@ -228,5 +244,4 @@ protected ServletListener delegate()
{
return weldListener;
}

}
Expand Up @@ -14,22 +14,26 @@
import org.jboss.weld.bootstrap.spi.Metadata;

public class ServletDeployment implements Deployment
{

{
private final WebAppBeanDeploymentArchive webAppBeanDeploymentArchive;
private final Collection<BeanDeploymentArchive> beanDeploymentArchives;
private final ServiceRegistry services;
private final Iterable<Metadata<Extension>> extensions;

public ServletDeployment(ServletContext servletContext, Bootstrap bootstrap)
{
this.webAppBeanDeploymentArchive = new WebAppBeanDeploymentArchive(servletContext, bootstrap);
this.webAppBeanDeploymentArchive = createWebAppBeanDeploymentArchive(servletContext, bootstrap);
this.beanDeploymentArchives = new ArrayList<BeanDeploymentArchive>();
this.beanDeploymentArchives.add(webAppBeanDeploymentArchive);
this.services = new SimpleServiceRegistry();
this.extensions = bootstrap.loadExtensions(Thread.currentThread().getContextClassLoader());
}

protected WebAppBeanDeploymentArchive createWebAppBeanDeploymentArchive(ServletContext servletContext, Bootstrap bootstrap)
{
return new WebAppBeanDeploymentArchive(servletContext, bootstrap);
}

public Collection<BeanDeploymentArchive> getBeanDeploymentArchives()
{
return beanDeploymentArchives;
Expand All @@ -44,15 +48,14 @@ public BeanDeploymentArchive loadBeanDeploymentArchive(Class<?> beanClass)
{
return webAppBeanDeploymentArchive;
}

public WebAppBeanDeploymentArchive getWebAppBeanDeploymentArchive()
{
return webAppBeanDeploymentArchive;
}

public Iterable<Metadata<Extension>> getExtensions()
{
return extensions;
}

}

1 comment on commit 68e172a

@pmuir
Copy link

@pmuir pmuir commented on 68e172a Nov 8, 2010

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we remove all the whitespace changes?

Shouldn't beans.txt be in the META-INF? I'm also not sure about the name beans.txt, it doesn't really describe what it does very well.

Please sign in to comment.