001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, 013 * software distributed under the License is distributed on an 014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 * KIND, either express or implied. See the License for the 016 * specific language governing permissions and limitations 017 * under the License. 018 * 019 */ 020package org.apache.directory.server.core.partition.impl.btree.jdbm; 021 022 023import java.io.ByteArrayInputStream; 024import java.io.ByteArrayOutputStream; 025import java.io.IOException; 026import java.io.ObjectInputStream; 027import java.io.ObjectOutput; 028import java.io.ObjectOutputStream; 029 030import jdbm.helper.Serializer; 031 032import org.apache.directory.api.ldap.model.name.Dn; 033import org.apache.directory.api.ldap.model.schema.SchemaManager; 034import org.apache.directory.server.i18n.I18n; 035import org.slf4j.Logger; 036import org.slf4j.LoggerFactory; 037 038 039/** 040 * Serialize and deserialize a Dn. 041 * <br><br> 042 * <b>This class must *not* be used outside of the server.</b> 043 * 044 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a> 045 */ 046public class DnSerializer implements Serializer 047{ 048 /** The serialVersionUID */ 049 private static final long serialVersionUID = 1L; 050 051 /** the logger for this class */ 052 private static final Logger LOG = LoggerFactory.getLogger( DnSerializer.class ); 053 054 /** 055 * Speedup for logs 056 */ 057 private static final boolean IS_DEBUG = LOG.isDebugEnabled(); 058 059 /** The schemaManager reference */ 060 private transient SchemaManager schemaManager; 061 062 063 /** 064 * Creates a new instance of DnSerializer. 065 * 066 * @param schemaManager The reference to the global schemaManager 067 */ 068 public DnSerializer( SchemaManager schemaManager ) 069 { 070 this.schemaManager = schemaManager; 071 } 072 073 074 /** 075 * This is the place where we serialize Dn 076 * 077 * @param object The element to serialize 078 * @return a byte[] containing the serialized element 079 * @throws IOException If teh serialization failed 080 */ 081 public byte[] serialize( Object object ) throws IOException 082 { 083 Dn dn = ( Dn ) object; 084 085 try ( ByteArrayOutputStream baos = new ByteArrayOutputStream(); 086 ObjectOutput out = new ObjectOutputStream( baos ) ) 087 { 088 089 // First, the Dn 090 dn.writeExternal( out ); 091 092 out.flush(); 093 094 if ( IS_DEBUG ) 095 { 096 LOG.debug( ">------------------------------------------------" ); 097 LOG.debug( "Serialized {}", dn ); 098 } 099 100 return baos.toByteArray(); 101 } 102 } 103 104 105 /** 106 * Deserialize a Dn. 107 * 108 * @param bytes the byte array containing the serialized Dn 109 * @return An instance of a Dn object 110 * @throws IOException if we can't deserialize the Dn 111 */ 112 public Object deserialize( byte[] bytes ) throws IOException 113 { 114 ObjectInputStream in = new ObjectInputStream( new ByteArrayInputStream( bytes ) ); 115 116 try 117 { 118 Dn dn = new Dn(); 119 120 dn.readExternal( in ); 121 122 return dn; 123 } 124 catch ( ClassNotFoundException cnfe ) 125 { 126 LOG.error( I18n.err( I18n.ERR_134, cnfe.getLocalizedMessage() ) ); 127 throw new IOException( cnfe.getLocalizedMessage() ); 128 } 129 } 130}