View Javadoc
1   /*
2    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
3    * The software in this package is published under the terms of the CPAL v1.0
4    * license, a copy of which has been included with this distribution in the
5    * LICENSE.txt file.
6    */
7   package org.mule.api.transport;
8   
9   import java.io.Serializable;
10  import java.util.Comparator;
11  
12  /**
13   * A PropertyScope is used to associate a message property with a lifetime.  A scope may be very 
14   * brief such as {@link #INVOCATION} which only lasts until a service has been invoked or longer 
15   * running such as {@link #SESSION}.
16   */
17  public final class PropertyScope implements Serializable
18  {
19      private static final long serialVersionUID = -4792653762048974018L;
20      
21      public static final String INVOCATION_NAME = "invocation";
22      public static final String INBOUND_NAME = "inbound";
23      public static final String OUTBOUND_NAME = "outbound";
24      public static final String SESSION_NAME = "session";
25      public static final String APPLICATION_NAME = "application";
26  
27      /**
28       * This scope is defined from the point that a Message is created until a service has processed the
29       * message. Properties set on endpoints will be found in this scope
30       */
31      public static final PropertyScope INVOCATION = new PropertyScope(INVOCATION_NAME, 0);
32  
33      /**
34       * This scope holds all inbound headers when a message is received. This scope is read only
35       */
36      public static final PropertyScope INBOUND = new PropertyScope(INBOUND_NAME, 1);
37  
38      /**
39       * This is the default scope when writing properties to a message. All properties written in this scope
40       * will be attached to the outbound message (or response message)
41       */
42      public static final PropertyScope OUTBOUND = new PropertyScope(OUTBOUND_NAME, 2);
43  
44      /**
45       * Defines the scope for any properties set on the session. Mule utilises the underlying transport for controlling the
46       * session where possible i.e. HttpSession. But Mule will fallback to an internal session mechanism where a session is
47       * encoded on the message with an expiry time associated with it.
48       */
49      public static final PropertyScope SESSION = new PropertyScope(SESSION_NAME, 3);
50  
51      /**
52       * This provides access to properties in the registry. By default this scope is not enabled since
53       * it will most likely have a performance impact. This is a read-only scope
54       */
55      public static final PropertyScope APPLICATION = new PropertyScope(APPLICATION_NAME, 4);
56  
57      /**
58       * An array of all scopes defined here
59       */
60      public static final PropertyScope[] ALL_SCOPES = new PropertyScope[]{INVOCATION, INBOUND, OUTBOUND, SESSION, APPLICATION};
61  
62      private String scope;
63      private int order;
64  
65      private PropertyScope(String scope, int order)
66      {
67          this.scope = scope;
68          this.order = order;
69      }
70  
71      public static PropertyScope get(String name)
72      {
73          if (INVOCATION.getScopeName().equals(name))
74          {
75              return INVOCATION;
76          }
77          else if (INBOUND.getScopeName().equals(name))
78          {
79              return INBOUND;
80          }
81          else if (OUTBOUND.getScopeName().equals(name))
82          {
83              return OUTBOUND;
84          }
85          else if (SESSION.getScopeName().equals(name))
86          {
87              return SESSION;
88          }
89          else if (APPLICATION.getScopeName().equals(name))
90          {
91              return APPLICATION;
92          }
93          else
94          {
95              return null;
96          }
97      }
98      
99      public String getScopeName()
100     {
101         return scope;
102     }
103 
104     public int getOrder()
105     {
106         return order;
107     }
108 
109     @Override
110     public String toString()
111     {
112         return getScopeName();
113     }
114 
115     @Override
116     public boolean equals(Object o)
117     {
118         if (this == o)
119         {
120             return true;
121         }
122         if (o == null || getClass() != o.getClass())
123         {
124             return false;
125         }
126 
127         PropertyScope that = (PropertyScope) o;
128 
129         if (order != that.order)
130         {
131             return false;
132         }
133         if (scope != null ? !scope.equals(that.scope) : that.scope != null)
134         {
135             return false;
136         }
137 
138         return true;
139     }
140 
141     @Override
142     public int hashCode()
143     {
144         int result;
145         result = (scope != null ? scope.hashCode() : 0);
146         result = 31 * result + order;
147         return result;
148     }
149 
150     /**
151      * Used for comparing {@link PropertyScope} instances in a map. The {@link PropertyScope#getOrder()}
152      * property is used to determine the order in the map
153      */
154     public static class ScopeComparator implements Comparator<PropertyScope>, Serializable
155     {
156         private static final long serialVersionUID = -3346258000312580166L;
157 
158         public int compare(PropertyScope o, PropertyScope o1)
159         {
160             if (o == o1)
161             {
162                 return 0;
163             }
164             if (o.equals(o1))
165             {
166                 return 0;
167             }
168             return (o.getOrder() < o1.getOrder() ? -1 : 1);
169         }
170     }
171 }
172