1 package be
.nikiroo
.jvcard
.launcher
;
3 import java
.io
.IOException
;
4 import java
.lang
.reflect
.Constructor
;
5 import java
.lang
.reflect
.InvocationTargetException
;
6 import java
.lang
.reflect
.Method
;
9 import be
.nikiroo
.jvcard
.Card
;
10 import be
.nikiroo
.jvcard
.launcher
.CardResult
.MergeCallback
;
13 * This class let you call "optional" methods, that is, methods and classes that
14 * may or may not be present.
17 * It currently offers services for:
19 * <li>remoting support</li>
20 * <li>TUI support</li>
29 * An {@link Exception} that is raised when you try to access functionality
30 * that has not been compiled into the code.
35 public class NotSupportedException
extends Exception
{
36 private static final long serialVersionUID
= 1L;
38 private boolean notCompiled
;
41 * Create a new {@link NotSupportedException}.
43 * @param notSupportedOption
44 * the option that is not supported
46 * FALSE when the operation is compiled in but not compatible
47 * for internal reasons
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
);
55 this.notCompiled
= notCompiled
;
59 * Check if the support is supposed to be compiled in the sources.
61 * @return TRUE if it should have worked (hence, if an internal error
64 public boolean isCompiledIn() {
70 * Create a new jVCard server on the given port, then run it.
75 * @throws NotSupportedException
76 * in case the option is not supported
80 @SuppressWarnings("unchecked")
81 static public void runServer(int port
) throws IOException
,
82 NotSupportedException
{
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);
108 * Start the TUI program.
111 * TRUE to force text mode, FALSE to force the Swing terminal
112 * emulator, null to automatically determine the best choice
114 * the files to show at startup
116 * @throws NotSupportedException
117 * in case the option is not supported
118 * @throws IOException
119 * in case of IO error
121 @SuppressWarnings("unchecked")
122 static public void startTui(Boolean textMode
, List
<String
> files
)
123 throws IOException
, NotSupportedException
{
125 @SuppressWarnings("rawtypes")
126 Class launcherClass
= Class
127 .forName("be.nikiroo.jvcard.tui.TuiLauncher");
128 Constructor
<?
> cons
= launcherClass
.getConstructors()[0];
129 Object instance
= cons
.newInstance(textMode
, files
);
130 Method start
= launcherClass
.getDeclaredMethod("start");
131 start
.invoke(instance
);
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
) {
146 throw new Optional().new NotSupportedException(e
, "TUI", false);
147 } catch (IndexOutOfBoundsException e
) {
148 throw new Optional().new NotSupportedException(e
, "TUI", false);
153 * Return the {@link Card} corresponding to the given URL, synchronised if
157 * the jvcard:// with resource name URL (e.g.:
158 * <tt>jvcard://localhost:4444/coworkers</tt>)
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)
164 * @throws NotSupportedException
165 * in case the option is not supported
166 * @throws IOException
167 * in case of IO error
169 @SuppressWarnings("unchecked")
170 static public CardResult
syncCard(String input
, MergeCallback callback
)
171 throws IOException
, NotSupportedException
{
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 });
178 Object o
= syncClass
.getConstructor(String
.class)
180 CardResult card
= (CardResult
) sync
.invoke(o
, false, callback
);
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);