⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 writeoperation.java

📁 高级java2 大学教程(含源码,经典的Java学习教程
💻 JAVA
字号:
// WriteOperation.java
// This application initializes an new Entry object, 
// and deposits this Entry to the JavaSpace.
package com.deitel.advjhtp1.javaspace.write;

// Jini core packages
import net.jini.core.lease.Lease;
import net.jini.core.transaction.TransactionException;

// Jini extension package
import net.jini.space.JavaSpace;

// Java core package
import java.rmi.RemoteException;

// Java extension package
import javax.swing.*;

// Deitel package
import com.deitel.advjhtp1.javaspace.common.*;

public class WriteOperation {

   private JavaSpace space;
   private static final String[] days = { "Monday", "Tuesday",
      "Wednesday", "Thursday", "Friday" };
   private String output = "\n";

   // WriteOperation constructor
   public WriteOperation( String hostname )
   {
      // get the JavaSpace
      String jiniURL = "jini://" +  hostname;
      JavaSpaceFinder findtool = new JavaSpaceFinder( jiniURL );
      space = findtool.getJavaSpace();
   }

   // deposit a new Entry to the JavaSpace
   public void writeEntry( String day )
   {
      // initialize a AttendeeCounter Entry and deposit 
      // Entry object to the JavaSpace
      try {
         AttendeeCounter counter = new AttendeeCounter( day );
         space.write( counter, null, Lease.FOREVER );

         output += "Initialize the Entry: \n";
         output += "   Day: " + day + "\n";
         output += "   Count: 0\n";
      }

      // handle exception network failure 
      catch ( RemoteException exception ) {
         exception.printStackTrace();
      }

      // handle exception invalid transaction
      catch ( TransactionException exception ) {
         exception.printStackTrace();
      }
   }

   // show output
   public void showOutput()
   {
      JTextArea outputArea = new JTextArea();
      outputArea.setText( output );
      JOptionPane.showMessageDialog( null, outputArea, 
         "WriteOperation Output", 
         JOptionPane.INFORMATION_MESSAGE );

      // terminate the program
      System.exit( 0 );
   }

   // method main
   public static void main( String args[] )
   {
      // get the hostname
      if ( args.length != 1 ) {
         System.out.println( 
            "Usage: WriteOperation hostname" );
         System.exit( 1 );
      }
      
      // get the user input day
	  String day = ( String ) JOptionPane.showInputDialog( 
         null, "Select Day", "Day Selection", 
         JOptionPane.QUESTION_MESSAGE, null, days, days[ 0 ] );

      // write an Entry
      WriteOperation write = new WriteOperation( args[ 0 ] );
      write.writeEntry( day );

      write.showOutput();
     
   } // end method main
}

/***************************************************************
 * (C) Copyright 2002 by Deitel & Associates, Inc. and         *
 * Prentice Hall. All Rights Reserved.                         *
 *                                                             *
 * DISCLAIMER: The authors and publisher of this book have     *
 * used their best efforts in preparing the book. These        *
 * efforts include the development, research, and testing of   *
 * the theories and programs to determine their effectiveness. *
 * The authors and publisher make no warranty of any kind,     *
 * expressed or implied, with regard to these programs or to   *
 * the documentation contained in these books. The authors     *
 * and publisher shall not be liable in any event for          *
 * incidental or consequential damages in connection with, or  *
 * arising out of, the furnishing, performance, or use of      *
 * these programs.                                             *
 ***************************************************************/

⌨️ 快捷键说明

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