home *** CD-ROM | disk | FTP | other *** search
/ linuxmafia.com 2016 / linuxmafia.com.tar / linuxmafia.com / pub / palmos / other-os / jsm-initial2.jar / jSyncManager / Server.java < prev    next >
Text File  |  2002-10-24  |  14KB  |  441 lines

  1. package jSyncManager;
  2.  
  3. import java.net.*;
  4. import jSyncManager.Conduit.*;
  5. import jSyncManager.Threads.*;
  6. import jSyncManager.Protocol.Util.*;
  7. import jSyncManager.Transport.*;
  8. import java.util.*;
  9. import java.io.*;
  10. import java.util.zip.*;
  11. import jSyncManager.GUIParts.JSyncManagerFrame;
  12. import java.rmi.server.*;
  13. import java.rmi.*;
  14.  
  15. /**
  16.  * Insert the type's description here.
  17.  * Creation date: (05/03/00 12:55:29 AM)
  18.  * @author: @author Brad BARCLAY <bbarclay@acm.org>
  19.  */
  20. public class Server extends UnicastRemoteObject implements SynchronizerListener, RemoteManagementListener {
  21.     private Vector conduits=new Vector();
  22.     private Thread threads[];
  23.     private Synchronizer synchronizers[];
  24.     private SLPTransportInterface transports[];
  25.     private boolean timeSync=false;
  26.     private Properties props=new Properties();
  27.     private boolean enableNetSync = false;
  28.     private int netSyncListenerPort = NETSYNC_DEFAULT_PORT;
  29.     private int NumNetSyncListeners = NETSYNC_DEFAULT_NUMLISTENERS;
  30.  
  31.     public static final int NETSYNC_DEFAULT_PORT = 22373;
  32.     public static final int NETSYNC_DEFAULT_NUMLISTENERS = 8;
  33.  
  34.     class LogPrintStream extends PrintWriter {
  35.         int synchronizerNum;
  36.         
  37.         public LogPrintStream(OutputStream out, int syncNum) {
  38.             super(out);
  39.             synchronizerNum=syncNum;
  40.         }
  41.  
  42.         public void println(String x) {
  43.             printLineToLog(synchronizerNum, x);
  44.         }
  45.  
  46.         public void printlnToSuper(String x) {
  47.             super.println(x);
  48.         }
  49.     }
  50.  
  51.     private LogPrintStream logStreams[];
  52.     private FileOutputStream fos=null;
  53.  
  54.     class ExitThread extends Thread {
  55.         public void run() {
  56.             try {
  57.                 System.out.print("*** Waiting 3 seconds...");
  58.                 synchronized(this) {
  59.                     wait(3000L);
  60.                 }
  61.             } catch (InterruptedException e) {}
  62.             System.out.println("Done!");
  63.             System.exit(0);
  64.         }
  65.     }
  66. /**
  67.  * JSyncManagerServer constructor comment.
  68.  */
  69. public Server() throws RemoteException {
  70.     super();
  71.     // Display copyright notice.
  72.     System.out.println("\njSyncManager Multiport Server "+Synchronizer.version+".");
  73.     System.out.println("Copyright (c) 1999, 2000 "+Synchronizer.author);
  74.     System.out.println("============================================================\n");
  75.     
  76.     // Determine settings
  77.     int numListeners=0;
  78.     FileInputStream fis=null;
  79.     String userHome=System.getProperty("jsyncman.server.home");
  80.     String seperator=System.getProperty("file.separator");
  81.     if(userHome==null) userHome=System.getProperty("user.home");
  82.  
  83.     try {
  84.         fis=new FileInputStream(userHome+seperator+"jsyncmanserver.properties");
  85.     } catch (Exception e) {
  86.         // File not found - abort
  87.         System.out.println("*** Properties file \"jsyncmanserver.properties\" not found in "+userHome);
  88.         System.out.println("Please set the jsyncman.server.home property to point to the directory containing your server properties file,");
  89.         System.out.println("or place a copy of your jsyncmanserver.properties file into "+userHome);
  90.         System.exit(1);
  91.     }
  92.  
  93.     try {
  94.         props.load(fis);
  95.     } catch (IOException e) {
  96.         System.out.println("*** Unable to load the properties file!");
  97.         System.out.println("The properties file has been found, but cannot be opened or parsed.");
  98.         System.out.println("Please check that the file is not locked and in use by another process, and that it is correctly formatted.");
  99.         System.exit(2);
  100.     }
  101.  
  102.     try {
  103.         numListeners=Integer.parseInt(props.getProperty("jsyncman.server.numListeners").trim());
  104.         if (numListeners<1) throw new NumberFormatException();
  105.     } catch (NumberFormatException e) {
  106.         System.out.println("*** Property jsyncman.server.numListeners must be a positive integer value!");
  107.         System.out.println("Please edit your jsyncmanserver.properties file to correct this fault.");
  108.         System.exit(3);
  109.     }
  110.  
  111.     String logFile=props.getProperty("jsyncman.server.logFile").trim();
  112.     if(logFile!=null) {
  113.         if(!logFile.toUpperCase().equals("CONSOLE")) {
  114.             try {
  115.                 fos=new FileOutputStream(userHome+seperator+logFile);
  116.             } catch (Exception e) {
  117.                 System.out.println("*** Unable to open the log file");
  118.                 System.out.println("The log file may be locked or in use.  The server will continue with logging disabled.");
  119.                 fos=null;
  120.             }
  121.         }
  122.     }
  123.     
  124.     transports=new SLPTransportInterface[numListeners];
  125.     synchronizers=new Synchronizer[numListeners];
  126.     threads=new Thread[numListeners];
  127.     logStreams=new LogPrintStream[numListeners];
  128.  
  129.     // Initialize transports, synchronizers, logs and threads.    
  130.     for(int i=0;i<numListeners;i++) {    
  131.         configTransport(i);
  132.         configSynchronizer(i);
  133.  
  134.         // Initialize logging for this synchronizer, if set
  135.         if(fos!=null) logStreams[i]=new LogPrintStream(fos, i);
  136.         else if(logFile!=null && logFile.toUpperCase().equals("CONSOLE")) {
  137.             logStreams[i]=new LogPrintStream(System.out, i);
  138.         }
  139.         if(logStreams[i]!=null) synchronizers[i].setLogStream(logStreams[i]);
  140.  
  141.         // Generate the thread
  142.         threads[i]=new Thread(synchronizers[i]);
  143.     }
  144.  
  145.     // Get the time sync option property
  146.     String timeSyncOpt=props.getProperty("jsyncman.server.timeSyncOption").trim();
  147.     if(timeSyncOpt!=null && timeSyncOpt.toLowerCase().equals("true")) {
  148.         timeSync=true;
  149.     }
  150.     
  151.     // Load the jConduits file
  152.     loadConduits(userHome+seperator);
  153. }
  154. /**
  155.  * Insert the method's description here.
  156.  * Creation date: (05/03/00 2:14:12 AM)
  157.  * @param num int
  158.  */
  159. private void configSynchronizer(int num) {
  160.     // Get the speed value for this synchronizer
  161.  
  162.     try {
  163.         int speed=Integer.parseInt(props.getProperty("jsyncman.server.speed."+num));
  164.         synchronizers[num]=new Synchronizer(transports[num], speed, this);    
  165.     } catch (Exception e) {
  166.         System.out.println("*** Property \"jsyncman.server.speed."+num+"\" contains an invalid value.");
  167.         System.out.println("Please make sure that the property is a positive integer and retry.");
  168.         System.exit(7);
  169.     }
  170. }
  171. /**
  172.  * Insert the method's description here.
  173.  * Creation date: (05/03/00 2:13:56 AM)
  174.  * @param num int
  175.  */
  176. private void configTransport(int num) {
  177.     // First get the transport type
  178.     String type=props.getProperty("jsyncman.server.type."+num).trim().toLowerCase();
  179.     if(type==null || (!type.equals("dock") & !type.equals("modem"))) {
  180.         System.out.println("*** Invalid port type specified in property \"jsyncman.server.type."+num+"\": "+type);
  181.         System.out.println("Valid types include DOCK and MODEM");
  182.         System.exit(4);
  183.     }
  184.         
  185.     // Next get the port name
  186.     String port=props.getProperty("jsyncman.server.port."+num).trim();
  187.     if(type==null || !hasPort(type, port)) {
  188.         System.out.println("*** Invalid serial port was specified in property \"jsyncman.server.port."+num+"\"");
  189.         System.out.println("Valid ports for this transport type are:");
  190.         String[] portNames;
  191.         if(type.equals("dock")) {
  192.             portNames=CommAPITransport.getPortNames();
  193.         } else {
  194.             portNames=ModemTransport.getPortNames();
  195.         }
  196.         for(int i=0;i<portNames.length;i++) {
  197.             System.out.println("     "+portNames[i]);
  198.         }
  199.         System.exit(5);
  200.     }
  201.  
  202.     // If type == modem, get the init string
  203.     String initString=null;
  204.     if(type.equals("modem")) {
  205.         initString=props.getProperty("jsyncman.server.initString."+num).trim();
  206.         if(initString==null) initString=new String("ATZ");
  207.     }
  208.     
  209.     // Initialize the transport
  210.     try {
  211.         if(type.equals("dock")) {
  212.             transports[num]=new CommAPITransport(port);
  213.         } else {
  214.             transports[num]=new ModemTransport(port, initString);
  215.         }
  216.     } catch (Exception e) {
  217.         System.out.println("*** Caught exception initializing transport "+num+".");
  218.         System.out.println("The server encountered an exception while attempting to create a ");
  219.         System.out.println("   Type   = "+type);
  220.         System.out.println("   Port   = "+port);
  221.         if(type.toLowerCase().equals("modem")) System.out.println("   Init   = "+initString);
  222.         System.out.println("   Exception: "+e.toString());
  223.         System.exit(6);
  224.     }
  225. }
  226. /**
  227.  * endOfSync method comment.
  228.  */
  229. public synchronized void endOfSync(Synchronizer s) {
  230.     // Do any cleanup here.
  231. }
  232. /**
  233.  * Insert the method's description here.
  234.  * Creation date: (05/03/00 1:38:56 AM)
  235.  */
  236.  
  237. protected void finalize() throws Throwable {
  238.     // Finalize here - should at least shut down the Synchronizers,
  239.     // and close the transports.
  240.     return;
  241. }
  242. /**
  243.  * getConduits method comment.
  244.  */
  245. public synchronized java.util.Vector getConduits(Synchronizer s) {
  246.     return conduits;
  247. }
  248. /**
  249.  * Insert the method's description here.
  250.  * Creation date: (05/03/00 4:03:33 AM)
  251.  * @return com.ibm.jSyncManager.Conduit.DefaultConduit
  252.  */
  253. public DefaultConduit getDefaultConduit(Synchronizer s) {
  254.     return null;
  255. }
  256. /**
  257.  * getLanguageBundle method comment.
  258.  */
  259. public synchronized java.util.ListResourceBundle getLanguageBundle() {
  260.     // Language bundles are not used by this class - return null.
  261.     return null;
  262. }
  263. /**
  264.  * getNewUserInfo method comment.
  265.  */
  266. public synchronized DLPUserInfo getNewUserInfo(Synchronizer s) {
  267.     // This class doesn't provide for adding new users - just return null.
  268.     return null;
  269. }
  270. /**
  271.  * getSyncType method comment.
  272.  */
  273. public int getSyncType(Synchronizer s) {
  274.     // Returns the Normal Sync type.
  275.     return Synchronizer.NORMAL_SYNC;
  276. }
  277. /**
  278.  * Insert the method's description here.
  279.  * Creation date: (05/03/00 2:31:17 AM)
  280.  * @return boolean
  281.  * @param type java.lang.String
  282.  * @param port java.lang.String
  283.  */
  284. private boolean hasPort(String type, String port) {
  285.     return true;
  286. /*    String portNames[];
  287.     
  288.     if(type.toLowerCase().equals("dock")) {
  289.         portNames=CommAPITransport.getPortNames();
  290.     } else {
  291.         portNames=ModemTransport.getPortNames();
  292.     }
  293.  
  294.     for(int i=0;i<portNames.length;i++) {
  295.         if(portNames[i].toUpperCase().equals(port.toUpperCase())) return true;
  296.     }
  297.     return false; */
  298. }
  299. /**
  300.  * Insert the method's description here.
  301.  * Creation date: (05/03/00 2:17:45 AM)
  302.  */
  303. private void loadConduits(String path) {
  304.     try {
  305.         FileInputStream fis2 = new FileInputStream(path + "jsyncmanserver.jconduits");
  306.         InflaterInputStream inflaterinputstream = new InflaterInputStream(fis2);
  307.         ObjectInputStream objectinputstream = new ObjectInputStream(inflaterinputstream);
  308.         conduits = (Vector)objectinputstream.readObject();
  309.         objectinputstream.close();
  310.         inflaterinputstream.close();
  311.         fis2.close();
  312.     } catch(Exception ex) {
  313.         conduits=new Vector();
  314.     }    
  315. }
  316. /**
  317.  * Starts the application.
  318.  * @param args an array of command-line arguments
  319.  */
  320. public static void main(java.lang.String[] args) {
  321.     //Runtime.runFinalizersOnExit(true);
  322.     String ourHostName;
  323.     String rmiName;
  324.  
  325.     try {
  326.         ourHostName=java.net.InetAddress.getLocalHost().getHostName();
  327.     } catch (java.net.UnknownHostException e) {
  328.         // This system doesn't have a host address, so we just disable RMI
  329.         ourHostName=null;
  330.     }
  331.  
  332.     String rmiState=System.getProperty("jsyncman.server.noadmin");
  333.     if(rmiState!=null && rmiState.toLowerCase().equals("true")) {
  334.         // Don't use the RMI admin stervices
  335.         ourHostName=null;
  336.     }
  337.     
  338.     if (ourHostName!=null && System.getSecurityManager() == null) {
  339.         System.setSecurityManager(new RMISecurityManager());
  340.     }
  341.  
  342.     try {
  343.         RemoteManagementListener server=new Server();
  344.         if(ourHostName!=null) {
  345.             rmiName=new String("//"+ourHostName+"/JSyncManServer");
  346.             Naming.rebind(rmiName, server);
  347.         }
  348.  
  349.         ((Server)server).startServer();
  350.     } catch (RemoteException e) {
  351.         // This should never happen
  352.         System.out.println("Exception encountered starting server: \n"+e.toString());
  353.         System.exit(99);
  354.     } catch (MalformedURLException e1) {
  355.         System.out.println("Exception encountered starting server: \n"+e1.toString());
  356.         System.exit(99);
  357.     }
  358. }
  359. /**
  360.  * midSync method comment.
  361.  */
  362. public synchronized void midSync(Synchronizer s) {
  363.     // Perform mid-sync tasks here.
  364.     return;
  365. }
  366. /**
  367.  * Insert the method's description here.
  368.  * Creation date: (05/03/00 3:05:23 AM)
  369.  * @param syncNum int
  370.  * @param x java.lang.String
  371.  */
  372. private void printLineToLog(int syncNum, String x) {
  373.     logStreams[syncNum].printlnToSuper("["+syncNum+"] "+x);
  374. }
  375. /**
  376.  * processUser method comment.
  377.  */
  378. public synchronized void processUser(DLPUserInfo userInfo, Synchronizer s) {
  379.     // Process the sync user here.
  380. }
  381. /**
  382.  * startOfSync method comment.
  383.  */
  384. public synchronized void startOfSync(Synchronizer s) {
  385.     // Provide synchronization startup configuration here.
  386.     return;
  387. }
  388. /**
  389.  * Insert the method's description here.
  390.  * Creation date: (05/03/00 12:57:59 AM)
  391.  */
  392. public void startServer() {
  393.     System.out.print("Starting server...");
  394.     
  395.     // Starts all of the server threads.
  396.     for(int i=0;i<threads.length;i++) {
  397.         threads[i].setDaemon(true);
  398.         threads[i].start();
  399.     }
  400.     System.out.println("Done!");
  401. }
  402. /**
  403.  * Informs the server to shutdown all operations.
  404.  * @param the remote administration password.
  405.  * @return a Boolean object representing the status of the request.
  406.  * <b>true</b> == request accepted, <b>false</b> otherwise.
  407.  * @exception java.rmi.RemoteException Any exception thrown while processing the request.
  408.  */
  409.  
  410. public Boolean stopServer(String password) throws java.rmi.RemoteException {
  411.     if(!password.toLowerCase().equals(props.getProperty("jsyncman.server.admin.password"))) return new Boolean(false);
  412.     System.out.print("*** Received stop server request from remote administration client. \nShutting down...");
  413.     for(int i=0;i<threads.length;i++) {
  414.         try {
  415.             synchronizers[i].stopSync();
  416.             threads[i].interrupt();
  417.             threads[i].stop();
  418.         } catch (Exception e) {}
  419.     }
  420.     System.out.println("Done!");
  421.     ExitThread et=new ExitThread();
  422.     et.start();
  423.     return new Boolean(true);
  424.     //System.exit(0);
  425. }
  426. /**
  427.  * useTimeSync method comment.
  428.  */
  429. public synchronized boolean useTimeSync(Synchronizer s) {
  430.     // Return the time sync setting here.
  431.     return timeSync;
  432. }
  433. /**
  434.  * validateUser method comment.
  435.  */
  436. public synchronized boolean validateUser(int userID, Synchronizer s) {
  437.     // Security validate the user ID here.
  438.     return true;
  439. }
  440. }
  441.