Project

General

Profile

« Previous | Next » 

Revision 4856

Added by daigle almost 16 years ago

Introduce replication user. Use the fileutil writer methods instead of writing directly.

View differences:

lib/style/skins/dev/replMain.html
72 72
<table border="0" cellpadding="4" cellspacing="0" width="100%" 
73 73
       class="tablepanel">
74 74
  <tr class="tablehead">
75
    <td><b>Force Replication</b></td>
75
    <td><b>Replicate Now</b></td>
76 76
  </tr>
77 77
  <tr>
78 78
    <td rowspan="2" width="50%">
src/edu/ucsb/nceas/metacat/MetacatReplication.java
66 66
  private static final String FIRSTTIME  = "replication.firsttimedreplication";
67 67
  private static final int    TIMEINTERVALLIMIT = 7200000;
68 68
  private static Logger logMetacat = Logger.getLogger(MetacatReplication.class);
69
  public static final String REPLICATIONUSER = "replication";
70 69

  
71 70
  /**
72 71
   * Initialize the servlet by creating appropriate database connections
......
659 658
      Hashtable<String,String> docinfoHash = dih.getDocInfo();
660 659

  
661 660
      // Get user owner of this docid
662
      String user = (String)docinfoHash.get("user_owner");
661
//      String user = (String)docinfoHash.get("user_owner");
662
      // replication user owns all docs from here on out.
663
      String user = SessionService.REPLICATION_USER_NAME;
663 664
      // Get home server of this docid
664 665
      String homeServer=(String)docinfoHash.get("home_server");
665 666
      String createdDate = (String)docinfoHash.get("date_created");
......
723 724
      
724 725
      MetacatReplication.replLog("document " + docid + " added to DB with " +
725 726
                                 "action " + dbaction);
726
      EventLog.getInstance().log(request.getRemoteAddr(), REPLICATIONUSER, docid, dbaction);
727
      EventLog.getInstance().log(request.getRemoteAddr(), SessionService.REPLICATION_USER_NAME, docid, dbaction);
727 728
    }//try
728 729
    catch(Exception e)
729 730
    {
......
757 758
    MetacatReplication.replLog("force replication delete docid " + docid);
758 759
    logMetacat.info("Force replication delete request from: "+ server);
759 760
    logMetacat.info("Force replication delete docid: "+docid);
760
    DocumentImpl.delete(docid, null, null, server);
761
    DocumentImpl.delete(docid, SessionService.REPLICATION_USER_NAME, null, server);
761 762
    MetacatReplication.replLog("document " + docid + " was successfully deleted ");
762
    EventLog.getInstance().log(request.getRemoteAddr(), REPLICATIONUSER, docid, "delete");
763
    EventLog.getInstance().log(request.getRemoteAddr(), SessionService.REPLICATION_USER_NAME, docid, "delete");
763 764
    logMetacat.info("document " + docid + " was successfully deleted ");
764 765
  }
765 766
  catch(Exception e)
......
834 835
      XMLReader docinfoParser = ReplicationHandler.initParser(dih);
835 836
      docinfoParser.parse(new InputSource(new StringReader(docInfoStr)));
836 837
      Hashtable<String,String> docinfoHash = dih.getDocInfo();
837
      String user = (String)docinfoHash.get("user_owner");
838
      //String user = (String)docinfoHash.get("user_owner");
839
      String user = (String)docinfoHash.get(SessionService.REPLICATION_USER_NAME);
838 840

  
839 841
      String docName = (String)docinfoHash.get("docname");
840 842

  
......
897 899
        //false means non-timed replication
898 900
        MetacatReplication.replLog("datafile " + docid + " added to DB with " +
899 901
                "action " + dbaction);
900
        EventLog.getInstance().log(request.getRemoteAddr(), REPLICATIONUSER, docid, dbaction);
902
        EventLog.getInstance().log(request.getRemoteAddr(), SessionService.REPLICATION_USER_NAME, docid, dbaction);
901 903
     }
902 904
    
903 905
    }
......
1183 1185
			if (FileUtil.getFileStatus(documentPath) == FileUtil.DOES_NOT_EXIST
1184 1186
					|| FileUtil.getFileSize(documentPath) == 0) {
1185 1187
				FileWriter fileWriter = new FileWriter(documentPath);
1186
				di.toXml(fileWriter, null, null, true);
1188
				di.toXml(fileWriter, SessionService.REPLICATION_USER_NAME, null, true);
1187 1189
			}
1188 1190

  
1189 1191
			// read the file from disk and sent it to PrintWriter
1190 1192
			// PrintWriter out = new PrintWriter(streamOut);
1191
			di.readFromFileSystem(out, null, null, documentPath);
1193
			di.readFromFileSystem(out, SessionService.REPLICATION_USER_NAME, null, documentPath);
1192 1194

  
1193 1195
			// response.setContentType("text/xml");
1194 1196
			// out.print(di.toString(null, null, true));
src/edu/ucsb/nceas/metacat/PermissionController.java
30 30
import java.sql.*;
31 31
import java.util.Enumeration;
32 32
import java.util.Hashtable;
33
import java.util.Stack;
34 33
import java.util.Vector;
35
import java.util.Iterator;
36 34

  
37 35
import org.apache.log4j.Logger;
38 36

  
39 37
import edu.ucsb.nceas.metacat.service.PropertyService;
38
import edu.ucsb.nceas.metacat.service.SessionService;
40 39
import edu.ucsb.nceas.metacat.util.MetacatUtil;
41 40
import edu.ucsb.nceas.utilities.PropertyNotFoundException;
42 41

  
......
45 44
   private String docId = null;
46 45
   private boolean hasSubTreeAccessControl = false; // flag if has a subtree
47 46
                                                    // access for this docid
48
   private Vector subTreeList = new Vector();
47
   private Vector<SubTree> subTreeList = new Vector<SubTree>();
49 48

  
50 49
   private long TOPLEVELSTARTNODEID = 0; //if start node is 0, means it is top
51 50
                                         //level document
......
107 106
    {
108 107
      return true;
109 108
    }
109
    
110
    if (user.equals(SessionService.REPLICATION_USER_NAME)) {
111
    	return true;
112
    }
110 113

  
111 114
    //create a userpackage including user, public and group member
112 115
    userPackage=createUsersPackage(user, groups);
......
242 245
  {
243 246
    boolean flag = true;
244 247
    // Get unaccessble subtree for this user
245
    Hashtable unaccessableSubTree = hasUnaccessableSubTree(user, groups,
248
    Hashtable<Long, SubTree> unaccessableSubTree = hasUnaccessableSubTree(user, groups,
246 249
                                                           myPermission);
247
    Enumeration en = unaccessableSubTree.elements();
250
    Enumeration<SubTree> en = unaccessableSubTree.elements();
248 251
    while (en.hasMoreElements())
249 252
    {
250 253
      SubTree tree = (SubTree)en.nextElement();
......
266 269
   * @param groups  the groups which the use is in
267 270
   * @param myPermission  permission type to check for
268 271
   */
