View Javadoc

1   /*
2    *  Copyright 2004-2006 Stefan Reuter
3    *
4    *  Licensed under the Apache License, Version 2.0 (the "License");
5    *  you may not use this file except in compliance with the License.
6    *  You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   *  Unless required by applicable law or agreed to in writing, software
11   *  distributed under the License is distributed on an "AS IS" BASIS,
12   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *  See the License for the specific language governing permissions and
14   *  limitations under the License.
15   *
16   */
17  package org.asteriskjava.manager.action;
18  
19  import java.lang.reflect.Method;
20  import java.util.Map;
21  
22  import org.asteriskjava.util.ReflectionUtil;
23  
24  /**
25   * This class implements the ManagerAction interface and can serve as base class
26   * for your concrete Action implementations.
27   * 
28   * @author srt
29   * @version $Id$
30   * @since 0.2
31   */
32  public abstract class AbstractManagerAction implements ManagerAction
33  {
34      /**
35       * Serializable version identifier.
36       */
37      static final long serialVersionUID = -7667827187378395689L;
38  
39      private String actionId;
40  
41      public abstract String getAction();
42  
43      public String getActionId()
44      {
45          return actionId;
46      }
47  
48      public void setActionId(String actionId)
49      {
50          this.actionId = actionId;
51      }
52  
53      @Override
54      public String toString()
55      {
56          StringBuffer sb;
57          Map<String, Method> getters;
58  
59          sb = new StringBuffer(getClass().getName() + "[");
60          sb.append("action='").append(getAction()).append("',");
61          getters = ReflectionUtil.getGetters(getClass());
62          for (Map.Entry<String, Method> entry : getters.entrySet())
63          {
64              final String attribute = entry.getKey();
65              if ("action".equals(attribute) || "class".equals(attribute))
66              {
67                  continue;
68              }
69  
70              try
71              {
72                  Object value;
73                  value = entry.getValue().invoke(this);
74                  sb.append(attribute).append("='").append(value).append("',");
75              }
76              catch (Exception e) // NOPMD
77              {
78                  // swallow
79              }
80          }
81          sb.append("systemHashcode=").append(System.identityHashCode(this));
82          sb.append("]");
83  
84          return sb.toString();
85      }
86  }