/*
*
* Copyright (c) 2000 Scott Oaks and Henry Wong. All Rights Reserved.
*
* Permission to use, copy, modify, and distribute this software
* and its documentation for NON-COMMERCIAL purposes and
* without fee is hereby granted.
*
* This sample source code is provided for example only,
* on an unsupported, as-is basis.
*
* AUTHOR MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
* THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
* TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
* PARTICULAR PURPOSE, OR NON-INFRINGEMENT. AUTHOR SHALL NOT BE LIABLE FOR
* ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
* DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
*
* THIS SOFTWARE IS NOT DESIGNED OR INTENDED FOR USE OR RESALE AS ON-LINE
* CONTROL EQUIPMENT IN HAZARDOUS ENVIRONMENTS REQUIRING FAIL-SAFE
* PERFORMANCE, SUCH AS IN THE OPERATION OF NUCLEAR FACILITIES, AIRCRAFT
* NAVIGATION OR COMMUNICATION SYSTEMS, AIR TRAFFIC CONTROL, DIRECT LIFE
* SUPPORT MACHINES, OR WEAPONS SYSTEMS, IN WHICH THE FAILURE OF THE
* SOFTWARE COULD LEAD DIRECTLY TO DEATH, PERSONAL INJURY, OR SEVERE
* PHYSICAL OR ENVIRONMENTAL DAMAGE ("HIGH RISK ACTIVITIES"). AUTHOR
* SPECIFICALLY DISCLAIMS ANY EXPRESS OR IMPLIED WARRANTY OF FITNESS FOR
* HIGH RISK ACTIVITIES.
*/
import java.util.*;
import java.io.*;
import java.rmi.*;
import java.rmi.server.*;
import net.jini.discovery.*;
import net.jini.core.lease.*;
import net.jini.core.event.*;
import net.jini.core.transaction.*;
import net.jini.core.transaction.server.*;
import net.jini.space.*;
import com.sun.jini.lookup.*;
import com.sun.jini.lease.*;
public class ConvertServiceImpl extends UnicastRemoteObject
implements ConvertServiceProxy, Runnable {
private ServerLandlord lord;
private ServerDelivery sender;
private JavaSpace js;
private ServiceFinder txsf;
public ConvertServiceImpl() throws RemoteException {
lord = new ServerLandlord();
sender = new ServerDelivery(this, lord);
// Get the JavaSpaces and Transaction Manager now so that
// the thread doesn't have to deal with exceptions if they
// can't be found
try {
ServiceFinder sf = new ServiceFinder(JavaSpace.class);
js = (JavaSpace) sf.getObject();
txsf = new ServiceFinder(TransactionManager.class);
} catch (IOException ioe) {
throw new RemoteException("Can't find java space", ioe);
}
// Start the thread to take and convert requests
new Thread(this).start();
}
public ConvertServiceRegistration getInstance(long duration) {
Hashtable ht = new Hashtable(13);
return new ConvertServiceRegistrationImpl(js, lord.newLease(ht, duration));
}
public EventRegistration trackConversions(long duration, RemoteEventListener rel, MarshalledObject key) {
return sender.addListener(rel, duration, key);
}
// This handles the conversions: the ConvertServiceRegistrationImpl
// class puts the request into the space; this thread takes the
// requests and calcualtes the answers
public void run() {
TransactionManager txm = (TransactionManager) txsf.getObject();
LeaseRenewalManager lrm = new LeaseRenewalManager();
Conversion request = new Conversion(false);
Conversion result;
while (true) {
Transaction.Created txn = null;
try {
// Take the request in a thread so that if the service
// exits before it writes the answer the request is not
// lost
txn = TransactionFactory.create(txm, Lease.FOREVER);
lrm.renewUntil(txn.lease, Lease.FOREVER, null);
// Get a request (one with the done field == false)
result = (Conversion) js.take(request,
txn.transaction, Long.MAX_VALUE);
try {
result.result = String.valueOf(result.value);
} catch (Exception e) {
// If we can't calculate the answer, we have to
// abort the transaction, but we can go get the
// next request
txn.transaction.abort();
lrm.remove(txn.lease);
continue;
}
// Write the answer.
result.done = new Boolean(true);
js.write(result, txn.transaction, Long.MAX_VALUE);
txn.transaction.commit();
lrm.remove(txn.lease);
} catch (Exception e) {
// These exceptions mean that we couldn't talk to the
// space or transaction manager, which we can't
// recover from.
System.out.println("Unrecoverable error" + e);
return;
}
}
}
public static void main(String[] args) throws Exception {
System.setSecurityManager(new RMISecurityManager());
String[] groups = new String[] { "" };
// Create the instance of the service; the JoinManager will
// register it and renew its leases with the lookup service
ConvertServiceImpl csi = (ConvertServiceImpl) new ConvertServiceImpl();
JoinManager manager = new JoinManager(csi, null, groups,
null, null, null);
}
}
|