/** * Return an array of byte from a given object * @param obj the object from which we must extract the bytes. * @return the byte[] from an object */ public static byte[] getBytesFromObject(Object obj) { return getBytesFromObject(obj, null); }
/** * Return an object from an array of bytes. Useful for BinaryRefAddr * Useful for BinaryRefAddr * @param bytes an array of bytes * @return an object or null if there is an error of if it's empty */ public static Object getObjectFromBytes(byte[] bytes) { return getObjectFromBytes(bytes, null); }
/** * Insert a key/value String/Object couple in the given Reference after serialization of the value. * @param ref Reference to be modified * @param key Address' key * @param value Object value (will be serialized) */ public static void insertInto(final Reference ref, final String key, final Object value) { if (value != null) { RefAddr addr = new BinaryRefAddr(key, getBytesFromObject(value)); ref.add(addr); } // else, print a debug message ? }
/** * Extract an Object from the Reference. Notice that this object have to * be castable to the given Class parameter. The ClassLoader where this * class live is used to deserialize the object. * If it cannot load the requested class, a ClassLoading error will probably occur. * @param ref Reference that holds the serialized version of the requested object * @param key Object's key * @param type Object's type (may be an interface) * @return an Object casted with Class<type> */ public static <T> T extractObjectFrom(final Reference ref, final String key, final Class<T> type) { BinaryRefAddr addr = (BinaryRefAddr) ref.get(key); if (addr == null) { return null; } byte[] serialized = (byte[]) addr.getContent(); Object result = getObjectFromBytes(serialized); return type.cast(result); }
/** * Retrieves the Reference of the object. The Reference contains the factory * used to create this object and the optional parameters used to configure * the factory. * @return the non-null Reference of the object. * @throws NamingException if a naming exception was encountered while * retrieving the reference. */ public Reference getReference() throws NamingException { // Build the reference to the factory FACTORY_TYPE Reference reference = new Reference(FACTORY_TYPE, FACTORY_NAME, null); // Add name reference.add(new StringRefAddr("name", getName())); // Add users byte[] bytes = JNDIUtils.getBytesFromObject(getUsers(), getLogger()); if (bytes != null) { reference.add(new BinaryRefAddr("users", bytes)); } // Add groups bytes = JNDIUtils.getBytesFromObject(groups, getLogger()); if (bytes != null) { reference.add(new BinaryRefAddr("groups", bytes)); } // Add roles bytes = JNDIUtils.getBytesFromObject(roles, getLogger()); if (bytes != null) { reference.add(new BinaryRefAddr("roles", bytes)); } return reference; }