001: /* ====================================================================
002: * The Jcorporate Apache Style Software License, Version 1.2 05-07-2002
003: *
004: * Copyright (c) 1995-2002 Jcorporate Ltd. All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions
008: * are met:
009: *
010: * 1. Redistributions of source code must retain the above copyright
011: * notice, this list of conditions and the following disclaimer.
012: *
013: * 2. Redistributions in binary form must reproduce the above copyright
014: * notice, this list of conditions and the following disclaimer in
015: * the documentation and/or other materials provided with the
016: * distribution.
017: *
018: * 3. The end-user documentation included with the redistribution,
019: * if any, must include the following acknowledgment:
020: * "This product includes software developed by Jcorporate Ltd.
021: * (http://www.jcorporate.com/)."
022: * Alternately, this acknowledgment may appear in the software itself,
023: * if and wherever such third-party acknowledgments normally appear.
024: *
025: * 4. "Jcorporate" and product names such as "Expresso" must
026: * not be used to endorse or promote products derived from this
027: * software without prior written permission. For written permission,
028: * please contact info@jcorporate.com.
029: *
030: * 5. Products derived from this software may not be called "Expresso",
031: * or other Jcorporate product names; nor may "Expresso" or other
032: * Jcorporate product names appear in their name, without prior
033: * written permission of Jcorporate Ltd.
034: *
035: * 6. No product derived from this software may compete in the same
036: * market space, i.e. framework, without prior written permission
037: * of Jcorporate Ltd. For written permission, please contact
038: * partners@jcorporate.com.
039: *
040: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
041: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
042: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
043: * DISCLAIMED. IN NO EVENT SHALL JCORPORATE LTD OR ITS CONTRIBUTORS
044: * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
045: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
046: * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
047: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
048: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
049: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
050: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
051: * SUCH DAMAGE.
052: * ====================================================================
053: *
054: * This software consists of voluntary contributions made by many
055: * individuals on behalf of the Jcorporate Ltd. Contributions back
056: * to the project(s) are encouraged when you make modifications.
057: * Please send them to support@jcorporate.com. For more information
058: * on Jcorporate Ltd. and its products, please see
059: * <http://www.jcorporate.com/>.
060: *
061: * Portions of this software are based upon other open source
062: * products and are subject to their respective licenses.
063: */
064:
065: /**
066: * DBCreate.java
067: *
068: * Copyright 1999, 2000, 2001 Jcorporate Ltd.
069: */package com.jcorporate.expresso.services.servlet;
070:
071: import com.jcorporate.expresso.core.ExpressoSchema;
072: import com.jcorporate.expresso.core.db.DBException;
073: import com.jcorporate.expresso.core.dbobj.Schema;
074: import com.jcorporate.expresso.core.dbobj.SchemaFactory;
075: import com.jcorporate.expresso.core.dbobj.SecuredDBObject;
076: import com.jcorporate.expresso.core.jsdkapi.GenericSession;
077: import com.jcorporate.expresso.core.misc.ConfigManager;
078: import com.jcorporate.expresso.core.misc.ConfigurationException;
079: import com.jcorporate.expresso.core.misc.CurrentLogin;
080: import com.jcorporate.expresso.core.misc.StringUtil;
081: import com.jcorporate.expresso.core.security.User;
082: import com.jcorporate.expresso.core.servlet.DBServlet;
083: import com.jcorporate.expresso.core.servlet.ServletInstallLog;
084: import com.jcorporate.expresso.core.utility.DBTool;
085: import com.jcorporate.expresso.services.dbobj.SchemaList;
086: import com.jcorporate.expresso.services.html.Button;
087: import com.jcorporate.expresso.services.html.Cell;
088: import com.jcorporate.expresso.services.html.CheckBox;
089: import com.jcorporate.expresso.services.html.DropDown;
090: import com.jcorporate.expresso.services.html.Form;
091: import com.jcorporate.expresso.services.html.HiddenField;
092: import com.jcorporate.expresso.services.html.Page;
093: import com.jcorporate.expresso.services.html.Paragraph;
094: import com.jcorporate.expresso.services.html.Row;
095: import com.jcorporate.expresso.services.html.Table;
096: import com.jcorporate.expresso.services.html.Text;
097: import org.apache.log4j.Logger;
098:
099: import javax.servlet.ServletConfig;
100: import javax.servlet.ServletException;
101: import javax.servlet.http.HttpServletRequest;
102: import javax.servlet.http.HttpServletResponse;
103: import java.io.IOException;
104: import java.util.Enumeration;
105: import java.util.Iterator;
106: import java.util.List;
107: import java.util.Vector;
108:
109: /**
110: * Servlet to initialize the database and various default items
111: * in it. Use it as a friendly alternative to the DBTool command line
112: * utility.
113: *
114: * @author Michael Nash
115: * @version $Revision: 1.25 $ $Date: 2004/11/17 20:48:18 $
116: */
117: public class DBCreate extends DBServlet {
118: private static Logger log = Logger.getLogger(DBCreate.class);
119:
120: /**
121: * Initialize the serv.et
122: *
123: * @param sc The servlet config object given to us by the servlet container
124: */
125: public void init(ServletConfig sc) throws ServletException {
126: super .init(sc);
127: } /* init(ServletConfig) */
128:
129: /**
130: * doGet method presents a form allowing the user to select several
131: * intialize options
132: *
133: * @param request Standard request object
134: * @param response Standard response object
135: * @throws ServletException If an uncaught exception occurrs
136: * @throws IOException If an I/O error occurrs communicating with
137: * the client
138: */
139: public void doGet(HttpServletRequest request,
140: HttpServletResponse response) throws ServletException,
141: IOException {
142:
143: try {
144: ExpressoSchema jc = (ExpressoSchema) ExpressoSchema
145: .getInstance();
146:
147: if (jc == null) {
148: jc = new ExpressoSchema();
149: }
150:
151: boolean databaseCreated = isInitialDatabaseCreated();
152: if (!databaseCreated) {
153: setSkipLogin(true);
154: }
155:
156: super .doGet(request, response);
157:
158: if (databaseCreated && !isAdmin(request, response)) {
159: throw new java.lang.SecurityException(
160: "You must have Administrative rights to execute DBCreate");
161: }
162:
163: Page myPage = new Page("Initial Setup Options");
164: Paragraph para = new Paragraph(new Text(getString(request,
165: "Initialize_Database")));
166: para.setCSSClass("jc-pageheader"); //$NON-NLS-1$
167: myPage.add(para);
168:
169: Form myForm = new Form();
170: myPage.add(myForm);
171:
172: Table formTable = new Table("formtable"); //$NON-NLS-1$
173: myForm.add(formTable);
174: myForm.setAction(getServletPrefix(request));
175:
176: Row oneRow = new Row("db row");
177: Cell oneCell = new Cell(new Text(getString(request,
178: "Context/Database_")));
179: oneCell.setCSSClass("jc-label");
180: oneRow.add(oneCell);
181:
182: DropDown dbContext = new DropDown("dbContext", "default");
183: String oneConfigKey = null;
184: String oneDescrip = null;
185:
186: for (Enumeration ie = ConfigManager.getAllConfigKeys(); ie
187: .hasMoreElements();) {
188: oneConfigKey = (String) ie.nextElement();
189:
190: try {
191: oneDescrip = StringUtil.notNull(ConfigManager
192: .getContext(oneConfigKey).getDescription());
193: } catch (ConfigurationException ce) {
194: oneDescrip = "";
195: }
196: if (oneDescrip.equals("")) { //$NON-NLS-1$
197: oneDescrip = oneConfigKey;
198: }
199:
200: dbContext.addOption(oneConfigKey, oneDescrip);
201: }
202:
203: oneRow.add(new Cell(dbContext));
204: formTable.add(oneRow);
205: oneRow = new Row();
206: formTable.add(oneRow);
207:
208: Text t = new Text(getString(request, "Create_Tables_"));
209: t.setCSSClass("jc-label");
210: oneRow.add(new Cell(t));
211: oneRow
212: .add(new Cell(new CheckBox("CreateTables", "Y",
213: true)));
214: oneRow = new Row();
215: formTable.add(oneRow);
216:
217: t = new Text(getString(request, "Verify_Created_Tables_"));
218: t.setCSSClass("jc-label");
219: oneRow.add(new Cell(t));
220: oneRow
221: .add(new Cell(new CheckBox("VerifyTables", "Y",
222: true)));
223: oneRow = new Row();
224: formTable.add(oneRow);
225:
226: t = new Text(getString(request, "Setup_Default_Security_"));
227: t.setCSSClass("jc-label");
228: oneRow.add(new Cell(t));
229: oneRow.add(new Cell(
230: new CheckBox("SetupSecurity", "Y", true)));
231: oneRow = new Row();
232: formTable.add(oneRow);
233: t = new Text(getString(request,
234: "Create_Default_Config_Valu"));
235: t.setCSSClass("jc-label");
236: oneRow.add(new Cell(t));
237: oneRow
238: .add(new Cell(new CheckBox("ConfigValues", "Y",
239: true)));
240: oneRow = new Row();
241: formTable.add(oneRow);
242: t = new Text(getString(request, "Perform_additonal_Setup_"));
243: t.setCSSClass("jc-label");
244: oneRow.add(new Cell(t));
245: oneRow.add(new Cell(new CheckBox("OtherSetup", "Y", true)));
246:
247: Table buttonTable = new Table("button table");
248: buttonTable.setAlignment("center");
249: buttonTable.add(new Row(new Cell(new Button("Run",
250: getString(request, "Run")))));
251: myForm.add(new HiddenField("back", StringUtil
252: .notNull(request.getParameter("back"))));
253: myForm.add(buttonTable);
254: myPage.display(request, response, getString(request,
255: "charset"));
256: } catch (Exception de) {
257: showError(de, request, response);
258: }
259: } /* doGet(HttpServletRequest, HttpServletResponse) */
260:
261: /**
262: * doPost receives the options from the form & calls methods to actually
263: * perform the intiailizations
264: *
265: * @param request Standard request object
266: * @param response Standard reponse object
267: * @throws ServletException If an uncaught exception occurrs
268: * @throws IOException If an I/O error occurrs communicating with
269: * the client
270: */
271: public void doPost(HttpServletRequest request,
272: HttpServletResponse response) throws ServletException,
273: IOException {
274: boolean databaseCreated = isInitialDatabaseCreated();
275: if (!databaseCreated) {
276: setSkipLogin(true);
277: }
278:
279: super .doPost(request, response);
280:
281: if (databaseCreated && !isAdmin(request, response)) {
282: throw new java.lang.SecurityException(
283: "You must have Administrative rights to execute DBCreate");
284: }
285:
286: String createTables = StringUtil.notNull(request
287: .getParameter("CreateTables"));
288: String validateTables = StringUtil.notNull(request
289: .getParameter("VerifyTables"));
290: String setupSecurity = StringUtil.notNull(request
291: .getParameter("SetupSecurity"));
292: String setupConfigValues = StringUtil.notNull(request
293: .getParameter("ConfigValues"));
294: String otherSetup = StringUtil.notNull(request
295: .getParameter("OtherSetup"));
296: String dbContext = StringUtil.notNull(request
297: .getParameter("dbContext"));
298: response.setContentType("text/html; charset=iso-8859-1");
299: java.io.PrintWriter out = response.getWriter();
300:
301: try {
302: ExpressoSchema jc = (ExpressoSchema) SchemaFactory
303: .getInstance()
304: .getSchema(
305: com.jcorporate.expresso.core.ExpressoSchema.class
306: .getName());
307:
308: if (jc == null) {
309: throw new ServletException(
310: "Unable to construct Expresso Schema. Check logs for details");
311: }
312:
313: out
314: .println("<html><head><title>Database Initialization</title></head><body class=\"jc-default\">"
315: + "<h1 align=\"center\">Database Initialization</h1>");
316:
317: Vector allSchemas = new Vector(5);
318: allSchemas.addElement(jc);
319:
320: SchemaList sl = null;
321: SchemaList oneSchema = null;
322: boolean noOtherSchemas = true;
323:
324: try {
325: sl = new SchemaList(SecuredDBObject.SYSTEM_ACCOUNT);
326: sl.setDataContext(dbContext);
327: sl.search();
328:
329: if (sl.getFoundCount() > 0) {
330: noOtherSchemas = false;
331: }
332:
333: // search for schema classes
334:
335: } catch (DBException de) {
336: noOtherSchemas = true;
337: }
338: if (!noOtherSchemas) {
339: for (Iterator e = sl.searchAndRetrieveList().iterator(); e
340: .hasNext();) {
341: oneSchema = (SchemaList) e.next();
342:
343: boolean schemaWarning = false;
344:
345: Schema mySchema2 = SchemaFactory.getInstance()
346: .getSchema(
347: oneSchema.getField("SchemaClass"));
348:
349: if (mySchema2 == null) {
350: schemaWarning = true;
351: } else {
352: allSchemas.addElement(mySchema2);
353: }
354:
355: if (schemaWarning) {
356: out
357: .println("<p>Warning: Unable to process schema "
358: + oneSchema
359: .getField("SchemaClass")
360: + " - see log for details</p>");
361: }
362: } /* for each registered schema in this db */
363:
364: } /* if we read other schemas */
365:
366: out.println("<p align=\"center\">"
367: + getString(request, "Processing_initializations")
368: + "</p>");
369:
370: ServletInstallLog installLog = new ServletInstallLog(out);
371:
372: if (createTables.equalsIgnoreCase("Y")) {
373: DBTool.createTables(installLog, allSchemas, dbContext);
374: }
375:
376: if (validateTables.equalsIgnoreCase("Y")) {
377: List errors = DBTool.compareTables(installLog,
378: allSchemas, dbContext);
379:
380: for (Iterator iterator = errors.iterator(); iterator
381: .hasNext();) {
382: String str = (String) iterator.next();
383: out.println("<p align=\"center\">" + str + "</p>");
384: }
385: }
386:
387: try {
388: //
389: //Setting up security goes before setting up default values.
390: //
391: if (setupSecurity.equalsIgnoreCase("Y")) {
392: DBTool.setupSecurity(installLog, allSchemas,
393: dbContext);
394: } /* if we check security */
395:
396: if (setupConfigValues.equalsIgnoreCase("Y")) {
397: DBTool.populateTables(installLog, allSchemas,
398: dbContext);
399: }
400: if (otherSetup.equalsIgnoreCase("Y")) {
401: DBTool.otherSetups(installLog, allSchemas,
402: dbContext);
403: }
404: out.println("<p align=\"center\">"
405: + getString(request, "All_functions_complete")
406: + "</p>");
407:
408: //Now that things are created, let's try initializing again.
409: ConfigManager.initializeAllDBObjects();
410: ConfigManager.mapOtherDBs();
411: } catch (Exception e) {
412:
413: // handle exception with local printing, to preserve previous output
414: out
415: .println("<p align=\"center\"> PROBLEM with initialization of database: </p>");
416: e.printStackTrace(out);
417: }
418:
419: } catch (Exception de) {
420: log.error("Error performing setup", de);
421: showError(de, request, response);
422: }
423: } /* doPost(HttpServletRequest, HttpServletResponse) */
424:
425: /**
426: * Checks to see if a database of any sort has already been created in
427: * the default context.
428: *
429: * @return True if the schemalist object exists in the system
430: */
431: protected boolean isInitialDatabaseCreated() {
432: try {
433: SchemaList sl = new SchemaList(
434: SecuredDBObject.SYSTEM_ACCOUNT);
435: sl.setDataContext("default");
436: sl.count();
437: } catch (DBException dbe) {
438: return false;
439: }
440: return true;
441: }
442:
443: /**
444: * Checks to see if the current user is an Admin.
445: *
446: * @param request The ServletRequest object
447: * @param response The Servlet Response object
448: * @return true if the CurrentLogin is an Admin User
449: * @throws ServletException if there's an error processing the request
450: */
451: protected boolean isAdmin(HttpServletRequest request,
452: HttpServletResponse response) throws ServletException {
453: try {
454: CurrentLogin cl = (CurrentLogin) GenericSession
455: .getAttribute(request, "CurrentLogin");
456: if (cl == null) {
457: return false;
458: }
459:
460: int uid = cl.getUid();
461: String dbName = cl.getDBName();
462: User u = new User();
463: u.setUid(uid);
464: u.setDataContext(dbName);
465: try {
466: if (!u.find()) {
467: return false;
468:
469: }
470: } catch (DBException ex1) {
471: log.error("Error performing find", ex1);
472: return false;
473: }
474:
475: java.util.List groupList = u.getGroupsList();
476: return groupList.contains(User.ADMIN_USER);
477: } catch (ClassCastException e) {
478: return false;
479: } catch (Exception ex) {
480: throw new ServletException("Error checking isAdmin:", ex);
481: }
482: }
483:
484: } /* DBCreate */
|