View Javadoc

1   /*
2    *  
3    *  Fosstrak LLRP Commander (www.fosstrak.org)
4    * 
5    *  Copyright (C) 2008 ETH Zurich
6    *
7    *  This program is free software: you can redistribute it and/or modify
8    *  it under the terms of the GNU General Public License as published by
9    *  the Free Software Foundation, either version 3 of the License, or
10   *  (at your option) any later version.
11   *
12   *  This program is distributed in the hope that it will be useful,
13   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   *  GNU General Public License for more details.
16   *
17   *  You should have received a copy of the GNU General Public License
18   *  along with this program.  If not, see <http://www.gnu.org/licenses/> 
19   *
20   */
21  
22  package org.fosstrak.llrp.commander;
23  
24  import org.eclipse.jface.dialogs.MessageDialog;
25  import org.eclipse.swt.widgets.Shell;
26  import org.fosstrak.llrp.adaptor.exception.LLRPRuntimeException;
27  import org.fosstrak.llrp.client.LLRPExceptionHandler;
28  import org.fosstrak.llrp.client.LLRPExceptionHandlerTypeMap;
29  
30  /**
31  * Exception Handler that processes Exceptions triggered in the AdaptorManagement. 
32  * The Exceptions get reported via a dialog box to the user. 
33  * @author zhanghao
34  * @author sawielan
35  *
36  */
37  public class ExceptionHandler implements LLRPExceptionHandler {
38  	
39  	// default caption for the dialog.	
40  	private final static String DIALOG_CAPTION = "LLRP Client Warning";
41  	
42  	private Shell shell;
43  	
44  	public ExceptionHandler(Shell aShell) {
45  		shell = aShell;
46  	}
47  	
48  	public void postExceptionToGUI(LLRPExceptionHandlerTypeMap exceptionType, LLRPRuntimeException e, String adaptorName, String readerName) {
49  		
50  
51  		final LLRPExceptionHandlerTypeMap aExceptionType = exceptionType;
52  		final String aAdapter = adaptorName;
53  		final String aReader = readerName;
54  		final LLRPRuntimeException ex = e;
55  		
56  		// we need a special thread access to post a method to a SWT widget concurrently.
57  		shell.getDisplay().asyncExec(new Runnable () {
58  
59  			public void run() {
60  				switch (aExceptionType) {
61  					case EXCEPTION_ADAPTOR_MANAGEMENT_NOT_INITIALIZED: {
62  						processMessage("AdaptorManagement is not initialized." + '\n' + 
63  								ex.getMessage());
64  						break;
65  					}
66  					case EXCEPTION_ADAPTOR_MANAGEMENT_CONFIG_NOT_STORABLE: {
67  						processMessage("Could not store the configuration." + '\n' + 
68  								ex.getMessage());
69  						break;
70  					}
71  					case EXCEPTION_ADAPTOR_ALREADY_EXISTS: {
72  						processMessage("Adaptor already exists: " +  aAdapter);
73  						break;
74  					}
75  					case EXCEPTION_READER_NOT_EXIST: {
76  						processReaderNotExist(aAdapter, aReader);
77  						break;
78  					}
79  					case EXCEPTION_ADAPTER_NOT_EXIST: {
80  						processAdapterNotExist(aAdapter);
81  						break;
82  					}
83  					case EXCEPTION_READER_LOST: {
84  						processReaderLost(aAdapter, aReader);
85  						break;
86  					}
87  					case EXCEPTION_ADAPTER_LOST: {
88  						processAdapterLost(aAdapter);
89  						break;
90  					}
91  					case EXCEPTION_MSG_SENDING_ERROR: {
92  						processSendingError(aAdapter, aReader);
93  						break;
94  					}
95  					case EXCEPTION_NO_READER_CONFIG_MSG: {
96  						processNoReaderConfigError(aAdapter, aReader);
97  						break;
98  					}
99  					case EXCEPTION_NO_ROSPEC_MSG: {
100 						processNoROSpecError(aAdapter, aReader);
101 						break;
102 					}
103 					case EXCEPTION_MSG_SYNTAX_ERROR: {
104 						processMsgSyntaxError();
105 						break;
106 					}
107 					default: {
108 						processGeneralException(aAdapter, aReader, ex.getMessage());
109 					}
110 				}
111 				ResourceCenter.getInstance().
112 					getReaderExplorerView().refresh();
113 			}
114 
115 		});
116 
117 	}
118 	
119 	private void processReaderNotExist(String aAdapter, String aReader) {
120 		String aText = "Reader " + aReader + " @ " + aAdapter + " is not found!";
121 		MessageDialog.openWarning(shell, DIALOG_CAPTION, aText);
122 	}
123 	
124 	private void processAdapterNotExist(String aAdapter) {
125 		String aText = "Adapter " + aAdapter + " is not found!";
126 		MessageDialog.openWarning(shell, DIALOG_CAPTION, aText);
127 	}
128 	
129 	private void processReaderLost(String aAdapter, String aReader) {
130 		String aText = "Reader " + aReader + " @ " + aAdapter + " is lost!";
131 		MessageDialog.openWarning(shell, DIALOG_CAPTION, aText);
132 	}
133 	
134 	private void processAdapterLost(String aAdapter) {
135 		String aText = "Adapter " + aAdapter + " is lost!";
136 		MessageDialog.openWarning(shell, DIALOG_CAPTION, aText);
137 	}
138 	
139 	private void processSendingError(String aAdapter, String aReader) {
140 		String aText = "Sending Message to " + aReader + " @ " + aAdapter + " failed!";
141 		MessageDialog.openWarning(shell, DIALOG_CAPTION, aText);
142 	}
143 	
144 	private void processNoReaderConfigError(String aAdapter, String aReader) {
145 		String aText = "No existing GET_READER_CONFIG_RESPONSE message \nfrom " + aReader + " @ " + aAdapter + ".\nPlease send GET_READER_CONFIG first.";
146 		MessageDialog.openWarning(shell, DIALOG_CAPTION, aText);
147 	}
148 	
149 	private void processNoROSpecError(String aAdapter, String aReader) {
150 		String aText = "No existing GET_ROSPECS_RESPONSE message \nfrom " + aReader + " @ " + aAdapter + ".\nPlease send GET_ROSPECS first.";
151 		MessageDialog.openWarning(shell, DIALOG_CAPTION, aText);
152 	}
153 	
154 	private void processGeneralException(String aAdapter, String aReader, String aText) {
155 		MessageDialog.openWarning(shell, DIALOG_CAPTION, aText);
156 	}
157 	
158 	private void processMsgSyntaxError() {
159 		String aText = "XML Syntax Error!";
160 		MessageDialog.openWarning(shell, DIALOG_CAPTION, aText);
161 	}
162 	
163 	private void processMessage(String message) {
164 		MessageDialog.openWarning(shell, DIALOG_CAPTION, message);
165 	}
166 }