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