KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > xdoclet > modules > ejb > lookup > LookupObjectSubTask


1 /*
2  * Copyright (c) 2001, 2002 The XDoclet team
3  * All rights reserved.
4  */

5 package xdoclet.modules.ejb.lookup;
6
7 import org.apache.commons.logging.Log;
8
9 import xjavadoc.XClass;
10
11 import xdoclet.XDocletException;
12 import xdoclet.XDocletMessages;
13 import xdoclet.modules.ejb.AbstractEjbCodeGeneratorSubTask;
14 import xdoclet.modules.ejb.EjbTagsHandler;
15 import xdoclet.modules.ejb.XDocletModulesEjbMessages;
16 import xdoclet.tagshandler.PackageTagsHandler;
17 import xdoclet.util.LogUtil;
18
19 import xdoclet.util.Translator;
20
21 /**
22  * @author Konstantin Pribluda
23  * @created October 3, 2001
24  * @ant.element display-name="Lookup Object" name="utilobject" parent="xdoclet.modules.ejb.EjbDocletTask"
25  * @version $Revision: 1.13 $
26  * @xdoclet.merge-file file="util-custom.xdt" relates-to="{0}Util.java" description="A text file containing custom
27  * template and/or java code to include in the utility class."
28  * @xdoclet.merge-file file="lookup-custom.xdt" relates-to="{0}Util.java" description="A text file containing custom
29  * template and/or java code to include in the utility class."
30  */

