| 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 | * An {@link Exception} that is raised when you try to access functionality |
| 29 | * that has not been compiled into the code. |
| 30 | * |
| 31 | * @author niki |
| 32 | * |
| 33 | */ |
| 34 | public class NotSupportedException extends Exception { |
| 35 | private static final long serialVersionUID = 1L; |
| 36 | |
| 37 | private boolean notCompiled; |
| 38 | |
| 39 | /** |
| 40 | * Create a new {@link NotSupportedException}. |
| 41 | * |
| 42 | * @param notSupportedOption |
| 43 | * the option that is not supported |
| 44 | * @param notCompiled |
| 45 | * FALSE when the operation is compiled in but not compatible |
| 46 | * for internal reasons |
| 47 | */ |
| 48 | public NotSupportedException(Exception e, String notSupportedOption, |
| 49 | boolean notCompiled) { |
| 50 | super((notCompiled ? "Option not supported: " |
| 51 | : "Internal error when trying to use: ") |
| 52 | + notSupportedOption, e); |
| 53 | |
| 54 | this.notCompiled = notCompiled; |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Check if the support is supposed to be compiled in the sources. |
| 59 | * |
| 60 | * @return TRUE if it should have worked (hence, if an internal error |
| 61 | * occurred) |
| 62 | */ |
| 63 | public boolean isCompiledIn() { |
| 64 | return !notCompiled; |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Create a new jVCard server on the given port, then run it. |
| 70 | * |
| 71 | * @param port |
| 72 | * the port to run on |
| 73 | * |
| 74 | * @throws NotSupportedException |
| 75 | * in case the option is not supported |
| 76 | * @throws IOException |
| 77 | * in case of IO error |
| 78 | */ |
| 79 | @SuppressWarnings("unchecked") |
| 80 | static public void runServer(int port) throws IOException, |
| 81 | NotSupportedException { |
| 82 | try { |
| 83 | @SuppressWarnings("rawtypes") |
| 84 | Class serverClass = Class |
| 85 | .forName("be.nikiroo.jvcard.remote.Server"); |
| 86 | Method run = serverClass |
| 87 | .getDeclaredMethod("run", new Class<?>[] {}); |
| 88 | run.invoke(serverClass.getConstructor(int.class).newInstance(port)); |
| 89 | } catch (NoSuchMethodException e) { |
| 90 | throw new Optional().new NotSupportedException(e, "remoting", true); |
| 91 | } catch (ClassNotFoundException e) { |
| 92 | throw new Optional().new NotSupportedException(e, "remoting", false); |
| 93 | } catch (SecurityException e) { |
| 94 | throw new Optional().new NotSupportedException(e, "remoting", false); |
| 95 | } catch (InstantiationException e) { |
| 96 | throw new Optional().new NotSupportedException(e, "remoting", false); |
| 97 | } catch (IllegalAccessException e) { |
| 98 | throw new Optional().new NotSupportedException(e, "remoting", false); |
| 99 | } catch (IllegalArgumentException e) { |
| 100 | throw new Optional().new NotSupportedException(e, "remoting", false); |
| 101 | } catch (InvocationTargetException e) { |
| 102 | throw new Optional().new NotSupportedException(e, "remoting", false); |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * Start the TUI program. |
| 108 | * |
| 109 | * @param textMode |
| 110 | * TRUE to force text mode, FALSE to force the Swing terminal |
| 111 | * emulator, null to automatically determine the best choice |
| 112 | * @param files |
| 113 | * the files to show at startup |
| 114 | * |
| 115 | * @throws NotSupportedException |
| 116 | * in case the option is not supported |
| 117 | * @throws IOException |
| 118 | * in case of IO error |
| 119 | */ |
| 120 | @SuppressWarnings("unchecked") |
| 121 | static public void startTui(Boolean textMode, List<String> files) |
| 122 | throws IOException, NotSupportedException { |
| 123 | try { |
| 124 | @SuppressWarnings("rawtypes") |
| 125 | Class launcherClass = Class |
| 126 | .forName("be.nikiroo.jvcard.tui.TuiLauncher"); |
| 127 | Method start = launcherClass.getDeclaredMethod("start", |
| 128 | new Class<?>[] { Boolean.class, List.class }); |
| 129 | start.invoke(launcherClass.newInstance(), textMode, files); |
| 130 | } catch (NoSuchMethodException e) { |
| 131 | throw new Optional().new NotSupportedException(e, "TUI", true); |
| 132 | } catch (ClassNotFoundException e) { |
| 133 | throw new Optional().new NotSupportedException(e, "TUI", false); |
| 134 | } catch (SecurityException e) { |
| 135 | throw new Optional().new NotSupportedException(e, "TUI", false); |
| 136 | } catch (InstantiationException e) { |
| 137 | throw new Optional().new NotSupportedException(e, "TUI", false); |
| 138 | } catch (IllegalAccessException e) { |
| 139 | throw new Optional().new NotSupportedException(e, "TUI", false); |
| 140 | } catch (IllegalArgumentException e) { |
| 141 | throw new Optional().new NotSupportedException(e, "TUI", false); |
| 142 | } catch (InvocationTargetException e) { |
| 143 | throw new Optional().new NotSupportedException(e, "TUI", false); |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | /** |
| 148 | * Return the {@link Card} corresponding to the given URL, synchronised if |
| 149 | * necessary. |
| 150 | * |
| 151 | * @param input |
| 152 | * the jvcard:// with resource name URL (e.g.: |
| 153 | * <tt>jvcard://localhost:4444/coworkers</tt>) |
| 154 | * @param callback |
| 155 | * the {@link MergeCallback} to call in case of conflict, or NULL |
| 156 | * to disallow conflict management (the {@link Card} will not be |
| 157 | * allowed to synchronise in case of conflicts) |
| 158 | * |
| 159 | * @throws NotSupportedException |
| 160 | * in case the option is not supported |
| 161 | * @throws IOException |
| 162 | * in case of IO error |
| 163 | */ |
| 164 | @SuppressWarnings("unchecked") |
| 165 | static public CardResult syncCard(String input, MergeCallback callback) |
| 166 | throws IOException, NotSupportedException { |
| 167 | try { |
| 168 | @SuppressWarnings("rawtypes") |
| 169 | Class syncClass = Class.forName("be.nikiroo.jvcard.remote.Sync"); |
| 170 | Method sync = syncClass.getDeclaredMethod("sync", new Class<?>[] { |
| 171 | boolean.class, MergeCallback.class }); |
| 172 | |
| 173 | Object o = syncClass.getConstructor(String.class) |
| 174 | .newInstance(input); |
| 175 | CardResult card = (CardResult) sync.invoke(o, false, callback); |
| 176 | |
| 177 | return card; |
| 178 | } catch (NoSuchMethodException e) { |
| 179 | throw new Optional().new NotSupportedException(e, "remoting", true); |
| 180 | } catch (ClassNotFoundException e) { |
| 181 | throw new Optional().new NotSupportedException(e, "remoting", false); |
| 182 | } catch (SecurityException e) { |
| 183 | throw new Optional().new NotSupportedException(e, "remoting", false); |
| 184 | } catch (InstantiationException e) { |
| 185 | throw new Optional().new NotSupportedException(e, "remoting", false); |
| 186 | } catch (IllegalAccessException e) { |
| 187 | throw new Optional().new NotSupportedException(e, "remoting", false); |
| 188 | } catch (IllegalArgumentException e) { |
| 189 | throw new Optional().new NotSupportedException(e, "remoting", false); |
| 190 | } catch (InvocationTargetException e) { |
| 191 | throw new Optional().new NotSupportedException(e, "remoting", false); |
| 192 | } |
| 193 | } |
| 194 | } |