You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tomcat.apache.org by jf...@apache.org on 2001/09/28 14:38:19 UTC
cvs commit: jakarta-tomcat-service/java/org/apache/support SimpleService.java
jfclere 01/09/28 05:38:19
Modified: java/org/apache/service ServiceController.java
ServiceListener.java ServicePermission.java
Added: . build.xml
java/org/apache/support SimpleService.java
Log:
Add build.xml so that it can be build (and arrange some java files).
Revision Changes Path
1.1 jakarta-tomcat-service/build.xml
Index: build.xml
===================================================================
<project name="service" default="build-main" basedir=".">
<!-- ===================== Initialize Property Values ================ -->
<property file="build.properties"/>
<property file="${user.home}/build.properties"/>
<property name="service.build" value="${basedir}/build"/>
<property name="service.support" value="${basedir}/support"/>
<target name="build-prepare">
<mkdir dir="${service.build}"/>
<mkdir dir="${service.build}/classes"/>
<mkdir dir="${service.build}/lib"/>
<mkdir dir="${service.support}"/>
<mkdir dir="${service.support}/classes"/>
<mkdir dir="${service.support}/lib"/>
</target>
<target name="build-main" depends="build-prepare">
<javac srcdir="java"
destdir="${service.build}/classes"
deprecation="on"
debug="${debug}"
optimize="${optimize}"
verbose="off"
excludes="**/CVS/**">
<exclude name="org/apache/support/**" />
</javac>
<jar jarfile="${service.build}/lib/service.jar"
basedir="${service.build}/classes"
/>
</target>
<target name="support" depends="build-main">
<javac srcdir="java"
destdir="${service.support}/classes"
deprecation="on"
debug="${debug}"
optimize="${optimize}"
verbose="off"
excludes="**/CVS/**">
<exclude name="org/apache/service/**" />
</javac>
<jar jarfile="${service.support}/lib/support.jar"
basedir="${service.support}/classes"
/>
</target>
<target name="clean" description="Clean all components">
<delete dir="${service.build}"/>
<delete dir="${service.support}"/>
</target>
</project>
1.2 +13 -13 jakarta-tomcat-service/java/org/apache/service/ServiceController.java
Index: ServiceController.java
===================================================================
RCS file: /home/cvs/jakarta-tomcat-service/java/org/apache/service/ServiceController.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- ServiceController.java 2001/07/30 23:25:00 1.1
+++ ServiceController.java 2001/09/28 12:38:19 1.2
@@ -62,44 +62,44 @@
* @author <a href="mailto:pier.fumagalli@sun.com">Pier Fumagalli</a>
* @author Copyright © 2000-2001 <a href="http://www.apache.org/">The
* Apache Software Foundation</a>. All rights reserved.
- * @version 1.0 <i>(CVS $Revision: 1.1 $)</i>
+ * @version 1.0 <i>(CVS $Revision: 1.2 $)</i>
*/
public interface ServiceController {
/**
*
*/
- public static shutdown()
- throws IllegalStatusException;
+ public void shutdown()
+ throws IllegalStateException;
/**
*
*/
- public static reload()
- throws IllegalStatusException;
+ public void reload()
+ throws IllegalStateException;
/**
*
*/
- public static fail()
- throws IllegalStatusException;
+ public void fail()
+ throws IllegalStateException;
/**
*
*/
- public static fail(String message)
- throws IllegalStatusException;
+ public void fail(String message)
+ throws IllegalStateException;
/**
*
*/
- public static fail(Exception exception)
- throws IllegalStatusException;
+ public void fail(Exception exception)
+ throws IllegalStateException;
/**
*
*/
- public static fail(String message, Exception exception)
- throws IllegalStatusException;
+ public void fail(String message, Exception exception)
+ throws IllegalStateException;
}
1.2 +2 -0 jakarta-tomcat-service/java/org/apache/service/ServiceListener.java
Index: ServiceListener.java
===================================================================
RCS file: /home/cvs/jakarta-tomcat-service/java/org/apache/service/ServiceListener.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- ServiceListener.java 2001/07/30 23:25:42 1.1
+++ ServiceListener.java 2001/09/28 12:38:19 1.2
@@ -56,6 +56,8 @@
* ========================================================================= */
package org.apache.service;
+import java.util.EventListener;
+
public interface ServiceListener extends EventListener {
}
1.4 +3 -3 jakarta-tomcat-service/java/org/apache/service/ServicePermission.java
Index: ServicePermission.java
===================================================================
RCS file: /home/cvs/jakarta-tomcat-service/java/org/apache/service/ServicePermission.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- ServicePermission.java 2001/07/30 23:23:22 1.3
+++ ServicePermission.java 2001/09/28 12:38:19 1.4
@@ -124,7 +124,7 @@
* @author <a href="mailto:pier.fumagalli@sun.com">Pier Fumagalli</a>
* @author Copyright © 2000-2001 <a href="http://www.apache.org/">The
* Apache Software Foundation</a>. All rights reserved.
- * @version 1.0 <i>(CVS $Revision: 1.3 $)</i>
+ * @version 1.0 <i>(CVS $Revision: 1.4 $)</i>
*/
public final class ServicePermission extends Permission {
@@ -260,7 +260,7 @@
// Create the appropriate mask if this is a control permission.
if (this.type==TYPE_CONTROL) {
this.mask=this.createControlMask(actions);
- return();
+ return;
}
}
@@ -287,7 +287,7 @@
*/
public int hashCode() {
this.setupDescription();
- return(this.dest.hashCode());
+ return(this.desc.hashCode());
}
/**
1.1 jakarta-tomcat-service/java/org/apache/support/SimpleService.java
Index: SimpleService.java
===================================================================
/* ========================================================================= *
* *
* The Apache Software License, Version 1.1 *
* *
* Copyright (c) 1999-2001 The Apache Software Foundation. *
* All rights reserved. *
* *
* ========================================================================= *
* *
* Redistribution and use in source and binary forms, with or without modi- *
* fication, are permitted provided that the following conditions are met: *
* *
* 1. Redistributions of source code must retain the above copyright notice *
* notice, this list of conditions and the following disclaimer. *
* *
* 2. Redistributions in binary form must reproduce the above copyright *
* notice, this list of conditions and the following disclaimer in the *
* documentation and/or other materials provided with the distribution. *
* *
* 3. The end-user documentation included with the redistribution, if any, *
* must include the following acknowlegement: *
* *
* "This product includes software developed by the Apache Software *
* Foundation <http://www.apache.org/>." *
* *
* Alternately, this acknowlegement may appear in the software itself, if *
* and wherever such third-party acknowlegements normally appear. *
* *
* 4. The names "The Jakarta Project", "WebApp", and "Apache Software *
* Foundation" must not be used to endorse or promote products derived *
* from this software without prior written permission. For written *
* permission, please contact <ap...@apache.org>. *
* *
* 5. Products derived from this software may not be called "Apache" nor may *
* "Apache" appear in their names without prior written permission of the *
* Apache Software Foundation. *
* *
* THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED WARRANTIES *
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY *
* AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL *
* THE APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY *
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL *
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS *
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) *
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, *
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN *
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE *
* POSSIBILITY OF SUCH DAMAGE. *
* *
* ========================================================================= *
* *
* This software consists of voluntary contributions made by many indivi- *
* duals on behalf of the Apache Software Foundation. For more information *
* on the Apache Software Foundation, please see <http://www.apache.org/>. *
* *
* ========================================================================= */
/* @version $Id: SimpleService.java,v 1.1 2001/09/28 12:38:19 jfclere Exp $ */
package org.apache.service.support;
import java.io.*;
import java.net.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Enumeration;
import java.util.Vector;
import org.apache.service.Service;
import org.apache.service.ServiceController;
import org.apache.service.ServiceContext;
public class SimpleService implements Service, Runnable {
private ServerSocket server=null;
private Thread thread=null;
private ServiceController controller=null;
private boolean stopping=false;
private String directory=null;
private Vector handlers=null;
public SimpleService() {
super();
System.err.println("SimpleService: instance "+this.hashCode()+
" created");
this.handlers=new Vector();
}
protected void finalize() {
System.err.println("SimpleService: instance "+this.hashCode()+
" garbage collected");
}
/**
* init and destroy were added in jakarta-tomcat-service.
*/
public void init(ServiceContext context)
throws Exception {
System.err.println("SimpleService: instance "+this.hashCode()+
" init");
}
public void destroy() {
System.err.println("SimpleService: instance "+this.hashCode()+
" destroy");
}
public void run() {
int number=0;
System.err.println("SimpleService: started acceptor loop");
try {
while(!this.stopping) {
Socket socket=this.server.accept();
Handler handler=new Handler(socket,this,this.controller);
handler.setConnectionNumber(number++);
handler.setDirectoryName(this.directory);
new Thread(handler).start();
}
} catch (IOException e) {
/* Don't dump any error message if we are stopping. A IOException
is generated when the ServerSocket is closed in stop() */
if (!this.stopping) e.printStackTrace(System.err);
}
/* Terminate all handlers that at this point are still open */
Enumeration openhandlers=this.handlers.elements();
while (openhandlers.hasMoreElements()) {
Handler handler=(Handler)openhandlers.nextElement();
System.err.println("SimpleService: dropping connection "+
handler.getConnectionNumber());
handler.close();
}
System.err.println("SimpleService: exiting acceptor loop");
}
public void load(ServiceController controller, String a[])
throws IOException {
int port=1200;
if (a.length>0) port=Integer.parseInt(a[0]);
if (a.length>1) this.directory=a[1];
else this.directory="/tmp";
/* Dump a message */
System.err.println("SimpleService: loading on port "+port);
/* Set up this simple service */
this.controller=controller;
this.server=new ServerSocket(port);
this.thread=new Thread(this);
}
public void start() {
/* Dump a message */
System.err.println("SimpleService: starting");
/* Start */
this.thread.start();
}
public void stop()
throws IOException, InterruptedException {
/* Dump a message */
System.err.println("SimpleService: stopping");
/* Close the ServerSocket. This will make our thread to terminate */
this.stopping=true;
this.server.close();
/* Wait for the main thread to exit and dump a message */
this.thread.join(5000);
System.err.println("SimpleService: stopped");
}
protected void addHandler(Handler handler) {
synchronized (handler) {
this.handlers.add(handler);
}
}
protected void removeHandler(Handler handler) {
synchronized (handler) {
this.handlers.remove(handler);
}
}
public static class Handler implements Runnable {
private ServiceController controller=null;
private SimpleService parent=null;
private String directory=null;
private Socket socket=null;
private int number=0;
public Handler(Socket s, SimpleService p, ServiceController c) {
super();
this.socket=s;
this.parent=p;
this.controller=c;
}
public void run() {
this.parent.addHandler(this);
System.err.println("SimpleService: connection "+this.number+
" opened from "+this.socket.getInetAddress());
try {
InputStream in=this.socket.getInputStream();
OutputStream out=this.socket.getOutputStream();
handle(in,out);
this.socket.close();
} catch (IOException e) {
e.printStackTrace(System.err);
}
System.err.println("SimpleService: connection "+this.number+
" closed");
this.parent.removeHandler(this);
}
public void close() {
try {
this.socket.close();
} catch (IOException e) {
e.printStackTrace(System.err);
}
}
public void setConnectionNumber(int number) {
this.number=number;
}
public int getConnectionNumber() {
return(this.number);
}
public void setDirectoryName(String directory) {
this.directory=directory;
}
public String getDirectoryName() {
return(this.directory);
}
public void log(String name)
throws IOException {
OutputStream file=new FileOutputStream(name,true);
PrintStream out=new PrintStream(file);
SimpleDateFormat fmt=new SimpleDateFormat();
out.println(fmt.format(new Date()));
out.close();
file.close();
}
public void handle(InputStream in, OutputStream os) {
PrintStream out=new PrintStream(os);
while(true) {
try {
/* If we don't have data in the System InputStream, we want
to ask to the user for an option. */
if (in.available()==0) {
out.println();
out.println("Please select one of the following:");
out.println(" 1) Shutdown");
out.println(" 2) Reload");
out.println(" 3) Create a file");
out.println(" 4) Disconnect");
out.print("Your choiche: ");
}
/* Read an option from the client */
int x=in.read();
switch (x) {
/* If the socket was closed, we simply return */
case -1:
return;
/* Attempt to shutdown */
case '1':
out.println("Attempting a shutdown...");
try {
this.controller.shutdown();
} catch (IllegalStateException e) {
out.println();
out.println("Can't shutdown now");
e.printStackTrace(out);
}
break;
/* Attempt to reload */
case '2':
out.println("Attempting a reload...");
try {
this.controller.reload();
} catch (IllegalStateException e) {
out.println();
out.println("Can't reload now");
e.printStackTrace(out);
}
break;
/* Disconnect */
case '3':
String name=this.getDirectoryName()+
"/SimpleService."+
this.getConnectionNumber()+
".tmp";
try {
this.log(name);
out.println("File '"+name+"' created");
} catch (IOException e) {
e.printStackTrace(out);
}
break;
/* Disconnect */
case '4':
out.println("Disconnecting...");
return;
/* Discard any carriage return / newline characters */
case '\r':
case '\n':
break;
/* We got something that we weren't supposed to get */
default:
out.println("Unknown option '"+(char)x+"'");
break;
}
/* If we get an IOException we return (disconnect) */
} catch (IOException e) {
System.err.println("SimpleService: IOException in "+
"connection "+
this.getConnectionNumber());
return;
}
}
}
}
}