KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sapia > soto > aop > AdviceDef


1 package org.sapia.soto.aop;
2
3 import org.sapia.soto.ConfigurationException;
4
5 import org.sapia.util.xml.confix.ObjectHandlerIF;
6
7
8 /**
9  * Models an advice definition.
10  *
11  * @author Yanick Duchesne
12  * <dl>
13  * <dt><b>Copyright:</b><dd>Copyright &#169; 2002-2003 <a HREF="http://www.sapia-oss.org">Sapia Open Source Software</a>. All Rights Reserved.</dd></dt>
14  * <dt><b>License:</b><dd>Read the license.txt file of the jar or visit the
15  * <a HREF="http://www.sapia-oss.org/license.html">license page</a> at the Sapia OSS web site</dd></dt>
16  * </dl>
17  */

18 public class AdviceDef implements ObjectHandlerIF {
19   private String JavaDoc _clazz;
20   private String JavaDoc _id;
21   private Advice _instance;
22
23   /**
24    * Constructor for AdviceDef.
25    */

26   public AdviceDef() {
27     super();
28   }
29
30   /**
31    * Sets the class name of the defined advice.
32    *
33    * @param clazz a class name.
34    */

35   public void setClass(String JavaDoc clazz) {
36     _clazz = clazz;
37   }
38
39   /**
40    * Sets the identifier of the defined advice.
41    *
42    * @param id
43    */

44   public void setId(String JavaDoc id) {
45     _id = id;
46   }
47
48   /***
49    * Returns the identifier of the defined advice.
50    *
51    * @return the identifier of the defined advice.
52    */

53   public String JavaDoc getId() {
54     return _id;
55   }
56
57   /**
58    * Returns an advice instance corresponding to this definition.
59    *
60    * @return an <code>Advice</code>.
61    */

62   public Advice getInstance() throws ConfigurationException {
63     if (_instance != null) {
64       return _instance;
65     }
66
67     if (_clazz == null) {
68       if (_id != null) {
69         throw new ConfigurationException(
70           "'class' attribute not specified on advice definition");
71       } else {
72         throw new ConfigurationException(
73           "'class' attribute not specified on advice definition: " + _id);
74       }
75     }
76
77     try {
78       return _instance = (Advice) Class.forName(_clazz).newInstance();
79     } catch (ClassNotFoundException JavaDoc e) {
80       throw new ConfigurationException("Could not find advice class", e);
81     } catch (IllegalAccessException JavaDoc e) {
82       throw new ConfigurationException(
83         "Could not access constructor for security reasons; does the advice class " +
84         _clazz + " has a public constructor?", e);
85     } catch (InstantiationException JavaDoc e) {
86       throw new ConfigurationException("Could not instantiate advice class", e);
87     }
88   }
89
90   public void setAdvice(Advice advice)
91     throws org.sapia.util.xml.confix.ConfigurationException {
92     if (_instance == null) {
93       _instance = (Advice) advice;
94     } else {
95       throw new org.sapia.util.xml.confix.ConfigurationException(
96         "Advice instance already specified");
97     }
98
99     _instance = advice;
100   }
101
102   /**
103    * @see ObjectHandlerIF#handleObject(java.lang.String, java.lang.Object)
104    */

105   public void handleObject(String JavaDoc name, Object JavaDoc obj)
106     throws org.sapia.util.xml.confix.ConfigurationException {
107     if (obj instanceof Advice) {
108       setAdvice((Advice) obj);
109     } else {
110       throw new org.sapia.util.xml.confix.ConfigurationException(
111         "element 'name' does not correspond to and Advice instance");
112     }
113   }
114 }
115
Popular Tags