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