View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2016 Mort Bay Consulting Pty. Ltd.
4   //  ------------------------------------------------------------------------
5   //  All rights reserved. This program and the accompanying materials
6   //  are made available under the terms of the Eclipse Public License v1.0
7   //  and Apache License v2.0 which accompanies this distribution.
8   //
9   //      The Eclipse Public License is available at
10  //      http://www.eclipse.org/legal/epl-v10.html
11  //
12  //      The Apache License v2.0 is available at
13  //      http://www.opensource.org/licenses/apache2.0.php
14  //
15  //  You may elect to redistribute this code under either of these licenses.
16  //  ========================================================================
17  //
18  
19  package org.eclipse.jetty.cdi.core;
20  
21  import java.util.ArrayList;
22  import java.util.HashMap;
23  import java.util.List;
24  import java.util.Map;
25  
26  import javax.enterprise.context.spi.Contextual;
27  
28  import org.eclipse.jetty.util.log.Log;
29  import org.eclipse.jetty.util.log.Logger;
30  
31  public class SimpleBeanStore
32  {
33      private static final Logger LOG = Log.getLogger(SimpleBeanStore.class);
34  
35      public Map<Contextual<?>, List<ScopedInstance<?>>> beans = new HashMap<>();
36  
37      public void addBean(ScopedInstance<?> instance)
38      {
39          if (LOG.isDebugEnabled())
40          {
41              LOG.debug("addBean({})",instance);
42          }
43          List<ScopedInstance<?>> instances = getBeans(instance.bean);
44          if (instances == null)
45          {
46              instances = new ArrayList<>();
47              beans.put(instance.bean,instances);
48          }
49          instances.add(instance);
50      }
51  
52      public void clear()
53      {
54          beans.clear();
55      }
56  
57      public void destroy()
58      {
59          if (LOG.isDebugEnabled())
60          {
61              LOG.debug("destroy() - {} beans",beans.size());
62          }
63          for (List<ScopedInstance<?>> instances : beans.values())
64          {
65              if (LOG.isDebugEnabled())
66              {
67                  LOG.debug("destroying - {} instance(s)",instances.size());
68              }
69              for (ScopedInstance<?> instance : instances)
70              {
71                  if (LOG.isDebugEnabled())
72                  {
73                      LOG.debug("destroying instance {}",instance);
74                  }
75                  instance.destroy();
76              }
77          }
78      }
79  
80      public List<ScopedInstance<?>> getBeans(Contextual<?> contextual)
81      {
82          if (LOG.isDebugEnabled())
83          {
84              LOG.debug("getBeans({})",contextual);
85          }
86          return beans.get(contextual);
87      }
88  
89      @Override
90      public String toString()
91      {
92          return String.format("%s@%X[size=%d]",this.getClass().getSimpleName(),hashCode(),beans.size());
93      }
94  }