Skip to content

Commit

Permalink
ignite-117 review
Browse files Browse the repository at this point in the history
  • Loading branch information
Yakov Zhdanov committed Jan 28, 2015
1 parent f0db0cc commit c351a7b
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 54 deletions.
Expand Up @@ -8621,7 +8621,7 @@ public static byte[] decodeHex(char[] data) throws IgniteCheckedException {


byte[] out = new byte[len >> 1]; byte[] out = new byte[len >> 1];


// two characters form the hex value. // Two characters form the hex value.
for (int i = 0, j = 0; j < len; i++) { for (int i = 0, j = 0; j < len; i++) {
int f = toDigit(data[j], j) << 4; int f = toDigit(data[j], j) << 4;


Expand Down Expand Up @@ -9098,37 +9098,40 @@ public static <T extends R, R> List<R> arrayList(Iterable<T> c, int cap,
return list; return list;
} }


public static byte[] calculateMD5Digest(InputStream input) throws NoSuchAlgorithmException, IOException { /**
* @param in
* @return
* @throws NoSuchAlgorithmException
* @throws IOException
*/
public static byte[] calculateMD5Digest(InputStream in) throws NoSuchAlgorithmException, IOException {
MessageDigest md = MessageDigest.getInstance("MD5"); MessageDigest md = MessageDigest.getInstance("MD5");
InputStream fis = new BufferedInputStream(input); InputStream fis = new BufferedInputStream(in);
byte[] dataBytes = new byte[1024]; byte[] dataBytes = new byte[1024];


int nread; int nread;
while ((nread = fis.read(dataBytes)) != -1) {
md.update(dataBytes, 0, nread);
}


byte[] md5Bytes = md.digest(); while ((nread = fis.read(dataBytes)) != -1)

md.update(dataBytes, 0, nread);
//convert the byte to hex format
StringBuilder sb = new StringBuilder("");
for (byte md5Byte : md5Bytes) {
sb.append(Integer.toString((md5Byte & 0xff) + 0x100, 16).substring(1));
}


return md5Bytes; return md.digest();
} }


public static String calculateMD5(InputStream input) throws NoSuchAlgorithmException, IOException { /**
byte[] md5Bytes = calculateMD5Digest(input); * @param in Input.
* @return
* @throws NoSuchAlgorithmException
* @throws IOException
*/
public static String calculateMD5(InputStream in) throws NoSuchAlgorithmException, IOException {
byte[] md5Bytes = calculateMD5Digest(in);


//convert the byte to hex format // Convert the byte to hex format.
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
for (byte md5Byte : md5Bytes) {
for (byte md5Byte : md5Bytes)
sb.append(Integer.toString((md5Byte & 0xff) + 0x100, 16).substring(1)); sb.append(Integer.toString((md5Byte & 0xff) + 0x100, 16).substring(1));
}


return sb.toString(); return sb.toString();
} }

} }
Expand Up @@ -17,16 +17,15 @@


package org.apache.ignite.internal.util.ipc.shmem; package org.apache.ignite.internal.util.ipc.shmem;


import org.apache.ignite.IgniteCheckedException; import org.apache.ignite.*;
import org.apache.ignite.internal.GridProductImpl; import org.apache.ignite.internal.*;
import org.apache.ignite.internal.util.typedef.internal.U; import org.apache.ignite.internal.util.typedef.internal.*;


import java.io.*; import java.io.*;
import java.net.URL; import java.net.*;
import java.nio.channels.FileLock; import java.nio.channels.*;
import java.security.NoSuchAlgorithmException; import java.security.*;
import java.util.Collection; import java.util.*;
import java.util.LinkedList;


/** /**
* Shared memory native loader. * Shared memory native loader.
Expand All @@ -39,12 +38,12 @@ public class GridIpcSharedMemoryNativeLoader {
/** Library name base. */ /** Library name base. */
private static final String LIB_NAME_BASE = "ggshmem"; private static final String LIB_NAME_BASE = "ggshmem";


/** Library name. */
private static final String LIB_NAME = LIB_NAME_BASE + "-" + GridProductImpl.VER;

/** Lock file path. */ /** Lock file path. */
private static final File LOCK_FILE = new File(System.getProperty("java.io.tmpdir"), "ggshmem.lock"); private static final File LOCK_FILE = new File(System.getProperty("java.io.tmpdir"), "ggshmem.lock");


/** Library name. */
static final String LIB_NAME = LIB_NAME_BASE + "-" + GridProductImpl.VER;

/** /**
* @return Operating system name to resolve path to library. * @return Operating system name to resolve path to library.
*/ */
Expand All @@ -63,8 +62,6 @@ private static String os() {
return name.replaceAll("\\W+", "_"); return name.replaceAll("\\W+", "_");
} }


static String libFileName(){return LIB_NAME;}

