Source Code Cross Referenced for Jetty6xAppServer.java in  » Net » Terracotta » com » tc » test » server » appserver » jetty6x » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Net » Terracotta » com.tc.test.server.appserver.jetty6x 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        package com.tc.test.server.appserver.jetty6x;
002:
003:        import org.apache.commons.io.IOUtils;
004:
005:        import com.tc.process.Exec;
006:        import com.tc.process.HeartBeatService;
007:        import com.tc.process.Exec.Result;
008:        import com.tc.test.TestConfigObject;
009:        import com.tc.test.server.ServerParameters;
010:        import com.tc.test.server.ServerResult;
011:        import com.tc.test.server.appserver.AbstractAppServer;
012:        import com.tc.test.server.appserver.AppServerParameters;
013:        import com.tc.test.server.appserver.AppServerResult;
014:        import com.tc.test.server.appserver.cargo.CargoLinkedChildProcess;
015:        import com.tc.test.server.util.AppServerUtil;
016:        import com.tc.util.PortChooser;
017:
018:        import java.io.BufferedReader;
019:        import java.io.File;
020:        import java.io.FileReader;
021:        import java.io.FileWriter;
022:        import java.io.PrintWriter;
023:        import java.util.ArrayList;
024:        import java.util.Arrays;
025:        import java.util.Iterator;
026:        import java.util.List;
027:        import java.util.Map;
028:        import java.util.Set;
029:
030:        public class Jetty6xAppServer extends AbstractAppServer {
031:            private static final String JAVA_CMD = System
032:                    .getProperty("java.home")
033:                    + File.separator + "bin" + File.separator + "java";
034:            private final String STOP_KEY = "secret";
035:            private final String JETTY_MAIN_CLASS = "org.mortbay.start.Main";
036:            private static final long START_STOP_TIMEOUT = 240 * 1000;
037:
038:            private static final String target = "<SystemProperty name=\"jetty.home\" default=\".\"/>/webapps";
039:
040:            private String configFile;
041:            private String instanceName;
042:            private File instanceDir;
043:            private File workDir;
044:
045:            private int jetty_port = 0;
046:            private int stop_port = 0;
047:            private Thread runner = null;
048:
049:            public Jetty6xAppServer(Jetty6xAppServerInstallation installation) {
050:                super (installation);
051:            }
052:
053:            public ServerResult start(ServerParameters parameters)
054:                    throws Exception {
055:                AppServerParameters params = (AppServerParameters) parameters;
056:                return startJetty(params);
057:            }
058:
059:            public void stop() throws Exception {
060:                final String[] cmd = new String[] { JAVA_CMD,
061:                        "-DSTOP.PORT=" + stop_port, "-DSTOP.KEY=" + STOP_KEY,
062:                        "-jar", "start.jar", "--stop" };
063:
064:                System.err.println("Stopping instance " + instanceName + "...");
065:                Process stop_cmd = Runtime.getRuntime().exec(cmd, null,
066:                        this .serverInstallDirectory());
067:                int exit_code = stop_cmd.waitFor();
068:                if (exit_code != 0) {
069:                    System.err.println("error stopping isntance "
070:                            + instanceName);
071:                }
072:
073:                if (runner != null) {
074:                    runner.join(START_STOP_TIMEOUT);
075:                }
076:
077:                if (runner.isAlive()) {
078:                    System.err.println("Instance " + instanceName + " on port "
079:                            + jetty_port + " still alive.");
080:                } else {
081:                    System.err.println("jetty instance " + instanceName
082:                            + " stopped");
083:                }
084:            }
085:
086:            private AppServerResult startJetty(AppServerParameters params)
087:                    throws Exception {
088:                prepareDeployment(params);
089:
090:                String[] jvmargs = params.jvmArgs().replaceAll("'", "").split(
091:                        "\\s+");
092:                List cmd = new ArrayList(Arrays.asList(jvmargs));
093:                cmd.add(0, JAVA_CMD);
094:                cmd.add("-cp");
095:                cmd.add(this .serverInstallDirectory()
096:                        + File.separator
097:                        + "start.jar"
098:                        + File.pathSeparator
099:                        + TestConfigObject.getInstance()
100:                                .linkedChildProcessClasspath());
101:                cmd.add("-Djetty.home=" + this .serverInstallDirectory());
102:                cmd.add("-Djetty.port=" + jetty_port);
103:                cmd.add("-DSTOP.PORT=" + stop_port);
104:                cmd.add("-DSTOP.KEY=" + STOP_KEY);
105:                cmd.add("-Djava.io.tmpdir=" + workDir.getAbsolutePath());
106:                cmd.add(CargoLinkedChildProcess.class.getName());
107:                cmd.add(JETTY_MAIN_CLASS);
108:                cmd.add(String.valueOf(HeartBeatService.listenPort()));
109:                cmd.add(instanceDir.getAbsolutePath());
110:                cmd.add(configFile);
111:
112:                final String[] cmdArray = (String[]) cmd
113:                        .toArray(new String[] {});
114:                final String nodeLogFile = new File(instanceDir + ".log")
115:                        .getAbsolutePath();
116:
117:                runner = new Thread("runner for " + instanceName) {
118:                    public void run() {
119:                        try {
120:                            Result result = Exec.execute(cmdArray, nodeLogFile,
121:                                    null, instanceDir);
122:                            if (result.getExitCode() != 0) {
123:                                System.err.println(result);
124:                            }
125:                        } catch (Exception e) {
126:                            e.printStackTrace();
127:                        }
128:                    }
129:                };
130:                runner.start();
131:                System.err.println("Starting jetty " + instanceName
132:                        + " on port " + jetty_port + "...");
133:                AppServerUtil.waitForPort(jetty_port, START_STOP_TIMEOUT);
134:                System.err.println("Started " + instanceName + " on port "
135:                        + jetty_port);
136:                return new AppServerResult(jetty_port, this );
137:            }
138:
139:            private void prepareDeployment(AppServerParameters params)
140:                    throws Exception {
141:                // move wars into the correct location
142:                Map wars = params.wars();
143:                if (wars != null && wars.size() > 0) {
144:                    File wars_dir = getWarsDirectory();
145:
146:                    Set war_entries = wars.entrySet();
147:                    Iterator war_entries_it = war_entries.iterator();
148:                    while (war_entries_it.hasNext()) {
149:                        Map.Entry war_entry = (Map.Entry) war_entries_it.next();
150:                        File war_file = (File) war_entry.getValue();
151:                        war_file
152:                                .renameTo(new File(wars_dir, war_file.getName()));
153:                    }
154:                }
155:
156:                // setup deployment config
157:                PortChooser portChooser = new PortChooser();
158:                jetty_port = portChooser.chooseRandomPort();
159:                stop_port = portChooser.chooseRandomPort();
160:
161:                instanceName = params.instanceName();
162:                instanceDir = new File(sandboxDirectory(), instanceName);
163:                workDir = new File(sandboxDirectory(), "work");
164:                workDir.mkdirs();
165:
166:                if (new File(instanceDir, "logs").mkdirs() == false) {
167:                    throw new Exception(
168:                            "Can't create logs directory for jetty instance: "
169:                                    + instanceName);
170:                }
171:                setProperties(params, jetty_port, instanceDir);
172:                createConfigFile();
173:            }
174:
175:            protected File getWarsDirectory() {
176:                return new File(this .sandboxDirectory(), "war");
177:            }
178:
179:            private void createConfigFile() throws Exception {
180:                String origialConfig = this .serverInstallDirectory()
181:                        .getAbsolutePath()
182:                        + File.separator + "etc" + File.separator + "jetty.xml";
183:                if (new File(origialConfig).exists() == false) {
184:                    throw new Exception(origialConfig + " doesn't exist.");
185:                }
186:
187:                StringBuffer buffer = new StringBuffer(1024);
188:                BufferedReader in = null;
189:                PrintWriter out = null;
190:
191:                try {
192:                    in = new BufferedReader(new FileReader(origialConfig));
193:                    String line;
194:                    while ((line = in.readLine()) != null) {
195:                        buffer.append(line).append("\n");
196:                    }
197:
198:                    int startIndex = buffer.indexOf(target);
199:                    if (startIndex > 0) {
200:                        int endIndex = startIndex + target.length();
201:                        buffer.replace(startIndex, endIndex, getWarsDirectory()
202:                                .getAbsolutePath());
203:                    } else {
204:                        throw new RuntimeException("Can't find target: "
205:                                + target);
206:                    }
207:
208:                    configFile = this .sandboxDirectory().getAbsolutePath()
209:                            + File.separator + "jetty.xml";
210:                    out = new PrintWriter(new FileWriter(configFile));
211:                    out.println(buffer.toString());
212:
213:                } finally {
214:                    IOUtils.closeQuietly(in);
215:                    IOUtils.closeQuietly(out);
216:                }
217:            }
218:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.