upgradesessionbean.java

来自「一个免费的CA,基于EJB平台的,老师叫我们测试,现把之共享出来让大家参考」· Java 代码 · 共 492 行 · 第 1/2 页

JAVA
492
字号
            }        }        debug("<upgrade()");        return true;    }    /** Called from other migrate methods, don't call this directly, call from an interface-method     */	public boolean migradeDatabase(String resource) {        // Fetch the resource file with SQL to modify the database tables        InputStream in = this.getClass().getResourceAsStream(resource);        if (in == null) {        	error("Can not read resource for database '"+resource+"', this database probably does not need table definition changes.");        	// no error        	return true;        }        // Migrate database tables to new columns etc        Connection con = null;        info("Start migration of database.");        try {            InputStreamReader inreader = new InputStreamReader(in);            con = JDBCUtil.getDBConnection(JNDINames.DATASOURCE);            SqlExecutor sqlex = new SqlExecutor(con, false);            sqlex.runCommands(inreader);        } catch (SQLException e) {            error("SQL error during database migration: ", e);            return false;        } catch (IOException e) {            error("IO error during database migration: ", e);            return false;        } finally {            JDBCUtil.close(con);        }        error("(this is not an error) Finished migrating database.");        return true;	}    /**      * @ejb.interface-method     * @jboss.method-attributes transaction-timeout="3600"     *      */	public boolean migradeDatabase31(String dbtype) {		error("(this is not an error) Starting upgrade from ejbca 3.1.x to ejbca 3.2.x");		boolean ret = migradeDatabase("/31_32/31_32-upgrade-"+dbtype+".sql");        error("(this is not an error) Finished migrating database.");        return ret;	}    /**      * @ejb.interface-method     * @jboss.method-attributes transaction-timeout="3600"     *      */	public boolean migradeDatabase33(String dbtype) {		error("(this is not an error) Starting upgrade from ejbca 3.3.x to ejbca 3.4.x");		boolean ret = migradeDatabase("/33_34/33_34-upgrade-"+dbtype+".sql");        error("(this is not an error) Finished migrating database.");        return ret;	}	/**      * @ejb.interface-method     * @jboss.method-attributes transaction-timeout="3600"     *      */	public boolean upgradeHardTokenClassPath() {		try {			ICAAdminSessionLocal casession = getCaAdminSession(); 	        Collection caids = casession.getAvailableCAs(administrator);	        Iterator iter = caids.iterator();	        while (iter.hasNext()) {	            int caid = ((Integer) iter.next()).intValue();	            casession.upgradeFromOldCAHSMKeyStore(administrator, caid);	        }					} catch (Exception e) {			error("Error upgrading hard token class path: ", e);			return false;		}        return true;	}	/**      * @ejb.interface-method     * @jboss.method-attributes transaction-timeout="3600"     *      */    public ArrayList logConfStep1() {        ArrayList datas = new ArrayList();        try {            Collection oldColl = oldLogHome.findAll();            Iterator it = oldColl.iterator();            while (it.hasNext()) {                OldLogConfigurationDataLocal odata = (OldLogConfigurationDataLocal)it.next();                LogConfData d = new LogConfData();                d.id = odata.getId();                d.data = odata.getLogConfiguration();                d.row = odata.getLogEntryRowNumber();                datas.add(d);            }            error("(this is not an error) read "+datas.size()+" old LogConfigurationData.");        } catch (Exception e) {            error("Error reading old LogConfigurationData: ", e);            return null;        }        return datas;    }    /**      * @ejb.interface-method     * @jboss.method-attributes transaction-timeout="3600"     *      */    public boolean logConfStep2(ArrayList datas) {        try {            Iterator it2 = datas.iterator();            while (it2.hasNext()) {                LogConfData d = (LogConfData) it2.next();                OldLogConfigurationDataLocal l = oldLogHome.findByPrimaryKey(d.id);                oldLogHome.remove(l.getPrimaryKey());            }            error("(this is not an error) deleted old LogConfigurationData.");        } catch (Exception e) {            error("Failed to delete old LogConfigurationData");            return false;        }        return true;    }    /**      * @ejb.interface-method     * @jboss.method-attributes transaction-timeout="3600"     *      */    public boolean logConfStep3(ArrayList datas) {        try {            // Start creating the new ones            Iterator it2 = datas.iterator();            error("(this is not an error) Upgrading "+datas.size()+" LogConfigurationData.");            while (it2.hasNext()) {                LogConfData d = (LogConfData)it2.next();                se.anatom.ejbca.log.LogConfiguration logConf = d.data;                org.ejbca.core.model.log.LogConfiguration newLog = new org.ejbca.core.model.log.LogConfiguration(                logConf.useLogDB(), logConf.useExternalLogDevices(), logConf.getConfigurationData());                logHome.create(d.id, newLog);                LogConfigurationDataLocal dl = logHome.findByPrimaryKey(d.id);                dl.setLogEntryRowNumber(d.row);            }            error("(this is not an error) Upgraded LogConfigurationData successfully.");        } catch (Exception e) {            error("Error upgrading LogConfigurationData: ", e);              return false;        }         return true;    }        private boolean upgradeUserDataVO() {        PreparedStatement ps1 = null;        PreparedStatement ps2 = null;        Connection con = null;        int count = 0;        try {            con = JDBCUtil.getDBConnection(JNDINames.DATASOURCE);            ps1 = con.prepareStatement("select fingerprint,userDataVO from CertReqHistoryData");            ps2 = con.prepareStatement("update CertReqHistoryData set userDataVO=? where fingerprint=?");            ResultSet rs = ps1.executeQuery();            se.anatom.ejbca.common.UserDataVO oldud = null;            while (rs.next()) {                boolean goon = true;                String fp = rs.getString(1);                String ud = rs.getString(2);                try {                    java.beans.XMLDecoder decoder = null;                    try {                      decoder = new java.beans.XMLDecoder(                                new java.io.ByteArrayInputStream(ud.getBytes("UTF8")));                    } catch (UnsupportedEncodingException e) {                        goon = false;                    }                    if (goon) {                        oldud  = (se.anatom.ejbca.common.UserDataVO)decoder.readObject();                                              }                    decoder.close();                } catch (Exception e) {                    error("Can not decode old UserDataVO for fingerprint "+fp+": ", e);                    goon = false;                }                if (goon) {                    org.ejbca.core.model.ra.UserDataVO newud = createNewUserDataVO(oldud);                    ByteArrayOutputStream baos = null;                     try {                        baos = new java.io.ByteArrayOutputStream();                        java.beans.XMLEncoder encoder = new java.beans.XMLEncoder(baos);                        encoder.writeObject(newud);                        encoder.close();                        String newudstr = baos.toString("UTF8");                        ps2.setString(1, newudstr);                        ps2.setString(2, fp);                        ps2.executeUpdate();                                                                    count ++;                    } catch (Exception e) {                        error("Can not create new UserDataVO for fingerprint "+fp+": ", e);                    } finally {                        try {                            if (baos != null) baos.close();                                                    } catch (Exception e) {}                     }                }                if ( (count % 1000) == 0) {                    error("(this is not an error) migrated "+count+" UserDataVO");                }            }            //con.commit();        } catch (SQLException sqle) {            error("Error updating CertReqHistoryData: ", sqle);            return false;        } finally {            JDBCUtil.close(ps1);            JDBCUtil.close(ps2);            JDBCUtil.close(con);        }        error("(this is not an error) migrated "+count+" UserDataVO");        return true;    }        private org.ejbca.core.model.ra.UserDataVO createNewUserDataVO(se.anatom.ejbca.common.UserDataVO old) {        org.ejbca.core.model.ra.UserDataVO ret = new org.ejbca.core.model.ra.UserDataVO(                old.getUsername(), old.getDN(), old.getCAId(),                old.getSubjectAltName(),old.getEmail(),old.getStatus(),                old.getType(),old.getEndEntityProfileId(),old.getCertificateProfileId(),                old.getTimeCreated(),old.getTimeModified(),old.getTokenType(),                old.getHardTokenIssuerId(),old.getExtendedinformation()                );        return ret;    }    /**     * Enum type to hold old logconfigurationdatalocal     *     */    private class LogConfData {        public Integer id;        public se.anatom.ejbca.log.LogConfiguration data;        public int row;    }} // UpgradeSessionBean

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?