leosutic 2003/08/14 15:11:51
Added: attributes/api/src/java/org/apache/avalon/attributes
Indexed.java AttributeIndex.java
Log:
Added support for building indexes of which classes have which attributes.
This will speed up the query "Which classes have attribute X" significantly.
Revision Changes Path
1.1
avalon-sandbox/attributes/api/src/java/org/apache/avalon/attributes/Indexed.java
Index: Indexed.java
===================================================================
package org.apache.avalon.attributes;
/**
* This attribute is used to mark attributes as being indexed.
* Indexed attributes will result in the attribute-jar-index tool
* creating an entry for a class marked with an attribute that is indexed.
* For example, if the attribute Service is Indexed, and the classes
* MyService and MyService2 have Service as a class attribute, the
* jar-index will be:
*
* AttributeType: Service
* Class: MyService
* Class: MyService2
*/
public class Indexed {
}
1.1
avalon-sandbox/attributes/api/src/java/org/apache/avalon/attributes/AttributeIndex.java
Index: AttributeIndex.java
===================================================================
package org.apache.avalon.attributes;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.Collection;
import java.util.Collections;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.HashSet;
import java.util.StringTokenizer;
public class AttributeIndex {
private final HashMap index = new HashMap ();
AttributeIndex (ClassLoader cl) throws Exception {
Enumeration enum = cl.getResources ("META-INF/attrs.index");
while (enum.hasMoreElements ()) {
URL url = (URL) enum.nextElement ();
loadFromURL (url);
}
}
private void addClass (String attributeClass, String clazz) {
Collection coll = (Collection) index.get (attributeClass);
if (coll == null) {
coll = new HashSet ();
index.put (attributeClass, coll);
}
coll.add (clazz);
}
private void loadFromURL (URL url) throws Exception {
URLConnection connection = url.openConnection ();
BufferedReader br = new BufferedReader (new InputStreamReader
(connection.getInputStream ()));
try {
String currentAttributeClass = null;
String line = null;
while ((line = br.readLine ()) != null) {
if (line.startsWith ("Attribute: ")) {
currentAttributeClass = line.substring ("Attribute: ".length
()).trim ();
} else if (line.startsWith ("Class: ")) {
String className = line.substring ("Class: ".length ()).trim
();
addClass (currentAttributeClass, className);
}
}
} finally {
br.close ();
}
}
/**
* Gets a Collection of the classes that have an attribute of the specified
class.
* The Collection contains the class names (String).
*/
public Collection getClassesWithAttribute (String attributeClass) {
if (index.containsKey (attributeClass)) {
return (Collection) index.get (attributeClass);
} else {
return Collections.EMPTY_SET;
}
}
public Collection getClassesWithAttribute (Class attributeClass) {
return getClassesWithAttribute (attributeClass.getName ());
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]