title: JSF custom scope - missing javascript
body: <p>I would like to configure custom scope in JSF application. We are using JSF 2.0 with Primefaces 5.3.17 on Websphere 8.0/8.5. For now we have RequestScoped Core- and SessionScoped Model-Beans with Named annotations. Model is passed to Core via Inject. Now we need to provide a way to parallel work with models on many browser tabs. Our idea is to use filters to inject generated tab id into response and then, later on, extract it from post requests and pass it in to Extension to load/create required bean from map with beans per view with is stored in session. I suppose that filters work fine - without scope change the generated tab Id is injected in action attribute in forms etc. and there is no error.</p>
  
  <p>But the problem is that when i set the new custom scope for some model bean to prove if it works. I got an exception that "jquery is not defined". For me Java part looks fine - required bean is created in context and then the same bean is used in core. But it turned out that every request which is responsible for loading js get empty response. I don't see any stacktrace and I have totally no idea why it happens, how and where can I find cause of that behavior.</p>
  
  <p>Some code samples:</p>
  
  <p>Context:</p>
  
  <pre><code>public class TabScopeContext implements Context, Serializable {
  
      /**
       * SerialId.
       */
      private static final long serialVersionUID = -558257182139134232L;
  
      public TabScopeContext() {}
  
      /**
       * {@inheritDoc}
       */
      @SuppressWarnings("unchecked")
      @Override
      public &lt;T&gt; T get(Contextual&lt;T&gt; contextual) {
          final Bean&lt;T&gt; bean = (Bean&lt;T&gt;) contextual;
          Map&lt;String, Object&gt; beans = getBeansForCurrentTab();
          if (beans.containsKey(bean.getName())) {
              return (T) beans.get(bean.getName());
          } else {
              return null;
          }
      }
  
      private Map&lt;String, Object&gt; getBeansForCurrentTab() {
          FacesContext context = FacesContext.getCurrentInstance();
          HttpSession session = (HttpSession) context.getExternalContext().getSession(false);
          Map&lt;Integer, Map&lt;String, Object&gt;&gt; tabScope;
  
          if (isTabScopeAlreadyInSession(session)) {
              tabScope = getTabScope(session);
          } else {
              tabScope = initScopeInSession(session);
          }
  
          Integer viewId = TabScopeViewIdPersister.getInstance().getViewId();
  
          if (isViewAlreadyInScope(viewId, tabScope)) {
              return tabScope.get(viewId);
          } else {
              return initBeansForView(tabScope, viewId);
          }
  
      }
  
      private Map&lt;String, Object&gt; initBeansForView(Map&lt;Integer, Map&lt;String, Object&gt;&gt; tabScope, Integer viewId) {
          Map&lt;String, Object&gt; beans = new HashMap&lt;String, Object&gt;();
          tabScope.put(viewId, beans);
  
          return beans;
      }
  
      private boolean isViewAlreadyInScope(Integer viewId, Map&lt;Integer, Map&lt;String, Object&gt;&gt; tabScope) {
          return tabScope.containsKey(viewId);
      }
  
      private Map&lt;Integer, Map&lt;String, Object&gt;&gt; initScopeInSession(HttpSession session) {
          Map&lt;Integer, Map&lt;String, Object&gt;&gt; beansPerView = new HashMap&lt;Integer, Map&lt;String, Object&gt;&gt;();
          session.setAttribute(TabScope.NAME, beansPerView);
  
          return beansPerView;
      }
  
      private boolean isTabScopeAlreadyInSession(HttpSession session) {
          return getTabScope(session) != null;
      }
  
      @SuppressWarnings("unchecked")
      private Map&lt;Integer, Map&lt;String, Object&gt;&gt; getTabScope(HttpSession session) {
          return (Map&lt;Integer, Map&lt;String, Object&gt;&gt;) session.getAttribute(TabScope.NAME);
      }
  
      /**
       * {@inheritDoc}
       */
      @SuppressWarnings("unchecked")
      @Override
      public &lt;T&gt; T get(Contextual&lt;T&gt; contextual, CreationalContext&lt;T&gt; creationalContext) {
          final Bean&lt;T&gt; bean = (Bean&lt;T&gt;) contextual;
  
          Map&lt;String, Object&gt; scopeBeansMap = getBeansForCurrentTab();
          if (scopeBeansMap.containsKey(bean.getName())) {
              return (T) scopeBeansMap.get(bean.getName());
          } else {
              T t = bean.create(creationalContext);
              scopeBeansMap.put(bean.getName(), t);
              return t;
          }
      }
  
      /**
       * {@inheritDoc}
       */
      @Override
      public Class&lt;? extends Annotation&gt; getScope() {
          return TabScope.class;
      }
  
      /**
       * {@inheritDoc}
       */
      @Override
      public boolean isActive() {
          return true;
      }
  
  }
  </code></pre>
  
  <p>Extension:</p>
  
  <pre><code>public class TabScopeContextExtension implements Extension {
  
  public void registerContext(@Observes AfterBeanDiscovery event) {
      event.addContext(new TabScopeContext());
  }
  
  }
  </code></pre>
  
  <p>Scope:</p>
  
  <pre><code>@NormalScope(passivating=true)
  @Target({TYPE, FIELD, METHOD })
  @Retention(value = RUNTIME)
  @Documented
  @Inherited
  public @interface TabScope {
  
      public static final String NAME = "TAB_SCOPE";
  }
  </code></pre>
  
  <p>Anyone have some idea how this problem can be connected with scope change and how to fix it?
  Thanks in advance for your help.</p>