001: /*
002: * $Id: PropertyScope.java 10489 2008-01-23 17:53:38Z dfeist $
003: * --------------------------------------------------------------------------------------
004: * Copyright (c) MuleSource, Inc. All rights reserved. http://www.mulesource.com
005: *
006: * The software in this package is published under the terms of the CPAL v1.0
007: * license, a copy of which has been included with this distribution in the
008: * LICENSE.txt file.
009: */
010: package org.mule.api.transport;
011:
012: import java.io.Serializable;
013: import java.util.Comparator;
014:
015: /**
016: * A PropertyScope is used to assoaciate a message property with a lifetime. Some scopes may be very brief such as
017: * {@link #INVOCATION} scope which only lasts until a service has been invoke or a longer running scope such as {@link #SESSION}
018: *
019: */
020: public final class PropertyScope implements Serializable {
021: public static final String INVOCATION_NAME = "invocation";
022: public static final String INBOUND_NAME = "inbound";
023: public static final String OUTBOUND_NAME = "outbound";
024: public static final String SESSION_NAME = "session";
025: public static final String APPLICATION_NAME = "application";
026:
027: /**
028: * This scope is defined from the point that a Message is created until a service has processed the
029: * message. Properties set on endpoints will be found in this scope
030: */
031: public static final PropertyScope INVOCATION = new PropertyScope(
032: INVOCATION_NAME, 0);
033:
034: /**
035: * This scope holds all inbound headers when a message is received. This scope is read only
036: */
037: public static final PropertyScope INBOUND = new PropertyScope(
038: INBOUND_NAME, 1);
039:
040: /**
041: * This is the default scope when writing properties to a message. All properties written in this scope
042: * will be attached to the outbound message (or response message)
043: */
044: public static final PropertyScope OUTBOUND = new PropertyScope(
045: OUTBOUND_NAME, 2);
046:
047: /**
048: * Defines the scope for any properties set on the session. Mule utilises the underlying transport for controlling the
049: * session where possible i.e. HttpSession. But Mule will fallback to an internal session mechanisim where a session is
050: * encoded on the message with an expiry time associated with it.
051: */
052: public static final PropertyScope SESSION = new PropertyScope(
053: SESSION_NAME, 3);
054:
055: /**
056: * This provides access to properties in the registry. By default this scope is not enabled since
057: * it will most likely have a performance impact. This is a read-only scope
058: */
059: public static final PropertyScope APPLICATION = new PropertyScope(
060: APPLICATION_NAME, 4);
061:
062: /**
063: * An array of all scopes defined here
064: */
065: public static final PropertyScope[] ALL_SCOPES = new PropertyScope[] {
066: INVOCATION, INBOUND, OUTBOUND, SESSION, APPLICATION };
067:
068: private String scope;
069: private int order;
070:
071: public PropertyScope(String scope, int order) {
072: this .scope = scope;
073: this .order = order;
074: }
075:
076: public String getScope() {
077: return scope;
078: }
079:
080: public int getOrder() {
081: return order;
082: }
083:
084: public String toString() {
085: return getScope();
086: }
087:
088: public boolean equals(Object o) {
089: if (this == o) {
090: return true;
091: }
092: if (o == null || getClass() != o.getClass()) {
093: return false;
094: }
095:
096: PropertyScope that = (PropertyScope) o;
097:
098: if (order != that.order) {
099: return false;
100: }
101: if (scope != null ? !scope.equals(that.scope)
102: : that.scope != null) {
103: return false;
104: }
105:
106: return true;
107: }
108:
109: public int hashCode() {
110: int result;
111: result = (scope != null ? scope.hashCode() : 0);
112: result = 31 * result + order;
113: return result;
114: }
115:
116: /**
117: * Used for comparing {@link PropertyScope} instances in a map. The {@link PropertyScope#getOrder()}
118: * property is used to determine the order in the map
119: */
120: public static class ScopeComarator implements Comparator,
121: Serializable {
122: public int compare(Object o, Object o1) {
123: if (o == o1) {
124: return 0;
125: }
126: if (o.equals(o1)) {
127: return 0;
128: }
129: return (((PropertyScope) o).getOrder() < ((PropertyScope) o1)
130: .getOrder() ? -1 : 1);
131: }
132: }
133: }
|