make key generation throw exceptions in case of errors
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Thu, 15 May 2008 14:01:45 +0000 (14:01 +0000)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Thu, 15 May 2008 14:01:45 +0000 (14:01 +0000)
git-svn-id: http://trooper/svn/projects/jSite/trunk@889 c3eda9e8-030b-0410-8277-bc7414b0a119

src/net/pterodactylus/jsite/core/Core.java
src/net/pterodactylus/jsite/core/CoreImpl.java
src/net/pterodactylus/jsite/core/NodeManager.java
src/net/pterodactylus/jsite/gui/SwingInterface.java

index 2815e38..d2a5e10 100644 (file)
@@ -116,11 +116,12 @@ public interface Core {
         * 
         * @param projectName
         *            The name of the project
-        * @return A newly created project, or <code>null</code> if key generation
-        *         failed
+        * @return A newly created project
         * @throws IOException
         *             if an I/O error occured communicating with the node
+        * @throws NoNodeException
+        *             if no node is configured
         */
-       public Project addProject(String projectName) throws IOException;
+       public Project createProject(String projectName) throws IOException, NoNodeException;
 
 }
index eae7b3a..3e96b10 100644 (file)
@@ -441,13 +441,10 @@ public class CoreImpl implements Core, NodeListener, RequestListener {
        /**
         * {@inheritDoc}
         */
-       public Project addProject(String projectName) throws IOException {
+       public Project createProject(String projectName) throws IOException, NoNodeException {
                Project project = new Project();
                project.setName(projectName);
                String[] keyPair = nodeManager.generateKeyPair();
-               if (keyPair == null) {
-                       return null;
-               }
                project.setPrivateKey(keyPair[0]);
                project.setPublicKey(keyPair[1]);
                return project;
index f772dc2..b9404c7 100644 (file)
@@ -429,17 +429,20 @@ public class NodeManager implements Iterable<Node>, PropertyChangeListener, High
         *         public key at index <code>1</code>
         * @throws IOException
         *             if an I/O error occurs communicating with the node
+        * @throws NoNodeException
+        *             if no node is configured
         */
-       public String[] generateKeyPair() throws IOException {
-               if (!nodes.isEmpty()) {
-                       Node node = nodes.get(0);
-                       HighLevelClient highLevelClient = nodeClients.get(node);
-                       try {
-                               KeyGenerationResult keyGenerationResult = highLevelClient.generateKey().getResult();
-                               return new String[] { keyGenerationResult.getInsertURI(), keyGenerationResult.getRequestURI() };
-                       } catch (InterruptedException e) {
-                               /* ignore. */
-                       }
+       public String[] generateKeyPair() throws IOException, NoNodeException {
+               if (nodes.isEmpty()) {
+                       throw new NoNodeException("no node configured");
+               }
+               Node node = nodes.get(0);
+               HighLevelClient highLevelClient = nodeClients.get(node);
+               try {
+                       KeyGenerationResult keyGenerationResult = highLevelClient.generateKey().getResult();
+                       return new String[] { keyGenerationResult.getInsertURI(), keyGenerationResult.getRequestURI() };
+               } catch (InterruptedException e) {
+                       /* ignore. */
                }
                return null;
        }
index 0accb58..3652388 100644 (file)
@@ -45,6 +45,7 @@ import javax.swing.UnsupportedLookAndFeelException;
 
 import net.pterodactylus.jsite.core.Core;
 import net.pterodactylus.jsite.core.CoreListener;
+import net.pterodactylus.jsite.core.NoNodeException;
 import net.pterodactylus.jsite.core.Node;
 import net.pterodactylus.jsite.core.Project;
 import net.pterodactylus.jsite.core.Request;
@@ -817,10 +818,14 @@ public class SwingInterface implements CoreListener, LoggingListener {
         */
        private void addProject() {
                try {
-                       Project project = core.addProject("New Project");
+                       Project project = core.createProject("New Project");
                        System.out.println("private: " + project.getPrivateKey() + ", public: " + project.getPublicKey());
                        mainWindow.addProject(project);
+               } catch (NoNodeException nne1) {
+                       /* TODO - add i18n */
+                       JOptionPane.showMessageDialog(mainWindow, I18n.get(""), I18n.get(""), JOptionPane.ERROR_MESSAGE);
                } catch (IOException e) {
+                       /* TODO - add i18n */
                        JOptionPane.showMessageDialog(mainWindow, I18n.get(""), I18n.get(""), JOptionPane.ERROR_MESSAGE);
                }
        }