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.scxml.env;
18  
19  import java.io.Serializable;
20  import java.util.HashSet;
21  import java.util.Iterator;
22  import java.util.Map;
23  import java.util.Set;
24  
25  import org.apache.commons.logging.Log;
26  import org.apache.commons.logging.LogFactory;
27  import org.apache.commons.scxml.ErrorReporter;
28  import org.apache.commons.scxml.model.SCXML;
29  import org.apache.commons.scxml.model.State;
30  import org.apache.commons.scxml.model.Transition;
31  import org.apache.commons.scxml.model.TransitionTarget;
32  import org.apache.commons.scxml.semantics.ErrorConstants;
33  
34  /***
35   * Custom error reporter that log execution errors.
36   */
37  public class SimpleErrorReporter implements ErrorReporter, Serializable {
38  
39      /*** Serial version UID. */
40      private static final long serialVersionUID = 1L;
41      /*** Log. */
42      private Log log = LogFactory.getLog(getClass());
43  
44      /***
45       * Constructor.
46       */
47      public SimpleErrorReporter() {
48          super();
49      }
50  
51      /***
52       * @see ErrorReporter#onError(String, String, Object)
53       */
54      public void onError(final String errorCode, final String errDetail,
55              final Object errCtx) {
56          //Note: the if-then-else below is based on the actual usage
57          // (codebase search), it has to be kept up-to-date as the code changes
58          String errCode = errorCode.intern();
59          StringBuffer msg = new StringBuffer();
60          msg.append(errCode).append(" (");
61          msg.append(errDetail).append("): ");
62          if (errCode == ErrorConstants.NO_INITIAL) {
63              if (errCtx instanceof SCXML) {
64                  //determineInitialStates
65                  msg.append("<SCXML>");
66              } else if (errCtx instanceof State) {
67                  //determineInitialStates
68                  //determineTargetStates
69                  msg.append("State " + LogUtils.getTTPath((State) errCtx));
70              }
71          } else if (errCode == ErrorConstants.UNKNOWN_ACTION) {
72              //executeActionList
73              msg.append("Action: " + errCtx.getClass().getName());
74          } else if (errCode == ErrorConstants.NON_DETERMINISTIC) {
75              //filterTransitionSet
76              msg.append(" [");
77              if (errCtx instanceof HashSet) {
78                  for (Iterator i = ((Set) errCtx).iterator(); i.hasNext();) {
79                      Transition t = (Transition) i.next();
80                      msg.append(LogUtils.transToString(t.getParent(),
81                          t.getTarget(), t));
82                      if (i.hasNext()) {
83                          msg.append(", ");
84                      }
85                  }
86              }
87              msg.append(']');
88          } else if (errCode == ErrorConstants.ILLEGAL_CONFIG) {
89              //isLegalConfig
90              if (errCtx instanceof Map.Entry) {
91                  TransitionTarget tt = (TransitionTarget)
92                      (((Map.Entry) errCtx).getKey());
93                  Set vals = (Set) (((Map.Entry) errCtx).getValue());
94                  msg.append(LogUtils.getTTPath(tt) + " : [");
95                  for (Iterator i = vals.iterator(); i.hasNext();) {
96                      TransitionTarget tx = (TransitionTarget) i.next();
97                      msg.append(LogUtils.getTTPath(tx));
98                      if (i.hasNext()) {
99                          msg.append(", ");
100                     }
101                 }
102                 msg.append(']');
103             } else if (errCtx instanceof Set) {
104                 Set vals = (Set) errCtx;
105                 msg.append("<SCXML> : [");
106                 for (Iterator i = vals.iterator(); i.hasNext();) {
107                     TransitionTarget tx = (TransitionTarget) i.next();
108                     msg.append(LogUtils.getTTPath(tx));
109                     if (i.hasNext()) {
110                         msg.append(", ");
111                     }
112                 }
113                 msg.append(']');
114             }
115         }
116         if (log.isWarnEnabled()) {
117             log.warn(msg.toString());
118         }
119     }
120 
121 }
122