Project

General

Profile

« Previous | Next » 

Revision 1023

Added by Jing Tao almost 22 years ago

Add two method: handleGetDataFileRequest and handleForceReplicateDataFile. The two methods will handle data file replication.

View differences:

src/edu/ucsb/nceas/metacat/MetacatReplication.java
137 137
    }
138 138
    if ( action.equals("readdata") ) 
139 139
    {
140
      ServletOutputStream out=response.getOutputStream();
140
      OutputStream out=response.getOutputStream();
141 141
      //to get the data file.
142 142
      handleGetDataFileRequest(out, params, response);
143 143
      out.close();
144 144
    }
145
    else if ( action.equals("forcereplicatedatafile") ) 
146
    {
147
     
148
      //OutputStream out=response.getOutputStream();
149
      //read a specific docid from remote host, and store it into local host
150
      handleForceReplicateDataFileRequest(params);
151
      //out.close();
152
    }
145 153
    else
146 154
    {
147 155
    PrintWriter out = response.getWriter();
......
187 195
    } else if ( action.equals("forcereplicate") ) {
188 196
      //read a specific docid from remote host, and store it into local host
189 197
      handleForceReplicateRequest(out, params, response);
190

  
198
   
191 199
    } else if ( action.equals("update") ) {
192 200
      //request an update list from the server
193 201
      handleUpdateRequest(out, params, response);
......
441 449
  }
442 450
  
443 451
  /**
452
   * when a forcereplication data file request comes in, local host sends a 
453
   * readdata request to the requesting server (remote server) for the specified 
454
   * docid. Then store it in local database and file system
455
   */
456
  private void handleForceReplicateDataFileRequest(Hashtable params)
457
  {
458
    //check if the metacat was configured to handle replication 
459
    if (!(util.getOption("replicationdata")).equals("on"))
460
    {
461
      return;
462
    }
463
    //make sure there is some parameters
464
    if(params.isEmpty())
465
    {
466
      return;
467
    }
468
    String server = ((String[])params.get("server"))[0]; // the server that
469
    //docid should include rev number
470
    String docid = ((String[])params.get("docid"))[0]; // sent the document
471
    if (docid==null)
472
    {
473
      util.debugMessage("Didn't specify docid for replication");
474
      return;
475
    }
476
    //docid was switch to two parts
477
    boolean override = false;
478
    int serverCode = 1;
479
    String dbaction=null;
480
    Connection conn=null;
481
    
482
    try 
483
    {
484
      //docid was switch to two parts
485
      String uniqueCode=MetaCatUtil.getDocIdFromString(docid);
486
      int rev=MetaCatUtil.getVersionFromString(docid);
487
      if(params.containsKey("dbaction")) 
488
      {
489
        dbaction = ((String[])params.get("dbaction"))[0];
490
      }
491
      else//default value is update
492
      {
493
        dbaction = "update";
494
      }
495
      serverCode = MetacatReplication.getServerCode(server);
496
      MetacatReplication.replLog("force replication request from " + server); 
497
      
498
      // get the document info from server
499
      URL docinfourl = new URL("https://" + server + 
500
                               "?server="+util.getLocalReplicationServerName()
501
                               +"&action=getdocumentinfo&docid=" + uniqueCode);
502
     
503
      String docInfoStr = MetacatReplication.getURLContent(docinfourl);
504

  
505
      //dih is the parser for the docinfo xml format
506
      DocInfoHandler dih = new DocInfoHandler();
507
      XMLReader docinfoParser = ReplicationHandler.initParser(dih);
508
      docinfoParser.parse(new InputSource(new StringReader(docInfoStr)));
509
      Hashtable docinfoHash = dih.getDocInfo();
510
      String user = (String)docinfoHash.get("user_owner");
511
     
512
      String docName = (String)docinfoHash.get("docname");
513
     
514
      String docType = (String)docinfoHash.get("doctype");
515
     
516
      
517
      conn = util.openDBConnection();
518
      
519
      //if action is delete, we don't delete the data file. Just archieve
520
      //the xml_documents
521
      if (dbaction.equals("delete"))
522
      {
523
        DocumentImpl.delete(conn,docid,user,null);
524
      }
525
      else if (dbaction.equals("insert"))
526
      {
527
        //register data information into xml_documents table
528
        DocumentImpl.registerDocument(docName,docType,docid,user,serverCode);
529
      
530
        //Get data file and store it into local file system.
531
        // sending back readdata request to server
532
        URL url = new URL("https://" + server + "?server="
533
                +util.getLocalReplicationServerName()
534
                +"&action=readdata&docid=" + docid);
535
        //Create file to store the data.
536
        String datafilepath = util.getOption("datafilepath");
537
        File dataDirectory = new File(datafilepath);
538
        //dataDirectory.mkdirs();
539
        File newFile = new File(dataDirectory, docid); 
540
       
541
        // create a buffered byte output stream
542
        // that uses a default-sized output buffer
543
        FileOutputStream fos = new FileOutputStream(newFile);
544
        BufferedOutputStream outPut = new BufferedOutputStream(fos);
545

  
546
        BufferedInputStream bis = null;
547
        bis = new BufferedInputStream(url.openStream());
548
        byte[] buf = new byte[4 * 1024]; // 4K buffer
549
        int b = bis.read(buf);
550
       
551
        while (b != -1) 
552
        {
553
          
554
          outPut.write(buf, 0, b);
555
          b = bis.read(buf);
556
        }
557
        bis.close();
558
	      outPut.close();
559
	      fos.close();
560
      
561
     }
562
     else if (dbaction.equals("update"))
563
     {
564
        //archieve old entry
565
        DocumentImpl.unRegisterDocument(conn,docid,user,null);
566
        //register new one
567
        DocumentImpl.registerDocument(docName,docType,docid,user,serverCode);
568
        // sending back readdata request to server
569
        URL url = new URL("https://" + server + "?server="
570
                +util.getLocalReplicationServerName()
571
                +"&action=readdata&docid=" + docid);
572
        //Create file to store the data.
573
        String datafilepath = util.getOption("datafilepath");
574
        File dataDirectory = new File(datafilepath);
575
        //dataDirectory.mkdirs();
576
        File newFile = new File(dataDirectory, docid);
577
        
578
        // create a buffered byte output stream
579
        // that uses a default-sized output buffer
580
        FileOutputStream fos = new FileOutputStream(newFile);
581
        BufferedOutputStream outPut = new BufferedOutputStream(fos);
582

  
583
        BufferedInputStream bis = null;
584
       
585
        bis = new BufferedInputStream(url.openStream());
586
        byte[] buf = new byte[4 * 1024]; // 4K buffer
587
        int b = bis.read(buf);
588
        System.out.println("before the while loop");
589
        while (b != -1) 
590
        {
591
          outPut.write(buf, 0, b);
592
          b = bis.read(buf);
593
        }
594
        bis.close();
595
	      outPut.close();
596
	      fos.close();
597
        
598
      
599
    }
600
    conn.close();
601
    
602
    MetacatReplication.replLog("document " + docid + " added to DB with " +
603
                                 "action " + dbaction);
604
    } catch(Exception e) 
605
    {
606
      try
607
      {
608
        conn.close();
609
      }
610
      catch (SQLException sqlE)
611
      {
612
        util.debugMessage(sqlE.getMessage());
613
      }
614
      System.out.println("ERROR in MetacatReplication.handleForceReplicate" +
615
                         "Request(): " + e.getMessage());
616
    }
617
  }
