View Javadoc

1   /*
2    * JBoss, Home of Professional Open Source.
3    * Copyright 2013, Red Hat Middleware LLC, and individual contributors
4    * as indicated by the @author tags. See the copyright.txt file in the
5    * distribution for a full listing of individual contributors.
6    *
7    * This is free software; you can redistribute it and/or modify it
8    * under the terms of the GNU Lesser General Public License as
9    * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software 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 GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */
22  package org.redhat.vmtruckloader.vmware.action;
23  
24  import java.rmi.RemoteException;
25  
26  import org.slf4j.Logger;
27  import org.slf4j.LoggerFactory;
28  
29  import com.vmware.vim25.DuplicateName;
30  import com.vmware.vim25.FileFault;
31  import com.vmware.vim25.InsufficientResourcesFault;
32  import com.vmware.vim25.InvalidDatastore;
33  import com.vmware.vim25.InvalidName;
34  import com.vmware.vim25.OutOfBounds;
35  import com.vmware.vim25.RuntimeFault;
36  import com.vmware.vim25.TaskInfo;
37  import com.vmware.vim25.VmConfigFault;
38  import com.vmware.vim25.mo.Task;
39  
40  /**
41   * @author Romain Pelisse - <romain@redhat.com>
42   *
43   * @param <T> - type returned by the callback method
44   */
45  public abstract class AbstractVMWareActionCallback<T> implements VMWareActionCallback<T> {
46  	
47  	private static final Logger LOGGER = LoggerFactory.getLogger(AbstractVMWareActionCallback.class);
48  
49  	protected void runTask(String errorMssg) {
50  		Task task;
51  		try {
52  			task = runTask();
53  			String status = task.waitForTask();
54  			if ( status != Task.SUCCESS ) {
55  				TaskInfo info = task.getTaskInfo();
56  				LOGGER.warn(info.getError().localizedMessage);
57  				throw new IllegalStateException(errorMssg);
58  			}
59  		} catch (InvalidName e) {
60  			throw new IllegalArgumentException(e);
61  		} catch (VmConfigFault e)  {
62  			throw new IllegalArgumentException(e);
63  		} catch (DuplicateName e) {
64  			throw new IllegalArgumentException(e);
65  		} catch (FileFault e) {
66  			throw new IllegalStateException(e);
67  		} catch (OutOfBounds e) {
68  			throw new IllegalStateException(e);
69  		} catch (InsufficientResourcesFault e) {
70  			throw new IllegalStateException(e);
71  		} catch (InvalidDatastore e) {
72  			throw new IllegalArgumentException(e);
73  		} catch (RuntimeFault e) {
74  			throw new IllegalStateException(e);
75  		} catch (RemoteException e) {
76  			throw new IllegalStateException(e);
77  		} catch (InterruptedException e) {
78  			LOGGER.warn("Wait for task timed out - task may not have finished successfully:" + e.getLocalizedMessage());
79  		} catch (Exception e ) {
80  			LOGGER.warn("Unknown Exception:" + e.getMessage());
81  			throw new IllegalStateException(e);
82  		}
83  	}
84  
85  	protected Task runTask() throws InvalidName, VmConfigFault, DuplicateName, FileFault, OutOfBounds, InsufficientResourcesFault, InvalidDatastore, RuntimeFault, RemoteException {
86  		throw new UnsupportedOperationException("Callback " + this.getClass() + " has invoked runTaks but not override it!");
87  	}
88  
89  }