@SuppressWarnings("unchecked")
  @Override
  public <T> T getEndpointInstance(Class<T> endpointClass) throws InstantiationException {
    // this is servlet container application context
    WebApplicationContext wac = ContextLoader.getCurrentWebApplicationContext();

    if (wac == null) {
      String message =
          "Failed to find the root WebApplicationContext. Was ContextLoaderListener not used?";
      logger.error(message);
      throw new IllegalStateException(message);
    }

    // obtain spring application context
    WebApplicationContext springWac =
        WebApplicationContextUtils.getWebApplicationContext(
            wac.getServletContext(), FrameworkServlet.SERVLET_CONTEXT_PREFIX + "appServlet");

    String beanName = ClassUtils.getShortNameAsProperty(endpointClass);
    if (springWac.containsBean(beanName)) {
      T endpoint = springWac.getBean(beanName, endpointClass);
      if (logger.isTraceEnabled()) {
        logger.trace("Using @ServerEndpoint singleton " + endpoint);
      }
      return endpoint;
    }

    Component annot = AnnotationUtils.findAnnotation(endpointClass, Component.class);
    if ((annot != null) && springWac.containsBean(annot.value())) {
      T endpoint = springWac.getBean(annot.value(), endpointClass);
      if (logger.isTraceEnabled()) {
        logger.trace("Using @ServerEndpoint singleton " + endpoint);
      }
      return endpoint;
    }

    beanName = getBeanNameByType(springWac, endpointClass);
    if (beanName != null) {
      return (T) springWac.getBean(beanName);
    }

    if (logger.isTraceEnabled()) {
      logger.trace("Creating new @ServerEndpoint instance of type " + endpointClass);
    }
    return springWac.getAutowireCapableBeanFactory().createBean(endpointClass);
  }
示例#2
0
  public static WebApplicationContext configureWebApplicationContext(
      ServletContext servletContext, WebApplicationContext parent) {
    ServletContextHolder.setServletContext(servletContext);
    GrailsApplication application =
        (GrailsApplication) parent.getBean(GrailsApplication.APPLICATION_ID);

    if (LOG.isDebugEnabled()) {
      LOG.debug("[GrailsContextLoader] Configuring Grails Application");
    }

    if (application.getParentContext() == null) {
      application.setApplicationContext(parent);
    }

    GrailsRuntimeConfigurator configurator = null;
    if (parent.containsBean(GrailsRuntimeConfigurator.BEAN_ID)) {
      // get configurator from parent application context
      configurator = (GrailsRuntimeConfigurator) parent.getBean(GrailsRuntimeConfigurator.BEAN_ID);
    } else {
      // get configurator from servlet context
      configurator =
          determineGrailsRuntimeConfiguratorFromServletContext(application, servletContext, parent);
    }

    if (configurator == null) {
      // no configurator, use default
      configurator = new GrailsRuntimeConfigurator(application, parent);
      if (parent.containsBean(GrailsPluginManager.BEAN_NAME)) {
        GrailsPluginManager pluginManager =
            (GrailsPluginManager) parent.getBean(GrailsPluginManager.BEAN_NAME);
        configurator.setPluginManager(pluginManager);
      }
    }

    final GrailsPluginManager pluginManager = configurator.getPluginManager();

    // return a context that obeys grails' settings
    WebApplicationContext webContext = configurator.configure(servletContext);
    pluginManager.setApplicationContext(webContext);

    configureServletContextAttributes(servletContext, application, pluginManager, webContext);
    LOG.info("[GrailsContextLoader] Grails application loaded.");
    return webContext;
  }
  @Override
  public RequestPostProcessor beforeMockMvcCreated(
      ConfigurableMockMvcBuilder<?> builder, WebApplicationContext context) {
    String securityBeanId = BeanIds.SPRING_SECURITY_FILTER_CHAIN;
    if (springSecurityFilterChain == null && context.containsBean(securityBeanId)) {
      springSecurityFilterChain = context.getBean(securityBeanId, Filter.class);
    }

    if (springSecurityFilterChain == null) {
      throw new IllegalStateException(
          "springSecurityFilterChain cannot be null. Ensure a Bean with the name "
              + securityBeanId
              + " implementing Filter is present or inject the Filter to be used.");
    }

    builder.addFilters(springSecurityFilterChain);

    return testSecurityContext();
  }
 private void initMainController(WebApplicationContext webContext) {
   if (webContext.containsBean(MAIN_SIMPLE_CONTROLLER)) {
     this.mainController = (Controller) webContext.getBean(MAIN_SIMPLE_CONTROLLER);
   }
 }
