View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.scxml2;
18  
19  import java.io.Serializable;
20  import java.util.Arrays;
21  import java.util.Collections;
22  import java.util.HashMap;
23  import java.util.HashSet;
24  import java.util.Map;
25  import java.util.Set;
26  
27  /**
28   * The SCXMLSystemContext is used as a read only Context wrapper
29   * and provides the SCXML (read only) system variables which are injected via the unwrapped {@link #getContext()}.
30   *
31   * @see <a href="http://www.w3.org/TR/scxml/#SystemVariables">http://www.w3.org/TR/scxml/#SystemVariables</a>
32   */
33  public class SCXMLSystemContext implements Context, Serializable {
34  
35      /**
36       * Serial version UID.
37       */
38      private static final long serialVersionUID = 1L;
39  
40      /**
41       * The protected system variables names as defined in the SCXML specification
42       * @see <a href="http://www.w3.org/TR/scxml/#SystemVariables">http://www.w3.org/TR/scxml/#SystemVariables</a>
43       */
44      public static final String EVENT_KEY = "_event";
45      public static final String SESSIONID_KEY = "_sessionid";
46      public static final String SCXML_NAME_KEY = "_name";
47      public static final String IOPROCESSORS_KEY = "_ioprocessors";
48      public static final String X_KEY = "_x";
49  
50      /** The Commons SCXML internal {@link #getPlatformVariables() platform variable key} holding the current SCXML
51       * status instance **/
52      public static final String STATUS_KEY = "status";
53  
54      /**
55       * The set of protected system variables names
56       */
57      private static final Set<String> PROTECTED_NAMES = new HashSet<String>(Arrays.asList(
58              new String[] {EVENT_KEY, SESSIONID_KEY, SCXML_NAME_KEY, IOPROCESSORS_KEY, X_KEY}
59      ));
60  
61      /**
62       * The wrapped system context
63       */
64  
65      private Context systemContext;
66  
67      /**
68       * The auto-generated next sessionId prefixed ID
69       * @see #generateSessionId()
70       */
71      private long nextSessionSequenceId;
72  
73      /**
74       * Initialize or replace systemContext
75       * @param systemContext the system context to set
76       * @throws java.lang.NullPointerException if systemContext == null
77       */
78      void setSystemContext(Context systemContext) {
79          if (this.systemContext != null) {
80              // replace systemContext
81              systemContext.getVars().putAll(this.systemContext.getVars());
82          }
83          else {
84              // create Platform variables map
85              systemContext.setLocal(X_KEY, new HashMap<String, Object>());
86          }
87          this.systemContext = systemContext;
88          this.protectedVars = Collections.unmodifiableMap(systemContext.getVars());
89      }
90  
91      /**
92       * The unmodifiable wrapped variables map from the wrapped system context
93       */
94      private Map<String, Object> protectedVars;
95  
96      public SCXMLSystemContext(Context systemContext) {
97          setSystemContext(systemContext);
98      }
99  
100     public String generateSessionId() {
101         return getContext().get(SESSIONID_KEY) + "-" + nextSessionSequenceId++;
102     }
103 
104     @Override
105     public void set(final String name, final Object value) {
106         if (PROTECTED_NAMES.contains(name)) {
107             throw new UnsupportedOperationException();
108         }
109         // non-protected variables are set on the parent of the system context (e.g. root context)
110         systemContext.getParent().set(name, value);
111     }
112 
113     @Override
114     public void setLocal(final String name, final Object value) {
115         throw new UnsupportedOperationException();
116     }
117 
118     @Override
119     public Object get(final String name) {
120         return systemContext.get(name);
121     }
122 
123     @Override
124     public boolean has(final String name) {
125         return systemContext.has(name);
126     }
127 
128     @Override
129     public boolean hasLocal(final String name) {
130         return systemContext.hasLocal(name);
131     }
132 
133     @Override
134     public Map<String, Object> getVars() {
135         return protectedVars;
136     }
137 
138     @Override
139     public void reset() {
140         throw new UnsupportedOperationException();
141     }
142 
143     @Override
144     public Context getParent() {
145         return systemContext.getParent();
146     }
147 
148     @Override
149     public SCXMLSystemContext getSystemContext() {
150         return this;
151     }
152 
153     /**
154      * @return The Platform specific system variables map stored under the {@link #X_KEY _x} root system variable
155      */
156     @SuppressWarnings("unchecked")
157     public Map<String, Object> getPlatformVariables() {
158         return (Map<String, Object>)get(X_KEY);
159     }
160 
161     /**
162      * @return Returns the wrapped (modifiable) system context
163      */
164     Context getContext() {
165         return systemContext;
166     }
167 }