use of cloud.piranha.core.impl.DefaultWebApplication in project piranha by piranhacloud.
the class MicroInnerDeployer method getWebApplication.
WebApplication getWebApplication(Archive<?> archive, ClassLoader newClassLoader) {
WebApplication webApplication = new DefaultWebApplication();
webApplication.setClassLoader(newClassLoader);
// The main resource representing the (war) archive itself.
webApplication.addResource(new ShrinkWrapResource(archive));
// Get the list of embedded archives containing a "/META-INF/resources" folder.
Node resourceNodes = archive.get("/META-INF/piranha/resource-libs");
if (resourceNodes != null) {
for (Node resourceNode : resourceNodes.getChildren()) {
ArchiveAsset resourceArchiveAsset = (ArchiveAsset) resourceNode.getAsset();
// Add the archive as a resource with the "/META-INF/resources" folder shifted to its root
webApplication.addResource(new ShrinkWrapResource("/META-INF/resources", resourceArchiveAsset.getArchive()));
}
}
return webApplication;
}
use of cloud.piranha.core.impl.DefaultWebApplication in project piranha by piranhacloud.
the class MicroPiranha method run.
/**
* Run method.
*/
@Override
public void run() {
long startTime = System.currentTimeMillis();
LOGGER.log(INFO, () -> "Starting Piranha");
webApplicationServer = new HttpWebApplicationServer();
if (httpPort > 0) {
HttpServer httpServer = ServiceLoader.load(HttpServer.class).findFirst().orElseThrow();
httpServer.setServerPort(httpPort);
httpServer.setHttpServerProcessor(webApplicationServer);
httpServer.start();
}
if (httpsPort > 0) {
HttpServer httpsServer = ServiceLoader.load(HttpServer.class).findFirst().orElseThrow();
httpsServer.setHttpServerProcessor(webApplicationServer);
httpsServer.setServerPort(httpsPort);
httpsServer.setSSL(true);
httpsServer.start();
}
String contextPath = null;
if (warFile != null && warFile.getName().toLowerCase().endsWith(".war")) {
contextPath = warFile.getName().substring(0, warFile.getName().length() - 4);
if (webAppDir == null) {
webAppDir = new File(contextPath);
}
extractWarFile(warFile, webAppDir);
}
if (webAppDir != null && webAppDir.exists()) {
if (contextPath == null) {
contextPath = webAppDir.getName();
}
DefaultWebApplication webApplication = new DefaultWebApplication();
webApplication.addResource(new DirectoryResource(webAppDir));
DefaultWebApplicationClassLoader classLoader = new DefaultWebApplicationClassLoader(webAppDir);
webApplication.setClassLoader(classLoader);
if (Boolean.getBoolean("cloud.piranha.modular.enable") || jpmsEnabled) {
setupLayers(classLoader);
}
if (classLoader.getResource("/META-INF/services/" + WebApplicationExtension.class.getName()) == null) {
DefaultWebApplicationExtensionContext extensionContext = new DefaultWebApplicationExtensionContext();
extensionContext.add(extensionClass);
extensionContext.configure(webApplication);
} else {
DefaultWebApplicationExtensionContext extensionContext = new DefaultWebApplicationExtensionContext();
ServiceLoader<WebApplicationExtension> serviceLoader = ServiceLoader.load(WebApplicationExtension.class, classLoader);
extensionContext.add(serviceLoader.iterator().next());
extensionContext.configure(webApplication);
}
if (contextPath.equalsIgnoreCase("ROOT")) {
contextPath = "";
} else if (!contextPath.startsWith("/")) {
contextPath = "/" + contextPath;
}
webApplication.setContextPath(contextPath);
webApplicationServer.addWebApplication(webApplication);
try {
webApplication.initialize();
} catch (Exception e) {
LOGGER.log(ERROR, "Failed to initialize web application");
System.exit(0);
}
}
if (webAppDir == null && warFile == null) {
LOGGER.log(WARNING, "No web application deployed");
}
webApplicationServer.start();
long finishTime = System.currentTimeMillis();
LOGGER.log(INFO, "Started Piranha");
LOGGER.log(INFO, "It took {0} milliseconds", finishTime - startTime);
if (pid != null) {
File pidFile = new File("tmp", "piranha-micro.pid");
if (!pidFile.getParentFile().exists()) {
if (!pidFile.getParentFile().mkdirs()) {
LOGGER.log(WARNING, "Unable to create tmp directory for PID file");
}
}
try (PrintWriter writer = new PrintWriter(new FileWriter(pidFile))) {
writer.println(pid);
writer.flush();
} catch (IOException ioe) {
LOGGER.log(WARNING, "Unable to write PID file", ioe);
}
}
while (!stop) {
if (pid != null) {
File pidFile = new File("tmp", "piranha-micro.pid");
if (!pidFile.exists()) {
stop();
}
}
try {
Thread.sleep(2000);
} catch (InterruptedException ie) {
Thread.currentThread().interrupt();
}
}
}
use of cloud.piranha.core.impl.DefaultWebApplication in project piranha by piranhacloud.
the class NanoPiranhaBuilder method build.
/**
* Build Piranha Nano.
*
* @return our instance of Piranha Nano.
*/
public NanoPiranha build() {
NanoPiranha piranha = new NanoPiranha();
if (webApplication == null) {
webApplication = new DefaultWebApplication();
}
piranha.setWebApplication(webApplication);
resources.forEach(resource -> webApplication.addResource(resource));
filters.entrySet().forEach(entry -> {
String filterName = entry.getKey();
Filter filter = entry.getValue();
NanoFilterConfig filterConfig = new NanoFilterConfig(piranha.getWebApplication());
filterConfig.setFilterName(filterName);
Map<String, String> initParameters = filterInitParameters.get(filterName);
if (initParameters != null) {
initParameters.entrySet().forEach(initParameter -> {
String name = initParameter.getKey();
String value = initParameter.getValue();
filterConfig.setInitParameter(name, value);
});
}
try {
filter.init(filterConfig);
} catch (ServletException se) {
throw new RuntimeException("Unable to initialize filter", se);
}
piranha.addFilter(filter);
});
if (servlet != null) {
NanoServletConfig servletConfig = new NanoServletConfig(piranha.getWebApplication());
servletConfig.setServletName(servletName);
servletInitParameters.entrySet().forEach(entry -> {
String name = entry.getKey();
String value = entry.getValue();
servletConfig.setInitParameter(name, value);
});
try {
servlet.init(servletConfig);
} catch (ServletException se) {
throw new RuntimeException("Unable to initialize servlet", se);
}
piranha.setServlet(servlet);
}
return piranha;
}
use of cloud.piranha.core.impl.DefaultWebApplication in project piranha by piranhacloud.
the class NanoRequestTest method testLogout.
/**
* Test logout method.
*/
@Test
void testLogout() {
try {
DefaultWebApplication webApplication = new DefaultWebApplication();
NanoResponse response = new NanoResponse();
response.setWebApplication(webApplication);
NanoRequest request = new NanoRequest();
request.setWebApplication(webApplication);
webApplication.linkRequestAndResponse(request, response);
request.logout();
} catch (ServletException ex) {
fail();
}
}
use of cloud.piranha.core.impl.DefaultWebApplication in project piranha by piranhacloud.
the class NanoRequestTest method testChangeSessionId.
/**
* Test changeSessionId method.
*/
@Test
void testChangeSessionId() {
DefaultWebApplication webApplication = new DefaultWebApplication();
NanoResponse response = new NanoResponse();
response.setWebApplication(webApplication);
NanoRequest request = new NanoRequest();
request.setWebApplication(webApplication);
webApplication.linkRequestAndResponse(request, response);
String sessionId1 = request.getSession(true).getId();
String sessionId2 = request.changeSessionId();
webApplication.unlinkRequestAndResponse(request, response);
assertNotEquals(sessionId1, sessionId2);
}
Aggregations