31 public class LookupObjectSubTask extends AbstractEjbCodeGeneratorSubTask
32 {
33     public final static String JavaDoc DEFAULT_UTIL_CLASS_PATTERN = "{0}Util";
34
35     private static String JavaDoc DEFAULT_TEMPLATE_FILE = "resources/lookup.xdt";
36
37     /**
38      * A configuration parameter for specifying the utility class name pattern. By default the value is used for
39      * deciding the utility name. {0} in the value mean current class's symbolic name which for an EJBean is the EJB
40      * name.
41      *
42      * @see #getUtilClassPattern()
43      */

44     private String JavaDoc utilClassPattern;
45
46     /**
47      * Include a performant GUID generator in the util object.
48      */

49     private boolean includeGUID = false;
50
51     /**
52      * Cache the homes?
53      */

54     private boolean cacheHomes = false;
55
56     /**
57      * The preferred kind of lookup code, which is either logical or physical.
58      */

59     private String JavaDoc kind = LookupKind.LOGICAL;
60
61     /**
62      * should local proxies to session beans be generated
63      */

64     private boolean localProxies = false;
65
66     /**
67      * Describe what the UtilObjectSubTask constructor does
68      */

69     public LookupObjectSubTask()
70     {
71         setTemplateURL(getClass().getResource(DEFAULT_TEMPLATE_FILE));
72         setDestinationFile(getUtilClassPattern() + ".java");
73         addOfType("javax.ejb.EntityBean");
74         addOfType("javax.ejb.SessionBean");
75         addOfType("javax.ejb.MessageDrivenBean");
76     }
77
78     /**
79      * should local proxies be returned instead of home interfaces be returned for SLSBs?
80      *
81      * @return
82      */

83     public boolean getLocalProxies()
84     {
85         return localProxies;
86     }
87
88     /**
89      * Gets the Kind attribute of the UtilObjectSubTask object
90      *
91      * @return The Kind value
92      */

93     public String JavaDoc getKind()
94     {
95         return kind;
96     }
97
98     /**
99      * Include a performant GUID generator in the util object.
100      *
101      * @return
102      */

103     public boolean getIncludeGUID()
104     {
105         return includeGUID;
106     }
107
108     /**
109      * Cache the homes?
110      *
111      * @return
112      */

113     public boolean getCacheHomes()
114     {
115         return cacheHomes;
116     }
117
118     /**
119      * Returns the configuration parameter for specifying the utility class name pattern. By default the value is used
120      * for deciding the utility name. {0} in the value mean current class's symbolic name which for an EJBean is the EJB
121      * name. If nothing explicitly specified by user then "{0}Util" is used by default.
122      *
123      * @return The UtilClassPattern value
124      */

125     public String JavaDoc getUtilClassPattern()
126     {
127         if (utilClassPattern != null) {
128             return utilClassPattern;
129         }
130         else {
131             return DEFAULT_UTIL_CLASS_PATTERN;
132         }
133     }
134
135     /**
136      * Should local proxies be dynamically generated for Stateless session beans? (Typically used with Hibernate instead
137      * of Entity Beans to develop outside the container.)
138      *
139      * @param localProxies
140      */

141     public void setLocalProxies(boolean localProxies)
142     {
143         this.localProxies = localProxies;
144     }
145
146     /**
147      * Sets the Kind attribute of the UtilObjectSubTask object
148      *
149      * @param kind The new Kind value
150      */

151     public void setKind(LookupKind kind)
152     {
153         this.kind = kind.getValue();
154     }
155
156     /**
157      * Sets the Pattern attribute of the UtilObjectSubTask object
158      *
159      * @param new_pattern The new Pattern value
160      */

161     public void setPattern(String JavaDoc new_pattern)
162     {
163         utilClassPattern = new_pattern;
164     }
165
166     /**
167      * Include a performant GUID generator in the util object.
168      *
169      * @param includeGUID include the GUID generator or not
170      */

171     public void setIncludeGUID(boolean includeGUID)
172     {
173         this.includeGUID = includeGUID;
174     }
175
176     /**
177      * Cache the homes?
178      *
179      * @param cacheHomes
180      */

181     public void setCacheHomes(boolean cacheHomes)
182     {
183         this.cacheHomes = cacheHomes;
184     }
185
186     /**
187      * Called to validate configuration parameters.
188      *
189      * @exception XDocletException
190      */

191     public void validateOptions() throws XDocletException
192     {
193         super.validateOptions();
194
195         if (getUtilClassPattern() == null || getUtilClassPattern().trim().equals("")) {
196             throw new XDocletException(Translator.getString(XDocletMessages.class, XDocletMessages.PARAMETER_MISSING_OR_EMPTY, new String JavaDoc[]{"pattern"}));
197         }
198
199         if (getUtilClassPattern().indexOf("{0}") == -1) {
200             throw new XDocletException(Translator.getString(XDocletModulesEjbMessages.class, XDocletModulesEjbMessages.PATTERN_HAS_NO_PLACEHOLDER));
201         }
202     }
203
204     /**
205      * Gets the GeneratedFileName attribute of the UtilObjectSubTask object
206      *
207      * @param clazz Describe what the parameter does
208      * @return The GeneratedFileName value
209      * @exception XDocletException
210      */

211     protected String JavaDoc getGeneratedFileName(XClass clazz) throws XDocletException
212     {
213         return PackageTagsHandler.packageNameAsPathFor(LookupUtilTagsHandler.getUtilClassFor(getCurrentClass())) + ".java";
214     }
215
216     /**
217      * Describe what the method does
218      *
219      * @exception XDocletException
220      */

221     protected void engineStarted() throws XDocletException
222     {
223         System.out.println(Translator.getString(XDocletModulesEjbMessages.class, XDocletModulesEjbMessages.GENERATING_UTIL_FOR,
224             new String JavaDoc[]{getCurrentClass().getQualifiedName()}));
225     }
226
227     /**
228      * Describe what the method does
229      *
230      * @param clazz Describe what the parameter does
231      * @return Describe the return value
232      * @exception XDocletException
233      */

234     protected boolean matchesGenerationRules(XClass clazz) throws XDocletException
235     {
236         Log log = LogUtil.getLog(LookupObjectSubTask.class, "matchesGenerationRules");
237
238         if (super.matchesGenerationRules(clazz) == false) {
239             log.debug("Skip bean " + clazz.getQualifiedName() + " because super.matchesGenerationRules() returned false.");
240             return false;
241         }
242
243         if (!EjbTagsHandler.isAConcreteEJBean(getCurrentClass())) {
244             log.debug("Skip bean " + clazz.getQualifiedName() + " because it's not a concrete bean.");
245             return false;
246         }
247
248         String JavaDoc generate = getCurrentClass().getDoc().getTagAttributeValue("ejb:util", "generate", true);
249
250         if ((generate != null) && (generate.equals("false") || generate.equals("no"))) {
251             log.debug("Skip util class for " + clazz.getQualifiedName() + " because of generate=" + generate + " flag.");
252             return false;
253         }
254
255         return true;
256     }
257
258     /**
259      * @author Ara Abrahamian (ara_e_w@yahoo.com)
260      * @created March 6, 2002
261      */

262     public static class LookupKind extends org.apache.tools.ant.types.EnumeratedAttribute
263     {
264         public final static String JavaDoc PHYSICAL = "physical";
265
266         public final static String JavaDoc LOGICAL = "logical";
267
268         /**
269          * Gets the Values attribute of the LookupKind object
270          *
271          * @return The Values value
272          */

273         public String JavaDoc[] getValues()
274         {
275             return (new String JavaDoc[]{
276                 PHYSICAL, LOGICAL
277                 });
278         }
279     }
280 }
281
Popular Tags