You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@directory.apache.org by el...@apache.org on 2011/03/06 23:37:01 UTC
svn commit: r1078612 - in
/directory/shared/trunk/ldap/model/src/main/java/org/apache/directory/shared/ldap/model/name:
Ava.java AvaSerializer.java Dn.java DnSerializer.java Rdn.java
RdnSerializer.java
Author: elecharny
Date: Sun Mar 6 22:37:00 2011
New Revision: 1078612
URL: http://svn.apache.org/viewvc?rev=1078612&view=rev
Log:
o Ava, rdn and Dn are externalizable classes
o the XXXSerializer reuse the readExternal and writeExternal methods now
Modified:
directory/shared/trunk/ldap/model/src/main/java/org/apache/directory/shared/ldap/model/name/Ava.java
directory/shared/trunk/ldap/model/src/main/java/org/apache/directory/shared/ldap/model/name/AvaSerializer.java
directory/shared/trunk/ldap/model/src/main/java/org/apache/directory/shared/ldap/model/name/Dn.java
directory/shared/trunk/ldap/model/src/main/java/org/apache/directory/shared/ldap/model/name/DnSerializer.java
directory/shared/trunk/ldap/model/src/main/java/org/apache/directory/shared/ldap/model/name/Rdn.java
directory/shared/trunk/ldap/model/src/main/java/org/apache/directory/shared/ldap/model/name/RdnSerializer.java
Modified: directory/shared/trunk/ldap/model/src/main/java/org/apache/directory/shared/ldap/model/name/Ava.java
URL: http://svn.apache.org/viewvc/directory/shared/trunk/ldap/model/src/main/java/org/apache/directory/shared/ldap/model/name/Ava.java?rev=1078612&r1=1078611&r2=1078612&view=diff
==============================================================================
--- directory/shared/trunk/ldap/model/src/main/java/org/apache/directory/shared/ldap/model/name/Ava.java (original)
+++ directory/shared/trunk/ldap/model/src/main/java/org/apache/directory/shared/ldap/model/name/Ava.java Sun Mar 6 22:37:00 2011
@@ -36,7 +36,6 @@ import org.apache.directory.shared.ldap.
import org.apache.directory.shared.ldap.model.schema.MatchingRule;
import org.apache.directory.shared.ldap.model.schema.SchemaManager;
import org.apache.directory.shared.util.Strings;
-import org.apache.directory.shared.util.Unicode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -782,9 +781,35 @@ public final class Ava implements Extern
throw new IOException( message );
}
- Unicode.writeUTF(out, upName);
- Unicode.writeUTF(out, upType);
- Unicode.writeUTF(out, normType);
+ if ( upName != null )
+ {
+ out.writeBoolean( true );
+ out.writeUTF( upName );
+ }
+ else
+ {
+ out.writeBoolean( false);
+ }
+
+ if ( upType != null )
+ {
+ out.writeBoolean( true );
+ out.writeUTF( upType );
+ }
+ else
+ {
+ out.writeBoolean( false);
+ }
+
+ if ( normType != null )
+ {
+ out.writeBoolean( true );
+ out.writeUTF( normType );
+ }
+ else
+ {
+ out.writeBoolean( false);
+ }
boolean isHR = !normValue.isBinary();
@@ -792,15 +817,13 @@ public final class Ava implements Extern
if ( isHR )
{
- Unicode.writeUTF(out, upValue.getString());
- Unicode.writeUTF(out, normValue.getString());
+ StringValue.serialize( upValue, out );
+ StringValue.serialize( normValue, out );
}
else
{
- out.writeInt( upValue.length() );
- out.write( upValue.getBytes() );
- out.writeInt( normValue.length() );
- out.write( normValue.getBytes() );
+ BinaryValue.serialize( upValue, out );
+ BinaryValue.serialize( normValue, out );
}
}
@@ -814,28 +837,43 @@ public final class Ava implements Extern
*/
public void readExternal( ObjectInput in ) throws IOException, ClassNotFoundException
{
- upName = Unicode.readUTF(in);
- upType = Unicode.readUTF(in);
- normType = Unicode.readUTF(in);
+ boolean hasUpName = in.readBoolean();
+
+ if ( hasUpName )
+ {
+ upName = in.readUTF();
+ }
+
+ boolean hasUpType = in.readBoolean();
+
+ if ( hasUpType )
+ {
+ upType = in.readUTF();
+ }
+
+ boolean hasNormType = in.readBoolean();
+
+ if ( hasNormType )
+ {
+ normType = in.readUTF();
+ }
boolean isHR = in.readBoolean();
if ( isHR )
{
- upValue = new StringValue( Unicode.readUTF(in) );
- normValue = new StringValue( Unicode.readUTF(in) );
+ upValue = StringValue.deserialize( schemaManager, in );
+ normValue = StringValue.deserialize( schemaManager, in );
}
else
{
- int upValueLength = in.readInt();
- byte[] upValueBytes = new byte[upValueLength];
- in.readFully( upValueBytes );
- upValue = new BinaryValue( upValueBytes );
-
- int valueLength = in.readInt();
- byte[] normValueBytes = new byte[valueLength];
- in.readFully( normValueBytes );
- normValue = new BinaryValue( normValueBytes );
+ upValue = BinaryValue.deserialize( schemaManager, in );
+ normValue = BinaryValue.deserialize( schemaManager, in );
+ }
+
+ if ( schemaManager != null )
+ {
+ attributeType = schemaManager.getAttributeType( upType );
}
}
Modified: directory/shared/trunk/ldap/model/src/main/java/org/apache/directory/shared/ldap/model/name/AvaSerializer.java
URL: http://svn.apache.org/viewvc/directory/shared/trunk/ldap/model/src/main/java/org/apache/directory/shared/ldap/model/name/AvaSerializer.java?rev=1078612&r1=1078611&r2=1078612&view=diff
==============================================================================
--- directory/shared/trunk/ldap/model/src/main/java/org/apache/directory/shared/ldap/model/name/AvaSerializer.java (original)
+++ directory/shared/trunk/ldap/model/src/main/java/org/apache/directory/shared/ldap/model/name/AvaSerializer.java Sun Mar 6 22:37:00 2011
@@ -23,13 +23,8 @@ import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;
-import org.apache.directory.shared.ldap.model.entry.BinaryValue;
-import org.apache.directory.shared.ldap.model.entry.StringValue;
-import org.apache.directory.shared.ldap.model.entry.Value;
import org.apache.directory.shared.ldap.model.exception.LdapInvalidDnException;
import org.apache.directory.shared.ldap.model.schema.SchemaManager;
-import org.apache.directory.shared.util.Strings;
-import org.apache.directory.shared.util.Unicode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -75,65 +70,14 @@ public final class AvaSerializer
* <li>valueLength</li>
* <li>value</li> The normalized value.
*
- * @param atav the AttributeTypeAndValue to serialize
+ * @param ava the AttributeTypeAndValue to serialize
* @param out the OutputStream in which the atav will be serialized
* @throws IOException If we can't serialize the atav
*/
- public static void serialize( Ava atav, ObjectOutput out ) throws IOException
+ public static void serialize( Ava ava, ObjectOutput out ) throws IOException
{
- if ( Strings.isEmpty(atav.getUpName())
- || Strings.isEmpty(atav.getUpType())
- || Strings.isEmpty(atav.getNormType())
- || ( atav.getUpValue().isNull() )
- || ( atav.getNormValue().isNull() ) )
- {
- String message = "Cannot serialize an wrong ATAV, ";
-
- if ( Strings.isEmpty(atav.getUpName()) )
- {
- message += "the upName should not be null or empty";
- }
- else if ( Strings.isEmpty(atav.getUpType()) )
- {
- message += "the upType should not be null or empty";
- }
- else if ( Strings.isEmpty(atav.getNormType()) )
- {
- message += "the normType should not be null or empty";
- }
- else if ( atav.getUpValue().isNull() )
- {
- message += "the upValue should not be null";
- }
- else if ( atav.getNormValue().isNull() )
- {
- message += "the value should not be null";
- }
-
- LOG.error( message );
- throw new IOException( message );
- }
-
- Unicode.writeUTF(out, atav.getUpName());
- Unicode.writeUTF(out, atav.getUpType());
- Unicode.writeUTF(out, atav.getNormType());
-
- boolean isHR = !atav.getNormValue().isBinary();
-
- out.writeBoolean( isHR );
-
- if ( isHR )
- {
- Unicode.writeUTF(out, atav.getUpValue().getString());
- Unicode.writeUTF(out, atav.getNormValue().getString());
- }
- else
- {
- out.writeInt( atav.getUpValue().length() );
- out.write( atav.getUpValue().getBytes() );
- out.writeInt( atav.getNormValue().length() );
- out.write( atav.getNormValue().getBytes() );
- }
+ ava.writeExternal( out );
+ out.flush();
}
@@ -151,45 +95,17 @@ public final class AvaSerializer
public static Ava deserialize( SchemaManager schemaManager, ObjectInput in )
throws IOException, LdapInvalidDnException
{
- String upName = Unicode.readUTF(in);
- String upType = Unicode.readUTF(in);
- String normType = Unicode.readUTF(in);
+ Ava ava = new Ava( schemaManager );
- boolean isHR = in.readBoolean();
-
try
{
- if ( isHR )
- {
- Value<String> upValue = new StringValue( Unicode.readUTF(in) );
- Value<String> normValue = new StringValue( Unicode.readUTF(in) );
-
- Ava atav =
- new Ava( schemaManager, upType, normType, upValue, normValue );
-
- return atav;
- }
- else
- {
- int upValueLength = in.readInt();
- byte[] upValue = new byte[upValueLength];
- in.readFully( upValue );
-
- int valueLength = in.readInt();
- byte[] normValue = new byte[valueLength];
- in.readFully( normValue );
-
- Ava atav =
- new Ava( upType, normType,
- new BinaryValue( upValue) ,
- new BinaryValue( normValue ), upName );
-
- return atav;
- }
+ ava.readExternal( in );
}
- catch ( LdapInvalidDnException ine )
+ catch ( ClassNotFoundException cnfe )
{
- throw new IOException( ine.getMessage() );
+ throw new IOException( cnfe.getMessage() );
}
+
+ return ava;
}
}
Modified: directory/shared/trunk/ldap/model/src/main/java/org/apache/directory/shared/ldap/model/name/Dn.java
URL: http://svn.apache.org/viewvc/directory/shared/trunk/ldap/model/src/main/java/org/apache/directory/shared/ldap/model/name/Dn.java?rev=1078612&r1=1078611&r2=1078612&view=diff
==============================================================================
--- directory/shared/trunk/ldap/model/src/main/java/org/apache/directory/shared/ldap/model/name/Dn.java (original)
+++ directory/shared/trunk/ldap/model/src/main/java/org/apache/directory/shared/ldap/model/name/Dn.java Sun Mar 6 22:37:00 2011
@@ -21,6 +21,10 @@
package org.apache.directory.shared.ldap.model.name;
+import java.io.Externalizable;
+import java.io.IOException;
+import java.io.ObjectInput;
+import java.io.ObjectOutput;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
@@ -59,7 +63,7 @@ import org.slf4j.LoggerFactory;
*
* @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
*/
-public final class Dn implements Iterable<Rdn>
+public final class Dn implements Iterable<Rdn>, Externalizable
{
/** The LoggerFactory used by this class */
protected static final Logger LOG = LoggerFactory.getLogger( Dn.class );
@@ -1683,4 +1687,72 @@ public final class Dn implements Iterabl
return true;
}
+
+
+ public void readExternal( ObjectInput in ) throws IOException, ClassNotFoundException
+ {
+ // Read the UPName
+ upName = in.readUTF();
+
+ // Read the NormName
+ normName = in.readUTF();
+
+ if ( normName.length() == 0 )
+ {
+ // As the normName is equal to the upName,
+ // we didn't saved the nbnormName on disk.
+ // restore it by copying the upName.
+ normName = upName;
+ }
+
+ // Read the RDNs. Is it's null, the number will be -1.
+ int nbRdns = in.readInt();
+
+ rdns = new ArrayList<Rdn>( nbRdns );
+
+ for ( int i = 0; i < nbRdns; i++ )
+ {
+ Rdn rdn = new Rdn( schemaManager );
+ rdn.readExternal( in );
+ rdns.add( rdn );
+ }
+ }
+
+
+ public void writeExternal( ObjectOutput out ) throws IOException
+ {
+ if ( upName == null )
+ {
+ String message = "Cannot serialize a NULL Dn";
+ LOG.error( message );
+ throw new IOException( message );
+ }
+
+ // Write the UPName
+ out.writeUTF( upName );
+
+ // Write the NormName if different
+ if ( upName.equals( normName ) )
+ {
+ out.writeUTF( "" );
+ }
+ else
+ {
+ out.writeUTF( normName );
+ }
+
+ // Should we store the byte[] ???
+
+ // Write the RDNs.
+ // First the number of RDNs
+ out.writeInt( size() );
+
+ // Loop on the RDNs
+ for ( Rdn rdn : rdns )
+ {
+ rdn.writeExternal( out );
+ }
+
+ out.flush();
+ }
}
Modified: directory/shared/trunk/ldap/model/src/main/java/org/apache/directory/shared/ldap/model/name/DnSerializer.java
URL: http://svn.apache.org/viewvc/directory/shared/trunk/ldap/model/src/main/java/org/apache/directory/shared/ldap/model/name/DnSerializer.java?rev=1078612&r1=1078611&r2=1078612&view=diff
==============================================================================
--- directory/shared/trunk/ldap/model/src/main/java/org/apache/directory/shared/ldap/model/name/DnSerializer.java (original)
+++ directory/shared/trunk/ldap/model/src/main/java/org/apache/directory/shared/ldap/model/name/DnSerializer.java Sun Mar 6 22:37:00 2011
@@ -29,7 +29,6 @@ import java.io.ObjectOutputStream;
import org.apache.directory.shared.ldap.model.exception.LdapInvalidDnException;
import org.apache.directory.shared.ldap.model.schema.SchemaManager;
-import org.apache.directory.shared.util.Unicode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -76,8 +75,6 @@ public final class DnSerializer
serialize( dn, out );
- out.flush();
-
return baos.toByteArray();
}
@@ -101,37 +98,8 @@ public final class DnSerializer
*/
public static void serialize( Dn dn, ObjectOutput out ) throws IOException
{
- if ( dn.getName() == null )
- {
- String message = "Cannot serialize a NULL Dn";
- LOG.error( message );
- throw new IOException( message );
- }
-
- // Write the UPName
- Unicode.writeUTF(out, dn.getName());
-
- // Write the NormName if different
- if ( dn.getName().equals( dn.getNormName() ) )
- {
- Unicode.writeUTF(out, "");
- }
- else
- {
- Unicode.writeUTF(out, dn.getNormName());
- }
-
- // Should we store the byte[] ???
-
- // Write the RDNs.
- // First the number of RDNs
- out.writeInt( dn.size() );
-
- // Loop on the RDNs
- for ( Rdn rdn:dn.getRdns() )
- {
- RdnSerializer.serialize( rdn, out );
- }
+ dn.writeExternal( out );
+ out.flush();
}
@@ -171,32 +139,16 @@ public final class DnSerializer
public static Dn deserialize( SchemaManager schemaManager, ObjectInput in )
throws IOException, LdapInvalidDnException
{
- // Read the UPName
- String upName = Unicode.readUTF(in);
-
- // Read the NormName
- String normName = Unicode.readUTF(in);
-
- if ( normName.length() == 0 )
+ Dn dn = new Dn( schemaManager );
+
+ try
{
- // As the normName is equal to the upName,
- // we didn't saved the nbnormName on disk.
- // restore it by copying the upName.
- normName = upName;
+ dn.readExternal( in );
}
-
- // Read the RDNs. Is it's null, the number will be -1.
- int nbRdns = in.readInt();
-
- Rdn[] rdns = new Rdn[nbRdns];
-
- for ( int i = 0; i < nbRdns; i++ )
+ catch ( ClassNotFoundException cnfe )
{
- Rdn rdn = RdnSerializer.deserialize( schemaManager, in );
- rdns[i] = rdn;
+ throw new IOException( cnfe.getMessage() );
}
-
- Dn dn = new Dn( schemaManager, upName, normName, rdns );
return dn;
}
Modified: directory/shared/trunk/ldap/model/src/main/java/org/apache/directory/shared/ldap/model/name/Rdn.java
URL: http://svn.apache.org/viewvc/directory/shared/trunk/ldap/model/src/main/java/org/apache/directory/shared/ldap/model/name/Rdn.java?rev=1078612&r1=1078611&r2=1078612&view=diff
==============================================================================
--- directory/shared/trunk/ldap/model/src/main/java/org/apache/directory/shared/ldap/model/name/Rdn.java (original)
+++ directory/shared/trunk/ldap/model/src/main/java/org/apache/directory/shared/ldap/model/name/Rdn.java Sun Mar 6 22:37:00 2011
@@ -1485,15 +1485,15 @@ public final class Rdn implements Clonea
public void writeExternal( ObjectOutput out ) throws IOException
{
out.writeInt( nbAtavs );
- Unicode.writeUTF(out, upName);
+ out.writeUTF( upName );
if ( upName.equals( normName ) )
{
- Unicode.writeUTF(out, "");
+ out.writeUTF( "" );
}
else
{
- Unicode.writeUTF(out, normName);
+ out.writeUTF( normName );
}
switch ( nbAtavs )
@@ -1502,13 +1502,13 @@ public final class Rdn implements Clonea
break;
case 1:
- out.writeObject( atav );
+ atav.writeExternal( out );
break;
default:
- for ( Ava value : atavs )
+ for ( Ava ava : atavs )
{
- out.writeObject( value );
+ ava.writeExternal( out );
}
break;
@@ -1532,10 +1532,10 @@ public final class Rdn implements Clonea
nbAtavs = in.readInt();
// Read the UPName
- upName = Unicode.readUTF(in);
+ upName = in.readUTF();
// Read the normName
- normName = Unicode.readUTF(in);
+ normName = in.readUTF();
if ( Strings.isEmpty(normName) )
{
@@ -1549,7 +1549,8 @@ public final class Rdn implements Clonea
break;
case 1:
- atav = (Ava) in.readObject();
+ atav = new Ava( schemaManager );
+ atav.readExternal( in );
atavType = atav.getNormType();
break;
@@ -1561,9 +1562,10 @@ public final class Rdn implements Clonea
for ( int i = 0; i < nbAtavs; i++ )
{
- Ava value = (Ava) in.readObject();
- atavs.add( value );
- atavTypes.put( value.getNormType(), value );
+ Ava ava = new Ava( schemaManager );
+ ava.readExternal( in );
+ atavs.add( ava );
+ atavTypes.put( ava.getNormType(), ava );
}
atav = null;
Modified: directory/shared/trunk/ldap/model/src/main/java/org/apache/directory/shared/ldap/model/name/RdnSerializer.java
URL: http://svn.apache.org/viewvc/directory/shared/trunk/ldap/model/src/main/java/org/apache/directory/shared/ldap/model/name/RdnSerializer.java?rev=1078612&r1=1078611&r2=1078612&view=diff
==============================================================================
--- directory/shared/trunk/ldap/model/src/main/java/org/apache/directory/shared/ldap/model/name/RdnSerializer.java (original)
+++ directory/shared/trunk/ldap/model/src/main/java/org/apache/directory/shared/ldap/model/name/RdnSerializer.java Sun Mar 6 22:37:00 2011
@@ -25,8 +25,6 @@ import java.io.ObjectOutput;
import org.apache.directory.shared.ldap.model.exception.LdapInvalidDnException;
import org.apache.directory.shared.ldap.model.schema.SchemaManager;
-import org.apache.directory.shared.util.Strings;
-import org.apache.directory.shared.util.Unicode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -71,23 +69,8 @@ public final class RdnSerializer
*/
public static void serialize( Rdn rdn, ObjectOutput out ) throws IOException
{
- out.writeInt( rdn.getNbAtavs() );
- Unicode.writeUTF(out, rdn.getName());
- Unicode.writeUTF(out, rdn.getNormName());
-
- switch ( rdn.getNbAtavs() )
- {
- case 0 :
- break;
-
- default :
- for ( Ava atav:rdn )
- {
- AvaSerializer.serialize(atav, out);
- }
-
- break;
- }
+ rdn.writeExternal( out );
+ out.flush();
}
@@ -106,45 +89,15 @@ public final class RdnSerializer
public static Rdn deserialize( SchemaManager schemaManager, ObjectInput in )
throws IOException, LdapInvalidDnException
{
- // Read the ATAV number
- int nbAtavs = in.readInt();
-
- // Read the UPName
- String upName = Unicode.readUTF(in);
-
- // Read the normName
- String normName = Unicode.readUTF(in);
-
- if ( Strings.isEmpty(normName) )
+ Rdn rdn = new Rdn( schemaManager );
+
+ try
{
- normName = upName;
+ rdn.readExternal( in );
}
-
- Rdn rdn = null;
-
- // Read through the Atavs
- switch ( nbAtavs )
+ catch ( ClassNotFoundException cnfe )
{
- case 0 :
- rdn = new Rdn( schemaManager, upName, normName, (Ava[])null );
- break;
-
- case 1 :
- Ava ava = AvaSerializer.deserialize( schemaManager, in );
-
- rdn = new Rdn( schemaManager, upName, normName, ava );
- break;
-
- default :
- Ava[] avas = new Ava[nbAtavs];
-
- for ( int i = 0; i < nbAtavs; i++ )
- {
- ava = AvaSerializer.deserialize( schemaManager, in );
- avas[i] = ava;
- }
-
- rdn = new Rdn( schemaManager, upName, normName, avas );
+ throw new IOException( cnfe.getMessage() );
}
return rdn;