001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.commons.scxml.env;
018:
019: import java.io.Serializable;
020: import java.util.HashSet;
021: import java.util.Iterator;
022: import java.util.Map;
023: import java.util.Set;
024:
025: import org.apache.commons.logging.Log;
026: import org.apache.commons.logging.LogFactory;
027: import org.apache.commons.scxml.ErrorReporter;
028: import org.apache.commons.scxml.model.SCXML;
029: import org.apache.commons.scxml.model.State;
030: import org.apache.commons.scxml.model.Transition;
031: import org.apache.commons.scxml.model.TransitionTarget;
032: import org.apache.commons.scxml.semantics.ErrorConstants;
033:
034: /**
035: * Custom error reporter that log execution errors.
036: */
037: public class SimpleErrorReporter implements ErrorReporter, Serializable {
038:
039: /** Serial version UID. */
040: private static final long serialVersionUID = 1L;
041: /** Log. */
042: private Log log = LogFactory.getLog(getClass());
043:
044: /**
045: * Constructor.
046: */
047: public SimpleErrorReporter() {
048: super ();
049: }
050:
051: /**
052: * @see ErrorReporter#onError(String, String, Object)
053: */
054: public void onError(final String errorCode, final String errDetail,
055: final Object errCtx) {
056: //Note: the if-then-else below is based on the actual usage
057: // (codebase search), it has to be kept up-to-date as the code changes
058: String errCode = errorCode.intern();
059: StringBuffer msg = new StringBuffer();
060: msg.append(errCode).append(" (");
061: msg.append(errDetail).append("): ");
062: if (errCode == ErrorConstants.NO_INITIAL) {
063: if (errCtx instanceof SCXML) {
064: //determineInitialStates
065: msg.append("<SCXML>");
066: } else if (errCtx instanceof State) {
067: //determineInitialStates
068: //determineTargetStates
069: msg.append("State "
070: + LogUtils.getTTPath((State) errCtx));
071: }
072: } else if (errCode == ErrorConstants.UNKNOWN_ACTION) {
073: //executeActionList
074: msg.append("Action: " + errCtx.getClass().getName());
075: } else if (errCode == ErrorConstants.NON_DETERMINISTIC) {
076: //filterTransitionSet
077: msg.append(" [");
078: if (errCtx instanceof HashSet) {
079: for (Iterator i = ((Set) errCtx).iterator(); i
080: .hasNext();) {
081: Transition t = (Transition) i.next();
082: msg.append(LogUtils.transToString(t.getParent(), t
083: .getTarget(), t));
084: if (i.hasNext()) {
085: msg.append(", ");
086: }
087: }
088: }
089: msg.append(']');
090: } else if (errCode == ErrorConstants.ILLEGAL_CONFIG) {
091: //isLegalConfig
092: if (errCtx instanceof Map.Entry) {
093: TransitionTarget tt = (TransitionTarget) (((Map.Entry) errCtx)
094: .getKey());
095: Set vals = (Set) (((Map.Entry) errCtx).getValue());
096: msg.append(LogUtils.getTTPath(tt) + " : [");
097: for (Iterator i = vals.iterator(); i.hasNext();) {
098: TransitionTarget tx = (TransitionTarget) i.next();
099: msg.append(LogUtils.getTTPath(tx));
100: if (i.hasNext()) {
101: msg.append(", ");
102: }
103: }
104: msg.append(']');
105: } else if (errCtx instanceof Set) {
106: Set vals = (Set) errCtx;
107: msg.append("<SCXML> : [");
108: for (Iterator i = vals.iterator(); i.hasNext();) {
109: TransitionTarget tx = (TransitionTarget) i.next();
110: msg.append(LogUtils.getTTPath(tx));
111: if (i.hasNext()) {
112: msg.append(", ");
113: }
114: }
115: msg.append(']');
116: }
117: }
118: if (log.isWarnEnabled()) {
119: log.warn(msg.toString());
120: }
121: }
122:
123: }
|