Optional: return only 2 types of exceptions
authorNiki Roo <niki@nikiroo.be>
Tue, 5 Apr 2016 18:24:55 +0000 (20:24 +0200)
committerNiki Roo <niki@nikiroo.be>
Tue, 5 Apr 2016 18:24:55 +0000 (20:24 +0200)
src/be/nikiroo/jvcard/launcher/Main.java
src/be/nikiroo/jvcard/launcher/Optional.java

index 6d552febfae8c16955cde0ad95480f32ef5bdc40..a10d68c51a5fd85f6c0c00f4a123421d0bbf5423 100644 (file)
@@ -15,6 +15,7 @@ import be.nikiroo.jvcard.Contact;
 import be.nikiroo.jvcard.Data;
 import be.nikiroo.jvcard.TypeInfo;
 import be.nikiroo.jvcard.launcher.CardResult.MergeCallback;
+import be.nikiroo.jvcard.launcher.Optional.NotSupportedException;
 import be.nikiroo.jvcard.parsers.Format;
 import be.nikiroo.jvcard.remote.Command;
 import be.nikiroo.jvcard.remote.SimpleSocket;
@@ -286,12 +287,12 @@ public class Main {
                case SERVER: {
                        try {
                                Optional.runServer(port);
-                       } catch (Exception e) {
-                               if (e instanceof IOException) {
-                                       ERR(StringId.CLI_ERR, StringId.CLI_ERR_CANNOT_START,
-                                                       ERR_INTERNAL);
-                                       return;
-                               } else if (e instanceof ClassNotFoundException) {
+                       } catch (IOException e) {
+                               ERR(StringId.CLI_ERR, StringId.CLI_ERR_CANNOT_START,
+                                               ERR_INTERNAL);
+                               return;
+                       } catch (NotSupportedException e) {
+                               if (!e.isCompiledIn()) {
                                        ERR(StringId.CLI_ERR, StringId.CLI_ERR_NO_REMOTING,
                                                        ERR_INTERNAL);
                                        return;
@@ -392,12 +393,12 @@ public class Main {
                case CONTACT_MANAGER: {
                        try {
                                Optional.startTui(textMode, files);
-                       } catch (Exception e) {
-                               if (e instanceof IOException) {
-                                       ERR(StringId.CLI_ERR, StringId.CLI_ERR_CANNOT_START,
-                                                       ERR_NO_FILE);
-                                       return;
-                               } else if (e instanceof ClassNotFoundException) {
+                       } catch (IOException e) {
+                               ERR(StringId.CLI_ERR, StringId.CLI_ERR_CANNOT_START,
+                                               ERR_NO_FILE);
+                               return;
+                       } catch (NotSupportedException e) {
+                               if (!e.isCompiledIn()) {
                                        ERR(StringId.CLI_ERR, StringId.CLI_ERR_NO_TUI, ERR_INTERNAL);
                                        return;
                                } else {
@@ -499,7 +500,7 @@ public class Main {
                        }
                } catch (IOException ioe) {
                        throw ioe;
-               } catch (Exception e) {
+               } catch (NotSupportedException e) {
                        throw new IOException("Remoting support not available", e);
                }
 
index cff57f953d34d3e36333531a423d1634ac554712..9ed726dfe857201285e9dc61078b632abb5c2d63 100644 (file)
@@ -24,38 +24,83 @@ import be.nikiroo.jvcard.launcher.CardResult.MergeCallback;
  *
  */
 class Optional {
+       /***
+        * An {@link Exception} that is raised when you try to access functionality
+        * that has not been compiled into the code.
+        * 
+        * @author niki
+        *
+        */
+       public class NotSupportedException extends Exception {
+               private static final long serialVersionUID = 1L;
+
+               private boolean notCompiled;
+
+               /**
+                * Create a new {@link NotSupportedException}.
+                * 
+                * @param notSupportedOption
+                *            the option that is not supported
+                * @param notCompiled
+                *            FALSE when the operation is compiled in but not compatible
+                *            for internal reasons
+                */
+               public NotSupportedException(Exception e, String notSupportedOption,
+                               boolean notCompiled) {
+                       super((notCompiled ? "Option not supported: "
+                                       : "Internal error when trying to use: ")
+                                       + notSupportedOption, e);
+
+                       this.notCompiled = notCompiled;
+               }
+
+               /**
+                * Check if the support is supposed to be compiled in the sources.
+                * 
+                * @return TRUE if it should have worked (hence, if an internal error
+                *         occurred)
+                */
+               public boolean isCompiledIn() {
+                       return !notCompiled;
+               }
+       }
+
        /**
         * Create a new jVCard server on the given port, then run it.
         * 
         * @param port
         *            the port to run on
         *
-        * @throws SecurityException
-        *             in case of internal error
-        * @throws NoSuchMethodException
-        *             in case of internal error
-        * @throws ClassNotFoundException
-        *             in case of internal error
-        * @throws IllegalAccessException
-        *             in case of internal error
-        * @throws InstantiationException
-        *             in case of internal error
-        * @throws InvocationTargetException
-        *             in case of internal error
-        * @throws IllegalArgumentException
-        *             in case of internal error
+        * @throws NotSupportedException
+        *             in case the option is not supported
         * @throws IOException
         *             in case of IO error
         */
        @SuppressWarnings("unchecked")
-       static public void runServer(int port) throws NoSuchMethodException,
-                       SecurityException, ClassNotFoundException, InstantiationException,
-                       IllegalAccessException, IllegalArgumentException,
-                       InvocationTargetException {
-               @SuppressWarnings("rawtypes")
-               Class serverClass = Class.forName("be.nikiroo.jvcard.remote.Server");
-               Method run = serverClass.getDeclaredMethod("run", new Class<?>[] {});
-               run.invoke(serverClass.getConstructor(int.class).newInstance(port));
+       static public void runServer(int port) throws IOException,
+                       NotSupportedException {
+               try {
+                       @SuppressWarnings("rawtypes")
+                       Class serverClass = Class
+                                       .forName("be.nikiroo.jvcard.remote.Server");
+                       Method run = serverClass
+                                       .getDeclaredMethod("run", new Class<?>[] {});
+                       run.invoke(serverClass.getConstructor(int.class).newInstance(port));
+               } catch (NoSuchMethodException e) {
+                       throw new Optional().new NotSupportedException(e, "remoting", true);
+               } catch (ClassNotFoundException e) {
+                       throw new Optional().new NotSupportedException(e, "remoting", false);
+               } catch (SecurityException e) {
+                       throw new Optional().new NotSupportedException(e, "remoting", false);
+               } catch (InstantiationException e) {
+                       throw new Optional().new NotSupportedException(e, "remoting", false);
+               } catch (IllegalAccessException e) {
+                       throw new Optional().new NotSupportedException(e, "remoting", false);
+               } catch (IllegalArgumentException e) {
+                       throw new Optional().new NotSupportedException(e, "remoting", false);
+               } catch (InvocationTargetException e) {
+                       throw new Optional().new NotSupportedException(e, "remoting", false);
+               }
        }
 
        /**
@@ -67,35 +112,36 @@ class Optional {
         * @param files
         *            the files to show at startup
         * 
-        * @throws SecurityException
-        *             in case of internal error
-        * @throws NoSuchMethodException
-        *             in case of internal error
-        * @throws ClassNotFoundException
-        *             in case of internal error
-        * @throws IllegalAccessException
-        *             in case of internal error
-        * @throws InstantiationException
-        *             in case of internal error
-        * @throws InvocationTargetException
-        *             in case of internal error
-        * @throws IllegalArgumentException
-        *             in case of internal error
+        * @throws NotSupportedException
+        *             in case the option is not supported
         * @throws IOException
         *             in case of IO error
         */
        @SuppressWarnings("unchecked")
        static public void startTui(Boolean textMode, List<String> files)
-                       throws NoSuchMethodException, SecurityException,
-                       ClassNotFoundException, InstantiationException,
-                       IllegalAccessException, IllegalArgumentException,
-                       InvocationTargetException {
-               @SuppressWarnings("rawtypes")
-               Class launcherClass = Class
-                               .forName("be.nikiroo.jvcard.tui.TuiLauncher");
-               Method start = launcherClass.getDeclaredMethod("start", new Class<?>[] {
-                               Boolean.class, List.class });
-               start.invoke(launcherClass.newInstance(), textMode, files);
+                       throws IOException, NotSupportedException {
+               try {
+                       @SuppressWarnings("rawtypes")
+                       Class launcherClass = Class
+                                       .forName("be.nikiroo.jvcard.tui.TuiLauncher");
+                       Method start = launcherClass.getDeclaredMethod("start",
+                                       new Class<?>[] { Boolean.class, List.class });
+                       start.invoke(launcherClass.newInstance(), textMode, files);
+               } catch (NoSuchMethodException e) {
+                       throw new Optional().new NotSupportedException(e, "TUI", true);
+               } catch (ClassNotFoundException e) {
+                       throw new Optional().new NotSupportedException(e, "TUI", false);
+               } catch (SecurityException e) {
+                       throw new Optional().new NotSupportedException(e, "TUI", false);
+               } catch (InstantiationException e) {
+                       throw new Optional().new NotSupportedException(e, "TUI", false);
+               } catch (IllegalAccessException e) {
+                       throw new Optional().new NotSupportedException(e, "TUI", false);
+               } catch (IllegalArgumentException e) {
+                       throw new Optional().new NotSupportedException(e, "TUI", false);
+               } catch (InvocationTargetException e) {
+                       throw new Optional().new NotSupportedException(e, "TUI", false);
+               }
        }
 
        /**
@@ -110,36 +156,39 @@ class Optional {
         *            to disallow conflict management (the {@link Card} will not be
         *            allowed to synchronise in case of conflicts)
         * 
-        * @throws SecurityException
-        *             in case of internal error
-        * @throws NoSuchMethodException
-        *             in case of internal error
-        * @throws ClassNotFoundException
-        *             in case of internal error
-        * @throws IllegalAccessException
-        *             in case of internal error
-        * @throws InstantiationException
-        *             in case of internal error
-        * @throws InvocationTargetException
-        *             in case of internal error
-        * @throws IllegalArgumentException
-        *             in case of internal error
+        * @throws NotSupportedException
+        *             in case the option is not supported
         * @throws IOException
         *             in case of IO error
         */
        @SuppressWarnings("unchecked")
        static public CardResult syncCard(String input, MergeCallback callback)
-                       throws ClassNotFoundException, NoSuchMethodException,
-                       SecurityException, InstantiationException, IllegalAccessException,
-                       IllegalArgumentException, InvocationTargetException, IOException {
-               @SuppressWarnings("rawtypes")
-               Class syncClass = Class.forName("be.nikiroo.jvcard.remote.Sync");
-               Method sync = syncClass.getDeclaredMethod("sync", new Class<?>[] {
-                               boolean.class, MergeCallback.class });
+                       throws IOException, NotSupportedException {
+               try {
+                       @SuppressWarnings("rawtypes")
+                       Class syncClass = Class.forName("be.nikiroo.jvcard.remote.Sync");
+                       Method sync = syncClass.getDeclaredMethod("sync", new Class<?>[] {
+                                       boolean.class, MergeCallback.class });
 
-               Object o = syncClass.getConstructor(String.class).newInstance(input);
-               CardResult card = (CardResult) sync.invoke(o, false, callback);
+                       Object o = syncClass.getConstructor(String.class)
+                                       .newInstance(input);
+                       CardResult card = (CardResult) sync.invoke(o, false, callback);
 
-               return card;
+                       return card;
+               } catch (NoSuchMethodException e) {
+                       throw new Optional().new NotSupportedException(e, "remoting", true);
+               } catch (ClassNotFoundException e) {
+                       throw new Optional().new NotSupportedException(e, "remoting", false);
+               } catch (SecurityException e) {
+                       throw new Optional().new NotSupportedException(e, "remoting", false);
+               } catch (InstantiationException e) {
+                       throw new Optional().new NotSupportedException(e, "remoting", false);
+               } catch (IllegalAccessException e) {
+                       throw new Optional().new NotSupportedException(e, "remoting", false);
+               } catch (IllegalArgumentException e) {
+                       throw new Optional().new NotSupportedException(e, "remoting", false);
+               } catch (InvocationTargetException e) {
+                       throw new Optional().new NotSupportedException(e, "remoting", false);
+               }
        }
 }