View Javadoc

1   /*
2    * Copyright 2007-2008 the original author or authors.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.gwe.drivers.bundleManagers;
18  
19  import org.apache.commons.logging.Log;
20  import org.apache.commons.logging.LogFactory;
21  import org.gwe.GWEAppContext;
22  import org.gwe.drivers.HandleCreationException;
23  import org.gwe.drivers.HandleOperationException;
24  import org.gwe.drivers.netAccess.ConnectorException;
25  import org.gwe.drivers.netAccess.HostHandle;
26  import org.gwe.drivers.netAccess.ShellCommand;
27  import org.gwe.utils.IOUtils;
28  import org.gwe.utils.security.KeyStore;
29  import org.gwe.utils.security.ResourceLink;
30  import org.gwe.utils.security.ThinURI;
31  
32  /**
33   * @author Marco Ruiz
34   * @since Nov 9, 2007
35   */
36  public class BundleHandle {
37  	
38      private static Log log = LogFactory.getLog(BundleHandle.class);
39  	
40  	protected BundleType type;
41  	protected String path;
42  	protected String name;
43  
44  	public BundleHandle(BundleType type, String name) {
45  		setType(type);
46  		setPath(IOUtils.getFilePath(name));
47  		setName(IOUtils.getFileName(name));
48  	}
49  
50  	public BundleHandle(BundleType type, String path, String name) {
51  		setType(type);
52  		setPath(path);
53  		setName(type.createName(name));
54  	}
55  
56  	public BundleType getType() {
57  		return type;
58  	}
59  
60  	public void setType(BundleType type) {
61  		this.type = type;
62  	}
63  
64  	public String getPath() {
65  		return path;
66  	}
67  
68  	public void setPath(String path) {
69  		this.path = path;
70  	}
71  
72  	public String getName() {
73  		return name;
74  	}
75  
76  	public void setName(String name) {
77  		this.name = name;
78  	}
79  	
80  	public String getUnbundleCommand() {
81  		return type.getUnbundleCommand() + " " + getName();
82  	}
83  	
84  	public ShellCommand getUnbundleShellCommand() {
85  		return new ShellCommand(type.getUnbundleCommand() + " " + getName(), getPath(), null);
86  	}
87  	
88  	public boolean deploy(ResourceLink<HostHandle> destLink, String deploymentRoot, KeyStore keys) throws DeploymentException {
89  		return deploy(destLink, IOUtils.getFilePath(deploymentRoot), deploymentRoot, keys);
90  	}
91  	
92  	public boolean deploy(ResourceLink<HostHandle> destLink, String unbundleCmdPath, String deploymentRoot, KeyStore keys) throws DeploymentException {
93  		String fullPath = getFullPath();
94  		
95  		// Verify destination folder does not exist
96  		String deploymentDir = ThinURI.asNormalizedFileURI(destLink.getURIHost(), deploymentRoot);
97          
98  		if (existsRemoteDirectory(deploymentDir, keys)) {
99  			log.info("Cannot deploy '" + fullPath + "' because target directory '" + deploymentDir + "' already exists. This bundle may have been already deployed there.");
100 			return false;
101 		}
102 		
103 		// Transfer bundle
104 		String destFile = transferTo(destLink, unbundleCmdPath, keys);
105 		
106 		// Unbundle bundle
107 		HostHandle conn;
108         try {
109 	        conn = destLink.createHandle();
110         } catch (HandleCreationException e) {
111         	throw new DeploymentException("connect to remote host to unbundle file '" + destFile + "'", e);
112         }
113         
114         try {
115     		String result = conn.runCommand(getUnbundleCommand(), unbundleCmdPath, null);
116     		conn.close();
117 			log.info("'" + fullPath + "' deployment results:\n" + result);
118         } catch (ConnectorException e) {
119         	throw new DeploymentException("execute remote command to unbundle file '" + destFile + "'", e);
120         } catch (HandleOperationException e) {
121         	// Ignore, bundle already unbundled
122         }
123 		
124 		// Delete bundle
125 		deleteFile(destFile, keys);
126 		return true;
127 	}
128 
129 	private boolean existsRemoteDirectory(String deploymentDir, KeyStore keys) throws DeploymentException {
130 	    try {
131 			return keys.createFileLink(deploymentDir).createHandle().isDirectory();
132         } catch (HandleCreationException e) {
133         	throw new DeploymentException("create handle to potential remote daemon installation directory '" + deploymentDir + "'", e);
134         } catch (HandleOperationException e) {
135         	throw new DeploymentException("inspect remote daemon installation directory '" + deploymentDir + "'", e);
136         }
137     }
138 
139 	public String transferTo(ResourceLink<HostHandle> destLink, String destPath, KeyStore keys) throws DeploymentException {
140 		String destFile = ThinURI.asNormalizedFileURI(destLink.getURIHost(), IOUtils.concatenatePaths(destPath, getName()));
141 		try {
142 	        GWEAppContext.getGridFileSystem().transferFile(getFullPath(), destFile, keys);
143         } catch (Exception e) {
144         	throw new DeploymentException("transfer file '" + getFullPath() + "' to remote destination '" + destFile + "' ", e);
145         }
146 	    return destFile;
147     }
148 
149 	private void deleteFile(String destFile, KeyStore keys) throws DeploymentException {
150 	    try {
151 			keys.createFileLink(destFile).createHandle().delete();
152         } catch (HandleCreationException e) {
153         	throw new DeploymentException("create handle to remote file '" + destFile + "'", e);
154         } catch (HandleOperationException e) {
155         	throw new DeploymentException("delete remote file '" + destFile + "'", e);
156         }
157     }
158 
159 	private String getFullPath() {
160 	    return IOUtils.concatenatePaths(getPath(), getName());
161     }
162 }