AbstractAction.java
001 /*
002  *
003  * Licensed to the Apache Software Foundation (ASF) under one
004  * or more contributor license agreements.  See the NOTICE file
005  * distributed with this work for additional information
006  * regarding copyright ownership.  The ASF licenses this file
007  * to you under the Apache License, Version 2.0 (the
008  * "License"); you may not use this file except in compliance
009  * with the License.  You may obtain a copy of the License at
010  
011  *   http://www.apache.org/licenses/LICENSE-2.0
012  
013  * Unless required by applicable law or agreed to in writing,
014  * software distributed under the License is distributed on an
015  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
016  * KIND, either express or implied.  See the License for the
017  * specific language governing permissions and limitations
018  * under the License.
019  *
020  */
021 package org.apache.qpid.management.ui.actions;
022 
023 import static org.apache.qpid.management.ui.Constants.ERROR_SERVER_CONNECTION;
024 
025 import java.io.IOException;
026 
027 import javax.net.ssl.SSLException;
028 import javax.net.ssl.SSLHandshakeException;
029 import javax.net.ssl.SSLKeyException;
030 import javax.net.ssl.SSLPeerUnverifiedException;
031 
032 import org.apache.qpid.management.ui.ApplicationRegistry;
033 import org.apache.qpid.management.ui.ApplicationWorkbenchAdvisor;
034 import org.apache.qpid.management.ui.Constants;
035 import org.apache.qpid.management.ui.jmx.MBeanUtility;
036 import org.apache.qpid.management.ui.views.NavigationView;
037 import org.eclipse.core.runtime.IStatus;
038 import org.eclipse.core.runtime.Status;
039 import org.eclipse.jface.action.IAction;
040 import org.eclipse.jface.dialogs.ErrorDialog;
041 import org.eclipse.jface.viewers.ISelection;
042 import org.eclipse.ui.IWorkbenchWindow;
043 import org.eclipse.ui.IWorkbenchWindowActionDelegate;
044 
045 public class AbstractAction
046 {
047     private NavigationView _navigationView;
048     
049     protected IWorkbenchWindow _window;
050     
051     public static final String SECURITY_FAILURE = "User authentication failed";   
052     public static final String SERVER_UNAVAILABLE = "Unable to connect to the specified Qpid JMX server";
053     public static final String INVALID_PERSPECTIVE = "Invalid Perspective";
054     public static final String CHANGE_PERSPECTIVE = "Please use the Qpid Management Perspective";
055     
056     private static final String SSL_EMPTY_TRUSTANCHORS = "the trustAnchors parameter must be non-empty";
057     private static final String SSL_UNABLE_TO_FIND_CERTPATH = "sun.security.provider.certpath.SunCertPathBuilderException: " +
058                                             "unable to find valid certification path to requested target";
059       
060     /**
061      * We will cache window object in order to
062      * be able to provide parent shell for the message dialog.
063      @see IWorkbenchWindowActionDelegate#init
064      */
065     public void init(IWorkbenchWindow window)
066     {
067         this._window = window;
068         if (_window.getShell() != null)
069         {
070             _window.getShell().setImage(ApplicationRegistry.getImage(Constants.CONSOLE_IMAGE));
071         }
072     }   
073 
074     protected NavigationView getNavigationView()
075     {
076         if (_navigationView == null)
077         {
078             _navigationView = (NavigationView)_window.getActivePage().findView(NavigationView.ID);
079         }
080         
081         return _navigationView;
082     }
083     
084     protected void handleException(Throwable ex, String title, String msg)
085     {
086         //ensure first that the exception is not due to running in the wrong eclipse perspective
087         NavigationView view = (NavigationView)_window.getActivePage().findView(NavigationView.ID);
088         if (view == null)
089         {
090             IStatus status = new Status(IStatus.WARNING, ApplicationWorkbenchAdvisor.PERSPECTIVE_ID,
091                     IStatus.OK, CHANGE_PERSPECTIVE, null)
092             ErrorDialog.openError(_window.getShell()"Warning", INVALID_PERSPECTIVE, status);
093             return;
094         }
095 
096         //default title if none given
097         if (title == null)
098         {
099             title = ERROR_SERVER_CONNECTION;
100         }
101 
102         //determine the error message to display
103         if (msg == null)
104         {
105             if (ex instanceof SSLException)
106             {
107                 if (ex instanceof SSLKeyException)
108                 {
109                     msg = "SSL key was invalid, please check the certificate configuration.";
110                     //Display error dialogue and return
111                     displayErrorDialogue(msg, title);
112                     return;
113                 }
114                 else if (ex instanceof SSLPeerUnverifiedException)
115                 {
116                     msg = "SSL peer identity could not be verified, please ensure valid certificate configuration.";
117                     //Display error dialogue and return
118                     displayErrorDialogue(msg, title);
119                     return;
120                 }
121                 else if (ex instanceof SSLHandshakeException)
122                 {
123                     if (ex.getMessage().contains(SSL_UNABLE_TO_FIND_CERTPATH))
124                     {
125                         msg = "Unable to certify the provided SSL certificate using the current SSL trust store.";
126                     }
127                     else
128                     {
129                         //cause unknown, provide a trace too
130                         MBeanUtility.printStackTrace(ex);
131                         msg = "SSL handhshake error.";
132                     }
133                     //Display error dialogue and return
134                     displayErrorDialogue(msg, title);
135                     return;
136                 }
137                 else
138                 {
139                     //general SSL Exception.
140                     if (ex.getMessage().contains(SSL_EMPTY_TRUSTANCHORS))
141                     {
142                         msg = "Unable to locate the specified SSL certificate trust store, please check the configuration.";
143                     }
144                     else
145                     {
146                         //cause unknown, print stack trace
147                         MBeanUtility.printStackTrace(ex);
148                         msg = "SSL connection error.";
149                     }
150                     //Display error dialogue and return
151                     displayErrorDialogue(msg, title);
152                     return;
153                 }
154             }
155             else if (ex instanceof IOException)
156             {
157                 //uncaught IOException, eg when trying to connect to a server/port with no JMX server running
158                 msg = SERVER_UNAVAILABLE;
159                 //Display error dialogue and return
160                 displayErrorDialogue(msg, title);
161                 return;
162             }
163             else if (ex instanceof SecurityException)
164             {
165                 //SecurityException when providing incorrect login credentials
166                 msg = SECURITY_FAILURE;
167                 //Display error dialogue and return
168                 displayErrorDialogue(msg, title);
169                 return;
170             }
171             else
172             {
173                 //Unknown exception type/reason. 
174                 msg = ex.getMessage();
175             }
176 
177             //if msg is still null, try reporting the cause.
178             if ((msg == null&& (ex.getCause() != null))
179             {
180                 msg = ex.getCause().getMessage();
181             }
182 
183             //failing all else, default non-descript error message.
184             if (msg == null)
185             {
186                 msg = "An unknown error has occured.";
187             }
188         }
189 
190         //Display error dialogue and print the exception stack trace
191         MBeanUtility.printStackTrace(ex);
192         displayErrorDialogue(msg, title);
193     }
194     
195     private void displayErrorDialogue(String msg, String title)
196     {
197         IStatus status = new Status(IStatus.ERROR, ApplicationWorkbenchAdvisor.PERSPECTIVE_ID,
198                 IStatus.OK, msg, null)
199         ErrorDialog.openError(_window.getShell()"Error", title, status);      
200     }
201 
202     /**
203      * Selection in the workbench has been changed. We can change the state of the 'real' action here
204      * if we want, but this can only happen after  the delegate has been created.
205      @see IWorkbenchWindowActionDelegate#selectionChanged
206      */
207     public void selectionChanged(IAction action, ISelection selection) {
208     }
209 
210     /**
211      * We can use this method to dispose of any system resources we previously allocated.
212      @see IWorkbenchWindowActionDelegate#dispose
213      */
214     public void dispose() {
215         
216     }
217 }