618
  /**
444 619
   * Grants or denies a lock to a requesting host.
445 620
   * The servlet parameters of interrest are:
446 621
   * docid: the docid of the file the lock is being requested for
......
542 717
  /**
543 718
   * Sends a datafile to a remote host
544 719
   */
545
  private void handleGetDataFileRequest(ServletOutputStream outPut, 
720
  private void handleGetDataFileRequest(OutputStream outPut, 
546 721
                            Hashtable params, HttpServletResponse response)
547 722
                                 
548 723
  {
724
    //check the metacat was configured to handle data replication
725
    if (!(util.getOption("replicationdata")).equals("on"))
726
    {
727
      return;
728
    }
729
    
549 730
    String filepath = util.getOption("datafilepath");
550 731
    String docId = ((String[])(params.get("docid")))[0];
732
    //check if the doicd is null
733
    if (docId==null)
734
    {
735
      util.debugMessage("Didn't specify docid for replication");
736
      return;
737
    }
551 738
  
552 739
    if(!filepath.endsWith("/")) 
553 740
    {
554 741
          filepath += "/";
555 742
    }
556 743
    String filename = filepath + docId;      //MIME type
557
    System.out.println("filename: "+filename);
744
    
558 745
    String contentType = null;//getServletContext().getMimeType(filename);
559
    System.out.println("after getMimeType");
746
   
560 747
    if (contentType == null) 
561 748
    {
562 749
       if (filename.endsWith(".xml")) 
......
592 779
          }
593 780
       }
594 781
   }
595
   System.out.println("contentye: "+contentType);
782
  
596 783
   response.setContentType(contentType);
597 784
   // if we decide to use "application/octet-stream" for all data returns
598 785
   // response.setContentType("application/octet-stream");
599 786
   FileInputStream fin = null;
600 787
   try 
601 788
   {
602
      System.out.println("in try");
603
      System.out.println(" after get output stream");
789
      
604 790
      fin = new FileInputStream(filename);
605
      System.out.println("here");
791
      
606 792
      byte[] buf = new byte[4 * 1024]; // 4K buffer
607 793
      int b = fin.read(buf);
608 794
      while (b != -1) 
609 795
      {
610
        System.out.println("in while loop");
796
        
611 797
        outPut.write(buf, 0, b);
612 798
        b = fin.read(buf);
613 799
      }
614
      System.out.println(" before close");
800
    
615 801
      fin.close();
616
      System.out.println(" after close");
802
   
617 803
   }  
618 804
   catch(Exception e)
619 805
   {

Also available in: Unified diff