7 Replies Latest reply on Feb 9, 2010 7:21 AM by Ashish Dasnurkar

    Unique Bean mapkey for ZK Skope attribute Map

    Thomas Müller Newbie

      All Zk specific Skopes provide a attribute Map<String, Object> where i want to store Weld created ManagedBeans with my Context implementation. I used bean.getName() in my createMapkey method as first aproach for the mapkey, but this is only set when the bean is @Named. As i understand the Bean is unique for a combination of type(s), Qualifier, Scope, Stereotype(?)... How can i obtain a unique key from a Bean instance to use as mapkey within a ZK-Skope attribute Map? E.g. could i use bean.hash() as key?


      My implemetation so far (copied and modified from a howto found somewhere...):


      public abstract class AbstractZkContext<T> implements Context {
      
          @Override
          public abstract Class getScope();
      
          public <T> T get(Contextual<T> contextual, CreationalContext<T> creationalContext) {
              final Bean bean = (Bean) contextual;
              final String key = createMapKey(bean);
              final Map<String, Object> viewMap = getViewMap();
              int hash = bean.hashCode();
              if (viewMap.containsKey(key)) {
                  return (T) viewMap.get(key);
              } else {
                  T t = (T) bean.create(creationalContext);
                  viewMap.put(key, t);
                  return t;
              }
          }
      
          protected abstract Map<String, Object> getViewMap();
      
          public <T> T get(Contextual<T> contextual) {
              final Bean bean = (Bean) contextual;
              final String key = createMapKey(bean);
      
              Map<String, Object> viewMap = getViewMap();
      
              if (viewMap.containsKey(key)) {
                  return (T) viewMap.get(key);
              } else {
                  return null;
              }
          }
      
          @Override
          public boolean isActive() {
              return true;
          }
      
          private String createMapKey(Bean bean) {
              return bean.getName();
          }
      }