[jboss-cvs] jboss-seam/src/main/org/jboss/seam/init ...
Gavin King
gavin.king at jboss.com
Mon Nov 13 19:15:01 EST 2006
User: gavin
Date: 06/11/13 19:15:01
Modified: src/main/org/jboss/seam/init Initialization.java
Log:
introduced jar-level and package-level components.xml
autodetect seam components via META-INF/components.xml files
Revision Changes Path
1.111 +100 -21 jboss-seam/src/main/org/jboss/seam/init/Initialization.java
(In the diff below, changes in quantity of whitespace are not shown.)
Index: Initialization.java
===================================================================
RCS file: /cvsroot/jboss/jboss-seam/src/main/org/jboss/seam/init/Initialization.java,v
retrieving revision 1.110
retrieving revision 1.111
diff -u -b -r1.110 -r1.111
--- Initialization.java 13 Nov 2006 20:20:21 -0000 1.110
+++ Initialization.java 14 Nov 2006 00:15:01 -0000 1.111
@@ -8,6 +8,7 @@
import java.io.IOException;
import java.io.InputStream;
+import java.net.URL;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap;
@@ -108,7 +109,7 @@
/**
* @author Gavin King
* @author <a href="mailto:theute at jboss.org">Thomas Heute</a>
- * @version $Revision: 1.110 $
+ * @version $Revision: 1.111 $
*/
public class Initialization
{
@@ -128,32 +129,74 @@
{
this.servletContext = servletContext;
initComponentsFromXmlDocument();
+ initComponentsFromXmlDocuments();
initPropertiesFromServletContext();
initPropertiesFromResource();
initJndiProperties();
}
+ private void initComponentsFromXmlDocuments()
+ {
+ Enumeration<URL> resources;
+ try
+ {
+ resources = Thread.currentThread().getContextClassLoader().getResources("META-INF/components.xml");
+ }
+ catch (IOException ioe)
+ {
+ throw new RuntimeException("error scanning META-INF/components.xml files", ioe);
+ }
+
+ Properties replacements = getReplacements();
+ while ( resources.hasMoreElements() )
+ {
+ URL url = resources.nextElement();
+ try
+ {
+ log.info("reading " + url);
+ installComponentsFromXmlElements( getDocument( url.openStream() ), replacements );
+ }
+ catch (Exception e)
+ {
+ throw new RuntimeException("error while reading " + url, e);
+ }
+ }
+
+ }
+
private void initComponentsFromXmlDocument()
{
InputStream stream = Resources.getResourceAsStream("/WEB-INF/components.xml", servletContext);
if (stream==null)
{
- log.info("no components.xml file found");
+ log.info("no /WEB-INF/components.xml file found");
}
else
{
- log.info("reading components.xml");
+ log.info("reading /WEB-INF/components.xml");
+ try
+ {
+ installComponentsFromXmlElements( getDocument(stream), getReplacements() );
+ }
+ catch (Exception e)
+ {
+ throw new RuntimeException("error while reading /WEB-INF/components.xml", e);
+ }
+ }
+ }
+
+ private Properties getReplacements()
+ {
try
{
Properties replacements = new Properties();
InputStream replaceStream = Resources.getResourceAsStream("components.properties");
if (replaceStream!=null) replacements.load( replaceStream );
- installComponentsFromXmlElements( getDocument(stream), replacements );
+ return replacements;
}
- catch (Exception e)
+ catch (IOException ioe)
{
- throw new RuntimeException("error while reading components.xml", e);
- }
+ throw new RuntimeException("error reading components.properties", ioe);
}
}
@@ -194,7 +237,10 @@
String value = factory.attributeValue("value");
if (method==null && value==null)
{
- throw new IllegalArgumentException("must specify either method or value in <factory/> declaration for variable: " + name);
+ throw new IllegalArgumentException(
+ "must specify either method or value in <factory/> declaration for variable: " +
+ name
+ );
}
ScopeType scope = scopeName==null ?
ScopeType.UNSPECIFIED :
@@ -220,7 +266,8 @@
return value;
}
- private void installComponentFromXmlElement(Element component, String className, Properties replacements) throws ClassNotFoundException
+ private void installComponentFromXmlElement(Element component, String className, Properties replacements)
+ throws ClassNotFoundException
{
String name = component.attributeValue("name");
String scopeName = component.attributeValue("scope");
@@ -340,37 +387,57 @@
private void installScannedComponents()
{
+ Set<Package> scannedPackages = new HashSet<Package>();
if ( isScannerEnabled )
{
- for ( Class<Object> scannedClass: new Scanner().getClasses() )
+ for ( Class<Object> scannedClass: new Scanner("seam.properties").getClasses() )
{
- installScannedComponentAndRoles(scannedClass);
- installComponentsFromDescriptor( descriptorFilename(scannedClass), scannedClass.getName() );
+ installScannedClass(scannedPackages, scannedClass);
+ }
+ for ( Class<Object> scannedClass: new Scanner("META-INF/components.xml").getClasses() )
+ {
+ installScannedClass(scannedPackages, scannedClass);
}
}
}
- private static String descriptorFilename(Class<Object> scannedClass)
+ private void installScannedClass(Set<Package> scannedPackages, Class<Object> scannedClass)
+ {
+ installScannedComponentAndRoles(scannedClass);
+ installComponentsFromDescriptor( classDescriptorFilename(scannedClass), scannedClass );
+ Package pkg = scannedClass.getPackage();
+ if ( pkg!=null && scannedPackages.add(pkg) )
+ {
+ installComponentsFromDescriptor( packageDescriptorFilename(pkg), scannedClass );
+ }
+ }
+
+ private static String classDescriptorFilename(Class<Object> scannedClass)
{
return scannedClass.getName().replace('.', '/') + ".component.xml";
}
- private void installComponentsFromDescriptor(String fileName, String className)
+ private static String packageDescriptorFilename(Package pkg)
+ {
+ return pkg.getName().replace('.', '/') + "/components.xml";
+ }
+
+ private void installComponentsFromDescriptor(String fileName, Class clazz)
{
- InputStream stream = Resources.getResourceAsStream(fileName);
+ InputStream stream = clazz.getClassLoader().getResourceAsStream(fileName); //note: this is correct, we do not need to scan other classloaders!
if (stream!=null)
{
try
{
- Properties replacements = new Properties(); //TODO: use components.properties
+ Properties replacements = getReplacements();
Document doc = getDocument(stream);
- if ( doc.getRootElement().getName().equals("component") )
+ if ( doc.getRootElement().getName().equals("components") )
{
- installComponentFromXmlElement( doc.getRootElement(), className, replacements );
+ installComponentsFromXmlElements( doc, replacements );
}
else
{
- installComponentsFromXmlElements(doc, replacements);
+ installComponentFromXmlElement( doc.getRootElement(), clazz.getName(), replacements );
}
}
catch (Exception e)
@@ -403,7 +470,7 @@
private void installRole(Class<Object> scannedClass, Role role)
{
ScopeType scope = Seam.getComponentRoleScope(scannedClass, role);
- componentDescriptors.add( new ComponentDescriptor( role.name(), scannedClass, scope, null) );
+ componentDescriptors.add( new ComponentDescriptor( role.name(), scannedClass, scope, null ) );
}
private void initPropertiesFromServletContext()
@@ -673,6 +740,12 @@
{
return method==null;
}
+
+ @Override
+ public String toString()
+ {
+ return "FactoryDescriptor(" + name + ')';
+ }
}
private static class ComponentDescriptor
@@ -710,6 +783,12 @@
{
return jndiName;
}
+
+ @Override
+ public String toString()
+ {
+ return "ComponentDescriptor(" + name + ')';
+ }
}
}
More information about the jboss-cvs-commits
mailing list