| 1 | package be.nikiroo.jvcard.launcher; |
| 2 | |
| 3 | import java.io.IOException; |
| 4 | import java.lang.reflect.InvocationTargetException; |
| 5 | import java.lang.reflect.Method; |
| 6 | import java.util.List; |
| 7 | |
| 8 | import be.nikiroo.jvcard.Card; |
| 9 | import be.nikiroo.jvcard.launcher.CardResult.MergeCallback; |
| 10 | |
| 11 | /** |
| 12 | * This class let you call "optional" methods, that is, methods and classes that |
| 13 | * may or may not be present. |
| 14 | * |
| 15 | * <p> |
| 16 | * It currently offers services for: |
| 17 | * <ul> |
| 18 | * <li>remoting support</li> |
| 19 | * <li>TUI support</li> |
| 20 | * </ul> |
| 21 | * </p> |
| 22 | * |
| 23 | * @author niki |
| 24 | * |
| 25 | */ |
| 26 | class Optional { |
| 27 | /** |
| 28 | * Create a new jVCard server on the given port, then run it. |
| 29 | * |
| 30 | * @param port |
| 31 | * the port to run on |
| 32 | * |
| 33 | * @throws SecurityException |
| 34 | * in case of internal error |
| 35 | * @throws NoSuchMethodException |
| 36 | * in case of internal error |
| 37 | * @throws ClassNotFoundException |
| 38 | * in case of internal error |
| 39 | * @throws IllegalAccessException |
| 40 | * in case of internal error |
| 41 | * @throws InstantiationException |
| 42 | * in case of internal error |
| 43 | * @throws InvocationTargetException |
| 44 | * in case of internal error |
| 45 | * @throws IllegalArgumentException |
| 46 | * in case of internal error |
| 47 | * @throws IOException |
| 48 | * in case of IO error |
| 49 | */ |
| 50 | @SuppressWarnings("unchecked") |
| 51 | static public void runServer(int port) throws NoSuchMethodException, |
| 52 | SecurityException, ClassNotFoundException, InstantiationException, |
| 53 | IllegalAccessException, IllegalArgumentException, |
| 54 | InvocationTargetException { |
| 55 | @SuppressWarnings("rawtypes") |
| 56 | Class serverClass = Class.forName("be.nikiroo.jvcard.remote.Server"); |
| 57 | Method run = serverClass.getDeclaredMethod("run", new Class<?>[] {}); |
| 58 | run.invoke(serverClass.getConstructor(int.class).newInstance(port)); |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Start the TUI program. |
| 63 | * |
| 64 | * @param textMode |
| 65 | * TRUE to force text mode, FALSE to force the Swing terminal |
| 66 | * emulator, null to automatically determine the best choice |
| 67 | * @param files |
| 68 | * the files to show at startup |
| 69 | * |
| 70 | * @throws SecurityException |
| 71 | * in case of internal error |
| 72 | * @throws NoSuchMethodException |
| 73 | * in case of internal error |
| 74 | * @throws ClassNotFoundException |
| 75 | * in case of internal error |
| 76 | * @throws IllegalAccessException |
| 77 | * in case of internal error |
| 78 | * @throws InstantiationException |
| 79 | * in case of internal error |
| 80 | * @throws InvocationTargetException |
| 81 | * in case of internal error |
| 82 | * @throws IllegalArgumentException |
| 83 | * in case of internal error |
| 84 | * @throws IOException |
| 85 | * in case of IO error |
| 86 | */ |
| 87 | @SuppressWarnings("unchecked") |
| 88 | static public void startTui(Boolean textMode, List<String> files) |
| 89 | throws NoSuchMethodException, SecurityException, |
| 90 | ClassNotFoundException, InstantiationException, |
| 91 | IllegalAccessException, IllegalArgumentException, |
| 92 | InvocationTargetException { |
| 93 | @SuppressWarnings("rawtypes") |
| 94 | Class launcherClass = Class |
| 95 | .forName("be.nikiroo.jvcard.tui.TuiLauncher"); |
| 96 | Method start = launcherClass.getDeclaredMethod("start", new Class<?>[] { |
| 97 | Boolean.class, List.class }); |
| 98 | start.invoke(launcherClass.newInstance(), textMode, files); |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Return the {@link Card} corresponding to the given URL, synchronised if |
| 103 | * necessary. |
| 104 | * |
| 105 | * @param input |
| 106 | * the jvcard:// with resource name URL (e.g.: |
| 107 | * <tt>jvcard://localhost:4444/coworkers</tt>) |
| 108 | * @param callback |
| 109 | * the {@link MergeCallback} to call in case of conflict, or NULL |
| 110 | * to disallow conflict management (the {@link Card} will not be |
| 111 | * allowed to synchronise in case of conflicts) |
| 112 | * |
| 113 | * @throws SecurityException |
| 114 | * in case of internal error |
| 115 | * @throws NoSuchMethodException |
| 116 | * in case of internal error |
| 117 | * @throws ClassNotFoundException |
| 118 | * in case of internal error |
| 119 | * @throws IllegalAccessException |
| 120 | * in case of internal error |
| 121 | * @throws InstantiationException |
| 122 | * in case of internal error |
| 123 | * @throws InvocationTargetException |
| 124 | * in case of internal error |
| 125 | * @throws IllegalArgumentException |
| 126 | * in case of internal error |
| 127 | * @throws IOException |
| 128 | * in case of IO error |
| 129 | */ |
| 130 | @SuppressWarnings("unchecked") |
| 131 | static public CardResult syncCard(String input, MergeCallback callback) |
| 132 | throws ClassNotFoundException, NoSuchMethodException, |
| 133 | SecurityException, InstantiationException, IllegalAccessException, |
| 134 | IllegalArgumentException, InvocationTargetException, IOException { |
| 135 | @SuppressWarnings("rawtypes") |
| 136 | Class syncClass = Class.forName("be.nikiroo.jvcard.remote.Sync"); |
| 137 | Method sync = syncClass.getDeclaredMethod("sync", new Class<?>[] { |
| 138 | boolean.class, MergeCallback.class }); |
| 139 | |
| 140 | Object o = syncClass.getConstructor(String.class).newInstance(input); |
| 141 | CardResult card = (CardResult) sync.invoke(o, false, callback); |
| 142 | |
| 143 | return card; |
| 144 | } |
| 145 | } |