KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > loader > enhancer > MethodEnhancerConfig


1 /*
2  * Copyright (c) 1998-2004 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  *
23  * Free Software Foundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Scott Ferguson
28  */

29
30 package com.caucho.loader.enhancer;
31
32 import java.util.logging.Logger;
33 import java.util.logging.Level;
34
35 import org.aopalliance.intercept.MethodInterceptor;
36
37 import com.caucho.aop.AopMethodEnhancer;
38
39 import com.caucho.bytecode.JAnnotation;
40 import com.caucho.bytecode.JMethod;
41
42 import com.caucho.config.ConfigException;
43
44 import com.caucho.java.gen.GenClass;
45 import com.caucho.java.gen.BaseMethod;
46
47 import com.caucho.util.L10N;
48
49 /**
50  * Configuration for a method-enhancer builder.
51  */

52 public class MethodEnhancerConfig implements MethodEnhancer {
53   private static final L10N L = new L10N(MethodEnhancerConfig.class);
54
55   private static final Logger log =
56     Logger.getLogger(MethodEnhancerConfig.class.getName());
57   
58   private EnhancerManager _manager;
59   
60   private Class _annotation;
61   private Class _type;
62   private boolean _isStatic = true;
63
64   private MethodEnhancer _enhancer;
65
66   /**
67    * Sets the manager.
68    */

69   public void setEnhancerManager(EnhancerManager manager)
70   {
71     _manager = manager;
72   }
73
74   /**
75    * Sets the annotation.
76    */

77   public void setAnnotation(Class ann)
78   {
79     _annotation = ann;
80   }
81
82   /**
83    * Gets the annotation.
84    */

85   public Class getAnnotation()
86   {
87     return _annotation;
88   }
89   
90   /**
91    * Sets the type of the method enhancer.
92    */

93   public void setType(Class type)
94     throws Exception
95   {
96     _type = type;
97
98     if (MethodEnhancer.class.isAssignableFrom(type)) {
99       _enhancer = (MethodEnhancer) type.newInstance();
100     }
101     else if (MethodInterceptor.class.isAssignableFrom(type)) {
102       AopMethodEnhancer aopEnhancer = new AopMethodEnhancer();
103       aopEnhancer.setType(type);
104       
105       _enhancer = aopEnhancer;
106     }
107     else
108       throw new ConfigException(L.l("'{0}' is an unsupported interceptor type. MethodInterceptor is required.",
109                     type.getName()));
110   }
111
112   /**
113    * Set true for a static instance.
114    */

115   public void setStatic(boolean isStatic)
116   {
117     _isStatic = isStatic;
118   }
119
120   /**
121    * Initializes the config.
122    */

123   public void init()
124     throws ConfigException
125   {
126     _enhancer.setAnnotation(_annotation);
127
128     if (_enhancer instanceof AopMethodEnhancer)
129       ((AopMethodEnhancer) _enhancer).setStatic(_isStatic);
130   }
131
132   /**
133    * Enhances the method.
134    *
135    * @param genClass the generated class
136    * @param jMethod the method to be enhanced
137    * @param jAnn the annotation to be enhanced
138    */

139   public void enhance(GenClass genClass,
140               JMethod jMethod,
141               JAnnotation jAnn)
142   {
143     _enhancer.enhance(genClass, jMethod, jAnn);
144   }
145 }
146
Popular Tags