View Javadoc

1   /*--------------------------------------------------------------------------
2    *  Copyright 2008 utgenome.org
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  // utgb-shell Project
18  //
19  // Server.java
20  // Since: Jan 10, 2008
21  //
22  // $URL$ 
23  // $Author$
24  //--------------------------------------
25  package org.utgenome.shell;
26  
27  import java.io.BufferedInputStream;
28  import java.io.File;
29  import java.io.FileInputStream;
30  import java.io.FileOutputStream;
31  import java.io.IOException;
32  
33  import org.utgenome.config.UTGBConfig;
34  import org.xerial.util.FileUtil;
35  import org.xerial.util.log.Logger;
36  
37  /**
38   * A UTGB sub command for starting up a UTGB portable server
39   * 
40   * @author leo
41   * 
42   */
43  public class Server extends UTGBShellCommand {
44  
45  	private static Logger _logger = Logger.getLogger(Server.class);
46  
47  	private UTGBPortableConfig option = new UTGBPortableConfig();
48  
49  	public Server() {
50  
51  	}
52  
53  	@Override
54  	public void execute(String[] args) throws Exception {
55  
56  		if (!isInProjectRoot())
57  			throw new UTGBShellException("not in the track project root");
58  
59  		// create war/utgb (GWT module) folder
60  		FileUtil.mkdirs(new File(getProjectRoot(), "war/" + option.gwtModule));
61  
62  		// copy resources
63  		maven(String.format("war:exploded -Dgwt.module=\"%s\"", option.gwtModule));
64  
65  		UTGBConfig config = loadUTGBConfig();
66  		String projectName = config.projectName;
67  		if (option.contextPath == null)
68  			option.contextPath = "/" + (projectName != null ? projectName : "utgb");
69  
70  		// create context.xml
71  		option.projectRoot = new File(globalOption.projectDir == null ? "" : globalOption.projectDir).getAbsolutePath();
72  		createContextXML(projectName, option.projectRoot, true);
73  
74  		if (_logger.isDebugEnabled())
75  			_logger.debug(option);
76  
77  		UTGBPortable server = new UTGBPortable(option);
78  
79  		server.addServerListener(new ServerListener() {
80  			public void beforeStart() {
81  				try {
82  					_logger.info("synchronizing the folder contents from " + option.webAppDir + " to " + option.workingDir);
83  
84  					rsync(getProjectResourcePath(option.webAppDir).getAbsolutePath(), getProjectResourcePath(option.workingDir).getAbsolutePath());
85  				}
86  				catch (Exception e) {
87  					_logger.error(e);
88  				}
89  
90  			}
91  
92  			public void afterStart() {
93  				_logger.info("started the server");
94  			}
95  
96  			public void afterStop() {
97  				_logger.info("stopped the server");
98  			}
99  
100 			public void beforeStop() {
101 				_logger.info("stopping the server");
102 			}
103 		});
104 		server.start();
105 
106 	}
107 
108 	public static void rsync(File src, File dest) throws UTGBShellException, IOException {
109 		if (!src.exists())
110 			return;
111 
112 		if (src.isDirectory()) {
113 
114 			String dirName = src.getName();
115 			// ignore .svn, .cvs folders
116 			if (dirName.endsWith(".svn") || dirName.endsWith(".cvs"))
117 				return;
118 
119 			// prepare the target directory
120 			if (dest.exists()) {
121 				if (!dest.isDirectory())
122 					throw new UTGBShellException("cannot create a directory " + getPath(dest) + ": a file with the same name already exists.");
123 			}
124 			else {
125 				_logger.info("create a directory: " + getPath(dest));
126 				dest.mkdirs();
127 			}
128 
129 			// copy contents under the direcotry
130 			for (File childFile : src.listFiles()) {
131 				rsync(childFile, new File(dest, childFile.getName()));
132 			}
133 		}
134 		else {
135 			// copy the src file
136 			boolean toOverwrite = dest.exists() ? src.lastModified() > dest.lastModified() : true;
137 			if (!toOverwrite)
138 				return;
139 
140 			// prepare the parent folder
141 			File parentFolder = dest.getParentFile();
142 			if (!parentFolder.exists()) {
143 				_logger.info("create a directory: " + getPath(parentFolder));
144 				parentFolder.mkdirs();
145 			}
146 			_logger.info("create a file: " + getPath(dest));
147 			BufferedInputStream in = new BufferedInputStream(new FileInputStream(src));
148 			FileOutputStream out = new FileOutputStream(dest);
149 			byte[] buffer = new byte[1024];
150 			int bytesRead = 0;
151 			while ((bytesRead = in.read(buffer)) > 0) {
152 				out.write(buffer, 0, bytesRead);
153 			}
154 			out.flush();
155 			out.close();
156 		}
157 
158 	}
159 
160 	public static void rsync(String src, String dest) throws UTGBShellException, IOException {
161 		rsync(new File(src), new File(dest));
162 	}
163 
164 	@Override
165 	public String name() {
166 		return "server";
167 	}
168 
169 	@Override
170 	public String getOneLinerDescription() {
171 		return "start up a portable web server";
172 	}
173 
174 	@Override
175 	public Object getOptionHolder() {
176 		return option;
177 	}
178 
179 }