KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > xdoclet > modules > ejb > home > HomeInterfaceSubTask


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

5 package xdoclet.modules.ejb.home;
6
7 import org.apache.commons.logging.Log;
8
9 import xjavadoc.XClass;
10 import xjavadoc.XTag;
11
12 import xdoclet.XDocletException;
13 import xdoclet.XDocletMessages;
14
15 import xdoclet.modules.ejb.AbstractEjbCodeGeneratorSubTask;
16 import xdoclet.modules.ejb.XDocletModulesEjbMessages;
17 import xdoclet.modules.ejb.home.HomeTagsHandler;
18 import xdoclet.modules.ejb.intf.InterfaceTagsHandler;
19 import xdoclet.tagshandler.PackageTagsHandler;
20
21 import xdoclet.util.LogUtil;
22 import xdoclet.util.Translator;
23
24 /**
25  * Generates remote home interfaces for EJBs.
26  *
27  * @author Ara Abrahamian (ara_e@email.com)
28  * @created Oct 15, 2001
29  * @ant.element display-name="Home Interface" name="homeinterface" parent="xdoclet.modules.ejb.EjbDocletTask"
30  * @version $Revision: 1.13 $
31  * @xdoclet.merge-file file="home-custom.xdt" relates-to="{0}Home.java" description="A text file containing custom
32  * template and/or java code to include in the home interface."
33  */

34 public class HomeInterfaceSubTask extends AbstractEjbCodeGeneratorSubTask
35 {
36     public final static String JavaDoc DEFAULT_HOMEINTERFACE_CLASS_PATTERN = "{0}Home";
37
38     protected final static String JavaDoc DEFAULT_TEMPLATE_FILE = "resources/home.xdt";
39
40     /**
41      * A configuration parameter for specifying the home interface name pattern. By default the value is used for
42      * deciding the home interface name. The placeholder "{0}" in the value means the current class's symbolic name
43      * which for an EJBean is the EJB name.
44      *
45      * @see #getHomeClassPattern()
46      */

47     protected String JavaDoc homeClassPattern;
48
49     /**
50      * Describe what the HomeInterfaceSubTask constructor does
51      */

52     public HomeInterfaceSubTask()
53     {
54         setTemplateURL(getClass().getResource(DEFAULT_TEMPLATE_FILE));
55         setDestinationFile(getHomeClassPattern() + ".java");
56         addOfType("javax.ejb.EntityBean");
57         addOfType("javax.ejb.SessionBean");
58     }
59
60
61     /**
62      * Returns the configuration parameter for specifying the home interface name pattern. By default the value is used
63      * for deciding the home interface name. The placeholder "{0}" in the value means the current class's symbolic name
64      * which for an EJBean is the EJB name. If nothing explicitly specified by user then "{0}Home" is used by default.
65      *
66      * @return The HomeClassPattern value
67      * @see #homeClassPattern
68      */

69     public String JavaDoc getHomeClassPattern()
70     {
71         if (homeClassPattern != null) {
72             return homeClassPattern;
73         }
74         else {
75             return DEFAULT_HOMEINTERFACE_CLASS_PATTERN;
76         }
77     }
78
79
80     /**
81      * The pattern by which the home interfaces are named. The placeholder "{0}" designates the EJB name.
82      *
83      * @param new_pattern The new Pattern value
84      * @ant.not-required No, defaults to {0}Home
85      */

86     public void setPattern(String JavaDoc new_pattern)
87     {
88         Log log = LogUtil.getLog(HomeInterfaceSubTask.class, "setPattern");
89
90         log.debug("Set pattern to " + new_pattern);
91
92         homeClassPattern = new_pattern;
93     }
94
95
96     /**
97      * Called to validate configuration parameters.
98      *
99      * @exception XDocletException
100      */

101     public void validateOptions() throws XDocletException
102     {
103         super.validateOptions();
104
105         if (getHomeClassPattern() == null || getHomeClassPattern().trim().equals("")) {
106             throw new XDocletException(Translator.getString(XDocletMessages.class, XDocletMessages.PARAMETER_MISSING_OR_EMPTY, new String JavaDoc[]{"pattern"}));
107         }
108
109         if (getHomeClassPattern().indexOf("{0}") == -1) {
110             throw new XDocletException(Translator.getString(XDocletModulesEjbMessages.class, XDocletModulesEjbMessages.PATTERN_HAS_NO_PLACEHOLDER));
111         }
112     }
113
114
115     /**
116      * Gets the GeneratedFileName attribute of the HomeInterfaceSubTask object
117      *
118      * @param clazz Describe what the parameter does
119      * @return The GeneratedFileName value
120      * @exception XDocletException
121      */

122     protected String JavaDoc getGeneratedFileName(XClass clazz) throws XDocletException
123     {
124         return PackageTagsHandler.packageNameAsPathFor(HomeTagsHandler.getHomeInterface("remote", getCurrentClass())) + ".java";
125     }
126
127
128     /**
129      * Describe what the method does
130      *
131      * @param clazz Describe what the parameter does
132      * @return Describe the return value
133      * @exception XDocletException
134      */

135     protected boolean matchesGenerationRules(XClass clazz) throws XDocletException
136     {
137         if (super.matchesGenerationRules(clazz) == false) {
138             return false;
139         }
140
141         Log log = LogUtil.getLog(HomeInterfaceSubTask.class, "matchesGenerationRules");
142
143         if (!InterfaceTagsHandler.isRemoteEjb(getCurrentClass())) {
144             log.debug("Reject file " + clazz.getQualifiedName() + " because of different view-type");
145             return false;
146         }
147
148         XTag interfaceTag = getCurrentClass().getDoc().getTag("ejb:home", false);
149
150         if (interfaceTag == null) {
151             return true;
152         }
153
154         String JavaDoc generate = interfaceTag.getAttributeValue("generate");
155
156         if ((generate != null) && (generate.indexOf("remote") == -1)) {
157             log.debug("Skip home interface for " + clazz.getQualifiedName() + " because of generate=" + generate + " flag.");
158             return false;
159         }
160
161         return true;
162     }
163
164
165     /**
166      * Describe what the method does
167      *
168      * @exception XDocletException
169      */

170     protected void engineStarted() throws XDocletException
171     {
172         System.out.println(Translator.getString(XDocletModulesEjbMessages.class, XDocletModulesEjbMessages.GENERATING_HOME_FOR,
173             new String JavaDoc[]{getCurrentClass().getQualifiedName()}));
174     }
175
176 }
177
Popular Tags