Project

General

Profile

« Previous | Next » 

Revision 4080

Added by daigle about 16 years ago

Merge 1.9 changes into Head

View differences:

MetacatReplication.java
26 26

  
27 27
package edu.ucsb.nceas.metacat;
28 28

  
29
import edu.ucsb.nceas.dbadapter.AbstractDatabase;
30 29
import java.util.*;
31 30
import java.util.Date;
32 31
import java.io.*;
33 32
import java.sql.*;
34 33
import java.net.*;
35
import java.lang.*;
36 34
import java.text.*;
35

  
37 36
import javax.servlet.*;
38 37
import javax.servlet.http.*;
39 38

  
39
import edu.ucsb.nceas.dbadapter.AbstractDatabase;
40
import edu.ucsb.nceas.metacat.service.PropertyService;
41
import edu.ucsb.nceas.metacat.service.SessionService;
42
import edu.ucsb.nceas.metacat.util.LDAPUtil;
43
import edu.ucsb.nceas.metacat.util.MetaCatUtil;
44
import edu.ucsb.nceas.metacat.util.SessionData;
45
import edu.ucsb.nceas.metacat.util.SystemUtil;
46
import edu.ucsb.nceas.utilities.PropertyNotFoundException;
47

  
40 48
import org.apache.log4j.Logger;
41 49
import org.xml.sax.*;
42 50

  
......
46 54
  private Date firstTime;
47 55
  private boolean timedReplicationIsOn = false;
48 56
  Timer replicationDaemon;
49
  private static MetaCatUtil util = new MetaCatUtil();
50 57
  private Vector fileLocks = new Vector();
51 58
  private Thread lockThread = null;
52 59
  private static final AbstractDatabase dbAdapter = MetaCatUtil.dbAdapter;
......
64 71
  public void init(ServletConfig config) throws ServletException
