+ /**
+ * Return an object from the server.
+ *
+ * @param <T>
+ * the expected type of object
+ * @param command
+ * the command to send (can contain at most ONE {@link Story})
+ *
+ * @return the object or NULL
+ */
+ private <T> T getRemoteObject(final Object[] command) {
+ return getRemoteObjectOrStory(command, false);
+ }
+
+ /**
+ * Return an object from the server.
+ *
+ * @param command
+ * the command to send (can contain at most ONE {@link Story})
+ *
+ * @return the object or NULL
+ */
+ private Story getRemoteStory(final Object[] command) {
+ return getRemoteObjectOrStory(command, true);
+ }
+
+ /**
+ * Return an object from the server.
+ *
+ * @param <T>
+ * the expected type of object
+ * @param command
+ * the command to send (can contain at most ONE {@link Story})
+ *
+ * @return the object or NULL
+ */
+ @SuppressWarnings("unchecked")
+ private <T> T getRemoteObjectOrStory(final Object[] command,
+ final boolean getStory) {
+ final Object[] result = new Object[1];
+ try {
+ new ConnectActionClientObject(host, port, true) {
+ @Override
+ public void action(Version serverVersion) throws Exception {
+ Story story = null;
+ for (int i = 0; i < command.length; i++) {
+ if (command[i] instanceof Story) {
+ story = (Story) command[i];
+ command[i] = null;
+ }
+ }
+
+ Object rep = send(command);
+
+ if (story != null) {
+ RemoteLibraryServer.sendStory(story, this);
+ }
+
+ if (getStory) {
+ rep = RemoteLibraryServer.recStory(this);
+ }
+
+ result[0] = rep;
+ }
+
+ @Override
+ protected void onError(Exception e) {
+ Instance.getTraceHandler().error(e);
+ }
+ }.connect();
+ } catch (IOException e) {
+ Instance.getTraceHandler().error(e);
+ }
+
+ try {
+ return (T) result[0];
+ } catch (Exception e) {
+ Instance.getTraceHandler().error(e);
+ return null;
+ }