KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > tool > codegen > internal > ServiceGenerator


1
2 /*
3  * Enhydra Java Application Server Project
4  *
5  * The contents of this file are subject to the Enhydra Public License
6  * Version 1.1 (the "License"); you may not use this file except in
7  * compliance with the License. You may obtain a copy of the License on
8  * the Enhydra web site ( http://www.enhydra.org/ ).
9  *
10  * Software distributed under the License is distributed on an "AS IS"
11  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
12  * the License for the specific terms governing rights and limitations
13  * under the License.
14  *
15  * The Initial Developer of the Enhydra Application Server is Lutris
16  * Technologies, Inc. The Enhydra Application Server and portions created
17  * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
18  * All Rights Reserved.
19  *
20  * Contributor(s):
21  * Paul Mahar
22  *
23  */

24 package org.enhydra.tool.codegen.internal;
25
26 // Toolbox imports
27
import org.enhydra.tool.codegen.wizard.CodeGenPanel;
28 import org.enhydra.tool.codegen.wizard.ServiceOptionPanel1;
29 import org.enhydra.tool.codegen.wizard.ProjectOptionPanel2;
30 import org.enhydra.tool.codegen.wizard.ServiceOptionPanel3;
31 import org.enhydra.tool.codegen.ProjectGenerator;
32 import org.enhydra.tool.codegen.GeneratorException;
33 import org.enhydra.tool.common.Replacement;
34 import org.enhydra.tool.common.ToolException;
35
36 // Standard imports
37
import java.util.ArrayList JavaDoc;
38 import java.util.Arrays JavaDoc;
39
40 /**
41  * A generator for Enhydra 4 services.
42  */

