Java Q&A
by Mike Jennings


Listing One
void _stdcall ServiceMain(int argc,char **args)

Listing Two
void _stdcall Handler(int signal)

Listing Three
typedef map<string,TService *> string_service_map;
string_service_map name_to_service;

Listing Four
void PreferredHandler(char *servicename,int signal);

Listing Five
bool ServiceStarting(DWORD checkpoint,DWORD wait_hint);
bool ServiceStopping(DWORD checkpoint,DWORD wait_hint);
bool ServiceStarted();
bool ServiceStopped();
bool ServiceNothingInteresting();

Listing Six
virtual bool TryToStart(TServiceController *controller)=0;
virtual void TryToStop(TServiceController *controller)=0;
virtual void TryToPause(TServiceController *controller)=0;
virtual void TryToContinue(TServiceController *controller)=0;
virtual void TryToShutdown(TServiceController *controller)=0;
virtual void TryToGetStatus(TServiceController *controller)=0;

Listing Seven
class TService
{
public:
virtual string ServiceName()=0;
virtual string DisplayName()=0;
virtual string_vector DependentServices()=0;

// parse any command-line parameters passed to the service
virtual void ParseCommandLine(int argc,char **args)=0;
// try to start the service, returns true if service has been initialized 
// properly. If it returns true, then main will be called
virtual bool TryToStart(TServiceController *controller)=0;
// main method returns when service has stopped
virtual void ServiceMain()=0;

virtual void TryToStop(TServiceController *controller)=0;
virtual void TryToPause(TServiceController *controller)=0;
virtual void TryToContinue(TServiceController *controller)=0;
virtual void TryToShutdown(TServiceController *controller)=0;
virtual void TryToGetStatus(TServiceController *controller)=0;

virtual ~TService() {}
};

Listing Eight
public interface JDaemon
{
public String getDaemonName();
public String getDisplayName();
public String[] getDependentDaemons();
public void parseCommandLine(String[] args);
// returns true if we were able to start
public boolean tryToStart(JDaemonController c);
public void main(); // called when service is running
public boolean tryToStop(JDaemonController c);
public boolean tryToPause(JDaemonController c);
public boolean tryToContinue(JDaemonController c);
public boolean tryToShutdown(JDaemonController c);
public boolean tryToGetStatus(JDaemonController c);
}

Listing Nine
public interface JDaemonController
{
public boolean daemonStarting(int checkpoint,int wait_hint);
public boolean daemonStopping(int checkpoint,int wait_hint);
public boolean daemonStarted();
public boolean daemonStopped();
public boolean daemonNothingInteresting();
}

Listing Ten
public class SampleDaemon implements JDaemon
{
boolean running=false;

public SampleDaemon()
  {
  }
public String getDaemonName()
  {
    return "SampleDaemon";
  }
public String getDisplayName()
  {
    return "SimpleJavaService";
  }
public String[] getDependentDaemons()
  {
    return null;
  }
public void parseCommandLine(String[] args)
  {
  }
public boolean tryToStart(JDaemonController c)
  {
    c.daemonStarting(1,500); // starting in less than 500 milliseconds
    running=true;
    c.daemonStarted();
    return true;
  }
public void main()
  {
    int i=0;
    while(running)
      {
        try
          {
            Thread.currentThread().sleep(500);
            System.out.println("hello "+i);
            i++;
          }
        catch(InterruptedException ie)
          {
          }  
      }
  }
public boolean tryToStop(JDaemonController c)
  {
    c.daemonStopping(1,500);
    running=false;
    return c.daemonStopped();
  }
public boolean tryToPause(JDaemonController c)
  {
    return c.daemonNothingInteresting();
  }
public boolean tryToContinue(JDaemonController c)
  {
    return c.daemonNothingInteresting();
  }
public boolean tryToShutdown(JDaemonController c)
  {
    return c.daemonNothingInteresting();
  }
public boolean tryToGetStatus(JDaemonController c)
  {
    return c.daemonNothingInteresting();
  }
}

Listing Eleven
public class NTServiceController implements JDaemonController
{
int pointer;
public NTServiceController()
  {
    pointer=0;
  }
public void setPointer(int p) {pointer=p;}
public int getPointer() {return pointer;}

public native boolean daemonStarting(int checkpoint,int wait_hint);
public native boolean daemonStopping(int checkpoint,int wait_hint);
public native boolean daemonStarted();
public native boolean daemonStopped();
public native boolean daemonNothingInteresting();
}

Listing Twelve
JNIEXPORT jboolean JNICALL
    NTServiceController_daemonStarting
  (JNIEnv *env,jobject jcontroller,jint checkpoint,jint wait_hint)
{
TServiceController *sc=(TServiceController *)
    env->CallIntMethod(jcontroller,ntscm_getPointer);
return sc->ServiceStarting(checkpoint,wait_hint);
}






4


