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 >
Wrap
Text File
|
2002-10-24
|
14KB
|
441 lines
package jSyncManager;
import java.net.*;
import jSyncManager.Conduit.*;
import jSyncManager.Threads.*;
import jSyncManager.Protocol.Util.*;
import jSyncManager.Transport.*;
import java.util.*;
import java.io.*;
import java.util.zip.*;
import jSyncManager.GUIParts.JSyncManagerFrame;
import java.rmi.server.*;
import java.rmi.*;
/**
* Insert the type's description here.
* Creation date: (05/03/00 12:55:29 AM)
* @author: @author Brad BARCLAY <bbarclay@acm.org>
*/
public class Server extends UnicastRemoteObject implements SynchronizerListener, RemoteManagementListener {
private Vector conduits=new Vector();
private Thread threads[];
private Synchronizer synchronizers[];
private SLPTransportInterface transports[];
private boolean timeSync=false;
private Properties props=new Properties();
private boolean enableNetSync = false;
private int netSyncListenerPort = NETSYNC_DEFAULT_PORT;
private int NumNetSyncListeners = NETSYNC_DEFAULT_NUMLISTENERS;
public static final int NETSYNC_DEFAULT_PORT = 22373;
public static final int NETSYNC_DEFAULT_NUMLISTENERS = 8;
class LogPrintStream extends PrintWriter {
int synchronizerNum;
public LogPrintStream(OutputStream out, int syncNum) {
super(out);
synchronizerNum=syncNum;
}
public void println(String x) {
printLineToLog(synchronizerNum, x);
}
public void printlnToSuper(String x) {
super.println(x);
}
}
private LogPrintStream logStreams[];
private FileOutputStream fos=null;
class ExitThread extends Thread {
public void run() {
try {
System.out.print("*** Waiting 3 seconds...");
synchronized(this) {
wait(3000L);
}
} catch (InterruptedException e) {}
System.out.println("Done!");
System.exit(0);
}
}
/**
* JSyncManagerServer constructor comment.
*/
public Server() throws RemoteException {
super();
// Display copyright notice.
System.out.println("\njSyncManager Multiport Server "+Synchronizer.version+".");
System.out.println("Copyright (c) 1999, 2000 "+Synchronizer.author);
System.out.println("============================================================\n");
// Determine settings
int numListeners=0;
FileInputStream fis=null;
String userHome=System.getProperty("jsyncman.server.home");
String seperator=System.getProperty("file.separator");
if(userHome==null) userHome=System.getProperty("user.home");
try {
fis=new FileInputStream(userHome+seperator+"jsyncmanserver.properties");
} catch (Exception e) {
// File not found - abort
System.out.println("*** Properties file \"jsyncmanserver.properties\" not found in "+userHome);
System.out.println("Please set the jsyncman.server.home property to point to the directory containing your server properties file,");
System.out.println("or place a copy of your jsyncmanserver.properties file into "+userHome);
System.exit(1);
}
try {
props.load(fis);
} catch (IOException e) {
System.out.println("*** Unable to load the properties file!");
System.out.println("The properties file has been found, but cannot be opened or parsed.");
System.out.println("Please check that the file is not locked and in use by another process, and that it is correctly formatted.");
System.exit(2);
}
try {
numListeners=Integer.parseInt(props.getProperty("jsyncman.server.numListeners").trim());
if (numListeners<1) throw new NumberFormatException();
} catch (NumberFormatException e) {
System.out.println("*** Property jsyncman.server.numListeners must be a positive integer value!");
System.out.println("Please edit your jsyncmanserver.properties file to correct this fault.");
System.exit(3);
}
String logFile=props.getProperty("jsyncman.server.logFile").trim();
if(logFile!=null) {
if(!logFile.toUpperCase().equals("CONSOLE")) {
try {
fos=new FileOutputStream(userHome+seperator+logFile);
} catch (Exception e) {
System.out.println("*** Unable to open the log file");
System.out.println("The log file may be locked or in use. The server will continue with logging disabled.");
fos=null;
}
}
}
transports=new SLPTransportInterface[numListeners];
synchronizers=new Synchronizer[numListeners];
threads=new Thread[numListeners];
logStreams=new LogPrintStream[numListeners];
// Initialize transports, synchronizers, logs and threads.
for(int i=0;i<numListeners;i++) {
configTransport(i);
configSynchronizer(i);
// Initialize logging for this synchronizer, if set
if(fos!=null) logStreams[i]=new LogPrintStream(fos, i);
else if(logFile!=null && logFile.toUpperCase().equals("CONSOLE")) {
logStreams[i]=new LogPrintStream(System.out, i);
}
if(logStreams[i]!=null) synchronizers[i].setLogStream(logStreams[i]);
// Generate the thread
threads[i]=new Thread(synchronizers[i]);
}
// Get the time sync option property
String timeSyncOpt=props.getProperty("jsyncman.server.timeSyncOption").trim();
if(timeSyncOpt!=null && timeSyncOpt.toLowerCase().equals("true")) {
timeSync=true;
}
// Load the jConduits file
loadConduits(userHome+seperator);
}
/**
* Insert the method's description here.
* Creation date: (05/03/00 2:14:12 AM)
* @param num int
*/
private void configSynchronizer(int num) {
// Get the speed value for this synchronizer
try {
int speed=Integer.parseInt(props.getProperty("jsyncman.server.speed."+num));
synchronizers[num]=new Synchronizer(transports[num], speed, this);
} catch (Exception e) {
System.out.println("*** Property \"jsyncman.server.speed."+num+"\" contains an invalid value.");
System.out.println("Please make sure that the property is a positive integer and retry.");
System.exit(7);
}
}
/**
* Insert the method's description here.
* Creation date: (05/03/00 2:13:56 AM)
* @param num int
*/
private void configTransport(int num) {
// First get the transport type
String type=props.getProperty("jsyncman.server.type."+num).trim().toLowerCase();
if(type==null || (!type.equals("dock") & !type.equals("modem"))) {
System.out.println("*** Invalid port type specified in property \"jsyncman.server.type."+num+"\": "+type);
System.out.println("Valid types include DOCK and MODEM");
System.exit(4);
}
// Next get the port name
String port=props.getProperty("jsyncman.server.port."+num).trim();
if(type==null || !hasPort(type, port)) {
System.out.println("*** Invalid serial port was specified in property \"jsyncman.server.port."+num+"\"");
System.out.println("Valid ports for this transport type are:");
String[] portNames;
if(type.equals("dock")) {
portNames=CommAPITransport.getPortNames();
} else {
portNames=ModemTransport.getPortNames();
}
for(int i=0;i<portNames.length;i++) {
System.out.println(" "+portNames[i]);
}
System.exit(5);
}
// If type == modem, get the init string
String initString=null;
if(type.equals("modem")) {
initString=props.getProperty("jsyncman.server.initString."+num).trim();
if(initString==null) initString=new String("ATZ");
}
// Initialize the transport
try {
if(type.equals("dock")) {
transports[num]=new CommAPITransport(port);
} else {
transports[num]=new ModemTransport(port, initString);
}
} catch (Exception e) {
System.out.println("*** Caught exception initializing transport "+num+".");
System.out.println("The server encountered an exception while attempting to create a ");
System.out.println(" Type = "+type);
System.out.println(" Port = "+port);
if(type.toLowerCase().equals("modem")) System.out.println(" Init = "+initString);
System.out.println(" Exception: "+e.toString());
System.exit(6);
}
}
/**
* endOfSync method comment.
*/
public synchronized void endOfSync(Synchronizer s) {
// Do any cleanup here.
}
/**
* Insert the method's description here.
* Creation date: (05/03/00 1:38:56 AM)
*/
protected void finalize() throws Throwable {
// Finalize here - should at least shut down the Synchronizers,
// and close the transports.
return;
}
/**
* getConduits method comment.
*/
public synchronized java.util.Vector getConduits(Synchronizer s) {
return conduits;
}
/**
* Insert the method's description here.
* Creation date: (05/03/00 4:03:33 AM)
* @return com.ibm.jSyncManager.Conduit.DefaultConduit
*/
public DefaultConduit getDefaultConduit(Synchronizer s) {
return null;
}
/**
* getLanguageBundle method comment.
*/
public synchronized java.util.ListResourceBundle getLanguageBundle() {
// Language bundles are not used by this class - return null.
return null;
}
/**
* getNewUserInfo method comment.
*/
public synchronized DLPUserInfo getNewUserInfo(Synchronizer s) {
// This class doesn't provide for adding new users - just return null.
return null;
}
/**
* getSyncType method comment.
*/
public int getSyncType(Synchronizer s) {
// Returns the Normal Sync type.
return Synchronizer.NORMAL_SYNC;
}
/**
* Insert the method's description here.
* Creation date: (05/03/00 2:31:17 AM)
* @return boolean
* @param type java.lang.String
* @param port java.lang.String
*/
private boolean hasPort(String type, String port) {
return true;
/* String portNames[];
if(type.toLowerCase().equals("dock")) {
portNames=CommAPITransport.getPortNames();
} else {
portNames=ModemTransport.getPortNames();
}
for(int i=0;i<portNames.length;i++) {
if(portNames[i].toUpperCase().equals(port.toUpperCase())) return true;
}
return false; */
}
/**
* Insert the method's description here.
* Creation date: (05/03/00 2:17:45 AM)
*/
private void loadConduits(String path) {
try {
FileInputStream fis2 = new FileInputStream(path + "jsyncmanserver.jconduits");
InflaterInputStream inflaterinputstream = new InflaterInputStream(fis2);
ObjectInputStream objectinputstream = new ObjectInputStream(inflaterinputstream);
conduits = (Vector)objectinputstream.readObject();
objectinputstream.close();
inflaterinputstream.close();
fis2.close();
} catch(Exception ex) {
conduits=new Vector();
}
}
/**
* Starts the application.
* @param args an array of command-line arguments
*/
public static void main(java.lang.String[] args) {
//Runtime.runFinalizersOnExit(true);
String ourHostName;
String rmiName;
try {
ourHostName=java.net.InetAddress.getLocalHost().getHostName();
} catch (java.net.UnknownHostException e) {
// This system doesn't have a host address, so we just disable RMI
ourHostName=null;
}
String rmiState=System.getProperty("jsyncman.server.noadmin");
if(rmiState!=null && rmiState.toLowerCase().equals("true")) {
// Don't use the RMI admin stervices
ourHostName=null;
}
if (ourHostName!=null && System.getSecurityManager() == null) {
System.setSecurityManager(new RMISecurityManager());
}
try {
RemoteManagementListener server=new Server();
if(ourHostName!=null) {
rmiName=new String("//"+ourHostName+"/JSyncManServer");
Naming.rebind(rmiName, server);
}
((Server)server).startServer();
} catch (RemoteException e) {
// This should never happen
System.out.println("Exception encountered starting server: \n"+e.toString());
System.exit(99);
} catch (MalformedURLException e1) {
System.out.println("Exception encountered starting server: \n"+e1.toString());
System.exit(99);
}
}
/**
* midSync method comment.
*/
public synchronized void midSync(Synchronizer s) {
// Perform mid-sync tasks here.
return;
}
/**
* Insert the method's description here.
* Creation date: (05/03/00 3:05:23 AM)
* @param syncNum int
* @param x java.lang.String
*/
private void printLineToLog(int syncNum, String x) {
logStreams[syncNum].printlnToSuper("["+syncNum+"] "+x);
}
/**
* processUser method comment.
*/
public synchronized void processUser(DLPUserInfo userInfo, Synchronizer s) {
// Process the sync user here.
}
/**
* startOfSync method comment.
*/
public synchronized void startOfSync(Synchronizer s) {
// Provide synchronization startup configuration here.
return;
}
/**
* Insert the method's description here.
* Creation date: (05/03/00 12:57:59 AM)
*/
public void startServer() {
System.out.print("Starting server...");
// Starts all of the server threads.
for(int i=0;i<threads.length;i++) {
threads[i].setDaemon(true);
threads[i].start();
}
System.out.println("Done!");
}
/**
* Informs the server to shutdown all operations.
* @param the remote administration password.
* @return a Boolean object representing the status of the request.
* <b>true</b> == request accepted, <b>false</b> otherwise.
* @exception java.rmi.RemoteException Any exception thrown while processing the request.
*/
public Boolean stopServer(String password) throws java.rmi.RemoteException {
if(!password.toLowerCase().equals(props.getProperty("jsyncman.server.admin.password"))) return new Boolean(false);
System.out.print("*** Received stop server request from remote administration client. \nShutting down...");
for(int i=0;i<threads.length;i++) {
try {
synchronizers[i].stopSync();
threads[i].interrupt();
threads[i].stop();
} catch (Exception e) {}
}
System.out.println("Done!");
ExitThread et=new ExitThread();
et.start();
return new Boolean(true);
//System.exit(0);
}
/**
* useTimeSync method comment.
*/
public synchronized boolean useTimeSync(Synchronizer s) {
// Return the time sync setting here.
return timeSync;
}
/**
* validateUser method comment.
*/
public synchronized boolean validateUser(int userID, Synchronizer s) {
// Security validate the user ID here.
return true;
}
}