KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > xdoclet > modules > ejb > entity > EntityCmpSubTask


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

5 package xdoclet.modules.ejb.entity;
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 Ara Abrahamian (ara_e@email.com)
23  * @created Oct 15, 2001
24  * @ant.element display-name="CMP" name="entitycmp" parent="xdoclet.modules.ejb.EjbDocletTask"
25  * @version $Revision: 1.8 $
26  * @xdoclet.merge-file file="entitycmp-custom.xdt" relates-to="{0}CMP.java" description="A text file containing custom
27  * template and/or java code to include in the EJB CMP class."
28  */

29 public class EntityCmpSubTask extends AbstractEjbCodeGeneratorSubTask
30 {
31     public final static String JavaDoc DEFAULT_ENTITYCMP_CLASS_PATTERN = "{0}CMP";
32
33     protected final static String JavaDoc DEFAULT_TEMPLATE_FILE = "resources/entitycmp.xdt";
34
35     /**
36      * A configuration parameter for specifying the concrete CMP entity bean class name pattern. By default the value is
37      * used for deciding the concrete CMP entity bean class name. {0} in the value mean current class's symbolic name
38      * which for an EJBean is the EJB name.
39      *
40      * @see #getEntityCmpClassPattern()
41      */

42     protected String JavaDoc entityCmpClassPattern;
43
44     private String JavaDoc cmpspec = CmpSpecVersion.CMP_2_0;
45
46     /**
47      * Describe what the EntityCmpSubTask constructor does
48      */

49     public EntityCmpSubTask()
50     {
51         setTemplateURL(getClass().getResource(DEFAULT_TEMPLATE_FILE));
52         setDestinationFile(getEntityCmpClassPattern() + ".java");
53         addOfType("javax.ejb.EntityBean");
54         setPackageSubstitutionInheritanceSupported(false);
55     }
56
57     /**
58      * Gets the CmpSpec attribute of the EntityCmpSubTask object
59      *
60      * @return The CmpSpec value
61      */

62     public String JavaDoc getCmpSpec()
63     {
64         return cmpspec;
65     }
66
67     /**
68      * Returns the configuration parameter for specifying the concrete CMP entity bean class name pattern. By default
69      * the value is used for deciding the concrete CMP entity bean class name. {0} in the value mean current class's
70      * symbolic name which for an EJBean is the EJB name. If nothing explicitly specified by user then "{0}CMP" is used
71      * by default.
72      *
73      * @return The EntityCmpClassPattern value
74      * @see #entityCmpClassPattern
75      */

76     public String JavaDoc getEntityCmpClassPattern()
77     {
78         if (entityCmpClassPattern != null) {
79             return entityCmpClassPattern;
80         }
81         else {
82             return DEFAULT_ENTITYCMP_CLASS_PATTERN;
83         }
84     }
85
86     /**
87      * Sets the CmpSpec attribute of the EntityCmpSubTask object
88      *
89      * @param cmpspec The new CmpSpec value
90      */

91     public void setCmpSpec(CmpSpecVersion cmpspec)
92     {
93         this.cmpspec = cmpspec.getValue();
94     }
95
96     /**
97      * Sets the Pattern attribute of the EntityCmpSubTask object
98      *
99      * @param new_pattern The new Pattern value
100      */

101     public void setPattern(String JavaDoc new_pattern)
102     {
103         entityCmpClassPattern = new_pattern;
104     }
105
106     /**
107      * Called to validate configuration parameters.
108      *
109      * @exception XDocletException
110      */

111     public void validateOptions() throws XDocletException
112     {
113         super.validateOptions();
114
115         if (getEntityCmpClassPattern() == null || getEntityCmpClassPattern().trim().equals("")) {
116             throw new XDocletException(Translator.getString(XDocletMessages.class, XDocletMessages.PARAMETER_MISSING_OR_EMPTY, new String JavaDoc[]{"pattern"}));
117         }
118
119         if (getEntityCmpClassPattern().indexOf("{0}") == -1) {
120             throw new XDocletException(Translator.getString(XDocletModulesEjbMessages.class, XDocletModulesEjbMessages.PATTERN_HAS_NO_PLACEHOLDER));
121         }
122     }
123
124     /**
125      * Gets the GeneratedFileName attribute of the EntityCmpSubTask object
126      *
127      * @param clazz Describe what the parameter does
128      * @return The GeneratedFileName value
129      * @exception XDocletException
130      */

131     protected String JavaDoc getGeneratedFileName(XClass clazz) throws XDocletException
132     {
133         return PackageTagsHandler.packageNameAsPathFor(CmpTagsHandler.getEntityCmpClassFor(getCurrentClass())) + ".java";
134     }
135
136
137     /**
138      * @param clazz Describe what the parameter does
139      * @return Describe the return value
140      * @exception XDocletException
141      * @todo refactor/merge this method with matchesGenerationRules from EntityBmpSubTask
142      */

143     protected boolean matchesGenerationRules(XClass clazz) throws XDocletException
144     {
145         Log log = LogUtil.getLog(EntityCmpSubTask.class, "matchesGenerationRules");
146
147         if (super.matchesGenerationRules(clazz) == false) {
148             log.debug("Skip bean " + clazz.getQualifiedName() + " because super.matchesGenerationRules() returned false.");
149             return false;
150         }
151
152         if (CmpTagsHandler.isEntityCmp(getCurrentClass()) == false) {
153             log.debug("Skip bean " + clazz.getQualifiedName() + " because of it's not BMP.");
154             return false;
155         }
156
157         String JavaDoc generate = clazz.getDoc().getTagAttributeValue("ejb:bean", "generate", false);
158
159         if ((generate != null) && (generate.equals("false") || generate.equals("no"))) {
160             log.debug("Skip entity cmp class for " + clazz.getQualifiedName() + " because of generate=" + generate + " flag.");
161             return false;
162         }
163
164         if (EjbTagsHandler.isAConcreteEJBean(getCurrentClass())) {
165             return true;
166         }
167         else {
168             log.debug("Skip bean " + clazz.getQualifiedName() + " because it's not a concrete bean.");
169             return false;
170         }
171     }
172
173     /**
174      * Describe what the method does
175      *
176      * @exception XDocletException
177      */

178     protected void engineStarted() throws XDocletException
179     {
180         System.out.println(Translator.getString(XDocletModulesEjbMessages.class, XDocletModulesEjbMessages.GENERATING_CMP_FOR,
181             new String JavaDoc[]{getCurrentClass().getQualifiedName()}));
182     }
183
184
185     /**
186      * @author Vincent Harcq (vincent.harcq@hubmethods.com)
187      * @created February 23, 2002
188      */

189     public static class CmpSpecVersion extends org.apache.tools.ant.types.EnumeratedAttribute
190     {
191         public final static String JavaDoc CMP_1_1 = "1.x";
192         public final static String JavaDoc CMP_2_0 = "2.x";
193
194         /**
195          * Gets the Versions attribute of the CmpSpecVersion class
196          *
197          * @return The Versions value
198          */

199         public final static String JavaDoc getVersions()
200         {
201             return CMP_1_1 + ',' + CMP_2_0;
202         }
203
204         /**
205          * Gets the Values attribute of the CmpSpecVersion object
206          *
207          * @return The Values value
208          */

209         public String JavaDoc[] getValues()
210         {
211             return (new String JavaDoc[]{
212                 CMP_1_1, CMP_2_0
213                 });
214         }
215     }
216 }
217
Popular Tags