📄 setupservlet.java
字号:
package org.gridsphere.servlets;import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;import org.gridsphere.layout.PortletLayoutEngine;import org.gridsphere.portlet.impl.PortletContextImpl;import org.gridsphere.portlet.impl.SportletProperties;import org.gridsphere.portlet.service.spi.PortletServiceFactory;import org.gridsphere.portletcontainer.GridSphereEvent;import org.gridsphere.portletcontainer.impl.GridSphereEventImpl;import org.gridsphere.services.core.persistence.PersistenceManagerRdbms;import org.gridsphere.services.core.persistence.PersistenceManagerService;import org.gridsphere.services.core.persistence.impl.CreateDatabase;import org.gridsphere.services.core.portal.PortalConfigService;import org.gridsphere.services.core.security.password.PasswordEditor;import org.gridsphere.services.core.security.password.PasswordManagerService;import org.gridsphere.services.core.security.role.PortletRole;import org.gridsphere.services.core.security.role.RoleManagerService;import org.gridsphere.services.core.user.User;import org.gridsphere.services.core.user.UserManagerService;import org.hibernate.StaleObjectStateException;import javax.portlet.PortletContext;import javax.servlet.ServletConfig;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import java.io.File;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.util.ArrayList;import java.util.List;import java.util.Properties;/** * @author <a href="mailto:novotny@gridsphere.org">Jason Novotny</a> * @version $Id$ */public class SetupServlet extends HttpServlet { private Log log = LogFactory.getLog(SetupServlet.class); private PortletLayoutEngine layoutEngine = PortletLayoutEngine.getInstance(); private RoleManagerService roleService = null; private UserManagerService userManagerService = null; private PasswordManagerService passwordService = null; private PortalConfigService portalConfigService = null; public void init(ServletConfig servletConfig) throws ServletException { super.init(servletConfig); } protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { PortletContext ctx = new PortletContextImpl(getServletContext()); GridSphereEventImpl event = new GridSphereEventImpl(ctx, req, res); if (req.getAttribute("setup") == null) { redirect(event); return; } String error = (String) req.getSession(true).getAttribute("error"); if (error != null) { req.setAttribute("error", error); req.getSession().removeAttribute("error"); } // check current GS release and the DB meta file String release = SportletProperties.getInstance().getProperty("gridsphere.release"); int idx = release.lastIndexOf(" "); String gsversion = release.substring(idx + 1); //System.err.println("gsversion=" + gsversion); String dbpath = getServletContext().getRealPath("/WEB-INF/CustomPortal/database"); File dbdir = new File(dbpath); String[] filenames = dbdir.list(); String currentVersion = null; for (int i = 0; i < filenames.length; i++) { if (filenames[i].startsWith("GS")) currentVersion = filenames[i]; } setupRoles(event); File thisdbfile = new File(dbpath + File.separator + "GS_" + gsversion); // if meta file exists, redirect to the portal unless admin needs to be created if (thisdbfile.exists()) { roleService = (RoleManagerService) PortletServiceFactory.createPortletService(RoleManagerService.class, true); userManagerService = (UserManagerService) PortletServiceFactory.createPortletService(UserManagerService.class, true); passwordService = (PasswordManagerService) PortletServiceFactory.createPortletService(PasswordManagerService.class, true); portalConfigService = (PortalConfigService) PortletServiceFactory.createPortletService(PortalConfigService.class, true); PersistenceManagerService pms = null; pms = (PersistenceManagerService) PortletServiceFactory.createPortletService(PersistenceManagerService.class, true); List admins = null; PersistenceManagerRdbms pm = null; try { log.info("Starting a database transaction"); pm = pms.createGridSphereRdbms(); pm.beginTransaction(); admins = roleService.getUsersInRole(PortletRole.ADMIN); log.info("Committing the database transaction"); pm.endTransaction(); } catch (StaleObjectStateException staleEx) { log.error("This interceptor does not implement optimistic concurrency control!"); log.error("Your application will not work until you add compensation actions!"); } catch (Throwable ex) { ex.printStackTrace(); pm.endTransaction(); try { pm.rollbackTransaction(); } catch (Throwable rbEx) { log.error("Could not rollback transaction after exception!", rbEx); } } if (admins.isEmpty()) { req.setAttribute(SportletProperties.LAYOUT_PAGE, "SetupAdmin"); } else { redirect(event); return; } } else { // do a databse update since an old version exists if (currentVersion != null) { req.setAttribute(SportletProperties.LAYOUT_PAGE, "UpdateDatabase"); } else { req.setAttribute(SportletProperties.LAYOUT_PAGE, "SetupDatabase"); } } layoutEngine.actionPerformed(event); layoutEngine.service(event); } protected void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { PortletContext ctx = new PortletContextImpl(getServletContext()); GridSphereEventImpl event = new GridSphereEventImpl(ctx, req, res); System.err.println("in do post!!!"); req.setAttribute(SportletProperties.LAYOUT_PAGE, "SetupDatabase"); try { String installType = req.getParameter("install"); if (installType != null) { if (installType.equals("default")) { createDefaultDatabase(); makeDatabase(); createDatabaseFile(); req.setAttribute(SportletProperties.LAYOUT_PAGE, "SetupAdmin"); } if (installType.equals("custom")) { createExternalDatabase(event); makeDatabase(); createDatabaseFile(); req.setAttribute(SportletProperties.LAYOUT_PAGE, "SetupAdmin"); } if (installType.equals("update")) { req.setAttribute(SportletProperties.LAYOUT_PAGE, "UpdateDatabase"); updateDatabase(); removeOldDatabaseFile(); createDatabaseFile(); } if (installType.equals("admin")) { req.setAttribute(SportletProperties.LAYOUT_PAGE, "SetupAdmin"); createAdmin(event); } } } catch (IllegalArgumentException e) { req.getSession(true).setAttribute("error", e.getMessage()); } redirect(event); } private void createDefaultDatabase() { InputStream hibInputStream = getServletContext().getResourceAsStream("/WEB-INF/CustomPortal/database/hibernate.properties"); String hibPath = getServletContext().getRealPath("/WEB-INF/CustomPortal/database/hibernate.properties"); try { FileOutputStream hibOut = new FileOutputStream(hibPath); Properties hibProps = new Properties(); String connURL = "jdbc:hsqldb:" + getServletContext().getRealPath("/WEB-INF/CustomPortal/database/gridsphere"); log.debug("using connURL= " + connURL); hibProps.load(hibInputStream); hibProps.setProperty("hibernate.connection.url", connURL); hibProps.store(hibOut, "Hibernate Properties"); } catch (IOException e) { log.error("Unable to load/save hibernate.properties", e); throw new IllegalArgumentException("Unable to save hibernate.properties file! Please check the log file for more details"); } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -