|
| 1 | +// |
| 2 | +// ======================================================================== |
| 3 | +// Copyright (c) 1995 Mort Bay Consulting Pty Ltd and others. |
| 4 | +// |
| 5 | +// This program and the accompanying materials are made available under the |
| 6 | +// terms of the Eclipse Public License v. 2.0 which is available at |
| 7 | +// https://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0 |
| 8 | +// which is available at https://www.apache.org/licenses/LICENSE-2.0. |
| 9 | +// |
| 10 | +// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 |
| 11 | +// ======================================================================== |
| 12 | +// |
| 13 | + |
| 14 | +package org.eclipse.jetty.docs.programming.server.deploy; |
| 15 | + |
| 16 | +import java.nio.file.Path; |
| 17 | +import java.util.List; |
| 18 | + |
| 19 | +import org.eclipse.jetty.deploy.Deployer; |
| 20 | +import org.eclipse.jetty.deploy.DeploymentScanner; |
| 21 | +import org.eclipse.jetty.deploy.StandardDeployer; |
| 22 | +import org.eclipse.jetty.server.Server; |
| 23 | +import org.eclipse.jetty.server.handler.ContextHandlerCollection; |
| 24 | +import org.eclipse.jetty.server.handler.DefaultHandler; |
| 25 | +import org.eclipse.jetty.util.component.Environment; |
| 26 | +import org.eclipse.jetty.xml.EnvironmentBuilder; |
| 27 | + |
| 28 | +@SuppressWarnings("unused") |
| 29 | +public class DeployDocs |
| 30 | +{ |
| 31 | + public void simple() throws Exception |
| 32 | + { |
| 33 | + // tag::simple[] |
| 34 | + Server server = new Server(); |
| 35 | + |
| 36 | + // ContextHandlerCollection is required by the deployer. |
| 37 | + ContextHandlerCollection contexts = new ContextHandlerCollection(); |
| 38 | + server.setHandler(contexts); |
| 39 | + |
| 40 | + // Optional, shows the contexts deployed on the Server. |
| 41 | + server.setDefaultHandler(new DefaultHandler()); |
| 42 | + |
| 43 | + // Create the deployer. |
| 44 | + Deployer deployer = new StandardDeployer(contexts); |
| 45 | + |
| 46 | + // Create the DeploymentScanner to monitor the webapps directory. |
| 47 | + DeploymentScanner scanner = new DeploymentScanner(server, deployer); |
| 48 | + scanner.setWebappsDirectories(List.of(Path.of("/path/to/webapps"))); |
| 49 | + // Link the lifecycle of the DeploymentScanner to the Server. |
| 50 | + server.addBean(scanner); |
| 51 | + |
| 52 | + // Create an environment, with the name of your choice, |
| 53 | + // and no extra class-path or module-path. |
| 54 | + EnvironmentBuilder envBuilder = new EnvironmentBuilder("simple"); |
| 55 | + Environment environment = envBuilder.build(); |
| 56 | + |
| 57 | + // Tell the DeploymentScanner about the environment |
| 58 | + // it should use to deploy web applications. |
| 59 | + scanner.configureEnvironment(environment.getName()); |
| 60 | + |
| 61 | + server.start(); |
| 62 | + // end::simple[] |
| 63 | + } |
| 64 | + |
| 65 | + public void jettyStatic() throws Exception |
| 66 | + { |
| 67 | + // tag::static[] |
| 68 | + Server server = new Server(); |
| 69 | + |
| 70 | + // ContextHandlerCollection is required by the deployer. |
| 71 | + ContextHandlerCollection contexts = new ContextHandlerCollection(); |
| 72 | + server.setHandler(contexts); |
| 73 | + |
| 74 | + // Optional, shows the contexts deployed on the Server. |
| 75 | + server.setDefaultHandler(new DefaultHandler()); |
| 76 | + |
| 77 | + // Create the deployer. |
| 78 | + Deployer deployer = new StandardDeployer(contexts); |
| 79 | + |
| 80 | + // Create the DeploymentScanner to monitor the webapps directory. |
| 81 | + DeploymentScanner scanner = new DeploymentScanner(server, deployer); |
| 82 | + scanner.setWebappsDirectories(List.of(Path.of("/path/to/webapps"))); |
| 83 | + // Link the lifecycle of the DeploymentScanner to the Server. |
| 84 | + server.addBean(scanner); |
| 85 | + |
| 86 | + // Create the static environment. |
| 87 | + EnvironmentBuilder envBuilder = new EnvironmentBuilder("static"); |
| 88 | + envBuilder.addClassPath("/path/to/jetty-staticapp-{jetty-version}.jar"); // <1> |
| 89 | + Environment environment = envBuilder.build(); |
| 90 | + |
| 91 | + // Tell the DeploymentScanner about the environment, and configure it for deployment. |
| 92 | + DeploymentScanner.EnvironmentConfig envConfig = scanner.configureEnvironment(environment.getName()); // <2> |
| 93 | + envConfig.setDefaultContextHandlerClassName("org.eclipse.jetty.staticapp.StaticAppContext"); |
| 94 | + |
| 95 | + server.start(); |
| 96 | + // end::static[] |
| 97 | + } |
| 98 | + |
| 99 | + public void jettyCore() throws Exception |
| 100 | + { |
| 101 | + // tag::core[] |
| 102 | + Server server = new Server(); |
| 103 | + |
| 104 | + // ContextHandlerCollection is required by the deployer. |
| 105 | + ContextHandlerCollection contexts = new ContextHandlerCollection(); |
| 106 | + server.setHandler(contexts); |
| 107 | + |
| 108 | + // Optional, shows the contexts deployed on the Server. |
| 109 | + server.setDefaultHandler(new DefaultHandler()); |
| 110 | + |
| 111 | + // Create the deployer. |
| 112 | + Deployer deployer = new StandardDeployer(contexts); |
| 113 | + |
| 114 | + // Create the DeploymentScanner to monitor the webapps directory. |
| 115 | + DeploymentScanner scanner = new DeploymentScanner(server, deployer); |
| 116 | + scanner.setWebappsDirectories(List.of(Path.of("/path/to/webapps"))); |
| 117 | + // Link the lifecycle of the DeploymentScanner to the Server. |
| 118 | + server.addBean(scanner); |
| 119 | + |
| 120 | + // Create the core environment. |
| 121 | + EnvironmentBuilder envBuilder = new EnvironmentBuilder("core"); |
| 122 | + envBuilder.addClassPath("/path/to/jetty-coreapp-{jetty-version}.jar"); // <1> |
| 123 | + Environment environment = envBuilder.build(); |
| 124 | + |
| 125 | + // Tell the DeploymentScanner about the environment, and configure it for deployment. |
| 126 | + DeploymentScanner.EnvironmentConfig envConfig = scanner.configureEnvironment(environment.getName()); // <2> |
| 127 | + envConfig.setDefaultContextHandlerClassName("org.eclipse.jetty.coreapp.CoreAppContext"); |
| 128 | + |
| 129 | + server.start(); |
| 130 | + // end::core[] |
| 131 | + } |
| 132 | + |
| 133 | + public void jakarta() throws Exception |
| 134 | + { |
| 135 | + // tag::jakarta[] |
| 136 | + Server server = new Server(); |
| 137 | + |
| 138 | + // ContextHandlerCollection is required by the deployer. |
| 139 | + ContextHandlerCollection contexts = new ContextHandlerCollection(); |
| 140 | + server.setHandler(contexts); |
| 141 | + |
| 142 | + // Optional, shows the contexts deployed on the Server. |
| 143 | + server.setDefaultHandler(new DefaultHandler()); |
| 144 | + |
| 145 | + // Create the deployer. |
| 146 | + Deployer deployer = new StandardDeployer(contexts); |
| 147 | + |
| 148 | + // Create the DeploymentScanner to monitor the webapps directory. |
| 149 | + DeploymentScanner scanner = new DeploymentScanner(server, deployer); |
| 150 | + scanner.setWebappsDirectories(List.of(Path.of("/path/to/webapps"))); |
| 151 | + // Link the lifecycle of the DeploymentScanner to the Server. |
| 152 | + server.addBean(scanner); |
| 153 | + |
| 154 | + // Create the {ee-current} environment. |
| 155 | + EnvironmentBuilder envBuilder = new EnvironmentBuilder("{ee-current}"); |
| 156 | + envBuilder.addClassPath("/path/to/jakarta.servlet-api-{servlet-current-version}.jar"); // <1> |
| 157 | + envBuilder.addClassPath("/path/to/jetty-{ee-current}-servlet-{jetty-version}.jar"); |
| 158 | + envBuilder.addClassPath("/path/to/jetty-{ee-current}-webapp-{jetty-version}.jar"); |
| 159 | + Environment environment = envBuilder.build(); |
| 160 | + |
| 161 | + // Tell the DeploymentScanner about the environment, and configure it for deployment. |
| 162 | + DeploymentScanner.EnvironmentConfig envConfig = scanner.configureEnvironment(environment.getName()); |
| 163 | + envConfig.setDefaultContextHandlerClassName("org.eclipse.jetty.{ee-current}.webapp.WebAppContext"); // <2> |
| 164 | + envConfig.setDefaultsDescriptor("/path/to/{ee-current}-default-web.xml"); // <3> |
| 165 | + // Other relevant Jakarta environment configurations. |
| 166 | + |
| 167 | + server.start(); |
| 168 | + // end::jakarta[] |
| 169 | + } |
| 170 | + |
| 171 | + public void multi() throws Exception |
| 172 | + { |
| 173 | + // tag::multi[] |
| 174 | + Server server = new Server(); |
| 175 | + |
| 176 | + // ContextHandlerCollection is required by the deployer. |
| 177 | + ContextHandlerCollection contexts = new ContextHandlerCollection(); |
| 178 | + server.setHandler(contexts); |
| 179 | + |
| 180 | + // Optional, shows the contexts deployed on the Server. |
| 181 | + server.setDefaultHandler(new DefaultHandler()); |
| 182 | + |
| 183 | + // Create the deployer. |
| 184 | + Deployer deployer = new StandardDeployer(contexts); |
| 185 | + |
| 186 | + // Create the DeploymentScanner to monitor the webapps directory. |
| 187 | + DeploymentScanner scanner = new DeploymentScanner(server, deployer); |
| 188 | + scanner.setWebappsDirectories(List.of(Path.of("/path/to/webapps"))); |
| 189 | + // Link the lifecycle of the DeploymentScanner to the Server. |
| 190 | + server.addBean(scanner); |
| 191 | + |
| 192 | + // Create the core environment. |
| 193 | + EnvironmentBuilder coreEnvBuilder = new EnvironmentBuilder("core"); |
| 194 | + coreEnvBuilder.addClassPath("/path/to/jetty-coreapp-{jetty-version}.jar"); |
| 195 | + Environment coreEnv = coreEnvBuilder.build(); |
| 196 | + |
| 197 | + // Create the {ee-current} environment. |
| 198 | + EnvironmentBuilder jakartaEnvBuilder = new EnvironmentBuilder("{ee-current}"); |
| 199 | + jakartaEnvBuilder.addClassPath("/path/to/jakarta.servlet-api-{servlet-current-version}.jar"); |
| 200 | + jakartaEnvBuilder.addClassPath("/path/to/jetty-{ee-current}-servlet-{jetty-version}.jar"); |
| 201 | + jakartaEnvBuilder.addClassPath("/path/to/jetty-{ee-current}-webapp-{jetty-version}.jar"); |
| 202 | + Environment jakartaEnv = jakartaEnvBuilder.build(); |
| 203 | + |
| 204 | + // Tell the DeploymentScanner about the environments, and configure them for deployment. |
| 205 | + |
| 206 | + DeploymentScanner.EnvironmentConfig coreEnvConfig = scanner.configureEnvironment(coreEnv.getName()); |
| 207 | + coreEnvConfig.setDefaultContextHandlerClassName("org.eclipse.jetty.coreapp.CoreAppContext"); |
| 208 | + |
| 209 | + DeploymentScanner.EnvironmentConfig jakartaEnvConfig = scanner.configureEnvironment(jakartaEnv.getName()); |
| 210 | + jakartaEnvConfig.setDefaultContextHandlerClassName("org.eclipse.jetty.{ee-current}.webapp.WebAppContext"); |
| 211 | + jakartaEnvConfig.setDefaultsDescriptor("/path/to/{ee-current}-default-web.xml"); |
| 212 | + // Other relevant Jakarta environment configurations. |
| 213 | + |
| 214 | + server.start(); |
| 215 | + // end::multi[] |
| 216 | + } |
| 217 | +} |
0 commit comments