1 package be
.nikiroo
.jvcard
.launcher
;
3 import java
.io
.IOException
;
4 import java
.lang
.reflect
.InvocationTargetException
;
5 import java
.lang
.reflect
.Method
;
8 import be
.nikiroo
.jvcard
.Card
;
9 import be
.nikiroo
.jvcard
.launcher
.CardResult
.MergeCallback
;
12 * This class let you call "optional" methods, that is, methods and classes that
13 * may or may not be present.
16 * It currently offers services for:
18 * <li>remoting support</li>
19 * <li>TUI support</li>
28 * An {@link Exception} that is raised when you try to access functionality
29 * that has not been compiled into the code.
34 public class NotSupportedException
extends Exception
{
35 private static final long serialVersionUID
= 1L;
37 private boolean notCompiled
;
40 * Create a new {@link NotSupportedException}.
42 * @param notSupportedOption
43 * the option that is not supported
45 * FALSE when the operation is compiled in but not compatible
46 * for internal reasons
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
);
54 this.notCompiled
= notCompiled
;
58 * Check if the support is supposed to be compiled in the sources.
60 * @return TRUE if it should have worked (hence, if an internal error
63 public boolean isCompiledIn() {
69 * Create a new jVCard server on the given port, then run it.
74 * @throws NotSupportedException
75 * in case the option is not supported
79 @SuppressWarnings("unchecked")
80 static public void runServer(int port
) throws IOException
,
81 NotSupportedException
{
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);
107 * Start the TUI program.
110 * TRUE to force text mode, FALSE to force the Swing terminal
111 * emulator, null to automatically determine the best choice
113 * the files to show at startup
115 * @throws NotSupportedException
116 * in case the option is not supported
117 * @throws IOException
118 * in case of IO error
120 @SuppressWarnings("unchecked")
121 static public void startTui(Boolean textMode
, List
<String
> files
)
122 throws IOException
, NotSupportedException
{
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);
148 * Return the {@link Card} corresponding to the given URL, synchronised if
152 * the jvcard:// with resource name URL (e.g.:
153 * <tt>jvcard://localhost:4444/coworkers</tt>)
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)
159 * @throws NotSupportedException
160 * in case the option is not supported
161 * @throws IOException
162 * in case of IO error
164 @SuppressWarnings("unchecked")
165 static public CardResult
syncCard(String input
, MergeCallback callback
)
166 throws IOException
, NotSupportedException
{
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 });
173 Object o
= syncClass
.getConstructor(String
.class)
175 CardResult card
= (CardResult
) sync
.invoke(o
, false, callback
);
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);