Search in sources :

Example 1 with Singleton

use of org.atmosphere.config.service.Singleton in project atmosphere by Atmosphere.

the class AtmosphereHandlerServiceInterceptor method mapAnnotatedService.

protected void mapAnnotatedService(boolean reMap, String path, AtmosphereRequest request, AtmosphereHandlerWrapper w) {
    synchronized (config.handlers()) {
        if (config.handlers().get(path) == null) {
            // AtmosphereHandlerService
            AtmosphereHandlerService m = w.atmosphereHandler.getClass().getAnnotation(AtmosphereHandlerService.class);
            if (m != null) {
                try {
                    String targetPath = m.path();
                    if (targetPath.contains("{") && targetPath.contains("}")) {
                        boolean singleton = w.atmosphereHandler.getClass().getAnnotation(Singleton.class) != null;
                        AtmosphereHandler newW = w.atmosphereHandler;
                        if (!singleton) {
                            newW = config.framework().newClassInstance(AtmosphereHandler.class, w.atmosphereHandler.getClass());
                        }
                        request.localAttributes().put(Named.class.getName(), path.substring(targetPath.indexOf("{")));
                        ((AtmosphereResourceImpl) request.resource()).atmosphereHandler(newW);
                        config.framework().addAtmosphereHandler(path, newW, config.getBroadcasterFactory().lookup(w.broadcaster.getClass(), path, true), w.interceptors);
                        request.setAttribute(FrameworkConfig.NEW_MAPPING, "true");
                    }
                } catch (Throwable e) {
                    logger.warn("Unable to create AtmosphereHandler", e);
                }
            }
        }
    }
}
Also used : AtmosphereHandlerService(org.atmosphere.config.service.AtmosphereHandlerService) Named(jakarta.inject.Named) AtmosphereHandler(org.atmosphere.cpr.AtmosphereHandler) Singleton(org.atmosphere.config.service.Singleton) AtmosphereResourceImpl(org.atmosphere.cpr.AtmosphereResourceImpl)

Example 2 with Singleton

use of org.atmosphere.config.service.Singleton in project atmosphere by Atmosphere.

the class MeteorServiceInterceptor method mapAnnotatedService.

protected void mapAnnotatedService(boolean reMap, String path, AtmosphereRequest request, AtmosphereFramework.AtmosphereHandlerWrapper w) {
    synchronized (config.handlers()) {
        if (config.handlers().get(path) == null) {
            // MeteorService
            if (ReflectorServletProcessor.class.isAssignableFrom(w.atmosphereHandler.getClass())) {
                ReflectorServletProcessor r = (ReflectorServletProcessor) w.atmosphereHandler;
                Servlet s = r.getServlet();
                if (s == null) {
                    logger.warn("Invalid ReflectorServletProcessor state. Servlet is null");
                    return;
                }
                MeteorService m = s.getClass().getAnnotation(MeteorService.class);
                if (m != null) {
                    String targetPath = m.path();
                    if (targetPath.contains("{") && targetPath.contains("}")) {
                        try {
                            boolean singleton = s.getClass().getAnnotation(Singleton.class) != null;
                            if (!singleton) {
                                r = config.framework().newClassInstance(ReflectorServletProcessor.class, ReflectorServletProcessor.class);
                                r.setServlet(config.framework().newClassInstance(Servlet.class, s.getClass()));
                                r.init(config);
                            }
                            request.localAttributes().put(Named.class.getName(), path.substring(targetPath.indexOf("{")));
                            ((AtmosphereResourceImpl) request.resource()).atmosphereHandler(r);
                            config.framework().addAtmosphereHandler(path, r, config.getBroadcasterFactory().lookup(w.broadcaster.getClass(), path, true), w.interceptors);
                            request.setAttribute(FrameworkConfig.NEW_MAPPING, "true");
                        } catch (Throwable e) {
                            logger.warn("Unable to create AtmosphereHandler", e);
                        }
                    }
                }
            }
        } else if (reMap) {
            request.setAttribute(FrameworkConfig.NEW_MAPPING, "true");
        }
    }
}
Also used : MeteorService(org.atmosphere.config.service.MeteorService) Named(jakarta.inject.Named) Singleton(org.atmosphere.config.service.Singleton) Servlet(jakarta.servlet.Servlet) ReflectorServletProcessor(org.atmosphere.handler.ReflectorServletProcessor) AtmosphereResourceImpl(org.atmosphere.cpr.AtmosphereResourceImpl)

Example 3 with Singleton

use of org.atmosphere.config.service.Singleton in project atmosphere by Atmosphere.

the class DefaultWebSocketProcessor method postProcessMapping.