269
  public Hashtable hasUnaccessableSubTree(String user, String[] groups,
272
  public Hashtable<Long, SubTree> hasUnaccessableSubTree(String user, String[] groups,
270 273
                                       String myPermission) throws McdbException
271 274
  {
272
    Hashtable resultUnaccessableSubTree = new Hashtable();
275
    Hashtable<Long, SubTree> resultUnaccessableSubTree = new Hashtable<Long, SubTree>();
273 276
    String [] principals=null;
274 277
    int permission =AccessControlList.intValue(myPermission);
275 278

  
......
369 372
   * is a subtree of subtree a. And user doesn't have read permission for both
370 373
   * so we only use subtree a is enough.
371 374
   */
372
  private Hashtable mergeEquivalentSubtree(Hashtable unAccessSubTree)
375
  private Hashtable<Long, SubTree> mergeEquivalentSubtree(Hashtable<Long, SubTree> unAccessSubTree)
373 376
  {
374
    Hashtable newSubTreeHash = new Hashtable();
377
    Hashtable<Long, SubTree> newSubTreeHash = new Hashtable<Long, SubTree>();
375 378
    boolean   needDelete = false;
376 379
    // check the parameters
377 380
    if (unAccessSubTree == null || unAccessSubTree.isEmpty())
......
383 386
      // look every subtree start point and stop point, to see if it is embedded
384 387
      // in another one. If embedded, they are equavelent and we can use bigger
385 388
      // one to replace smaller one
386
      Enumeration en = unAccessSubTree.elements();
389
      Enumeration<SubTree> en = unAccessSubTree.elements();
387 390
      while (en.hasMoreElements())
388 391
      {
389 392
        SubTree tree    = (SubTree)en.nextElement();
......
391 394
        long    startId = tree.getStartNodeId();
392 395
        long    endId   = tree.getEndNodeId();
393 396

  
394
        Enumeration enu = unAccessSubTree.elements();
397
        Enumeration<SubTree> enu = unAccessSubTree.elements();
395 398
        while (enu.hasMoreElements())
396 399
        {
397 400
          SubTree subTree = (SubTree)enu.nextElement();
398
          String subTreeId= subTree.getSubTreeId();
401
          //String subTreeId= subTree.getSubTreeId();
399 402
          long   subTreeStartId = subTree.getStartNodeId();
400 403
          long   subTreeEndId   = subTree.getEndNodeId();
401 404
          //compare and if the first subtree is a subtree of the second
......
655 658
                  throws SQLException, Exception
656 659
  {
657 660
	Vector<AccessControlForSingleFile> accessControl = new Vector<AccessControlForSingleFile>();
658
    boolean hasRow;
661
    //boolean hasRow;
659 662
    PreparedStatement pStmt = null;
660 663
    DBConnection conn = null;
661 664
    int serialNumber = -1;
......
754 757
   PreparedStatement pStmt = null;
755 758
   int permissionValue=permission;
756 759
   int permissionValueInTable;
757
   int ticketCount;
760
   //int ticketCount;
758 761
   DBConnection conn = null;
759 762
   int serialNumber = -1;
760 763
   boolean topLever = false;
......
1081 1084
                               boolean withRevision)
1082 1085
                             throws SQLException,McdbException, Exception
1083 1086
   {
1084
      Hashtable<String, String> unAccessibleIdList = new Hashtable();
1085
      Hashtable allIdList = getAllInlineDataIdList(docid);
1087
      Hashtable<String, String> unAccessibleIdList = new Hashtable<String, String>();
1088
      Hashtable<String, String> allIdList = getAllInlineDataIdList(docid);
1086 1089
      Enumeration<String> en = allIdList.keys();
1087 1090
      while (en.hasMoreElements())
1088 1091
      {
......
1124 1127
    * about the inline data. The key is subtree id and data is a inline internal
1125 1128
    * file name
1126 1129
    */
1127
   private static Hashtable getAllInlineDataIdList(String docid) throws SQLException
1130
   private static Hashtable<String, String> getAllInlineDataIdList(String docid) throws SQLException
1128 1131
   {
1129
     Hashtable inlineDataList = new Hashtable();
1132
     Hashtable<String, String> inlineDataList = new Hashtable<String, String>();
1130 1133
     String sql = "SELECT subtreeid, docid FROM xml_access WHERE " +
1131 1134
                   "accessfileid = ? AND subtreeid  IS NOT NULL";
1132 1135
     PreparedStatement pStmt=null;
src/edu/ucsb/nceas/metacat/DocumentImpl.java
65 65
import edu.ucsb.nceas.utilities.FileUtil;
66 66
import edu.ucsb.nceas.utilities.PropertyNotFoundException;
67 67
import edu.ucsb.nceas.utilities.StringUtil;
68
import edu.ucsb.nceas.utilities.UtilException;
68 69

  
69 70
import org.apache.log4j.Logger;
70 71
import org.xml.sax.ContentHandler;
......
1394 1395
    private static void writeToFileSystem(String xml, String accNumber) throws McdbException {
1395 1396

  
1396 1397
		// write the document to disk
1397
		FileWriter fileWriter = null;
1398
//		FileWriter fileWriter = null;
1398 1399
		String documentDir = null;
1399 1400
		String documentPath = null;
1400 1401

  
......
1416 1417
			}
1417 1418
			
1418 1419
			if (accNumber != null
1419
					&& FileUtil.getFileStatus(documentPath) == FileUtil.DOES_NOT_EXIST) {
1420
					&& (FileUtil.getFileStatus(documentPath) == FileUtil.DOES_NOT_EXIST 
1421
							|| FileUtil.getFileSize(documentPath) == 0)) {
1420 1422

  
1421
				fileWriter = new FileWriter(documentPath);
1422
				fileWriter.write(xml);
1423
				fileWriter.close();
1423
				FileUtil.writeFile(documentPath, xml);
1424
//				fileWriter = new FileWriter(documentPath);
1425
//				fileWriter.write(xml);
1426
//				fileWriter.close();
1424 1427
			}			
1425 1428

  
1426 1429
		} catch (PropertyNotFoundException pnfe) {
1427 1430
			throw new McdbException("Could not write file: " + documentPath + " : "
1428 1431
					+ pnfe.getMessage());
1429
		} catch (IOException ioe) {
1432
		} catch (UtilException ue) {
1430 1433
			throw new McdbException("Could not write file: " + documentPath + " : "
1431
					+ ioe.getMessage());
1434
					+ ue.getMessage());
1432 1435
		}
1433 1436
	}	
1434 1437
    
src/edu/ucsb/nceas/metacat/ReplicationHandler.java
28 28

  
29 29
import edu.ucsb.nceas.metacat.service.DatabaseService;
30 30
import edu.ucsb.nceas.metacat.service.PropertyService;
31
import edu.ucsb.nceas.metacat.service.SessionService;
31 32
import edu.ucsb.nceas.metacat.util.MetacatUtil;
32 33
import edu.ucsb.nceas.utilities.PropertyNotFoundException;
33 34

  
34 35
import java.sql.*;
35 36
import java.util.*;
36 37
import java.util.Date;
37
import java.lang.Thread;
38 38
import java.io.*;
39 39
import java.net.*;
40 40
import java.text.*;
......
42 42
import org.apache.log4j.Logger;
43 43
import org.xml.sax.AttributeList;
44 44
import org.xml.sax.ContentHandler;
45
import org.xml.sax.DTDHandler;
46
import org.xml.sax.EntityResolver;
47 45
import org.xml.sax.ErrorHandler;
48 46
import org.xml.sax.InputSource;
49 47
import org.xml.sax.XMLReader;
50
import org.xml.sax.SAXException;
51
import org.xml.sax.SAXParseException;
52 48
import org.xml.sax.helpers.XMLReaderFactory;
53 49
import org.xml.sax.helpers.DefaultHandler;
54 50

  
......
134 130
                                        // ReplicationServer got from
135 131
                                        // Server list
136 132
    String server = null; // Variable to store server name
137
    String update;
138
    Vector responses = new Vector();
133
//    String update;
134
    Vector<String> responses = new Vector<String>();
139 135
    URL u;
140 136

  
141 137

  
......
393 389
          REVINSERTNUMBER++;
394 390
      }
395 391
      String ip = getIpFromURL(u);
396
      EventLog.getInstance().log(ip, MetacatReplication.REPLICATIONUSER, accNumber, actions);
392
      EventLog.getInstance().log(ip, SessionService.REPLICATION_USER_NAME, accNumber, actions);
397 393
      
398 394

  
399 395
    }//try
......
522 518
            REVINSERTNUMBER++;
523 519
        }
524 520
        String ip = getIpFromURL(u);
525
        EventLog.getInstance().log(ip, MetacatReplication.REPLICATIONUSER, accNumber, actions);
521
        EventLog.getInstance().log(ip, SessionService.REPLICATION_USER_NAME, accNumber, actions);
526 522
        
527 523
      }//if
528 524
      else
......
588 584
         MetacatReplication.replLog("Doc " + docId + " deleted");
589 585
         URL u = new URL("https://"+notifyServer);
590 586
         String ip = getIpFromURL(u);
591
         EventLog.getInstance().log(ip, MetacatReplication.REPLICATIONUSER, docId, "delete");
587
         EventLog.getInstance().log(ip, SessionService.REPLICATION_USER_NAME, docId, "delete");
592 588
      }
