setup-global.jspf 4.26 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
<%--
  -	$RCSfile$
  -	$Revision$
  -	$Date$
--%>

<%@ page import="java.lang.reflect.Method,
                 java.beans.PropertyDescriptor,
                 java.sql.Connection,
                 org.jivesoftware.database.DbConnectionManager,
                 java.io.File,
                 java.sql.Statement,
                 java.sql.SQLException,
                 java.util.Map,
                 org.jivesoftware.util.ClassUtils" %>

<%  // Figure out if we've already run setup:

	boolean doSetup = false;
    //
    try {
        // ServiceLookup lookup = ServiceLookupFactory.getServiceLookup();
        Class serviceLookupFactoryClass = loadClass("org.jivesoftware.messenger.container.ServiceLookupFactory");
        Method getLookupMethod = serviceLookupFactoryClass.getMethod("getServiceLookup", (Class[])null);
        Object serviceLookupObj = getLookupMethod.invoke(serviceLookupFactoryClass, (Object)null);

        // Container container = (Container)lookup.lookup(Container.class);
        Method lookupMethod = serviceLookupObj.getClass().getMethod("lookup", (Class[])new Class[]{java.lang.Class.class});
        Object containerObj = lookupMethod.invoke(serviceLookupObj, (Object[])new Class[]{org.jivesoftware.messenger.container.Container.class});

        // boolean isSetup = container.isSetupMode()
        Method isSetupModeMethod = containerObj.getClass().getMethod("isSetupMode", (Class[])null);
        Object isSetupObj = isSetupModeMethod.invoke(containerObj, (Object)null);
        boolean setupMode = ((Boolean)isSetupObj).booleanValue();

        if (setupMode) {
            doSetup = true;
        }
    }
    catch (Throwable t) {
        //t.printStackTrace();
        doSetup = true;
    }

    if (!doSetup) {
        response.sendRedirect("setup-completed.jsp");
        return;
    }

    // embedded mode?
    boolean embeddedMode = false;
    try {
Matt Tucker's avatar
Matt Tucker committed
53
        ClassUtils.forName("org.jivesoftware.messenger.starter.ServerStarter");
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
        embeddedMode = true;
    }
    catch (Exception ignored) {}

    // sidebar var for sidebar page - it has to be global.
    boolean showSidebar = true;
%>

<%! // Trys to load a class 3 different ways.
    Class loadClass(String className) throws ClassNotFoundException {
        Class theClass = null;
        try {
            theClass = Class.forName(className);
        }
        catch (ClassNotFoundException e1) {
            try {
                theClass = Thread.currentThread().getContextClassLoader().loadClass(className);
            }
            catch (ClassNotFoundException e2) {
                theClass = getClass().getClassLoader().loadClass(className);
            }
        }
        return theClass;
    }

    final PropertyDescriptor getPropertyDescriptor(PropertyDescriptor[] pd, String name) {
        for (int i=0; i<pd.length; i++) {
            if (name.equals(pd[i].getName())) {
                return pd[i];
            }
        }
        return null;
    }

    boolean testConnection(Map errors) {
        boolean success = true;
        Connection con = null;
        try {
            con = DbConnectionManager.getConnection();
            if (con == null) {
                success = false;
                errors.put("general","A connection to the database could not be "
                    + "made. View the error message by opening the "
Matt Tucker's avatar
Matt Tucker committed
97
                    + "\"" + File.separator + "logs" + File.separator + "error.log\" log "
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
                    + "file, then go back to fix the problem.");
            }
            else {
            	// See if the Jive db schema is installed.
            	try {
            		Statement stmt = con.createStatement();
            		// Pick an arbitrary table to see if it's there.
            		stmt.executeQuery("SELECT * FROM jiveID");
            		stmt.close();
            	}
            	catch (SQLException sqle) {
                    success = false;
                    errors.put("general","The Jive Messenger database schema does not "
                        + "appear to be installed. Follow the installation guide to "
                        + "fix this error.");
            	}
            }
        }
        catch (Exception ignored) {}
        finally {
            try {
        	    con.close();
            } catch (Exception ignored) {}
        }
        return success;
    }
%>