View Javadoc

1   /*
2    * $Id: RegistryLookupBindings.java 21599 2011-03-22 02:06:16Z dirk.olmes $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
9    */
10  package org.mule.module.scripting.component;
11  
12  import org.mule.api.registry.Registry;
13  
14  import java.util.Collection;
15  import java.util.Map;
16  import java.util.Set;
17  
18  import javax.script.Bindings;
19  
20  /**
21   * This class will attempt to lookup up objects inside the registry in case they don't
22   * exist in the delegate binding. This makes it possible to reference named objects 
23   * inside of Mule (such as a Spring bean) from a script with no extra work.
24   */
25  public class RegistryLookupBindings implements Bindings
26  {  
27      private final Bindings delegate;
28      private final Registry registry;
29  
30      public RegistryLookupBindings(Registry registry, Bindings delegate)
31      {
32          this.registry = registry;
33          this.delegate = delegate;
34      }
35  
36      public Object put(String name, Object value)
37      {
38          return delegate.put(name, value);
39      }
40  
41      public void putAll(Map<? extends String, ? extends Object> toMerge)
42      {
43          delegate.putAll(toMerge);
44      }
45  
46      public boolean containsKey(Object key)
47      {
48          boolean containsKey = delegate.containsKey(key);
49          if (!containsKey)
50          {
51              return registry.lookupObject(key.toString()) != null;
52          }
53          return containsKey;
54      }
55  
56      public Object get(Object key)
57      {
58          Object object = delegate.get(key);
59          if (object == null)
60          {
61              object = registry.lookupObject(key.toString());
62          }
63          return object;
64      }
65  
66      public Object remove(Object key)
67      {
68          return delegate.remove(key);
69      }
70  
71      public int size()
72      {
73          return delegate.size();
74      }
75  
76      public boolean isEmpty()
77      {
78          return delegate.isEmpty();
79      }
80  
81      public boolean containsValue(Object value)
82      {
83          return delegate.containsValue(value);
84      }
85  
86      public void clear()
87      {
88          delegate.clear();
89      }
90  
91      public Set<String> keySet()
92      {
93          return delegate.keySet();
94      }
95  
96      public Collection<Object> values()
97      {
98          return delegate.values();
99      }
100 
101     public Set<java.util.Map.Entry<String, Object>> entrySet()
102     {
103         return delegate.entrySet();
104     }
105 
106     public boolean equals(Object o)
107     {
108         return delegate.equals(o);
109     }
110 
111     public int hashCode()
112     {
113         return delegate.hashCode();
114     }
115     
116 
117 }