43 public class ServiceGenerator extends ProjectGenerator {
44
45     /**
46      * Name to use specify at the command line.
47      */

48     public static final String JavaDoc COMMAND_NAME = "service"; // nores
49

50     /**
51      * Descriptive name for generator.
52      */

53     public static final String JavaDoc displayName = "Enhydra Service";
54     public static final String JavaDoc wizardTitle = "Enhydra Service Wizard";
55     private CodeGenPanel[] panels = null;
56
57     /**
58      * Create an instance of the Web Application generator.
59      */

60     public ServiceGenerator() {
61         setReplacementSet(new ServiceReplacementSet());
62         setOptionSet(new ServiceOptionSet());
63     }
64
65     public CodeGenPanel[] getWizardPanels() throws GeneratorException {
66
67         // don't initialize until called.
68
if (panels == null && isSwing()) {
69             panels = new CodeGenPanel[3];
70             panels[0] = new ServiceOptionPanel1();
71             panels[1] = new ProjectOptionPanel2();
72             panels[2] = new ServiceOptionPanel3();
73             for (int i = 0; i < panels.length; i++) {
74                 panels[i].setOptionSet(getOptionSet());
75                 panels[i].readOptionSet();
76             }
77         }
78         return panels;
79     }
80
81     /**
82      * Implements AppGenerator
83      */

84     public String JavaDoc[] getOutputExcludes() {
85         String JavaDoc[] ex = new String JavaDoc[0];
86         ArrayList JavaDoc list = null;
87
88         list = new ArrayList JavaDoc(Arrays.asList(super.getOutputExcludes()));
89
90         // not to be resourced
91
if (!isMBean()) { // nores
92
list.add("qualfiers.xml"); // nores
93
list.add("MBean.java"); // nores
94
}
95         list.trimToSize();
96         ex = new String JavaDoc[list.size()];
97         ex = (String JavaDoc[]) list.toArray(ex);
98         list.clear();
99         return ex;
100     }
101
102     protected String JavaDoc[] getInputIncludes() {
103         ArrayList JavaDoc list = null;
104         String JavaDoc[] includes = new String JavaDoc[0];
105
106         list = new ArrayList JavaDoc(Arrays.asList(super.getInputIncludes()));
107         if (isMBean()) {
108             list.add("mbean");
109         } else {
110             list.add("nombean");
111         }
112         list.trimToSize();
113         includes = new String JavaDoc[list.size()];
114         includes = (String JavaDoc[]) list.toArray(includes);
115         list.clear();
116         return includes;
117     }
118
119     // override ProjectGenerator
120
public void initReplacementSet() throws GeneratorException {
121         super.initReplacementSet();
122         try {
123             String JavaDoc serviceName = null;
124
125             serviceName =
126                 getOptionSet().lookup(ServiceOptionSet.SERVICE).getValue();
127             getReplacementSet().lookup(ServiceReplacementSet.at_SERVICE_at).setReplaceWith(serviceName);
128         } catch (ToolException e) {
129             e.printStackTrace();
130             throw new GeneratorException(e,
131                                          res.getString("Unable_to_init_rep"));
132         }
133     }
134
135     /**
136      * Get short name used in command line. May also be used
137      * to locate template directory.
138      *
139      * @return
140      * Command line identifier.
141      */

142     public String JavaDoc getCommandName() {
143         return COMMAND_NAME;
144     }
145
146     /**
147      * Name to display in command line messages and in the wizard.
148      *
149      * @return
150      * Display name for generator.
151      */

152     public String JavaDoc getDisplayName() {
153         return ServiceGenerator.displayName;
154     }
155
156     public String JavaDoc getWizardTitle() {
157         return ServiceGenerator.wizardTitle;
158     }
159
160     /**
161      * Get text that breifly explains the application that
162      * the generator creates.
163      *
164      * @return
165      * Descriptive text to display in a wizard or in a command
166      * line help response.
167      */

168     public String JavaDoc getDescription() {
169         return "Generate a simple service that extends the "
170                + "Enhydra application server. Refer to the service developer's "
171                + "guide for a detailed explaintion of how to implement an "
172                + "Enhydra service.";
173     }
174
175     protected String JavaDoc[] getDefaultAddinSteps() {
176         String JavaDoc[] steps = new String JavaDoc[5];
177
178         steps[0] = "Build your project to compile all source files";
179         steps[1] = "Start the Enhydra Multiserver from the command line:<BR>&nbsp;&nbsp;<code>$ cd <i>enhydra root</i>/bin</code><BR>&nbsp;&nbsp;<code>$ ./multiserver</code>";
180         steps[2] = "Deploy the project by selecting Tools | Kelp Deployer.<BR> Click the Deploy button to create an archive and deploy it to the running multiserver.";
181         steps[3] = "Verify that the service has been successfully deployed by inspecting the multiserver console output.<BR><BR>";
182         steps[4] = getCodeExampleStep();
183         return steps;
184     }
185
186     protected String JavaDoc[] getDefaultShellSteps() {
187         String JavaDoc[] steps = new String JavaDoc[3];
188         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
189
190         buf.append("Use &lt;eas_root&gt;/bin/ant to compile your project");
191         buf.append(" using the build.xml file.");
192         steps[0] = buf.toString();
193         buf.setLength(0);
194         buf.append("To deploy your service, copy the jar from output/lib");
195         buf.append(" to &lt;eas_root&gt;/deploy");
196         steps[1] = buf.toString();
197         steps[2] = getCodeExampleStep();
198         return steps;
199     }
200
201     private String JavaDoc getCodeExampleStep() {
202         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
203         String JavaDoc pack = new String JavaDoc();
204         String JavaDoc service = new String JavaDoc();
205
206         try {
207             pack =
208                 getReplacementSet().lookup(ServiceReplacementSet.JAVA_PACKAGE).getReplaceWith()[0];
209             service =
210                 getReplacementSet().lookup(ServiceReplacementSet.at_SERVICE_at).getReplaceWith()[0];
211         } catch (ToolException e) {
212             e.printStackTrace(System.err);
213             pack = "&lt;package_name&gt;";
214             service = "&lt;service_name&gt;";
215         }
216         buf.append("To access your new service from a client");
217         buf.append(" application:");
218         buf.append("<code><br>");
219         buf.append("<br>javax.naming.Context ctx = new javax.naming.InitialContext();");
220         buf.append("<br>Object obj = ctx.lookup(\"ems:/Services/"
221                    + service + "/manager\");");
222         buf.append("<br>" + pack + '.' + service + "Target target = (" + pack
223                    + '.' + service + "Target) obj;");
224         buf.append("<br>String greeting = target.getGreeting();");
225         buf.append("<br></code>");
226         return buf.toString();
227     }
228
229     private boolean isMBean() {
230         boolean mbean = true;
231
232         try {
233             mbean = getOptionSet().lookup(ServiceOptionSet.MBEAN).isValue();
234         } catch (GeneratorException e) {
235             e.printStackTrace();
236             mbean = true;
237         }
238         return mbean;
239     }
240
241 }
242
Popular Tags