1 /*
2 * PROJECT : DAR Runtime and Tools
3 * COPYRIGHT : Copyright (C) 1999-2004 tim.stephenson@enableit.org
4 * LICENSE : GNU LESSER GENERAL PUBLIC LICENSE
5 * Version 2.1, February 1999
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */
21 package org.enableit.db.darrt.web;
22
23
24 // Java imports
25 import java.io.File;
26 import java.io.StringWriter;
27 import java.util.HashMap;
28 import java.util.List;
29 import java.util.Map;
30
31 import javax.servlet.http.HttpServletRequest;
32 import javax.servlet.http.HttpServletResponse;
33
34 import org.apache.log4j.Logger;
35 import org.apache.struts.action.Action;
36 import org.apache.struts.action.ActionError;
37 import org.apache.struts.action.ActionErrors;
38 import org.apache.struts.action.ActionForm;
39 import org.apache.struts.action.ActionForward;
40 import org.apache.struts.action.ActionMapping;
41 import org.enableit.db.beans.Database;
42 import org.enableit.db.beans.Provider;
43 import org.enableit.db.darrt.Globals;
44 import org.enableit.db.darrt.MetaDataFactory;
45 import org.enableit.db.darrt.SchemaConstants;
46 import org.enableit.db.darrt.SchemaExporter;
47
48
49 /***
50 *
51 *
52 * @author Tim Stephenson
53 * @version $Revision: 1.17 $ $Date: 2004/03/20 05:25:03 $
54 * @struts:action name="SchemaForm" path="Schema" scope="request"
55 * input="/schema.jsp" validate="true"
56 */
57 public class SchemaAction extends Action {
58 // Properties -----------------------------------------------------------------
59 public static final String PARM_OUTPUT_MODE = "outputMode";
60
61 /***
62 * The Log4J <code>Logger</code> doing the logging.
63 */
64 private static Logger logger = Logger.getLogger(SchemaAction.class);
65
66 /***
67 * A cached <code>SchemaExporter</code> for this action instance to use.
68 */
69 private SchemaExporter se;
70
71 // Methods --------------------------------------------------------------------
72
73 /***
74 * @return true if any existing schema file should be removed and a
75 * current one exported.
76 */
77 protected boolean getRefresh(ActionMapping mapping, ActionForm form,
78 HttpServletRequest request, HttpServletResponse response) {
79 logger.info("METHOD_ENTRY: getRefresh");
80
81 boolean refresh = false;
82
83 String sRefresh = request.getParameter("refresh");
84
85 if (sRefresh == null) {
86 sRefresh = (String) request.getAttribute(Globals.FORCE_SCHEMA_REFRESH);
87 }
88
89 // test for value set as attribute, then as parameter
90 if (Globals.FORCE_SCHEMA_REFRESH_TRUE.equalsIgnoreCase(sRefresh)) {
91 refresh = true;
92 }
93
94 logger.info("METHOD_EXIT: getRefresh, returning: " + refresh);
95
96 return refresh;
97 }
98
99 /***
100 * Process a request for database schema meta-data, making the result
101 * available via both Java object and Xml document in the application
102 * scope.
103 */
104 public ActionForward execute(ActionMapping mapping, ActionForm form,
105 HttpServletRequest request, HttpServletResponse response)
106 throws Exception {
107 logger.info("METHOD_ENTRY: execute");
108
109 ActionErrors errors = (ActionErrors) request.getAttribute(Action.ERROR_KEY);
110
111 ActionForward forwardTo = null;
112
113 /* logger.warn("No data source specified.") ;
114 if (errors == null) {
115 errors = new ActionErrors() ;
116 }
117 ActionError error = new ActionError(
118 "errors.required", "Data Source");
119 errors.add(ActionErrors.GLOBAL_ERROR, error) ;
120 saveErrors(request, errors);
121 return new ActionForward(mapping.getInput()) ;
122 } */
123
124 //SchemaExporter se = getSchemaExporter();
125 Provider provider = getProvider(request);
126
127 try {
128 MetaDataFactory factory = MetaDataFactory.getInstance();
129
130 // Use temp dir not 'Real root' which is not accessible if war unexploded (as in Weblogic)
131 File tempDir = (File) getServlet().getServletContext().getAttribute("javax.servlet.context.tempdir");
132
133 factory.setOperDir(tempDir);
134
135 Database database = factory.getMetaData(provider,
136 getRefresh(mapping, form, request, response));
137
138 // If got here will have database by now
139 logger.debug("Placing database attribute in application scope.");
140
141 Map dbMap = (Map) getServlet().getServletContext().getAttribute(Globals.DATABASE);
142
143 if (dbMap == null) {
144 dbMap = new HashMap();
145 }
146
147 dbMap.put(provider.getJdbc2DatasourceName(), database);
148 getServlet().getServletContext().setAttribute(Globals.DATABASE,
149 dbMap);
150
151 StringWriter databaseXml = new StringWriter();
152
153 database.marshal(databaseXml);
154 databaseXml.close();
155
156 Map dbXmlMap = (Map) getServlet().getServletContext().getAttribute(Globals.DATABASE_XML);
157
158 if (dbXmlMap == null) {
159 dbXmlMap = new HashMap();
160 }
161
162 dbXmlMap.put(provider.getJdbc2DatasourceName(),
163 databaseXml.toString());
164 getServlet().getServletContext().setAttribute(Globals.DATABASE_XML,
165 dbXmlMap);
166
167 // Allow for the fact that may be invoked as part of other action
168 if (request.getAttribute(Globals.FORWARD_TO) != null) {
169 forwardTo = mapping.findForward((String) request.getAttribute(
170 Globals.FORWARD_TO));
171 } else {
172 forwardTo = mapping.findForward("success");
173 }
174 } catch (org.enableit.db.DBException e) {
175 logger.error(e.getMessage(), e);
176
177 if (errors == null) {
178 errors = new ActionErrors();
179 }
180
181 ActionError error = new ActionError("app.dbexception",
182 e.getMessage());
183
184 errors.add(ActionErrors.GLOBAL_ERROR, error);
185 saveErrors(request, errors);
186
187 return new ActionForward(mapping.getInput());
188 } catch (Exception e) {
189 logger.error(e.getMessage(), e);
190 throw new javax.servlet.ServletException(e.getMessage());
191 }
192
193 // Return the forward instruction
194 logger.info("METHOD_EXIT: execute, forwarding to: "
195 + forwardTo.getPath());
196
197 return forwardTo;
198 }
199
200 /***
201 * Determine the file output mode (file per schema or file per table or both).
202 * @param request
203 * @return Emunerated value from <code>SchemaContants.OM_XXX</code>,
204 * never null
205 */
206 private int getOutputMode(HttpServletRequest request) {
207 int outputMode = SchemaConstants.OM_SINGLE_FILE_AND_FILE_PER_TABLE;
208
209 try {
210 outputMode = Integer.parseInt(request.getParameter(PARM_OUTPUT_MODE));
211 } catch (Exception e) {
212 logger.warn("Output mode not found, using default.");
213
214 // ignore
215 }
216
217 return outputMode;
218 }
219
220 /***
221 * Support lazy instantiation of a <code>SchemaExporter</code> instance.
222 */
223
224 /*private SchemaExporter getSchemaExporter() {
225 if (se == null) {
226 se = new SchemaExporter() ;
227 }
228 return se;
229 }*/
230
231 /***
232 * @return A schema file if one is located or null if must create new one.
233 */
234
235 /*private File getSchemaFile(ActionMapping mapping,
236 ActionForm form,
237 HttpServletRequest request,
238 HttpServletResponse response, String dsName) {
239
240 // Use temp dir not 'Real root' which is not accessible if war unexploded (as in Weblogic)
241 File tempDir = (File) getServlet().getServletContext().getAttribute(
242 "javax.servlet.context.tempdir") ;
243
244 // First try the default operating directory ...
245 File schemaFile = new File(getSchemaExporter().getOperDir(),
246 getSchemaExporter().getFileName(getProvider(dsName)) + ".xml") ;
247 // ... and then the web-app temp dir
248 if (schemaFile.exists()) {
249 // Write it to web-app temp, which we will use henceforth
250 //FileReader
251 //Database database = Database.unmarshal(new FileReader(schemaFile)) ;
252 //database.marshal(new FileWriter(new File(tempDir,
253 //getSchemaExporter().getFileName(getProvider(dsName)) + ".xml"))) ;
254 } else {
255 getSchemaExporter().setOperDir(tempDir) ;
256 schemaFile = new File(tempDir,
257 getSchemaExporter().getFileName(getProvider(dsName)) + ".xml") ;
258 }
259
260 if (getRefresh(mapping, form, request, response)) {
261 schemaFile.delete();
262 // This dir may hold single table definitions
263 new File(schemaFile.getParentFile(), "schema").delete() ;
264 }
265 return schemaFile ;
266 }*/
267
268 /***
269 * Locate or create new Provider to export schema of.
270 * @param dsName The data source name whose schema is being exported.
271 * @return
272 */
273 private Provider getProvider(HttpServletRequest request) {
274 Provider provider = (Provider) getServlet().getServletContext()
275 .getAttribute("Provider");
276
277 if (provider == null) {
278 provider = new Provider();
279
280 // Find the datasource name
281 String dsName = request.getParameter("dataSource");
282 List dsNames = (List) getServlet().getServletContext().getAttribute(Globals.DATA_SOURCE_NAMES);
283
284 if ((dsName == null) && (dsNames != null) && (dsNames.size() > 0)) {
285 dsName = (String) dsNames.get(0);
286 logger.warn("No data source specified, defaulting to: "
287 + dsName);
288 } else if (dsName == null) {
289 throw new IllegalArgumentException("No dataSource parameter,"
290 + " and no datasources found by action servlet");
291 }
292
293 provider.setJdbc2DatasourceName(dsName);
294
295 // Find the schema name (if set), in Sybase this is database name
296 String schemaName = request.getParameter("schemaName");
297
298 if ((schemaName != null) && (schemaName.trim().length() > 0)) {
299 provider.setSchemaName(schemaName);
300 } else {
301 logger.warn("No 'schemaName' specified.");
302
303 // optional, do not error if none found
304 }
305
306 getServlet().getServletContext().setAttribute("Provider", provider);
307 }
308
309 return provider;
310 }
311 }
This page was automatically generated by Maven