use of cloud.piranha.extension.annotationscan.internal.InternalAnnotationScanAnnotationManager in project piranha by piranhacloud.
the class MicroInnerDeployer method start.
/**
* Start the application.
*
* @param applicationArchive the application archive.
* @param classLoader the classloader.
* @param handlers the handlers.
* @param config the configuration.
* @return the map.
*/
public Map<String, Object> start(Archive<?> applicationArchive, ClassLoader classLoader, Map<String, Function<URL, URLConnection>> handlers, Map<String, Object> config) {
try {
WebApplication webApplication = getWebApplication(applicationArchive, classLoader);
LOGGER.log(INFO, "Starting web application " + applicationArchive.getName() + " on Piranha Micro " + webApplication.getAttribute(MICRO_PIRANHA));
// The global archive stream handler is set to resolve "shrinkwrap://" URLs (created from strings).
// Such URLs come into being primarily when code takes resolves a class or resource from the class loader by URL
// and then takes the string form of the URL representing the class or resource.
GlobalArchiveStreamHandler streamHandler = new GlobalArchiveStreamHandler(webApplication);
// Life map to the StaticURLStreamHandlerFactory used by the root class loader
handlers.put("shrinkwrap", streamHandler::connect);
// Source of annotations
Index index = getIndex();
// Target of annotations
AnnotationManager annotationManager = new InternalAnnotationScanAnnotationManager();
webApplication.getManager().setAnnotationManager(annotationManager);
// Copy annotations from our "annotations" collection from source index to target manager
forEachWebAnnotation(webAnnotation -> addAnnotationToIndex(index, webAnnotation, annotationManager));
// Collect sub-classes/interfaces of our "instances" collection from source index to target manager
forEachInstance(instanceClass -> addInstanceToIndex(index, instanceClass, annotationManager));
// Collect any sub-classes/interfaces from any HandlesTypes annotation
getAnnotations(index, HandlesTypes.class).map(this::getTarget).forEach(annotationTarget -> getAnnotationInstances(annotationTarget, HandlesTypes.class).map(HandlesTypes.class::cast).forEach(handlesTypesInstance -> stream(handlesTypesInstance.value()).forEach(e -> {
if (e.isAnnotation()) {
addAnnotationToIndex(index, e, annotationManager);
} else {
addInstanceToIndex(index, e, annotationManager);
}
})));
// Setup the default identity store, which is used as the default "username and roles database" for
// (Servlet) security.
initIdentityStore(webApplication);
setApplicationContextPath(webApplication, config, applicationArchive);
DefaultWebApplicationExtensionContext extensionContext = new DefaultWebApplicationExtensionContext();
for (WebApplicationExtension extension : ServiceLoader.load(WebApplicationExtension.class)) {
extensionContext.add(extension);
}
extensionContext.configure(webApplication);
webApplication.initialize();
webApplication.start();
if ((boolean) config.get("micro.http.start")) {
HttpWebApplicationServer webApplicationServer = new HttpWebApplicationServer();
webApplicationServer.addWebApplication(webApplication);
ServiceLoader<HttpServer> httpServers = ServiceLoader.load(HttpServer.class);
httpServer = httpServers.findFirst().orElseThrow();
httpServer.setServerPort((Integer) config.get("micro.port"));
httpServer.setSSL(Boolean.getBoolean("piranha.http.ssl"));
httpServer.setHttpServerProcessor(webApplicationServer);
httpServer.start();
}
return Map.of("deployedServlets", webApplication.getServletRegistrations().keySet(), "deployedApplication", new MicroInnerApplication(webApplication), "deployedContextRoot", webApplication.getContextPath());
} catch (IOException e) {
throw new IllegalStateException(e);
} catch (Exception e) {
throw e;
}
}
use of cloud.piranha.extension.annotationscan.internal.InternalAnnotationScanAnnotationManager in project piranha by piranhacloud.
the class ServletAnnotationsExtensionTest method testOnStartup7.
/**
* Test onStartup method.
*
* @throws Exception when a serious error occurs.
*/
@Test
void testOnStartup7() throws Exception {
DefaultWebApplication webApplication = new DefaultWebApplication();
webApplication.getManager().setAnnotationManager(new InternalAnnotationScanAnnotationManager());
webApplication.addInitializer(new AnnotationScanInitializer());
DefaultWebApplicationClassLoader classLoader = new DefaultWebApplicationClassLoader(new File(""));
classLoader.getResourceManager().addResource(new ClassResource(Test3Filter.class.getName()));
webApplication.setClassLoader(classLoader);
DefaultWebApplicationExtensionContext context = new DefaultWebApplicationExtensionContext();
context.add(ServletAnnotationsExtension.class);
context.configure(webApplication);
webApplication.initialize();
FilterRegistration registration = webApplication.getFilterRegistration("Test3Filter");
assertNotNull(registration);
FilterEnvironment filterEnvironment = (FilterEnvironment) registration;
assertEquals("value", filterEnvironment.getInitParameter("name"));
}
use of cloud.piranha.extension.annotationscan.internal.InternalAnnotationScanAnnotationManager in project piranha by piranhacloud.
the class ServletAnnotationsExtensionTest method testOnStartup3.
/**
* Test onStartup method.
*
* @throws Exception when a serious error occurs.
*/
@Test
void testOnStartup3() throws Exception {
DefaultWebApplication webApplication = new DefaultWebApplication();
webApplication.getManager().setAnnotationManager(new InternalAnnotationScanAnnotationManager());
webApplication.addInitializer(new AnnotationScanInitializer());
DefaultWebApplicationClassLoader classLoader = new DefaultWebApplicationClassLoader(new File(""));
classLoader.getResourceManager().addResource(new ClassResource(Test3Servlet.class.getName()));
webApplication.setClassLoader(classLoader);
DefaultWebApplicationExtensionContext context = new DefaultWebApplicationExtensionContext();
context.add(ServletAnnotationsExtension.class);
context.configure(webApplication);
webApplication.initialize();
ServletRegistration registration = webApplication.getServletRegistration("Test3Servlet");
assertNotNull(registration);
assertFalse(registration.getMappings().isEmpty());
}
use of cloud.piranha.extension.annotationscan.internal.InternalAnnotationScanAnnotationManager in project piranha by piranhacloud.
the class ServletAnnotationsExtensionTest method testOnStartup5.
/**
* Test onStartup method.
*
* @throws Exception when a serious error occurs.
*/
@Test
void testOnStartup5() throws Exception {
DefaultWebApplication webApplication = new DefaultWebApplication();
webApplication.getManager().setAnnotationManager(new InternalAnnotationScanAnnotationManager());
webApplication.addInitializer(new AnnotationScanInitializer());
DefaultWebApplicationClassLoader classLoader = new DefaultWebApplicationClassLoader(new File(""));
classLoader.getResourceManager().addResource(new ClassResource(TestFilter.class.getName()));
webApplication.setClassLoader(classLoader);
DefaultWebApplicationExtensionContext context = new DefaultWebApplicationExtensionContext();
context.add(ServletAnnotationsExtension.class);
context.configure(webApplication);
webApplication.initialize();
FilterRegistration registration = webApplication.getFilterRegistration("TestFilter");
assertNotNull(registration);
}
use of cloud.piranha.extension.annotationscan.internal.InternalAnnotationScanAnnotationManager in project piranha by piranhacloud.
the class ServletAnnotationsExtensionTest method testOnStartup4.
/**
* Test onStartup method.
*
* @throws Exception when a serious error occurs.
*/
@Test
void testOnStartup4() throws Exception {
DefaultWebApplication webApplication = new DefaultWebApplication();
webApplication.getManager().setAnnotationManager(new InternalAnnotationScanAnnotationManager());
webApplication.addInitializer(new AnnotationScanInitializer());
DefaultWebApplicationClassLoader classLoader = new DefaultWebApplicationClassLoader(new File(""));
classLoader.getResourceManager().addResource(new ClassResource(Test4Servlet.class.getName()));
webApplication.setClassLoader(classLoader);
DefaultWebApplicationExtensionContext context = new DefaultWebApplicationExtensionContext();
context.add(ServletAnnotationsExtension.class);
context.configure(webApplication);
webApplication.initialize();
ServletRegistration registration = webApplication.getServletRegistration("Test4Servlet");
assertNotNull(registration);
ServletEnvironment servletEnvironment = (ServletEnvironment) registration;
assertEquals("value", servletEnvironment.getInitParameter("name"));
}
Aggregations