KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > xdoclet > modules > ejb > intf > LocalInterfaceSubTask


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

5 package xdoclet.modules.ejb.intf;
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.intf.InterfaceTagsHandler;
18 import xdoclet.tagshandler.PackageTagsHandler;
19
20 import xdoclet.util.LogUtil;
21 import xdoclet.util.Translator;
22
23 /**
24  * Generates local interfaces for EJBs.
25  *
26  * @author Ara Abrahamian (ara_e@email.com)
27  * @created Oct 15, 2001
28  * @ant.element display-name="Local Interface" name="localinterface" parent="xdoclet.modules.ejb.EjbDocletTask"
29  * @version $Revision: 1.12 $
30  * @xdoclet.merge-file file="local-custom.xdt" relates-to="{0}Local.java" description="A text file containing custom
31  * template and/or java code to include in the local interface."
32  */

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

46     protected String JavaDoc localClassPattern;
47
48
49     /**
50      */

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

67     public String JavaDoc getLocalClassPattern()
68     {
69         if (localClassPattern != null) {
70             return localClassPattern;
71         }
72         else {
73             return DEFAULT_LOCAL_CLASS_PATTERN;
74         }
75     }
76
77
78     /**
79      * The pattern by which the interfaces are named. The placeholder "{0}" designates the EJB name.
80      *
81      * @param new_pattern
82      * @ant.not-required No, defaults to {0}Local
83      */

84     public void setPattern(String JavaDoc new_pattern)
85     {
86         localClassPattern = new_pattern;
87     }
88
89
90     /**
91      * Called to validate configuration parameters.
92      *
93      * @exception XDocletException
94      */

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

116     protected String JavaDoc getGeneratedFileName(XClass clazz) throws XDocletException
117     {
118         return PackageTagsHandler.packageNameAsPathFor(InterfaceTagsHandler.getComponentInterface("local", getCurrentClass())) + ".java";
119     }
120
121
122     /**
123      * @param clazz Describe what the parameter does
124      * @return Describe the return value
125      * @exception XDocletException
126      * @todo (Aslak) This needs refactoring. Nearly all matchesGenerationRules implementations
127      * are the same!
128      */

129     protected boolean matchesGenerationRules(XClass clazz) throws XDocletException
130     {
131         if (super.matchesGenerationRules(clazz) == false) {
132             return false;
133         }
134
135         Log log = LogUtil.getLog(LocalInterfaceSubTask.class, "matchesGenerationRules");
136
137         if (!InterfaceTagsHandler.isLocalEjb(getCurrentClass())) {
138             log.debug("Reject file " + clazz.getQualifiedName() + " because of different view-type");
139             return false;
140         }
141
142         XTag interfaceTag = getCurrentClass().getDoc().getTag("ejb:interface");
143
144         if (interfaceTag == null) {
145             return true;
146         }
147
148         String JavaDoc generate = interfaceTag.getAttributeValue("generate");
149
150         if ((generate != null) && (generate.indexOf("local") == -1)) {
151             log.debug("Skip remote interface for " + clazz.getQualifiedName() + " because of generate=" + generate + " flag.");
152             return false;
153         }
154
155         return true;
156     }
157
158
159     /**
160      * @exception XDocletException
161      */

162     protected void engineStarted() throws XDocletException
163     {
164         System.out.println(Translator.getString(XDocletModulesEjbMessages.class, XDocletModulesEjbMessages.GENERATING_LOCAL_FOR,
165             new String JavaDoc[]{getCurrentClass().getQualifiedName()}));
166     }
167
168 }
169
Popular Tags