001 /**
002 *
003 */
004 package de.jw.cloud42.webapp;
005
006 import java.io.File;
007
008 import java.util.logging.Logger;
009
010 import javax.faces.application.FacesMessage;
011
012
013 import org.jboss.seam.ScopeType;
014 import org.jboss.seam.annotations.In;
015 import org.jboss.seam.annotations.Name;
016
017 import org.jboss.seam.annotations.Scope;
018 import org.jboss.seam.annotations.Synchronized;
019
020 import org.jboss.seam.faces.FacesMessages;
021 import org.richfaces.event.UploadEvent;
022
023
024 import de.jw.cloud42.core.domain.RemoteResult;
025 import de.jw.cloud42.core.remoting.RemoteControl;
026 import de.jw.cloud42.webapp.utils.FileUtils;
027
028 /**
029 * Seam component wrapper around remoting functions of Cloud42.
030 *
031 * @author fbitzer
032 *
033 */
034 @Name("remotingManager")
035 @Scope(ScopeType.SESSION)
036 @Synchronized(timeout=1000000000)
037 public class RemotingManager {
038
039 @In
040 UserManager userManager;
041
042 /**
043 * Inject faces messages to trigger error and success messages.
044 */
045 @In
046 FacesMessages facesMessages;
047
048
049
050
051
052 //input fields used in the view (remoting dialog)
053 private String command;
054 private boolean useBatch;
055 private byte[] batchFile;
056 private RemoteResult result;
057
058
059
060 /**
061 * fileUploadListener function for RichFaces fileupload component.
062 * Reads the content of the uploaded file into a byte array and stores it in the
063 * <code>batchFile</code> property.
064 * @param e
065 */
066 public void fileUploadListener(UploadEvent e){
067
068 try {
069
070 File f = e.getUploadItem().getFile();
071
072 this.batchFile = FileUtils.getBytesFromFile(f);
073
074 e.getUploadItem().getFile().delete();
075 }
076 catch (Exception ex) {
077 Logger.getAnonymousLogger().warning("Uploading file failed: " + ex.getMessage());
078 }
079 }
080
081
082 /**
083 * Execute a command or a batch file.
084 * @param dnsName Hostname of AMI.
085 * @param keyName Name of keypair to use.
086 */
087 public void execute(String dnsName, String keyName){
088
089 RemoteControl c = new RemoteControl();
090
091 //get the private key for the keyname
092 String privateKey = userManager.getKeyForName(keyName);
093
094
095
096 if (useBatch && batchFile != null) {
097 result = c.executeBatch(dnsName, privateKey, batchFile);
098 batchFile = null;
099 } else {
100 result = c.executeCommand(dnsName, privateKey, command);
101 }
102
103 if (result == null) {
104 facesMessages.addFromResourceBundle(FacesMessage.SEVERITY_ERROR, "msg_commandNotExecuted");
105 result = new RemoteResult();//result must not be null because of databinding
106 } else {
107 facesMessages.addFromResourceBundle(FacesMessage.SEVERITY_INFO, "msg_commandExecuted");
108
109 }
110
111 }
112
113 /**
114 * Reset the remoting dialog by resetting bean values.
115 */
116 public void resetRemotingDialog(){
117 command = "";
118 useBatch = false;
119 batchFile = null;
120 result = new RemoteResult();
121 }
122
123 /**
124 * @return the command
125 */
126 public String getCommand() {
127 return command;
128 }
129
130 /**
131 * @param command the command to set
132 */
133 public void setCommand(String command) {
134 this.command = command;
135 }
136
137 /**
138 * @return the useBatch
139 */
140 public boolean isUseBatch() {
141 return useBatch;
142 }
143
144 /**
145 * @param useBatch the useBatch to set
146 */
147 public void setUseBatch(boolean useBatch) {
148 this.useBatch = useBatch;
149 }
150
151 /**
152 * @return the batchFile
153 */
154 public byte[] getBatchFile() {
155 return batchFile;
156 }
157
158 /**
159 * @param batchFile the batchFile to set
160 */
161 public void setBatchFile(byte[] batchFile) {
162 this.batchFile = batchFile;
163 }
164
165 /**
166 * @return the result
167 */
168 public RemoteResult getResult() {
169 return result;
170 }
171
172 /**
173 * @param result the result to set
174 */
175 public void setResult(RemoteResult result) {
176 this.result = result;
177 }
178
179 }