65 72
  {
66 73
     //initialize db connections to handle any update requests
67
    MetaCatUtil util = new MetaCatUtil();
68
    //deltaT = util.getOption("deltaT");
74
    //deltaT = util.getProperty("deltaT");
69 75
    //the default deltaT can be set from metacat.properties
70 76
    //create a thread to do the delta-T check but don't execute it yet
71 77
    replicationDaemon = new Timer(true);
72 78
    try
73 79
    {
74
       timedReplicationIsOn = (new Boolean(util.getOption(TIMEREPLICATION ).trim())).booleanValue();
80
       timedReplicationIsOn = (new Boolean(PropertyService.getProperty(TIMEREPLICATION ).trim())).booleanValue();
75 81
       logMetacat.info("The timed replication on is"+timedReplicationIsOn);
76
       timeInterval = (new Long(util.getOption(TIMEREPLICATIONINTERVAl).trim())).longValue();
82
       timeInterval = (new Long(PropertyService.getProperty(TIMEREPLICATIONINTERVAl).trim())).longValue();
77 83
       logMetacat.warn("The timed replication time Inerval is "+ timeInterval);
78
       String firstTimeStr = util.getOption(FIRSTTIME);
84
       String firstTimeStr = PropertyService.getProperty(FIRSTTIME);
79 85
       logMetacat.warn("first replication time form property is "+firstTimeStr);
80 86
       firstTime = ReplicationHandler.combinateCurrentDateAndGivenTime(firstTimeStr);
81 87
       logMetacat.warn("After combine current time, the real first time is "
......
171 177
          // start, stop, getall and servercontrol need to check
172 178
          // if user is administor
173 179
          HttpSession sess = request.getSession(true);
180
          SessionData sessionData = null;
174 181
          String sess_id = "";
175 182
          String username = "";
176 183
          String[] groupnames = {""};
177
          Hashtable sessionHash = MetaCatServlet.getSessionHash();
184

  
178 185
          if (params.containsKey("sessionid")) 
179 186
          {
180 187
             sess_id = ((String[]) params.get("sessionid"))[0];
181 188
             logMetacat.info("in has sessionid "+ sess_id);
182
             if (sessionHash.containsKey(sess_id)) 
189
             if (SessionService.isSessionRegistered(sess_id)) 
183 190
             {
184 191
                  logMetacat.info("find the id " + sess_id + " in hash table");
185
                  sess = (HttpSession) sessionHash.get(sess_id);
192
                  sessionData = SessionService.getRegisteredSession(sess_id);
186 193
             }
187 194
           } 
188
           username = (String) sess.getAttribute("username");
195
          if (sessionData == null) {
196
        	  sessionData = new SessionData(sess.getId(), 
197
					(String) sess.getAttribute("username"), 
198
					(String[]) sess.getAttribute("groups"),
199
					(String) sess.getAttribute("password"));
200
          }
201
          
202
           username = sessionData.getUserName();
189 203
           logMetacat.warn("The user name from session is: "+ username);
190
           groupnames = (String[]) sess.getAttribute("groupnames");
191
           if (!MetaCatUtil.isAdministrator(username, groupnames)) 
204
           groupnames = sessionData.getGroupNames();
205
           if (!LDAPUtil.isAdministrator(username, groupnames)) 
192 206
           {
193 207
               PrintWriter out = response.getWriter();
194 208
               out.print("<error>");
......
231 245
      replicationDaemon.cancel();
232 246
      replicationDaemon = new Timer(true);
233 247
      timedReplicationIsOn = false;
234
      MetaCatUtil.setOption(TIMEREPLICATION, (new Boolean(timedReplicationIsOn)).toString());
248
      PropertyService.setProperty(TIMEREPLICATION, (new Boolean(timedReplicationIsOn)).toString());
235 249
      out.println("Replication Handler Stopped");
236 250
      MetacatReplication.replLog("deltaT handler stopped");
237 251

  
......
278 292
      
279 293
      timedReplicationIsOn = true;
280 294
      // save settings to property file
281
      MetaCatUtil.setOption(TIMEREPLICATION, (new Boolean(timedReplicationIsOn)).toString());
295
      PropertyService.setProperty(TIMEREPLICATION, (new Boolean(timedReplicationIsOn)).toString());
282 296
      // note we couldn't use firstTime object because it has date info
283 297
      // we only need time info such as 10:00 PM
284
      MetaCatUtil.setOption(FIRSTTIME, firstTimeStr);
285
      MetaCatUtil.setOption(TIMEREPLICATIONINTERVAl, (new Long(timeInterval)).toString());
298
      PropertyService.setProperty(FIRSTTIME, firstTimeStr);
299
      PropertyService.setProperty(TIMEREPLICATIONINTERVAl, (new Long(timeInterval)).toString());
286 300
      replicationDaemon.cancel();
287 301
      replicationDaemon = new Timer(true);
288 302
      replicationDaemon.scheduleAtFixedRate(new ReplicationHandler(), firstTime,
......
511 525

  
512 526
  }
513 527

  
514
  // download certificate with the public key from certURL and
515
  // upload it onto this server; it then must be imported as a
516
  // trusted certificate
517
  private void downloadCertificate (String certURL)
518
                throws FileNotFoundException, IOException, MalformedURLException
519
  {
520
    MetaCatUtil util = new MetaCatUtil();
521
    String certPath = util.getOption("certPath"); //the path to be uploaded to
528
   	// download certificate with the public key from certURL and
529
	// upload it onto this server; it then must be imported as a
530
	// trusted certificate
531
	private void downloadCertificate(String certURL) throws FileNotFoundException,
532
			IOException, MalformedURLException, PropertyNotFoundException {
533
		
534
		// the path to be uploaded to
535
		String certPath = PropertyService.getProperty("application.tomcatDir")
536
				+ "/webapps/" + PropertyService.getProperty("application.context"); 
522 537

  
523
    // get filename from the URL of the certificate
524
    String filename = certURL;
525
    int slash = Math.max(filename.lastIndexOf('/'), filename.lastIndexOf('\\'));
526
    if ( slash > -1 ) {
527
      filename = filename.substring(slash + 1);
528
    }
538
		// get filename from the URL of the certificate
539
		String filename = certURL;
540
		int slash = Math.max(filename.lastIndexOf('/'), filename.lastIndexOf('\\'));
541
		if (slash > -1) {
542
			filename = filename.substring(slash + 1);
543
		}
529 544

  
530
    // open file output strem to write the input into it
531
    File f = new File(certPath, filename);
532
    synchronized (f) {
533
      try {
534
        if ( f.exists() ) {
535
          throw new IOException("File already exist: " + f.getCanonicalFile());
536
          //if ( f.exists() && !f.canWrite() ) {
537
          //  throw new IOException("Not writable: " + f.getCanonicalFile());
538
        }
539
      } catch (SecurityException se) {
540
        // if a security manager exists,
541
        // its checkRead method is called for f.exist()
542
        // or checkWrite method is called for f.canWrite()
543
        throw se;
544
      }
545
		// open file output strem to write the input into it
546
		File f = new File(certPath, filename);
547
		synchronized (f) {
548
			try {
549
				if (f.exists()) {
550
					throw new IOException("File already exist: " + f.getCanonicalFile());
551
					// if ( f.exists() && !f.canWrite() ) {
552
					// throw new IOException("Not writable: " +
553
					// f.getCanonicalFile());
554
				}
555
			} catch (SecurityException se) {
556
				// if a security manager exists,
557
				// its checkRead method is called for f.exist()
558
				// or checkWrite method is called for f.canWrite()
559
				throw se;
560
			}
545 561

  
546
      // create a buffered byte output stream
547
      // that uses a default-sized output buffer
548
      FileOutputStream fos = new FileOutputStream(f);
549
      BufferedOutputStream out = new BufferedOutputStream(fos);
562
			// create a buffered byte output stream
563
			// that uses a default-sized output buffer
564
			FileOutputStream fos = new FileOutputStream(f);
565
			BufferedOutputStream out = new BufferedOutputStream(fos);
550 566

  
551
      // this should be http url
552
      URL url = new URL(certURL);
553
      BufferedInputStream bis = null;
554
      try {
555
        bis = new BufferedInputStream(url.openStream());
556
        byte[] buf = new byte[4 * 1024]; // 4K buffer
557
        int b = bis.read(buf);
558
        while (b != -1) {
559
          out.write(buf, 0, b);
560
          b = bis.read(buf);
561
        }
562
      } finally {
563
        if (bis != null) bis.close();
564
      }
565
      // the input and the output streams must be closed
566
      bis.close();
567
            out.flush();
568
            out.close();
569
            fos.close();
570
    } // end of synchronized(f)
571
  }
567
			// this should be http url
568
			URL url = new URL(certURL);
569
			BufferedInputStream bis = null;
570
			try {
571
				bis = new BufferedInputStream(url.openStream());
572
				byte[] buf = new byte[4 * 1024]; // 4K buffer
573
				int b = bis.read(buf);
574
				while (b != -1) {
575
					out.write(buf, 0, b);
576
					b = bis.read(buf);
577
				}
578
			} finally {
579
				if (bis != null)
580
					bis.close();
581
			}
582
			// the input and the output streams must be closed
583
			bis.close();
584
			out.flush();
585
			out.close();
586
			fos.close();
587
		} // end of synchronized(f)
588
	}
572 589

  
573 590
  /**
574
   * when a forcereplication request comes in, local host sends a read request
575
   * to the requesting server (remote server) for the specified docid.
576
   * Then store it in local database.
577
   */
591
	 * when a forcereplication request comes in, local host sends a read request
592
	 * to the requesting server (remote server) for the specified docid. Then
593
	 * store it in local database.
594
	 */
578 595
  private void handleForceReplicateRequest(PrintWriter out, Hashtable params,
579 596
                                           HttpServletResponse response, HttpServletRequest request)
580 597
  {
......
599 616
      logMetacat.info("Force replication action: "+dbaction);
600 617
      // sending back read request to remote server
601 618
      URL u = new URL("https://" + server + "?server="
602
                +util.getLocalReplicationServerName()
619
                +MetaCatUtil.getLocalReplicationServerName()
603 620
                +"&action=read&docid=" + docid);
604 621
      String xmldoc = MetacatReplication.getURLContent(u);
605 622

  
606 623
      // get the document info from server
607 624
      URL docinfourl = new URL("https://" + server +
608
                               "?server="+util.getLocalReplicationServerName()
625
                               "?server="+MetaCatUtil.getLocalReplicationServerName()
609 626
                               +"&action=getdocumentinfo&docid=" + docid);
610 627

  
611 628
      String docInfoStr = MetacatReplication.getURLContent(docinfourl);
......
759 776
      logMetacat.info("Force replication action: "+dbaction);
760 777
      // get the document info from server
761 778
      URL docinfourl = new URL("https://" + server +
762
                               "?server="+util.getLocalReplicationServerName()
779
                               "?server="+MetaCatUtil.getLocalReplicationServerName()
763 780
                               +"&action=getdocumentinfo&docid=" + docid);
764 781

  
765 782
      String docInfoStr = MetacatReplication.getURLContent(docinfourl);
......
798 815
        //Get data file and store it into local file system.
799 816
        // sending back readdata request to server
800 817
        URL url = new URL("https://" + server + "?server="
801
                +util.getLocalReplicationServerName()
818
                +MetaCatUtil.getLocalReplicationServerName()
802 819
                +"&action=readdata&docid=" + docid);
803
        String datafilePath = util.getOption("datafilepath");
820
        String datafilePath = PropertyService.getProperty("application.datafilepath");
804 821
        //register data file into xml_documents table and wite data file
805 822
        //into file system
806 823
        DocumentImpl.writeDataFileInReplication(url.openStream(), datafilePath,
......
941 958

  
942 959
  {
943 960
    // File path for data file
944
    String filepath = util.getOption("datafilepath");
961
    String filepath;
945 962
    // Request docid
946 963
    String docId = ((String[])(params.get("docid")))[0];
947 964
    //check if the doicd is null
......
955 972
    //in the key store, this is security issue
956 973
    try
957 974
    {
975
      filepath = PropertyService.getProperty("application.datafilepath");
958 976
      String server = ((String[])params.get("server"))[0];
959 977
      URL u = new URL("https://" + server + "?server="
960
                +util.getLocalReplicationServerName()
978
                +MetaCatUtil.getLocalReplicationServerName()
961 979
                +"&action=test");
962 980
      String test = MetacatReplication.getURLContent(u);
963 981
      //couldn't pass the test
......
1062 1080
      //in the key store, this is security issue
1063 1081
      String server = ((String[])params.get("server"))[0];
1064 1082
      URL u = new URL("https://" + server + "?server="
1065
                +util.getLocalReplicationServerName()
1083
                +MetaCatUtil.getLocalReplicationServerName()
1066 1084
                +"&action=test");
1067 1085
      String test = MetacatReplication.getURLContent(u);
1068 1086
      //couldn't pass the test
......
1144 1162
      //try to open a https stream to test if the request server's public key
1145 1163
      //in the key store, this is security issue
1146 1164
      URL u = new URL("https://" + server + "?server="
1147
                +util.getLocalReplicationServerName()
1165
                +MetaCatUtil.getLocalReplicationServerName()
1148 1166
                +"&action=test");
1149 1167
      String test = MetacatReplication.getURLContent(u);
1150 1168
      //couldn't pass the test
......
1180 1198

  
1181 1199
      // Append local server's name and replication servlet to doclist
1182 1200
      doclist.append("<?xml version=\"1.0\"?><replication>");
1183
      doclist.append("<server>").append(util.getLocalReplicationServerName());
1184
      //doclist.append(util.getOption("replicationpath"));
1201
      doclist.append("<server>").append(MetaCatUtil.getLocalReplicationServerName());
1202
      //doclist.append(util.getProperty("replicationpath"));
1185 1203
      doclist.append("</server><updates>");
1186 1204

  
1187 1205
      // Get correct docid that reside on this server according the requesting
......
1220 1238
      ResultSet rs = pstmt.getResultSet();
1221 1239
      boolean tablehasrows = rs.next();
1222 1240
      //If metacat configed to replicate data file
1223
      //if ((util.getOption("replicationsenddata")).equals("on"))
1241
      //if ((util.getProperty("replicationsenddata")).equals("on"))
1224 1242
      boolean replicateData = serverList.getDataReplicationValue(server);
1225 1243
      if (replicateData)
1226 1244
      {
......
1228 1246
        {
1229 1247
          String recordDoctype = rs.getString(3);
1230 1248
          Vector packagedoctypes = MetaCatUtil.getOptionList(
1231
                                     MetaCatUtil.getOption("packagedoctype"));
1249
                                     PropertyService.getProperty("packagedoctype"));
1232 1250
          //if this is a package file, put it at the end
1233 1251
          //because if a package file is read before all of the files it
1234 1252
          //refers to are loaded then there is an error
......
1253 1271
                doclist.append("</docid><rev>").append(rs.getInt(2));
1254 1272
                doclist.append("</rev>");
1255 1273
                doclist.append("<datafile>");
1256
                doclist.append(MetaCatUtil.getOption("datafileflag"));
1274
                doclist.append(PropertyService.getProperty("datafileflag"));
1257 1275
                doclist.append("</datafile>");
1258 1276
                doclist.append("</updatedDocument>");
1259 1277
              }//else
......
1276 1294
          if(!recordDoctype.equals("BIN"))
1277 1295
          { //don't replicate data files
1278 1296
            Vector packagedoctypes = MetaCatUtil.getOptionList(
1279
                                     MetaCatUtil.getOption("packagedoctype"));
1297
                                     PropertyService.getProperty("packagedoctype"));
1280 1298
            if(recordDoctype != null && !packagedoctypes.contains(recordDoctype))
1281 1299
            {   //if this is a package file, put it at the end
1282 1300
              //because if a package file is read before all of the files it
......
1400 1418
            if (recordDoctype.equals("BIN"))
1401 1419
            {
1402 1420
                revDocList.append("<datafile>");
1403
                revDocList.append(MetaCatUtil.getOption("datafileflag"));
1421
                revDocList.append(PropertyService.getProperty("datafileflag"));
1404 1422
                revDocList.append("</datafile>");
1405 1423
            }
1406 1424
            revDocList.append("</revisionDocument>");
......
1459 1477
        sb.append("</entry_type><source_doctype>").append(rs.getString(2));
1460 1478
        sb.append("</source_doctype><target_doctype>").append(rs.getString(3));
1461 1479
        sb.append("</target_doctype><public_id>").append(rs.getString(4));
1462
        sb.append("</public_id><system_id>").append(rs.getString(5));
1480
        // system id may not have server url on front.  Add it if not.
1481
        String systemID = rs.getString(5);
1482
        if (!systemID.startsWith("http://")) {
1483
        	systemID = SystemUtil.getServerURL() + systemID;
1484
        }
1485
        sb.append("</public_id><system_id>").append(systemID);
1463 1486
        sb.append("</system_id></row>");
1464 1487

  
1465 1488
        tablehasrows = rs.next();
......
1674 1697
    Hashtable sl = new Hashtable();
1675 1698
    DBConnection dbConn = null;
1676 1699
    int serialNumber = -1;
1677
    //MetaCatUtil ut=new MetaCatUtil();
1678 1700
    docId=MetaCatUtil.getDocIdFromString(docId);
1679 1701
    PreparedStatement pstmt=null;
1680 1702
    int serverLocation;
......
1766 1788
    int serialNumber = -1;
1767 1789
    PreparedStatement pstmt = null;
1768 1790
    int serverCode = 1;
1769
    //MetaCatUtil ut = new MetaCatUtil();
1770 1791
    String docId=MetaCatUtil.getDocIdFromString(accNum);
1771 1792

  
1772 1793
    try
......
1855 1876
  }
1856 1877

  
1857 1878
  /**
1858
   * Method for writing replication messages to a log file specified in
1859
   * metacat.properties
1860
   */
1861
  public static void replLog(String message)
1862
  {
1863
    try
1864
    {
1865
      FileOutputStream fos = new FileOutputStream(
1866
                                 util.getOption("replicationlog"), true);
1867
      PrintWriter pw = new PrintWriter(fos);
1868
      SimpleDateFormat formatter = new SimpleDateFormat ("yy-MM-dd HH:mm:ss");
1869
      java.util.Date localtime = new java.util.Date();
1870
      String dateString = formatter.format(localtime);
1871
      dateString += " :: " + message;
1872
      //time stamp each entry
1873
      pw.println(dateString);
1874
      pw.flush();
1875
    }
1876
    catch(Exception e)
1877
    {
1878
      System.out.println("error writing to replication log from " +
1879
                         "MetacatReplication.replLog: " + e.getMessage());
1880
      //e.printStackTrace(System.out);
1881
    }
1882
  }
1879
	 * Method for writing replication messages to a log file specified in
1880
	 * metacat.properties
1881
	 */
1882
	public static void replLog(String message) {
1883
		try {
1884
			FileOutputStream fos = 
1885
				new FileOutputStream(PropertyService.getProperty("application.tomcatDir")
1886
					+ "/logs/metacatreplication.log", true);
1887
			PrintWriter pw = new PrintWriter(fos);
1888
			SimpleDateFormat formatter = new SimpleDateFormat("yy-MM-dd HH:mm:ss");
1889
			java.util.Date localtime = new java.util.Date();
1890
			String dateString = formatter.format(localtime);
1891
			dateString += " :: " + message;
1892
			// time stamp each entry
1893
			pw.println(dateString);
1894
			pw.flush();
1895
		} catch (Exception e) {
1896
			System.out.println("error writing to replication log from "
1897
					+ "MetacatReplication.replLog: " + e.getMessage());
1898
			// e.printStackTrace(System.out);
1899
		}
1900
	}
1883 1901

  
1884 1902
  /**
1885
   * Method for writing replication messages to a log file specified in
1886
   * metacat.properties
1887
   */
1903
	 * Method for writing replication messages to a log file specified in
1904
	 * metacat.properties
1905
	 */
1888 1906
  public static void replErrorLog(String message)
1889 1907
  {
1890 1908
    try
1891 1909
    {
1892 1910
      FileOutputStream fos = new FileOutputStream(
1893
                                 util.getOption("replicationerrorlog"), true);
1911
                                 PropertyService.getProperty("replicationerrorlog"), true);
1894 1912
      PrintWriter pw = new PrintWriter(fos);
1895 1913
      SimpleDateFormat formatter = new SimpleDateFormat ("yy-MM-dd HH:mm:ss");
1896 1914
      java.util.Date localtime = new java.util.Date();

Also available in: Unified diff