/** * Unregister previous receiver for an interface, to allow to register an other one and/or stop * properly linked threads<br> * Do nothing if no receiver is rgister for the interface * * @param <TYPE> Interface type * @param interfaceIDL Interface class */ @SuppressWarnings("unchecked") public static <TYPE> void stopReceiver(final Class<TYPE> interfaceIDL) { final String className = interfaceIDL.getName() + JHelpIDL.TARGET; final TYPE receiver = (TYPE) JHelpIDL.INSTANCES.get(className); if (receiver == null) { return; } ((MessageHandler<?>) receiver).terminate(); JHelpIDL.INSTANCES.remove(className); }
/** * Obtain the sender instance for call distant methods for an interface * * @param <TYPE> Interface type * @param interfaceIDL Interface class * @return Sender instance for call distant methods for an interface * @throws ClassNotFoundException If the interface is not registered */ @SuppressWarnings("unchecked") public static <TYPE> TYPE obtainSenderInstance(final Class<TYPE> interfaceIDL) throws ClassNotFoundException { final String className = interfaceIDL.getName() + JHelpIDL.SOURCE; TYPE sender = (TYPE) JHelpIDL.INSTANCES.get(className); if (sender == null) { sender = (TYPE) Reflector.newInstance(className, JHelpIDL.CLASS_LOADER); JHelpIDL.INSTANCES.put(className, sender); } return sender; }
/** * Link a receiver to an interface.<br> * It a way to say what to answer when receiver message.<br> * For one interface, only one receiver is allowed, have to call {@link #stopReceiver(Class)} for * be able change the registered object * * @param <TYPE> Interface type * @param interfaceIDLImplementation Given implementation * @param interfaceIDL Interface class * @throws ClassNotFoundException If interface not registered * @throws NoSuchMethodException If registeration had previously issue * @throws SecurityException If the interface targetted is protected by security form refelection * @throws InstantiationException If registeration had previously issue * @throws IllegalAccessException If the interface targetted is protected by security form * refelection * @throws IllegalArgumentException If the implementation given is wrong type * @throws InvocationTargetException If the interface targetted is protected by security form * refelection or registeration had previously issue */ @SuppressWarnings("unchecked") public static <TYPE> void registerReceiver( final TYPE interfaceIDLImplementation, final Class<TYPE> interfaceIDL) throws ClassNotFoundException, NoSuchMethodException, SecurityException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException { if (interfaceIDLImplementation == null) { throw new NullPointerException("interfaceIDLImplementation musn't be null"); } final String className = interfaceIDL.getName() + JHelpIDL.TARGET; TYPE receiver = (TYPE) JHelpIDL.INSTANCES.get(className); if (receiver != null) { throw new IllegalStateException("A receiver is already registered !"); } final Class<?> claz = JHelpIDL.CLASS_LOADER.loadClass(className); final Constructor<?> constructor = claz.getConstructor(interfaceIDL); receiver = (TYPE) constructor.newInstance(interfaceIDLImplementation); JHelpIDL.INSTANCES.put(className, receiver); }
/** * Indicates if a receiver is already registered for a given interface * * @param <TYPE> Interface type * @param interfaceIDL Interface class * @return {@code true} if a receiver is already registered for a given interface */ public static <TYPE> boolean hasReceiver(final Class<TYPE> interfaceIDL) { final String className = interfaceIDL.getName() + JHelpIDL.TARGET; return JHelpIDL.INSTANCES.get(className) != null; }