/** /**
* @return Platform. * @return Platform.
*/ */
Expand Down Expand Up @@ -111,7 +108,7 @@ public static void load() throws IgniteCheckedException {
private static void doLoad() throws IgniteCheckedException { private static void doLoad() throws IgniteCheckedException {
assert Thread.holdsLock(GridIpcSharedMemoryNativeLoader.class); assert Thread.holdsLock(GridIpcSharedMemoryNativeLoader.class);


Collection<Throwable> errs = new LinkedList<>(); Collection<Throwable> errs = new ArrayList<>();


try { try {
// Load native library (the library directory should be in java.library.path). // Load native library (the library directory should be in java.library.path).
Expand Down Expand Up @@ -210,7 +207,7 @@ private static boolean extract(Collection<Throwable> errs, URL src, File target)
InputStream is = null; InputStream is = null;


try { try {
if (!target.exists() || ! haveEqualMD5(target, src)) { if (!target.exists() || !haveEqualMD5(target, src)) {
is = src.openStream(); is = src.openStream();


if (is != null) { if (is != null) {
Expand Down Expand Up @@ -244,15 +241,21 @@ private static boolean extract(Collection<Throwable> errs, URL src, File target)
return false; return false;
} }


/**
* @param target
* @param src
* @return
* @throws NoSuchAlgorithmException
* @throws IOException
*/
private static boolean haveEqualMD5(File target, URL src) throws NoSuchAlgorithmException, IOException { private static boolean haveEqualMD5(File target, URL src) throws NoSuchAlgorithmException, IOException {
try (InputStream targetIS = new FileInputStream(target); try (InputStream targetIS = new FileInputStream(target);
InputStream srcIS = src.openStream()){ InputStream srcIS = src.openStream()) {


String targetMD5 = U.calculateMD5(targetIS); String targetMD5 = U.calculateMD5(targetIS);
String srcMD5 = U.calculateMD5(srcIS); String srcMD5 = U.calculateMD5(srcIS);


return targetMD5.equals(srcMD5); return targetMD5.equals(srcMD5);
} }
} }

} }
@@ -1,36 +1,46 @@
package org.apache.ignite.internal.util.ipc.shmem; package org.apache.ignite.internal.util.ipc.shmem;


import junit.framework.TestCase; import junit.framework.*;
import org.apache.ignite.internal.util.GridJavaProcess; import org.apache.ignite.internal.util.*;
import org.apache.ignite.internal.util.typedef.internal.*;


import java.io.BufferedReader; import java.io.*;
import java.io.IOException; import java.util.*;
import java.io.InputStreamReader;
import java.util.Collections;


public class GridIpcSharedMemoryNativeLoaderSelfTest extends TestCase { public class GridIpcSharedMemoryNativeLoaderSelfTest extends TestCase {


public void testLoadWithCorruptedLibFile() throws Exception { public void testLoadWithCorruptedLibFile() throws Exception {
if (System.getProperty("os.name").toLowerCase().trim().startsWith("win")) return; if (U.isWindows())

return;
Process ps = GridJavaProcess.exec(LoadWithCorruptedLibFileTestRunner.class, null, null, null, null, Collections.<String>emptyList(), null).getProcess();
Process ps = GridJavaProcess.exec(
LoadWithCorruptedLibFileTestRunner.class,
null,
null,
null,
null,
Collections.<String>emptyList(),
null
).getProcess();


readStreams(ps); readStreams(ps);

int code = ps.waitFor(); int code = ps.waitFor();


assertEquals("Returned code have to be 0.", 0, code); assertEquals("Returned code have to be 0.", 0, code);
} }


private void readStreams(Process proc) throws IOException { private void readStreams(Process proc) throws IOException {
BufferedReader stdOut = new BufferedReader(new InputStreamReader(proc.getInputStream())); BufferedReader stdOut = new BufferedReader(new InputStreamReader(proc.getInputStream()));

String s; String s;
while ((s = stdOut.readLine()) != null) {
while ((s = stdOut.readLine()) != null)
System.out.println("OUT>>>>>> " + s); System.out.println("OUT>>>>>> " + s);
}


BufferedReader errOut = new BufferedReader(new InputStreamReader(proc.getErrorStream())); BufferedReader errOut = new BufferedReader(new InputStreamReader(proc.getErrorStream()));
while ((s = errOut.readLine()) != null) {
while ((s = errOut.readLine()) != null)
System.out.println("ERR>>>>>> " + s); System.out.println("ERR>>>>>> " + s);
}
} }
} }
Expand Up @@ -6,7 +6,7 @@


public class LoadWithCorruptedLibFileTestRunner { public class LoadWithCorruptedLibFileTestRunner {
public static final String TMP_DIR_FOR_TEST = System.getProperty("user.home"); public static final String TMP_DIR_FOR_TEST = System.getProperty("user.home");
public static final String LOADED_LIB_FILE_NAME = System.mapLibraryName(GridIpcSharedMemoryNativeLoader.libFileName()); public static final String LOADED_LIB_FILE_NAME = System.mapLibraryName(GridIpcSharedMemoryNativeLoader.LIB_NAME);


public static void main(String[] args) throws Exception { public static void main(String[] args) throws Exception {
System.setProperty("java.io.tmpdir", TMP_DIR_FOR_TEST); System.setProperty("java.io.tmpdir", TMP_DIR_FOR_TEST);
Expand All @@ -31,6 +31,4 @@ private static void createCorruptedLibFile() throws IOException {
out.write("Corrupted file information instead of good info.\n".getBytes()); out.write("Corrupted file information instead of good info.\n".getBytes());
} }
} }


} }

0 comments on commit c351a7b

Please sign in to comment.