You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@directory.apache.org by ka...@apache.org on 2008/10/24 20:37:37 UTC
svn commit: r707705 - in
/directory/sandbox/kayyagari/apacheds-olm/src/main/java/org/apache/directory/olm:
./ util/
Author: kayyagari
Date: Fri Oct 24 11:37:36 2008
New Revision: 707705
URL: http://svn.apache.org/viewvc?rev=707705&view=rev
Log:
o added javadoc
o lots of formating
Modified:
directory/sandbox/kayyagari/apacheds-olm/src/main/java/org/apache/directory/olm/Attribute.java
directory/sandbox/kayyagari/apacheds-olm/src/main/java/org/apache/directory/olm/Entry.java
directory/sandbox/kayyagari/apacheds-olm/src/main/java/org/apache/directory/olm/SourceFileWriter.java
directory/sandbox/kayyagari/apacheds-olm/src/main/java/org/apache/directory/olm/SourceGenerator.java
directory/sandbox/kayyagari/apacheds-olm/src/main/java/org/apache/directory/olm/util/AttributeFieldInterceptor.java
directory/sandbox/kayyagari/apacheds-olm/src/main/java/org/apache/directory/olm/util/CodeEnhancer.java
directory/sandbox/kayyagari/apacheds-olm/src/main/java/org/apache/directory/olm/util/EntryFieldInterceptor.java
Modified: directory/sandbox/kayyagari/apacheds-olm/src/main/java/org/apache/directory/olm/Attribute.java
URL: http://svn.apache.org/viewvc/directory/sandbox/kayyagari/apacheds-olm/src/main/java/org/apache/directory/olm/Attribute.java?rev=707705&r1=707704&r2=707705&view=diff
==============================================================================
--- directory/sandbox/kayyagari/apacheds-olm/src/main/java/org/apache/directory/olm/Attribute.java (original)
+++ directory/sandbox/kayyagari/apacheds-olm/src/main/java/org/apache/directory/olm/Attribute.java Fri Oct 24 11:37:36 2008
@@ -19,17 +19,18 @@
*/
package org.apache.directory.olm;
+
import java.util.Iterator;
-import java.util.Set;
+
/**
*
- * TODO Attribute.
+ * Represents an LDAP Attribute.
*
* @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
* @version $Rev$, $Date$
*/
-public interface Attribute //extends java.io.Serializable
+public interface Attribute
{
/**
*
@@ -38,7 +39,8 @@
* @return the name of the attribute
*/
String getAttrName();
-
+
+
/**
*
* gets the value of the attribute.
@@ -46,8 +48,8 @@
* @return value of the attribute
*/
Object getAttrValue();
-
-
+
+
/**
*
* gets the syntax of the attribute.
@@ -55,24 +57,69 @@
* @return the syntax of the attribute, null if no syntax defined
*/
String getSyntax();
-
+
+
/**
*
- * gets all the values if the Attribute is multivalued.
- *
+ * gets all the values if the Attribute is multivalued, null if it is single valued.
+ *
+ * @see for single valued Attribute use #getAttrValue()
+ *
* @return iterator values of the attribute
*/
public Iterator getAttrValues();
-
+
+
+ /**
+ *
+ * tells if the Attribute is Obsolete.
+ *
+ * @return true if obsolete, false otherwise
+ */
boolean isObsolete();
-
+
+
+ /**
+ *
+ * tells if the Attribute holds only single value.
+ *
+ * @return true if single value, false otherwise
+ */
boolean isSingleValue();
-
+
+
+ /**
+ *
+ * tells if the Attribute is a collective value.
+ *
+ * @return true if it is collective, flase otherwise
+ */
boolean isCollective();
-
+
+
+ /**
+ *
+ * tells if the Attribute cannot be modified by user.
+ *
+ * @return true if cannot be modified, flase otherwise
+ */
boolean isNoUserModification();
-
+
+
+ /**
+ *
+ * gives the OID of the corresponding schema attributeType.
+ *
+ * @return OID of the schema attributeType
+ */
String getOid();
-
+
+
+ /**
+ *
+ * gives the appropriate Java class used for representing the LDAP attribute's value in java.
+ *
+ * @return Class of the underlying java type
+ */
Class getJavaType();
}
Modified: directory/sandbox/kayyagari/apacheds-olm/src/main/java/org/apache/directory/olm/Entry.java
URL: http://svn.apache.org/viewvc/directory/sandbox/kayyagari/apacheds-olm/src/main/java/org/apache/directory/olm/Entry.java?rev=707705&r1=707704&r2=707705&view=diff
==============================================================================
--- directory/sandbox/kayyagari/apacheds-olm/src/main/java/org/apache/directory/olm/Entry.java (original)
+++ directory/sandbox/kayyagari/apacheds-olm/src/main/java/org/apache/directory/olm/Entry.java Fri Oct 24 11:37:36 2008
@@ -19,32 +19,113 @@
*/
package org.apache.directory.olm;
+
+import java.util.Iterator;
import java.util.Set;
-import org.apache.directory.shared.ldap.name.LdapDN;
import org.apache.directory.shared.ldap.schema.ObjectClassTypeEnum;
+
/**
*
- * TODO Entry.
- *
+ * Represents an LDAP Entry.
+ *
+ * Note: This java class holds all the MUST and MAY attributes present in the
+ * objectclass hierarchy of the objectclass represented by this entry
+ *
* @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
* @version $Rev$, $Date$
*/
public interface Entry
{
+ /**
+ *
+ * gives the name of the objectclass represented by this entry.
+ *
+ * @return name of the objectclass
+ */
String getName();
+
+ /**
+ *
+ * gives the OID of the objectclass represented by this entry.
+ *
+ * @return OID of the objectclass
+ */
String getOid();
-
+
+
+ /**
+ *
+ * gives the <b>non-null</b> <i>Attribute</i>s present in this entry
+ * including the attributes of any other Entry associated with this entry.
+ *
+ * Note: The associated Entry is something which is not part of the objectclass hierarchy and
+ * non-structural if this Entry is structural
+ *
+ * @return all non-null Attributes part of this Entry
+ */
Set<Attribute> getAttributes();
-
+
+
+ /**
+ * <pre>
+ * gives <i>all</i> (i.e both null and non-null ) Attributes part of this Entry's object class
+ * hierarchy.
+ * It doesn't contain any attributes of the additional entries (those which are not part of the
+ * OC hierarchy)that were added later</pre>
+ *
+ * @return all attributes that are part of this Entry's OC hierarchy
+ */
Attribute[] getAllAttributes();
-
+
+
+ /**
+ *
+ * gives the type of the objectclass represented by this entry.
+ *
+ * @see ObjectClassTypeEnum
+ *
+ * @return the type of the entry's objectclass
+ */
ObjectClassTypeEnum getType();
-
+
+
+ /**
+ *
+ * gives the DN in the string format.
+ *
+ * @return the distinguished name of this entry
+ */
String getDN();
-
+
+
+ /**
+ *
+ * set the DN of this entry.
+ *
+ * @param dn the DN of this entry
+ */
void setDN( String dn );
+
+
+ /**
+ *
+ * adds an Entry which is not part of the OC hierarchy of this Entry's objectclass.
+ *
+ * @param entry an entry to be added
+ */
+ void addEntry( Entry entry );
+
+
+ /**
+ *
+ * gives the additional entries that were added.
+ *
+ * @return additional entries that were added.
+ * An empty iterator will be returned if there are no additional entries.
+ */
+ Iterator<Entry> getAdditionalEntries();
}
Modified: directory/sandbox/kayyagari/apacheds-olm/src/main/java/org/apache/directory/olm/SourceFileWriter.java
URL: http://svn.apache.org/viewvc/directory/sandbox/kayyagari/apacheds-olm/src/main/java/org/apache/directory/olm/SourceFileWriter.java?rev=707705&r1=707704&r2=707705&view=diff
==============================================================================
--- directory/sandbox/kayyagari/apacheds-olm/src/main/java/org/apache/directory/olm/SourceFileWriter.java (original)
+++ directory/sandbox/kayyagari/apacheds-olm/src/main/java/org/apache/directory/olm/SourceFileWriter.java Fri Oct 24 11:37:36 2008
@@ -27,7 +27,7 @@
/**
*
- * TODO SourceFileWriter.
+ * SourceFileWriter.
*
* @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
* @version $Rev$, $Date$
@@ -40,12 +40,27 @@
/** static logger */
private static final Logger LOG = LoggerFactory.getLogger( SourceFileWriter.class );
+ /**
+ *
+ * Creates a new instance of SourceFileWriter.
+ *
+ * @param targetRoot the root folder in which the source files will be saved
+ */
public SourceFileWriter( File targetRoot )
{
TARGET_ROOT = targetRoot;
}
+ /**
+ *
+ * writes the java source file.
+ *
+ * @param className name of the java file (without package part in the name)
+ * @param javaSource the java source
+ * @param strPackage package name
+ * @throws Exception incase of failures in writing the file
+ */
public void writeFile( final String className, final String javaSource, final String strPackage ) throws Exception
{
File packgePath = null;
@@ -71,4 +86,5 @@
fileWriter.close();
}
+
}
Modified: directory/sandbox/kayyagari/apacheds-olm/src/main/java/org/apache/directory/olm/SourceGenerator.java
URL: http://svn.apache.org/viewvc/directory/sandbox/kayyagari/apacheds-olm/src/main/java/org/apache/directory/olm/SourceGenerator.java?rev=707705&r1=707704&r2=707705&view=diff
==============================================================================
--- directory/sandbox/kayyagari/apacheds-olm/src/main/java/org/apache/directory/olm/SourceGenerator.java (original)
+++ directory/sandbox/kayyagari/apacheds-olm/src/main/java/org/apache/directory/olm/SourceGenerator.java Fri Oct 24 11:37:36 2008
@@ -45,36 +45,68 @@
/**
*
- * Generates source from the given schema(s)
+ * Generates Entry and Attribute java source files from the given schema(s)
*
* @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
* @version $Rev$, $Date$
*/
public class SourceGenerator
{
+ /** the ANTLR parser used for parsing the schema files*/
private OpenLdapSchemaParser parser;
+
+ /** the mappings for syntax OID to java types*/
+ // TODO: it will be good to have the implemented matching rules
+ // so that can be used for better dirty tracking of fields
private Properties syntaxes;
+ /** holds all the attributes present in the schema(s)*/
private List<AttributeTypeLiteral> attrLiterals = new ArrayList<AttributeTypeLiteral>();
+
+ /** holds all the objectclasses present in the schema(s)*/
private List<ObjectClassLiteral> objClassLiterals = new ArrayList<ObjectClassLiteral>();
+ /**
+ * the defualt java type used in the case of no syntax(neither its own nor an inherited one)
+ * for an attribute
+ */
private static final String DEFAULT_SYNTAX_TYPE = "java.lang.String";
+ /** generated source file writer*/
private SourceFileWriter sourceWriter;
private String strPackage = "org.apache.directory.olm";
+ /* keys used for distinguishing the must and may attributes*/
private static final String MUST = "MUST";
private static final String MAY = "MAY";
+
private static final String OBJECTCLASS_FILE_SUFFIX = "Entry";
+ /* maps used for various requirement to replace the template placeholders*/
+
+ /**
+ * map for holding the attribute name(in lowercase) to the
+ * corresponding FQCN of Attribute type
+ */
Map<String, String> attrNameToClassNameMap = new TreeMap<String, String>();
+
+ /**
+ * map for holding the attribute name(in lowercase) to the javatype of
+ * the value it holds (based on the syntax)
+ */
+ //NOTE: any special chars like '-' will <b>NOT</b> be converted while putting in this map
Map<String, String> attrNameToJavaTypeMap = new HashMap<String, String>();
+
+ /** map for holding the syntax defined or inherited for each attribute*/
Map<String, String> attrNameToSyntaxOidMap = new HashMap<String, String>();
+
+ /** map for holding the objectclass name to the FQCN of the corresponding Entry type*/
Map<String, String> objClassToClassNameMap = new TreeMap<String, String>();
-
+
+ /** root folder for writing the generated source files*/
private File sourceRoot;
-
+
/** static logger */
private static final Logger LOG = LoggerFactory.getLogger( SourceGenerator.class );
@@ -83,10 +115,10 @@
{
this.syntaxes = syntaxes;
parser = new OpenLdapSchemaParser();
-
+
sourceRoot = new File( "/opt/workspace/apacheds-olm/target/generated-source" );
-
- if( ! sourceRoot.exists() )
+
+ if ( !sourceRoot.exists() )
{
sourceRoot.mkdirs();
}
@@ -95,12 +127,18 @@
}
+ /**
+ * @see #loadSchema(File)
+ */
public void loadSchema( String schemaFilePath ) throws Exception
{
loadSchema( new File( schemaFilePath ) );
}
+ /**
+ * @see #loadSchema(File)
+ */
public void loadSchema( Set<String> schemaFilePaths ) throws Exception
{
for ( String schema : schemaFilePaths )
@@ -110,6 +148,13 @@
}
+ /**
+ *
+ * parses the schema and extracts all the attribute and ojectclass literals.
+ *
+ * @param schemaFile the schema file to be parsed
+ * @throws Exception incase of any errors while parsing the schema file
+ */
public void loadSchema( File schemaFile ) throws Exception
{
parser.parse( schemaFile );
@@ -119,9 +164,16 @@
}
+ /**
+ *
+ * <p>generates the java source files from the attribue and objectclass literals
+ * extracted after parsing the schema file(s).</p>
+ *
+ * @throws Exception
+ */
public void generateSource() throws Exception
{
- LOG.debug( "Generating source..." );
+ LOG.info( "Generating source..." );
for ( AttributeTypeLiteral attrLiteral : attrLiterals )
{
@@ -149,43 +201,60 @@
generateObjectClassSource( objClass );
}
-
+
writeNameClassMap();
}
+ /**
+ *
+ * writes the mapping of attribute/objectclass name to FQCN to a file
+ *
+ * @throws Exception incase of failure in writing to a file
+ */
private void writeNameClassMap() throws Exception
{
StringBuilder sb = new StringBuilder();
-
+
sb.append( "# Attribute name to fully qualified class name mappings \n\n" );
-
- for( String attrName : attrNameToClassNameMap.keySet() )
+
+ for ( String attrName : attrNameToClassNameMap.keySet() )
{
- sb.append( attrName )
- .append( " = " )
- .append( attrNameToClassNameMap.get( attrName ) )
- .append( '\n' );
+ sb.append( attrName ).append( " = " ).append( attrNameToClassNameMap.get( attrName ) ).append( '\n' );
}
-
+
sb.append( "\n\n" );
sb.append( "# ObjectClass name to fully qualified class name mappings \n\n" );
-
- for( String objClass : objClassToClassNameMap.keySet() )
+
+ for ( String objClass : objClassToClassNameMap.keySet() )
{
- sb.append( objClass )
- .append( " = " )
- .append( objClassToClassNameMap.get( objClass ) )
- .append( '\n' );
+ sb.append( objClass ).append( " = " ).append( objClassToClassNameMap.get( objClass ) ).append( '\n' );
}
-
+
File file = new File( sourceRoot, "classmapping.properties" );
FileWriter fwriter = new FileWriter( file );
fwriter.write( sb.toString() );
fwriter.close();
}
-
-
+
+
+ /**
+ *
+ * <p>generate a java source file representing ObjectClass.<br>
+ * Note: here the source file contains all the attributes that are mentioned
+ * in the MUST and MAY sections of all objectclasses present in the hierarchy of
+ * the current objectclass under question</p>
+ *
+ * <p>Note: any special chars present in the name of objectclass that are not acceptable
+ * in java type names will be replaced with a "_" character e.g '-' with '_'</p>
+ *
+ * <em>Please note that the original attribute name is kept intact and only the
+ * modified one is used in the source</em>
+ *
+ * @param objClass the objectclass for which a java source file has to be generated
+ * @throws Exception if incase of any missing attributetype or objectclass literal
+ * that are required(both may and must) for this objectclass and its hierarchy
+ */
private void generateObjectClassSource( ObjectClassLiteral objClass ) throws Exception
{
String name = objClass.getNames()[0];
@@ -209,14 +278,16 @@
for ( String s : mustAttrs )
{
- mustAttrNameClassSet.add( new NameClassPair( s, attrNameToClassNameMap.get( s.toLowerCase() ), attrNameToJavaTypeMap.get( s.toLowerCase() ) ) );
+ mustAttrNameClassSet.add( new NameClassPair( s, attrNameToClassNameMap.get( s.toLowerCase() ),
+ attrNameToJavaTypeMap.get( s.toLowerCase() ) ) );
}
for ( String s : mayAttrs )
{
//TODO remove this debug statement
LOG.debug( s + ": " + attrNameToClassNameMap.get( s.toLowerCase() ) );
- NameClassPair np = new NameClassPair( s, attrNameToClassNameMap.get( s.toLowerCase() ), attrNameToJavaTypeMap.get( s.toLowerCase() ) );
+ NameClassPair np = new NameClassPair( s, attrNameToClassNameMap.get( s.toLowerCase() ),
+ attrNameToJavaTypeMap.get( s.toLowerCase() ) );
mayAttrNameClassSet.add( np );
}
@@ -240,22 +311,31 @@
template.setAttribute( "className", modifiedName );
template.setAttribute( "package", strPackage );
- if ( objClass.getClassType() != ObjectClassTypeEnum.STRUCTURAL )
- {
- template.setAttribute( "abstract", true );
- }
+ // if ( objClass.getClassType() != ObjectClassTypeEnum.STRUCTURAL )
+ // {
+ // template.setAttribute( "abstract", true );
+ // }
sourceWriter.writeFile( modifiedName, template.toString(), strPackage );
// the key should be the unaltered attributeName
objClassToClassNameMap.put( name.toLowerCase(), strPackage + "." + modifiedName );
-
+
LOG.debug( "Completed source generation for ObjectClass '{}'", modifiedName );
}
- private void getAttributesFromObjClass( ObjectClassLiteral objClass, Map<String, Set<String>> map, Set<String> objClassSuperiors )
- throws Exception
+ /**
+ *
+ * recursively gets all the Attributes from a ObjectClass and its superiors.
+ *
+ * @param objClass objectclass
+ * @param map map to hold the gathered must and may Attributes
+ * @param objClassSuperiors set of superior objectclass names for the given <i>objClass</i>
+ * @throws Exception incase of failure in looking up an object class (most likely in a missing objectclass case)
+ */
+ private void getAttributesFromObjClass( ObjectClassLiteral objClass, Map<String, Set<String>> map,
+ Set<String> objClassSuperiors ) throws Exception
{
String[] superClasses = objClass.getSuperiors();
if ( superClasses != null )
@@ -303,6 +383,14 @@
}
+ /**
+ *
+ * gets an ObjectClass by searching based on its name.
+ *
+ * @param name name of the objectclass to find
+ * @return the objectclass literal
+ * @throws Exception if not object class is found with the given <i>name</i>
+ */
private ObjectClassLiteral getObjClassByName( String name ) throws Exception
{
for ( ObjectClassLiteral obj : objClassLiterals )
@@ -321,6 +409,13 @@
}
+ /**
+ *
+ * gets the superior attribute with the given name.
+ *
+ * @param supAttrName name of the attribute
+ * @return an <i>AttributeTypeLiteral</i> with the given name, null if not found
+ */
private AttributeTypeLiteral getSupAttribute( String supAttrName )
{
LOG.debug( "getting the SUP with name " + supAttrName );
@@ -341,6 +436,13 @@
}
+ /**
+ *
+ * generates a java source file from the <i>AttributeTypeLiteral</i>.
+ *
+ * @param attrLiteral the attributetype literal
+ * @throws Exception incase of any problems in writing the genrated source to the file
+ */
private void generateAttributeSource( AttributeTypeLiteral attrLiteral ) throws Exception
{
if ( attrLiteral.getSuperior() != null )
@@ -360,19 +462,19 @@
if ( attrLiteral.getSyntax() == null )
{
template.setAttribute( "syntax", getSuperSyntax( attrLiteral ) );
-
+
template.setAttribute( "argType", DEFAULT_SYNTAX_TYPE );
attrNameToJavaTypeMap.put( name.toLowerCase(), DEFAULT_SYNTAX_TYPE );
}
else
{
String javaType = syntaxes.getProperty( attrLiteral.getSyntax() );
-
+
template.setAttribute( "argType", javaType );
attrNameToJavaTypeMap.put( name.toLowerCase(), javaType );
-
+
attrNameToSyntaxOidMap.put( name.toLowerCase(), attrLiteral.getSyntax() );
-
+
template.setAttribute( "syntax", attrLiteral.getSyntax() );
}
@@ -389,11 +491,11 @@
template.setAttribute( "oid", attrLiteral.getOid() );
-// if ( attrLiteral.getSuperior() != null )
-// {
-// template.setAttribute( "extends", true );
-// template.setAttribute( "superClass", attrNameToClassNameMap.get( attrLiteral.getSuperior().toLowerCase() ) );
-// }
+ // if ( attrLiteral.getSuperior() != null )
+ // {
+ // template.setAttribute( "extends", true );
+ // template.setAttribute( "superClass", attrNameToClassNameMap.get( attrLiteral.getSuperior().toLowerCase() ) );
+ // }
template.setAttribute( "attrName", modifiedName );
template.setAttribute( "package", strPackage );
@@ -412,31 +514,31 @@
*
* gets the syntax of a attribute by walking up the AttributeType hierarchy.
*
- * @param attrLiteral
- * @return
+ * @param attrLiteral the <i>AttributeTypeLiteral</i>
+ * @return syntax string
*/
private String getSuperSyntax( AttributeTypeLiteral attrLiteral )
{
String superior = attrLiteral.getSuperior();
String syntax = "";
-
- if( superior != null )
+
+ if ( superior != null )
{
AttributeTypeLiteral superiorAttr = getSupAttribute( superior );
- if( superiorAttr.getSyntax() != null )
+ if ( superiorAttr.getSyntax() != null )
{
- syntax = superiorAttr.getSyntax();
+ syntax = superiorAttr.getSyntax();
}
- else if( superiorAttr.getSuperior() != null )
+ else if ( superiorAttr.getSuperior() != null )
{
getSuperSyntax( superiorAttr );
}
}
-
+
return syntax;
}
-
-
+
+
public static void main( String[] args ) throws Exception
{
Properties syntaxes = new Properties();
@@ -445,10 +547,10 @@
SourceGenerator sg = new SourceGenerator( syntaxes );
boolean runTestSchema = false;
- if( runTestSchema )
+ if ( runTestSchema )
{
sg.loadSchema( "src/main/resources/test.schema" );
-
+
}
else
{
@@ -458,7 +560,7 @@
schemas.add( "/opt/workspace/apacheds/apacheds/schema-bootstrap/src/main/schema/apache.schema" );
schemas.add( "/opt/workspace/apacheds/apacheds/schema-bootstrap/src/main/schema/core.schema" );
schemas.add( "/opt/workspace/apacheds/apacheds/schema-extras/src/main/schema/cosine.schema" );
-
+
sg.loadSchema( schemas );
sg.generateSource();
}
Modified: directory/sandbox/kayyagari/apacheds-olm/src/main/java/org/apache/directory/olm/util/AttributeFieldInterceptor.java
URL: http://svn.apache.org/viewvc/directory/sandbox/kayyagari/apacheds-olm/src/main/java/org/apache/directory/olm/util/AttributeFieldInterceptor.java?rev=707705&r1=707704&r2=707705&view=diff
==============================================================================
--- directory/sandbox/kayyagari/apacheds-olm/src/main/java/org/apache/directory/olm/util/AttributeFieldInterceptor.java (original)
+++ directory/sandbox/kayyagari/apacheds-olm/src/main/java/org/apache/directory/olm/util/AttributeFieldInterceptor.java Fri Oct 24 11:37:36 2008
@@ -26,42 +26,56 @@
import org.apache.directory.olm.Entry;
+/**
+ *
+ * A FieldInterceptor used for intercepting the fields of Attributes.
+ *
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ * @version $Rev$, $Date$
+ */
public class AttributeFieldInterceptor extends FieldInterceptor
{
-// private Map<String, Object> dirtyFieldMap = new HashMap<String, Object>();
-
+ /** the entry to which this interceptor's Attribute belongs */
private Entry entry;
+
+ /**
+ *
+ * Creates a new instance of AttributeFieldInterceptor.
+ *
+ * @param entry the entry to which this interceptor's Attribute belongs
+ */
public AttributeFieldInterceptor( Entry entry )
{
- if( entry == null )
+ if ( entry == null )
{
throw new IllegalArgumentException( "Entry cannot be null" );
}
-
+
this.entry = entry;
}
-
-// public boolean isDirty()
-// {
-// return ( ! dirtyFieldMap.isEmpty() );
-// }
-
-
+
+
+ /**
+ * calls the interceptWrite on the Entry of this interceptor's Attribute
+ * by setting the source object as Entry's reference, name to the Attribute's name
+ * and oldValue to the actual Attribute reference. (the newValue is left as it is)
+ *
+ * In a sense it simulates a write in a Attribute as a change happened to the field representing
+ * this Attribute in the corresponding Entry.
+ *
+ * This avoids the need to check the dirty status for each field of a Attribute, just checking the the dirty
+ * status of each 'Attribute' field inside the Entry is enough.
+ *
+ */
+ @Override
protected void interceptWrite( Object obj, String name, Object oldValue, Object newValue )
{
-// System.out.println( "intercept attribute obj:" + obj + ", name: " + name + ", oldValue: " + oldValue + ", newValue: "
-// + newValue );
-// if ( ( ! dirtyFieldMap.containsKey( name ) ) && oldValue != newValue )
-// {
-// dirtyFieldMap.put( name, newValue );
-// }
-
EntryFieldInterceptor interceptor = ( EntryFieldInterceptor ) ( ( InterceptFieldEnabled ) entry ).getInterceptFieldCallback();
// this name is important to identify the modified field
name = ( ( Attribute ) obj ).getAttrName();
-
+
interceptor.interceptWrite( entry, name, obj, newValue );
}
@@ -69,7 +83,7 @@
@Override
protected void interceptRead( Object obj, String name, Object oldValue )
{
- System.out.println( "interceptRead name: " + name + ", oldValue: " + oldValue );
+ // System.out.println( "interceptRead name: " + name + ", oldValue: " + oldValue );
}
-
+
}
Modified: directory/sandbox/kayyagari/apacheds-olm/src/main/java/org/apache/directory/olm/util/CodeEnhancer.java
URL: http://svn.apache.org/viewvc/directory/sandbox/kayyagari/apacheds-olm/src/main/java/org/apache/directory/olm/util/CodeEnhancer.java?rev=707705&r1=707704&r2=707705&view=diff
==============================================================================
--- directory/sandbox/kayyagari/apacheds-olm/src/main/java/org/apache/directory/olm/util/CodeEnhancer.java (original)
+++ directory/sandbox/kayyagari/apacheds-olm/src/main/java/org/apache/directory/olm/util/CodeEnhancer.java Fri Oct 24 11:37:36 2008
@@ -24,29 +24,25 @@
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
-import java.lang.reflect.Field;
+import java.io.InputStream;
import net.sf.cglib.core.ClassGenerator;
import net.sf.cglib.core.DebuggingClassWriter;
import net.sf.cglib.transform.ClassReaderGenerator;
-import net.sf.cglib.transform.ClassTransformer;
-import net.sf.cglib.transform.ClassTransformerFactory;
import net.sf.cglib.transform.TransformingClassGenerator;
-import net.sf.cglib.transform.impl.InterceptFieldEnabled;
import net.sf.cglib.transform.impl.InterceptFieldTransformer;
-import org.apache.directory.olm.InetOrgPersonEntry;
import org.objectweb.asm.Attribute;
import org.objectweb.asm.ClassReader;
import org.objectweb.asm.ClassWriter;
-import org.objectweb.asm.Opcodes;
-import org.objectweb.asm.Type;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
+
/**
*
- * TODO CodeEnhancer.
+ * CodeEnhancer. Injects a new interface to intercept the fields' read/write access
+ * in Entry and Attribute implementations.
*
* @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
* @version $Rev$, $Date$
@@ -56,70 +52,66 @@
private static Logger LOG = LoggerFactory.getLogger( CodeEnhancer.class );
- ClassTransformerFactory transformFactory = new ClassTransformerFactory()
- {
-
- public ClassTransformer newInstance()
- {
- return new InterceptFieldTransformer( new FieldFilter() );
- }
-
- };
-
-
- protected ClassGenerator getGenerator( ClassReader r )
+ private ClassGenerator getGenerator( ClassReader r )
{
-// return new TransformingClassGenerator( new ClassReaderGenerator( r, attributes(), getFlags() ),
-// transformFactory.newInstance() );
- return new TransformingClassGenerator( new ClassReaderGenerator( r, attributes(), getFlags() ), new InterceptFieldTransformer( new FieldFilter() ) );
+ // return new TransformingClassGenerator( new ClassReaderGenerator( r, attributes(), getFlags() ),
+ // transformFactory.newInstance() );
+ return new TransformingClassGenerator( new ClassReaderGenerator( r, attributes(), getFlags() ),
+ new InterceptFieldTransformer( new FieldFilter() ) );
}
- protected int getFlags()
+ private int getFlags()
{
return 0;
}
- protected Attribute[] attributes()
+ /**
+ *
+ * TODO attributes.
+ *
+ * @return
+ */
+ private Attribute[] attributes()
{
return null;
}
- private void enhance( File classFile ) throws Exception
+ public void enhance( File classFile ) throws Exception
{
- java.io.InputStream is = new FileInputStream( classFile ); //classPath.getResourceAsStream( name.replace( '.', '/' ) + ".class" );
+ InputStream classStream = new FileInputStream( classFile );
- if ( is == null )
+ if ( classStream == null )
{
throw new FileNotFoundException( classFile.getAbsolutePath() );
}
- ClassReader r = new ClassReader( is );
- is.close();
+ ClassReader r = new ClassReader( classStream );
+ classStream.close();
String[] interfaces = r.getInterfaces();
-
- if( ! canEnhance( interfaces ) )
+
+ if ( !canEnhance( interfaces ) )
{
LOG.warn( "Neither an Entry nor an Attribute skipping enhancing" );
return;
}
-
- if( isEnhanced( interfaces ) )
+
+ if ( isEnhanced( interfaces ) )
{
- LOG.warn( "Skipping class {} as it was already enhanced.", classFile.getAbsolutePath() );
- return;
+ LOG.warn( "Skipping class {} as it was already enhanced.", classFile.getAbsolutePath() );
+ return;
}
-
-// if( isAttribute( interfaces ) )
-// {
-// LOG.warn( "TODO need to enance Attributes for dirty tracking of underlying collection of multi valued attributes." );
-// return;
-// }
-
- ClassWriter w = new CustomDebuggingClassWriter( ClassWriter.COMPUTE_MAXS );
+
+ // if( isAttribute( interfaces ) )
+ // {
+ // LOG.warn( "TODO need to enance Attributes for dirty tracking of underlying collection of multi valued attributes." );
+ // return;
+ // }
+
+ ClassWriter w = new DebuggingClassWriter( ClassWriter.COMPUTE_MAXS ); // use CustomDebuggingClassWriter for additional features
getGenerator( r ).generateClass( w );
byte[] b = w.toByteArray();
@@ -130,108 +122,91 @@
fout.close();
}
-
+
private boolean isEnhanced( String[] interfaces )
{
- for( String s : interfaces )
+ for ( String s : interfaces )
{
s = s.replace( "/", "." );// class file doesn't contain a '.' in a FQCN
- if( s.equals( "net.sf.cglib.transform.impl.InterceptFieldEnabled" ) )
+ if ( s.equals( "net.sf.cglib.transform.impl.InterceptFieldEnabled" ) )
{
return true;
}
}
-
+
return false;
}
-
+
private boolean isAttribute( String[] interfaces )
{
- for( String s : interfaces )
+ for ( String s : interfaces )
{
s = s.replace( "/", "." );// class file doesn't contain a '.' in a FQCN
- if( s.equals( "org.apache.directory.olm.Attribute" ) )
+ if ( s.equals( "org.apache.directory.olm.Attribute" ) )
{
return true;
}
}
-
+
return false;
}
-
+
+
private boolean canEnhance( String[] interfaces )
{
boolean isAttribute = false;
boolean isEntry = false;
-
- for( String s : interfaces )
+
+ for ( String s : interfaces )
{
s = s.replace( "/", "." );// class file doesn't contain a '.' in a FQCN
- if( s.equals( "org.apache.directory.olm.Attribute" ) )
+ if ( s.equals( "org.apache.directory.olm.Attribute" ) )
{
isAttribute = true;
}
-
- if( s.equals( "org.apache.directory.olm.Entry" ) )
+
+ if ( s.equals( "org.apache.directory.olm.Entry" ) )
{
isEntry = true;
}
}
-
- return ( isEntry || isAttribute );
- }
-
-
- public static void main( String[] args ) throws Exception
- {
- CodeEnhancer ce = new CodeEnhancer();
- File rootDir = new File( "/opt/workspace/apacheds-olm/target/classes" );
- ce.enhanceAll( rootDir.listFiles() );
-
-// String claz = InetOrgPersonEntry.class.getName().replace( ".", "/" ) + ".class";
-// claz = Cn.class.getName().replace( ".", "/" ) + ".class";
-// File classFile = new File( ce.getClass().getClassLoader().getSystemResource( claz ).getPath() );
-// ce.enhance( classFile );
-// ce.interceptPerson();
+ return ( isEntry || isAttribute );
}
public void enhanceAll( File[] dirs ) throws Exception
{
- for( File f : dirs )
+ for ( File f : dirs )
{
- if( f.isDirectory() )
+ if ( f.isDirectory() )
{
enhanceAll( f.listFiles() );
}
- else if( f.isFile() && f.getName().endsWith( ".class" ) )
+ else if ( f.isFile() && f.getName().endsWith( ".class" ) )
{
enhance( f );
}
}
}
-
-
- public void interceptPerson() throws Exception
- {
- InetOrgPersonEntry person = new InetOrgPersonEntry( "cn", "sn" );
- ( ( InterceptFieldEnabled ) person ).setInterceptFieldCallback( new EntryFieldInterceptor() );
- person.setCn( "cn" );
- person.getCn().addValue( "xyz" );
- person.setSn( "sn" );
- person.setTelephoneNumber( "08855289560" );
- person.setAudio( new byte[0] );
-
- Field f = person.getClass().getDeclaredField( "dirty" );
- f.setAccessible( true );
- System.out.println( f.get( person ) );
- f.set( person, true );
- System.out.println( f.get( person ) );
- }
+
}
+/*
+ just incase if rquired to use a ClassTransformerFactory
+ ClassTransformerFactory transformFactory = new ClassTransformerFactory()
+ {
+
+ public ClassTransformer newInstance()
+ {
+ return new InterceptFieldTransformer( new FieldFilter() );
+ }
+
+ };
+
+ */
+/* can be used incase if additional fields need to be injected
class CustomDebuggingClassWriter extends DebuggingClassWriter
{
@@ -249,3 +224,4 @@
}
}
+*/
\ No newline at end of file
Modified: directory/sandbox/kayyagari/apacheds-olm/src/main/java/org/apache/directory/olm/util/EntryFieldInterceptor.java
URL: http://svn.apache.org/viewvc/directory/sandbox/kayyagari/apacheds-olm/src/main/java/org/apache/directory/olm/util/EntryFieldInterceptor.java?rev=707705&r1=707704&r2=707705&view=diff
==============================================================================
--- directory/sandbox/kayyagari/apacheds-olm/src/main/java/org/apache/directory/olm/util/EntryFieldInterceptor.java (original)
+++ directory/sandbox/kayyagari/apacheds-olm/src/main/java/org/apache/directory/olm/util/EntryFieldInterceptor.java Fri Oct 24 11:37:36 2008
@@ -26,28 +26,49 @@
/**
*
- * TODO EntryFieldInterceptor.
+ * A field interceptor for Entry implementations.
*
* @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
* @version $Rev$, $Date$
*/
public class EntryFieldInterceptor extends FieldInterceptor
{
+ /** map to hold all the dirty <i>Attribute</i>s*/
private Map<String, Attribute> dirtyAttrMap = new HashMap<String, Attribute>();
+
+ //TODO need to have a @NoIntercept annotation for better handling of
+ // fields which doesn't need interception or which are not of type Attribute
+ private Map<String, Object> dirtyFieldMap = new HashMap<String, Object>();
+ /**
+ *
+ * tells if the Entity's any Attributes are changed (dirty).
+ *
+ * @return true if modified, flase otherwise
+ */
public boolean isDirty()
{
return ( ! dirtyAttrMap.isEmpty() );
}
+ /**
+ *
+ */
protected void interceptWrite( Object obj, String name, Object oldValue, Object newValue )
{
System.out.println( "intercept obj:" + obj + ", name: " + name + ", oldValue: " + oldValue + ", newValue: "
+ newValue );
- if ( ( ! dirtyAttrMap.containsKey( name ) ) && oldValue != newValue )
+ if( obj instanceof Attribute )
+ {
+ if ( ( ! dirtyAttrMap.containsKey( name ) ) && oldValue != newValue )
+ {
+ dirtyAttrMap.put( name, ( Attribute ) oldValue );
+ }
+ }
+ else if ( ( ! dirtyFieldMap.containsKey( name ) ) && oldValue != newValue )
{
- dirtyAttrMap.put( name, ( Attribute ) oldValue );
+ dirtyFieldMap.put( name, oldValue );
}
}
@@ -58,6 +79,12 @@
}
+ public Map<String, Object> getDirtyFieldMap()
+ {
+ return dirtyFieldMap;
+ }
+
+
@Override
protected void interceptRead( Object obj, String name, Object oldValue )
{