public static <T extends Streamable> T readGenericStreamable(DataInput in, ClassLoader loader) throws Exception { T retval=null; int b=in.readByte(); if(b == 0) return null; short magic_number=in.readShort(); Class<?> clazz; if(magic_number != -1) { retval=ClassConfigurator.create(magic_number); } else { String classname=in.readUTF(); clazz=ClassConfigurator.get(classname, loader); retval=(T)clazz.newInstance(); } retval.readFrom(in); return retval; }
protected static void writeAuthToken(DataOutput out, AuthToken tok) throws Exception { out.writeByte(tok == null? 0 : 1); if(tok == null) return; short id=ClassConfigurator.getMagicNumber(tok.getClass()); out.writeShort(id); if(id < 0) { String classname=tok.getClass().getName(); out.writeUTF(classname); } tok.writeTo(out); }
List<Triple<Short,String,Boolean>> mapping=readMappings(magic_number_file); for(Triple<Short,String,Boolean> tuple: mapping) { short m=tuple.getVal1(); if(external) { if(magicMap[m] != null) alreadyInMagicMap(m, tuple.getVal2()); continue; alreadyInMagicMap(m, clazz.getName()); checkSameId((Header)inst, m); classMap.put(clazz, m); mapping=readMappings(protocol_id_file); // Read jg-protocol-ids.xml for(Triple<Short,String,Boolean> tuple: mapping) { short m=tuple.getVal1(); if(external) { if(protocol_names.containsKey(m)) alreadyInProtocolsMap(m, tuple.getVal2()); continue; alreadyInProtocolsMap(m, clazz.getName()); protocol_ids.put(clazz, m); protocol_names.put(m, clazz);
throw new IllegalArgumentException("magic ID (" + magic + ") must be >= " + MIN_CUSTOM_MAGIC_NUMBER); if(magicMapUser.containsKey(magic) || classMap.containsKey(clazz)) alreadyInMagicMap(magic, clazz.getName()); checkSameId((Header)inst, magic); val=((Header)inst).create();
fork.enableStats(this.configuration.isStatisticsEnabled()); fork.setUnknownForkHandler(new UnknownForkHandler() { private final short id = ClassConfigurator.getProtocolId(RequestCorrelator.class);
if(use_magic_number) { short magic_number=in.readShort(); clazz=ClassConfigurator.getInstance(false).get(magic_number); if (clazz==null) { throw new ClassNotFoundException("Class for magic number "+magic_number+" cannot be found."); clazz=ClassConfigurator.getInstance(false).get(classname); if (clazz==null) { throw new ClassNotFoundException(classname);
@Override public void connect() { if (isConnected) return; synchronized (ClassConfigurator.class) { if (ClassConfigurator.get(ClusterBufferHeader.ID) == null) ClassConfigurator.add(ClusterBufferHeader.ID, ClusterBufferHeader.class); } try { ((JChannelReceiver) channel.getReceiver()).addReceiver(msg -> { if (msg.getHeader(ClusterBufferHeader.ID) != null) { if (!isLocked) { receiveBuffer.writeBytes(msg.getBuffer()); messageListener.accept(serializer.deserializeCommands(receiveBuffer)); receiveBuffer.clear(); } }}); ((JChannelReceiver) channel.getReceiver()).addViewAcceptor(this::rebuildAddresses); } catch (Exception e) { throw Exceptions.runtime(e); } finally { isConnected = true; } }
@SuppressWarnings("unchecked") @Override public void readFrom(DataInput in) throws Exception { try { String classname=in.readUTF(); _classCallable=ClassConfigurator.get(classname); } catch (ClassNotFoundException e) { throw new IOException("failed to read class from classname", e); } _constructorNumber = in.readByte(); short numberOfArgs = in.readByte(); _args = new Object[numberOfArgs]; for (int i = 0; i < numberOfArgs; ++i) { try { _args[i] = Util.readObject(in); } catch (Exception e) { throw new IOException("failed to read arg", e); } } }
private static void writeOtherAddress(Address addr, DataOutputStream out) throws IOException { ClassConfigurator conf=null; try { conf=ClassConfigurator.getInstance(false); } catch(ChannelException e) { IOException new_ex=new IOException(); new_ex.initCause(e); throw new_ex; } short magic_number=conf != null? conf.getMagicNumber(addr.getClass()) : -1; // write the class info if(magic_number == -1) { out.write(0); out.writeUTF(addr.getClass().getName()); } else { out.write(1); out.writeShort(magic_number); } // write the data itself addr.writeTo(out); }
private static Address readOtherAddress(DataInput in) throws Exception { short magic_number=in.readShort(); Address addr=ClassConfigurator.create(magic_number); addr.readFrom(in); return addr; }
public static void addProtocol(short id, Class protocol) { if(id < MIN_CUSTOM_PROTOCOL_ID) throw new IllegalArgumentException("protocol ID (" + id + ") needs to be greater than or equal to " + MIN_CUSTOM_PROTOCOL_ID); if(protocol_ids.containsKey(protocol)) alreadyInProtocolsMap(id, protocol.getName()); protocol_ids.put(protocol, id); protocol_names.putIfAbsent(id, protocol); }
public static String printHeaders(final Header[] hdrs) { if(hdrs == null) return ""; StringBuilder sb=new StringBuilder(); boolean first=true; for(Header hdr: hdrs) { if(hdr == null) break; short id=hdr.getProtId(); if(first) first=false; else sb.append(", "); Class clazz=ClassConfigurator.getProtocol(id); String name=clazz != null? clazz.getSimpleName() : Short.toString(id); sb.append(name).append(": ").append(hdr); } return sb.toString(); }
public void init() throws Exception { this.id=ClassConfigurator.getProtocolId(TP.class);
private static Address readOtherAddress(DataInputStream in) throws IOException, IllegalAccessException, InstantiationException { ClassConfigurator conf; try { conf=ClassConfigurator.getInstance(false); } catch(ChannelException e) { IllegalAccessException new_ex=new IllegalAccessException(); new_ex.initCause(e); throw new_ex; } int b=in.read(); short magic_number; String classname; Class cl=null; Address addr; if(b == 1) { magic_number=in.readShort(); cl=conf.get(magic_number); } else { classname=in.readUTF(); cl=conf.get(classname); } addr=(Address)cl.newInstance(); addr.readFrom(in); return addr; }
@SuppressWarnings("unchecked") @Override public void readFrom(DataInput in) throws Exception { try { String classname=in.readUTF(); _classCallable=ClassConfigurator.get(classname); } catch (ClassNotFoundException e) { throw new IOException("failed to read class from classname", e); } _constructorNumber = in.readByte(); short numberOfArgs = in.readByte(); _args = new Object[numberOfArgs]; for (int i = 0; i < numberOfArgs; ++i) { try { _args[i] = Util.readObject(in); } catch (Exception e) { throw new IOException("failed to read arg", e); } } }
public static void writeGenericStreamable(Streamable obj, DataOutputStream out) throws IOException { short magic_number; String classname; if(obj == null) { out.write(0); return; } try { out.write(1); magic_number=ClassConfigurator.getInstance(false).getMagicNumber(obj.getClass()); // write the magic number or the class name if(magic_number == -1) { out.writeBoolean(false); classname=obj.getClass().getName(); out.writeUTF(classname); } else { out.writeBoolean(true); out.writeShort(magic_number); } // write the contents obj.writeTo(out); } catch(ChannelException e) { throw new IOException("failed writing object of type " + obj.getClass() + " to stream: " + e.toString()); } }
throw new IllegalArgumentException("magic ID (" + magic + ") must be >= " + MIN_CUSTOM_MAGIC_NUMBER); if(magicMapUser.containsKey(magic) || classMap.containsKey(clazz)) alreadyInMagicMap(magic, clazz.getName()); checkSameId((Header)inst, magic); val=((Header)inst).create();
protected static Header readHeader(DataInput in) throws Exception { short magic_number=in.readShort(); Header hdr=ClassConfigurator.create(magic_number); hdr.readFrom(in); return hdr; }
public static void addProtocol(short id, Class protocol) { if(id < MIN_CUSTOM_PROTOCOL_ID) throw new IllegalArgumentException("protocol ID (" + id + ") needs to be greater than or equal to " + MIN_CUSTOM_PROTOCOL_ID); if(protocol_ids.containsKey(protocol)) alreadyInProtocolsMap(id, protocol.getName()); protocol_ids.put(protocol, id); protocol_names.putIfAbsent(id, protocol); }