593 589

  
594 590
    }//try
......
1065 1061
					continue;
1066 1062
				}
1067 1063

  
1068
			} catch (Exception e) {
1069
				logMetacat.error("error to handle update doc in " + tableName
1070
						+ " in time replication" + e.getMessage());
1064
			} 
1065
			
1066
			catch (Exception e) {
1067
				logMetacat.error("error handling update doc in " + tableName
1068
						+ " in timed replication: " + e.getMessage());
1071 1069
				continue;
1072 1070
			}
1073 1071

  
src/edu/ucsb/nceas/metacat/service/SessionService.java
40 40
	private static Hashtable<String, SessionData> sessionHash = null;
41 41
	
42 42
	private static final String PUBLIC_SESSION_ID = "0";
43
	public static final String PUBLIC_USER_NAME = "public";
44
	
45
	private static final String REPLICATION_SESSION_ID = "1";
46
	public static final String REPLICATION_USER_NAME = "replication";
43 47

  
44 48
	/**
45 49
	 * private constructor since this is a singleton
......
47 51
	private SessionService() throws ServiceException {
48 52
		sessionHash = new Hashtable<String, SessionData>();
49 53
		logMetacat.debug("Registering public session id: " + PUBLIC_SESSION_ID);
50
		registerSession(PUBLIC_SESSION_ID, "public", null, null);
54
		registerSession(PUBLIC_SESSION_ID, PUBLIC_USER_NAME, null, null);
55
		registerSession(REPLICATION_SESSION_ID, REPLICATION_USER_NAME, null, null);
51 56
	}
52 57
	
53 58
	/**
......
159 164
	}
160 165
	
161 166
	/**
167
	 * Get the replication session from the session hash table. 
168
	 */
169
	public static SessionData getReplicationSession() {
170
		return sessionHash.get(REPLICATION_SESSION_ID);
171
	}
172
	
173
	/**
162 174
	 * Keep a session active by updating its last accessed time. 
163 175
	 * 
164 176
	 * @param sessionId
src/edu/ucsb/nceas/metacat/Eml200SAXHandler.java
2680 2680
          }
2681 2681
          else
2682 2682
          {
2683
            throw new SAXException("User does not have permission to update " +
2684
                  "of access rules for data file "+ docid);
2683
            throw new SAXException("User: " + user + " does not have permission to update " +
2684
                  "access rules for data file "+ docid);
2685 2685
          }
2686 2686
        }//try
2687 2687
        catch(Exception e)

Also available in: Unified diff