Skip to content

Commit

Permalink
- JDBC_PING.serialize()/deserialize() methods use now Util.XXX() meth…
Browse files Browse the repository at this point in the history
…ods to marshal, and pull them up into Discovery
  • Loading branch information
belaban committed May 27, 2011
1 parent 62c0f90 commit d796370
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 49 deletions.
26 changes: 26 additions & 0 deletions src/org/jgroups/protocols/Discovery.java
Expand Up @@ -499,6 +499,32 @@ protected final View makeView(Vector<Address> mbrs) {
return new View(new ViewId(local_addr), mbrs);
}

/**
* Creates a byte[] representation of the PingData, but DISCARDING the view it contains.
* @param data the PingData instance to serialize.
* @return
*/
protected byte[] serializeWithoutView(PingData data) {
final PingData clone = new PingData(data.getAddress(), null, data.isServer(), data.getLogicalName(), data.getPhysicalAddrs());
try {
return Util.streamableToByteBuffer(clone);
}
catch(Exception e) {
log.error("Error", e);
return null;
}
}

protected PingData deserialize(final byte[] data) {
try {
return (PingData)Util.streamableFromByteBuffer(PingData.class, data);
}
catch(Exception e) {
log.error("Error", e);
return null;
}
}


private void sendDiscoveryResponse(Address logical_addr, List<PhysicalAddress> physical_addrs,
boolean is_server, String logical_name, Address sender) {
Expand Down
54 changes: 5 additions & 49 deletions src/org/jgroups/protocols/JDBC_PING.java
Expand Up @@ -2,24 +2,15 @@

import org.jgroups.Address;
import org.jgroups.annotations.Property;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import org.jgroups.util.Util;

import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
import java.sql.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
* <p>Discovery protocol using a JDBC connection to a shared database.
Expand Down Expand Up @@ -303,41 +294,6 @@ protected void deleteSelf() throws SQLException {
delete(group_addr, ownAddress);
}

/**
* Creates a byte[] representation of the PingData, but DISCARDING
* the view it contains.
* @param data the PingData instance to serialize.
* @return
*/
protected byte[] serializeWithoutView(PingData data) {
final PingData clone = new PingData(data.getAddress(), null, data.isServer(), data.getLogicalName(), data.getPhysicalAddrs());
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream( 512 );
DataOutputStream outputStream = new DataOutputStream(byteArrayOutputStream);
try {
clone.writeTo(outputStream);
} catch (IOException e) {
//not expecting this to happen as it's an in-memory stream
log.error("Error", e);
}
return byteArrayOutputStream.toByteArray();
}

protected PingData deserialize(final byte[] data) {
final PingData pingData = new PingData();
final ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(data);
final DataInputStream outputStream = new DataInputStream(byteArrayInputStream);
try {
pingData.readFrom(outputStream);
} catch (IllegalAccessException e) {
log.error("Error", e);
} catch (InstantiationException e) {
log.error("Error", e);
} catch (IOException e) {
// not expecting this to happen as it's an in-memory stream
log.error("Error", e);
}
return pingData;
}

protected void closeConnection(final Connection connection) {
try {
Expand Down

0 comments on commit d796370

Please sign in to comment.