示例#5
0
  @Override
  public void create(HttpServletRequest request, ModelMap modelMap, Object command)
      throws Exception {

    // AtchFileServiceImpl AtchFileServiceImpl = new AtchFileServiceImpl();
    // AtchFileServiceImpl.create(request, modelMap, command);

    // 1.기본 파람 속성 설정
    this.setDefaultProperty(request, command);
    String layoutNm = (String) request.getAttribute("layoutNm");
    String progrmNm = (String) request.getAttribute("progrmNm");
    // 2.첨부파일
    // atchFile(request, response, command);

    String viewNm = null;
    try {
      // 3.리턴 메세지 설정
      request.getSession().setAttribute("RETURN_MSG", "success.common.insert");

      // 1.idGnrService 구현체 객체를 구한다.
      if (context.containsBean(progrmNm + "IdGnrService")) {
        EgovIdGnrService idGnrService =
            (EgovIdGnrService) context.getBean(progrmNm + "IdGnrService");
        // 3.IDS를 이용 유니크 아이디를 구한다.
        String nextId = idGnrService.getNextStringId();
        // 4.Insert에 필요한 유니크 아이디를 구한후 커맨드 객체에 저장한다.itemId 는 defaultVo에 선언 되어있다.
        BeanUtils.setProperty(command, "itemId", nextId);
      } else {
        super.log.info("customFramework Message:'" + progrmNm + "' 해당 IDS 사용 안함.");
      }
      // viewNm 값은 executeCreate 이전에 설정하는 이유는 executeCreate에서 제정의 할 경우를 대비하여 이전에 설정해 놓는다.
      viewNm =
          "redirect:"
              + (layoutNm == "default" ? "" : "/" + layoutNm)
              + "/"
              + progrmNm
              + "List.do?searchUse";
      BeanUtils.setProperty(command, "viewNm", viewNm);
      // 4.create 로직 실행
      this.executeCreate(request, modelMap, command); // 생성 실행
    } catch (DuplicateKeyException e) { // 중복데이타 쿼리 오류 처리 - 등록 폼으로 이동.
      // 1.viewNm 설정
      viewNm = layoutNm + "/" + progrmNm + "/" + progrmNm + "Form";
      BeanUtils.setProperty(command, "viewNm", viewNm);
      // 2.레퍼런스데이타 메소드 호출
      this.referenceData(request, modelMap, command);
      // 3.리턴 메세지 설정
      request.getSession().setAttribute("RETURN_MSG", "중복코드 오류 입니다.");
      // 4.폼 플레그 설정
      modelMap.put("registerFlag", "CREATE");
    } catch (DataIntegrityViolationException e) { // DB 릴레이션 오류
      // 1.viewNm 설정
      viewNm = layoutNm + "/" + progrmNm + "/" + progrmNm + "Form";
      BeanUtils.setProperty(command, "viewNm", viewNm);
      // 2.레퍼런스데이타 메소드 호출
      this.referenceData(request, modelMap, command);
      // 3.리턴 메세지 설정
      request.getSession().setAttribute("RETURN_MSG", "DB 릴레이션 오류가 발생하였습니다.");
      // 4.폼 플레그 설정
      modelMap.put("registerFlag", "CREATE");
    }
  }