protected WebSocketHandler postProcessMapping(WebSocket webSocket, AtmosphereRequest request, WebSocketHandlerProxy w) {
    WebSocketHandlerProxy p = null;
    String path = w.path();
    if (wildcardMapping()) {
        String pathInfo = null;
        try {
            pathInfo = request.getPathInfo();
        } catch (IllegalStateException ex) {
        // http://java.net/jira/browse/GRIZZLY-1301
        }
        if (pathInfo != null) {
            path = request.getServletPath() + pathInfo;
        } else {
            path = request.getServletPath();
        }
        if (path == null || path.isEmpty()) {
            path = "/";
        }
        synchronized (handlers) {
            p = handlers.get(path);
            if (p == null) {
                // AtmosphereHandlerService
                WebSocketHandlerService a = w.proxied.getClass().getAnnotation(WebSocketHandlerService.class);
                if (a != null) {
                    String targetPath = a.path();
                    if (targetPath.indexOf("{") != -1 && targetPath.indexOf("}") != -1) {
                        try {
                            boolean singleton = w.proxied.getClass().getAnnotation(Singleton.class) != null;
                            if (!singleton) {
                                w = new WebSocketHandlerProxy(a.broadcaster(), framework.newClassInstance(WebSocketHandler.class, w.proxied.getClass()));
                            }
                            registerWebSocketHandler(path, new WebSocketHandlerProxy(a.broadcaster(), w));
                            request.localAttributes().put(Named.class.getName(), path.substring(targetPath.indexOf("{")));
                            p = handlers.get(path);
                        } catch (Throwable e) {
                            logger.warn("Unable to create WebSocketHandler", e);
                        }
                    }
                }
            }
        }
    }
    try {
        webSocket.resource().setBroadcaster(AnnotationUtil.broadcaster(framework, p != null ? p.broadcasterClazz : w.broadcasterClazz, path));
    } catch (Exception e) {
        logger.error("", e);
    }
    return p != null ? p : w;
}
Also used : Named(jakarta.inject.Named) WebSocketHandlerService(org.atmosphere.config.service.WebSocketHandlerService) Singleton(org.atmosphere.config.service.Singleton) ServletException(jakarta.servlet.ServletException) AtmosphereMappingException(org.atmosphere.cpr.AtmosphereMappingException) IOException(java.io.IOException)

Example 4 with Singleton

use of org.atmosphere.config.service.Singleton in project atmosphere by Atmosphere.

the class ManagedServiceInterceptor method mapAnnotatedService.

protected void mapAnnotatedService(boolean reMap, String path, AtmosphereRequest request, AtmosphereFramework.AtmosphereHandlerWrapper w) {
    synchronized (config.handlers()) {
        if (config.handlers().get(path) == null) {
            // ManagedService
            if (AnnotatedProxy.class.isAssignableFrom(w.atmosphereHandler.getClass())) {
                AnnotatedProxy ap = (AnnotatedProxy) w.atmosphereHandler;
                ManagedAnnotation a = managed(ap, request.resource());
                if (a != null) {
                    String targetPath = a.path();
                    if (targetPath.contains("{") && targetPath.contains("}")) {
                        try {
                            boolean singleton = ap.target().getClass().getAnnotation(Singleton.class) != null;
                            if (!singleton) {
                                ap = proxyHandler();
                                final Object o = config.framework().newClassInstance(Object.class, AnnotatedProxy.class.cast(w.atmosphereHandler).target().getClass());
                                ap.configure(config, o);
                            }
                            request.localAttributes().put(Named.class.getName(), path.substring(targetPath.indexOf("{")));
                            if (ap.pathParams()) {
                                request.localAttributes().put(PathParam.class.getName(), new String[] { path, targetPath });
                            }
                            ((AtmosphereResourceImpl) request.resource()).atmosphereHandler(ap);
                            config.framework().addAtmosphereHandler(path, ap, config.getBroadcasterFactory().lookup(a.broadcaster(), path, true), w.interceptors);
                            request.setAttribute(FrameworkConfig.NEW_MAPPING, "true");
                        } catch (Throwable e) {
                            logger.warn("Unable to create AtmosphereHandler", e);
                        }
                    }
                }
            }
        } else if (reMap) {
            request.setAttribute(FrameworkConfig.NEW_MAPPING, "true");
        }
    }
}
Also used : Named(jakarta.inject.Named) AnnotatedProxy(org.atmosphere.handler.AnnotatedProxy) Singleton(org.atmosphere.config.service.Singleton) AtmosphereResourceImpl(org.atmosphere.cpr.AtmosphereResourceImpl) PathParam(org.atmosphere.config.service.PathParam)

Aggregations

Named (jakarta.inject.Named)4 Singleton (org.atmosphere.config.service.Singleton)4 AtmosphereResourceImpl (org.atmosphere.cpr.AtmosphereResourceImpl)3 Servlet (jakarta.servlet.Servlet)1 ServletException (jakarta.servlet.ServletException)1 IOException (java.io.IOException)1 AtmosphereHandlerService (org.atmosphere.config.service.AtmosphereHandlerService)1 MeteorService (org.atmosphere.config.service.MeteorService)1 PathParam (org.atmosphere.config.service.PathParam)1 WebSocketHandlerService (org.atmosphere.config.service.WebSocketHandlerService)1 AtmosphereHandler (org.atmosphere.cpr.AtmosphereHandler)1 AtmosphereMappingException (org.atmosphere.cpr.AtmosphereMappingException)1 AnnotatedProxy (org.atmosphere.handler.AnnotatedProxy)1 ReflectorServletProcessor (org.atmosphere.handler.ReflectorServletProcessor)1