com.alachisoft.ncache.serialization.standard.SurrogateHelper Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of nc-serialization Show documentation
Show all versions of nc-serialization Show documentation
Internal package of Alachisoft.
package com.alachisoft.ncache.serialization.standard;
import com.alachisoft.ncache.serialization.core.io.TypeSurrogateSelector;
import com.alachisoft.ncache.serialization.core.io.surrogates.SerializationSurrogate;
import javassist.ClassPool;
import javassist.CtClass;
import javassist.CtMethod;
import javassist.NotFoundException;
import java.lang.reflect.Constructor;
import java.util.HashMap;
/**
* @author Basit Anwer
*/
public class SurrogateHelper {
public static Object CreateGenericTypeInstance(TypeSurrogateSelector mSurrogateTypeSelector, Class genericType, HashMap attributeOrder, String cacheContext, boolean portable, short subHandle, HashMap nonCompactFieldsMap) throws Exception {
// FileWriter file = new FileWriter("C:\\DEBUG.txt", true);
//
// BufferedWriter dos = new BufferedWriter(file);
//
//
// dos.write(genericType.toString() + "\n");
// dos.write("*********************" + "\n");
if (genericType == null) {
throw new NullPointerException("genericType");
}
try {
ClassPool pool = ClassPool.getDefault();
CtClass dynamicSurrugate = null;
Class classLoader = null;
try {
classLoader = Class.forName("com.alachisoft.ncache.serialization.standard.DynamicSurrogate" + genericType.getName().toLowerCase() + cacheContext);
} catch (ClassNotFoundException classNotFoundException) {
}
int i = 0;
while (classLoader == null) {
try {
if (classLoader == null) {
dynamicSurrugate = pool.get("com.alachisoft.ncache.serialization.standard.DynamicSurrogate");
dynamicSurrugate.defrost();
try {
dynamicSurrugate = pool.get("com.alachisoft.ncache.serialization.standard.DynamicSurrogate" + genericType.getName().toLowerCase() + cacheContext);
dynamicSurrugate.defrost();
} catch (NotFoundException notFoundException) {
}
dynamicSurrugate.replaceClassName("com.alachisoft.ncache.serialization.standard.DynamicSurrogate", "com.alachisoft.ncache.serialization.standard.DynamicSurrogate"
+ genericType.getName().toLowerCase() + cacheContext);
//If Ever Ever Class.forName fails to detect a class already present in jvm and we move on to commit the sin of modifying it
//just to make sure that never happens we deforst it first (Safety first)
//This function is always called once when initializing a cache
dynamicSurrugate.defrost();
CtMethod readObject = dynamicSurrugate.getDeclaredMethod("readObject");
CtMethod writeObject = dynamicSurrugate.getDeclaredMethod("writeObject");
StringBuffer serializationCode = new StringBuffer();
StringBuffer deserializationCode = new StringBuffer();
GenerateDynamicCode.generateCode(genericType, serializationCode, deserializationCode, attributeOrder, portable, subHandle, mSurrogateTypeSelector, cacheContext, nonCompactFieldsMap);
// System.out.println(genericType.toString().toUpperCase());
// System.out.println("***************************************");
// System.out.println(serializationCode);
// System.out.println("---------------------------------------");
// System.out.println(deserializationCode);
// System.out.println("########################################");
writeObject.insertAt(1, serializationCode.toString());
readObject.insertAt(1, deserializationCode.toString());
// Sometimes Exception with message 'null' is thrown
// This exception is generated at random times with random frequencies and at random classes
// Current workaround is that since this class name has been compiled but not build
// we rerun this loop at exception and defrost this specific class and at rerun of this procedure it works fine
// although at second run this anomoly is resolved but for protective measures number of loops are kept to three
// after third we throw exception to user to restart service. (chance of it happening is too low to even consider)
try {
classLoader = dynamicSurrugate.toClass();
} catch (Exception exception) {
// dos.write(Integer.toString(i) + "\n");
// dos.write(exception.toString() + "\n");
// dos.write(exception.toString() + "\n");
// dos.write("---------------------" + "\n");
i++;
if (i == 5) {
throw new Exception("Please restart Service; a null exception has been thrown by javassist while generationg code at runtime: "
+ "'sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)'");
}
classLoader = null;
}
}
} catch (Exception exception) {
// dos.close();
throw exception;
}
// dos.close();
}
SerializationSurrogate h;
Constructor[] ctorlist = null;
try {
ctorlist = classLoader.getDeclaredConstructors();
} catch (SecurityException ex) {
throw new Exception(ex.getMessage());
} catch (Exception exc) {
throw exc;
}
h = (SerializationSurrogate) ctorlist[0].newInstance(genericType);
h.setSubTypeHandle(subHandle);
return h;
} catch (Exception ex) {
//Logger.getLogger("com.alachisoft.ncache.serialization.standard.SurrogateHelper").log(Level.SEVERE, null, ex);
throw ex;
}
}
}