1 package org.jahia.tools.migration; 2 3 import org.w3c.dom.Element ; 4 import org.w3c.dom.NodeList ; 5 import org.w3c.dom.Document ; 6 import javax.xml.parsers.DocumentBuilder ; 7 import javax.xml.parsers.DocumentBuilderFactory ; 8 import java.io.FileInputStream ; 9 import java.util.Vector ; 10 import java.util.Iterator ; 11 12 import java.sql.Statement ; 13 import java.sql.ResultSet ; 14 import java.sql.SQLException ; 15 import java.sql.Connection ; 16 17 import javax.naming.NamingException ; 18 import javax.naming.directory.SearchResult ; 19 import javax.naming.NamingEnumeration ; 20 import javax.naming.directory.InitialDirContext ; 21 import javax.naming.directory.SearchControls ; 22 23 30 public class JahiaTableMigrator { 31 32 public static final String USAGE = "JahiaTableMigrator <properties_file>"; 34 public static final String ROOT_TAG = "migrator-configuration"; 35 public static final String TABLES_ROOT_TAG = "table"; 36 37 public static final int CONFIGURATION_ERROR = 1; 38 39 private static LDAPConnector ldapconn; 41 42 private static DBConnector dbconn; 44 45 private Vector tablesToMigrate; 47 48 private static org.apache.log4j.Logger logger = 49 org.apache.log4j.Logger.getLogger (JahiaTableMigrator.class); 50 51 public static void main(String [] args) { 53 JahiaTableMigrator jtm = new JahiaTableMigrator(); 54 if (jtm.loadConfiguration(args[0])) { 55 jtm.migrate(); 57 }else { 58 logger.info("Configuration error..."); 60 System.exit(CONFIGURATION_ERROR); 61 } 62 } 63 64 67 public JahiaTableMigrator() { 68 } 69 70 private boolean loadConfiguration(String fileName) { 71 boolean result = false; 72 logger.info("loading configuration"); 73 Document documentRoot = null; 74 75 try { 76 DocumentBuilderFactory dfactory = DocumentBuilderFactory.newInstance (); 78 DocumentBuilder docBuilder = dfactory.newDocumentBuilder (); 79 FileInputStream configStream = new FileInputStream (fileName); 80 documentRoot = docBuilder.parse (configStream); 81 documentRoot.normalize (); } catch (Throwable t) { 83 logger.error("loading configuration ended", t); 84 return false; 85 } 86 87 Element rootTag = documentRoot.getDocumentElement (); 89 90 if (ROOT_TAG.equals(rootTag.getTagName())) { 91 ldapconn = new LDAPConnector(rootTag); 93 dbconn = new DBConnector(rootTag); 94 95 tablesToMigrate = loadTablesDefinition(rootTag); 97 98 result = ldapconn.isInitialized() && dbconn.isInitialized() && tablesToMigrate.size() > 0; 100 } 101 102 logger.info("loading configuration ended"); 103 return result; 104 } 105 106 private Vector loadTablesDefinition(Element element) { 107 logger.info("loading tables definitions to migrate"); 108 Vector result = new Vector (); 109 110 NodeList tablesList = element.getElementsByTagName (TABLES_ROOT_TAG); 112 for (int i = 0; i < tablesList.getLength (); i++) { 113 result.add(new TableToMigrate((Element )tablesList.item(i))); 115 } 116 117 logger.info("table definitions loaded"); 118 return result; 119 } 120 121 private void migrate() { 122 logger.info("Starting migration..."); 123 try { 124 for (Iterator ite = tablesToMigrate.iterator(); ite.hasNext();) { 125 migrateTable((TableToMigrate)ite.next()); 127 } 128 } catch (SQLException sqle) { 129 logger.error("SQLException occured", sqle); 130 } catch (NamingException ne) { 131 logger.error("NamingException occured", ne); 132 } 133 logger.info("Migration ended..."); 134 } 135 136 private void migrateTable(TableToMigrate ttm) throws SQLException , NamingException { 137 logger.info("Table to migrate : " + ttm.getTableName()); 138 Connection conn = dbconn.getConnection(); 140 conn.setAutoCommit(false); 141 142 Statement stmt = conn.createStatement(); 144 ResultSet content = stmt.executeQuery("SELECT DISTINCT " 145 + ttm.getColumnName() 146 + " FROM " 147 + ttm.getTableName() 148 + " WHERE " 149 + ttm.getColumnName() 150 + " LIKE '" 151 + ttm.getDbCriteria() 152 + "'"); 153 154 while (content.next()) { 156 String value = removePrefix(ttm.getDbPrefix(), content.getString(1)); 158 NamingEnumeration ldapentries = getLdapEntry(value, ttm); 160 if (!ldapentries.hasMore()) 161 logger.warn("No results for this value :: " + value); 163 164 while (ldapentries.hasMore()) { 165 SearchResult srlt = (SearchResult )ldapentries.nextElement(); 166 if (ldapentries.hasMore()) { 167 logger.warn("duplicate ldap entry found for " + value); 169 } else { 170 String newvalue = (String )srlt.getAttributes().get(ttm.getNewAttr()).get(); 172 logger.info("ldap entry found, migrating from " + value + " to " + newvalue); 173 updateTable(conn, ttm.getDbPrefix() + value, ttm.getDbPrefix() + newvalue, ttm); 174 } 175 } 176 } 177 178 conn.commit(); 180 stmt.close(); 181 conn.close(); 182 } 183 184 private NamingEnumeration getLdapEntry(String value, TableToMigrate ttm) throws NamingException { 185 InitialDirContext idc = ldapconn.getContext(); 187 188 SearchControls sctrls = new SearchControls (); 190 sctrls.setSearchScope(SearchControls.SUBTREE_SCOPE); 191 sctrls.setReturningAttributes(new String []{ttm.getNewAttr()}); 192 193 String filter = "(&" + ttm.getLdapCriteria() + "(" + ttm.getCurrentAttr() + "=" + value + "))"; 195 196 return idc.search(ldapconn.getBaseDN(), 198 filter, 199 sctrls); 200 } 201 202 private String removePrefix(String prefix, String value) { 203 if (value.startsWith(prefix)) 204 return value.substring(prefix.length()); 205 else 206 return value; 207 } 208 209 private void updateTable(Connection conn, String oldValue, String newValue, TableToMigrate ttm) throws SQLException { 210 Statement stmt = conn.createStatement(); 212 213 stmt.execute("UPDATE " 215 + ttm.getTableName() 216 + " SET " 217 + ttm.getColumnName() 218 + "='" 219 + newValue 220 + "' WHERE " 221 + ttm.getColumnName() 222 + "='" 223 + oldValue 224 + "'"); 225 226 conn.commit(); 228 stmt.close(); 229 } 230 } | Popular Tags |