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

    Unique Bean mapkey for ZK Skope attribute Map

    oberinspector

      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();
          }
      }




        • 1. Re: Unique Bean mapkey for ZK Skope attribute Map
          alin.heyoulin.qq.com

          private String createMapKey(Bean bean) {
                  if(bean instanceof RIBean)
                     return (RIBean)bean.getId();
                  else
                     throw Exception(not a RIBean);
              }

          • 2. Re: Unique Bean mapkey for ZK Skope attribute Map
            pmuir

            There is no generic ID function suitable for all beans. I'm also unaware of any generally unique way of identifying all beans.

            • 3. Re: Unique Bean mapkey for ZK Skope attribute Map
              alin.heyoulin.qq.com

              Yes



              where i want to store Weld created ManagedBeans

              Thomas Müller Just get ManagedBeans :-)

              • 4. Re: Unique Bean mapkey for ZK Skope attribute Map
                pmuir

                I assumed he really meant all beans, as storing just managed beans would be an odd thing to do.

                • 5. Re: Unique Bean mapkey for ZK Skope attribute Map
                  oberinspector

                  yes... i mean all beans... i just started with weld and i probably misused the therms... ;)
                  I want to programm against the CDI API... RIBean.getId() is weld spcific and not portable.


                  I need to ensure equity only for the configured Skope... so a combination of the most significant type and the Qualifier OR the name in case of @Named beans should be sufficient to build a key.


                  Or do i miss something?


                  /Thomas

                  • 6. Re: Unique Bean mapkey for ZK Skope attribute Map
                    swd847

                    Theoretically no, although in practice this is probably fine. Using the spec it is possible to add beans that have the exact same bean type and qualifiers, so these would end up with the same key.


                    This is not actually as useless as it sounds, as these beans can have producer methods that produce different stuff. e.g. using the seam xml extension you can do:



                        <test:SomeBean>
                            <test:someField>
                                <Produces/>
                                <test:ProducerQualifier value="1" />
                                <value>1</value>
                            </test:someField>
                        </test:SomeBean>
                       
                         <test:SomeBean>
                            <test:someField>
                                <Produces/>
                                <test:ProducerQualifier value="2" />
                                <value>2</value>
                            </test:someField>
                        </test:SomeBean>
                    
                    



                    This will mean you end up with three different beans with the same bean type and qualifiers (as the original bean will still be installed), however they produce different stuff.

                    • 7. Re: Unique Bean mapkey for ZK Skope attribute Map
                      ashishlin

                      Seems to me you need a (Contextual,Key) mapping. In your createMapKey() you need a way to keep track of whether or not a contextual was created before. If not generate a unique key yourself. Take a look at AbstractMapContext class that comes with weld.