/* * RMIDemoImpl.java 1.1 (5 May 2000) * * Copyright 1998 by Bill Giel/KC Multimedia and Design Group, Inc., * All rights reserved. * * Disclaimer of Warranty. Software is provided "AS IS," * without a warranty of any kind. ALL EXPRESS OR IMPLIED * REPRESENTATIONS AND WARRANTIES, INCLUDING ANY IMPLIED * WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR * PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. IN NO * EVENT WILL THE DEVELOPER OR ITS LICENSORS BE LIABLE FOR * ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, * SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, * HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, * RELATING TO THE USE, DOWNLOAD, DISTRIBUTION OF OR INABILITY * TO USE SOFTWARE, EVEN IF THE DEVELOPER OR ITS LICENSORS HAS * BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. */ package com.kcmultimedia.demo; import java.rmi.*; import java.rmi.server.*; import java.rmi.registry.*; import java.util.*; import java.net.*; import java.io.*; /** * Server side RMI demo application * * @version 1.0 11/10/98 * @author Bill Giel */ public class RMIDemoImpl extends UnicastRemoteObject implements RMIDemo, SCMEventListener { private static final String logFile = "RMIDemo.log"; private static Date now; String[] answers = { "Most definitely.", "I don't think so.", "Undoubtedly.", "Ask again later.", "It is unlikely.", "Please rephrase the question.", "I think so.", "Possibly", "Probably not.", "Cannot answer that.", "For sure." }; public RMIDemoImpl() throws RemoteException { super(); SCMEventManager scm = SCMEventManager.getInstance(); scm.addSCMEventListener(this); } public void handleSCMEvent(SCMEvent event) { if(event.getID() == SCMEvent.SERVICE_STOPPED){ writeToLog("Service Stopped."); } } private static synchronized void writeToLog(String logentry) { now = new Date(); try{ PrintWriter out = new PrintWriter(new FileWriter(logFile,true),true); out.println("[" + now.toString() + "] " + logentry); out.close(); }catch(Exception e){ System.out.println("Error writing to logfile"); e.printStackTrace(); } } public String getAnswer() { Random random = new Random(new Date().getTime()); double d = random.nextDouble() * 10 + .5; return answers[(int)d]; } public static void main(String[] args) { //if(null == System.getSecurityManager()){ //System.setSecurityManager(new RMISecurityManager()); //} String hostname; try{ RMIDemoImpl obj = new RMIDemoImpl(); if(args.length == 0){ InetAddress ia = InetAddress.getLocalHost(); hostname = ia.getHostName(); } else{ hostname = args[0]; } //Try to create a registry listening on the default RMI port //If it throws an ExportException a registry is probably running //on the port, in which case we do nothing and simply try to //use the existing registry. try{ LocateRegistry.createRegistry(1099); }catch(ExportException ex){} Naming.rebind("//" + hostname + "/" + BOUNDNAME, obj); String msg = BOUNDNAME + " bound in RMI Registry"; System.out.println(msg); writeToLog(msg); }catch(Exception e){ System.out.println("RMIDemoImpl error"); e.printStackTrace(); } } }