You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@avalon.apache.org by do...@apache.org on 2001/03/16 14:16:47 UTC
cvs commit: jakarta-avalon-cornerstone/src/java/org/apache/cornerstone/demos/xcommander/xcommands Chat.java Echo.java package.html
donaldp 01/03/16 05:16:47
Modified: src/conf avalon-demo-assembly.xml
src/java/org/apache/cornerstone/blocks/connection
DefaultConnectionManager.java
src/java/org/apache/cornerstone/blocks/sockets
DefaultSocketFactory.java
Added: src/content/xcommander socketTester.fla socketTester.html
socketTester.swf
src/java/org/apache/cornerstone/demos/xcommander
CommandHandler.java DefaultSAXHandler.java
DocumentHandler.java ElementHandler.java
GlobalResult.java XCommand.java
XCommanderHandler.java XCommanderServer.java
XCommanderServer.xinfo XCommanderService.java
package.html
src/java/org/apache/cornerstone/demos/xcommander/doc-files
sample-command.xml
src/java/org/apache/cornerstone/demos/xcommander/saxhandlers
AbstractElementHandler.java
ArgumentsElementHandler.java
ArraylistElementHandler.java
BooleanElementHandler.java
CommandElementHandler.java
ConstructorElementHandler.java
DoubleElementHandler.java EmptyElementHandler.java
FloatElementHandler.java IntElementHandler.java
MethodElementHandler.java ObjectElementHandler.java
ReferenceElementHandler.java
StringElementHandler.java TypeElementHandler.java
package.html
src/java/org/apache/cornerstone/demos/xcommander/xcommands
Chat.java Echo.java package.html
Log:
Added in demo commander. An xml communication server that works with flas.
Submitted by: "Leo Simons" <ma...@leosimons.com>
Revision Changes Path
1.3 +11 -2 jakarta-avalon-cornerstone/src/conf/avalon-demo-assembly.xml
Index: avalon-demo-assembly.xml
===================================================================
RCS file: /home/cvs/jakarta-avalon-cornerstone/src/conf/avalon-demo-assembly.xml,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- avalon-demo-assembly.xml 2001/03/13 04:51:55 1.2
+++ avalon-demo-assembly.xml 2001/03/16 13:16:43 1.3
@@ -154,8 +154,17 @@
</configuration>
</block>
-</phoenix>
+ <block class="org.apache.cornerstone.demos.xcommander.XCommanderServer"
+ name="xcommander-server" >
+ <provide name="sockets" role="org.apache.cornerstone.services.sockets.SocketManager"/>
+ <provide name="connections"
+ role="org.apache.cornerstone.services.connection.ConnectionManager" />
+ <!-- Note trailing file separator in destinationURL -->
+ <configuration>
+ <port>4557</port>
+ </configuration>
+ </block>
-
+</phoenix>
\ No newline at end of file
1.1 jakarta-avalon-cornerstone/src/content/xcommander/socketTester.fla
<<Binary file>>
1.1 jakarta-avalon-cornerstone/src/content/xcommander/socketTester.html
Index: socketTester.html
===================================================================
<HTML>
<HEAD>
<TITLE>socketTester</TITLE>
</HEAD>
<BODY bgcolor="#FFFFFF">
<!-- URL's used in the movie-->
<!-- text used in the movie-->
<OBJECT classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"
codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=5,0,0,0"
WIDTH=550 HEIGHT=400>
<PARAM NAME=movie VALUE="socketTester.swf"> <PARAM NAME=quality VALUE=high> <PARAM NAME=bgcolor VALUE=#FFFFFF> <EMBED src="socketTester.swf" quality=high bgcolor=#FFFFFF WIDTH=550 HEIGHT=400 TYPE="application/x-shockwave-flash" PLUGINSPAGE="http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash"></EMBED>
</OBJECT>
</BODY>
</HTML>
1.1 jakarta-avalon-cornerstone/src/content/xcommander/socketTester.swf
<<Binary file>>
1.2 +0 -2 jakarta-avalon-cornerstone/src/java/org/apache/cornerstone/blocks/connection/DefaultConnectionManager.java
Index: DefaultConnectionManager.java
===================================================================
RCS file: /home/cvs/jakarta-avalon-cornerstone/src/java/org/apache/cornerstone/blocks/connection/DefaultConnectionManager.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- DefaultConnectionManager.java 2001/02/22 07:52:16 1.1
+++ DefaultConnectionManager.java 2001/03/16 13:16:44 1.2
@@ -26,8 +26,6 @@
/**
* This is the service through which ConnectionManagement occurs.
*
- * WARNING: This is very early ALPHA and the interface will most likely change.
- *
* @author <a href="mailto:donaldp@apache.org">Peter Donald</a>
*/
public class DefaultConnectionManager
1.2 +4 -2 jakarta-avalon-cornerstone/src/java/org/apache/cornerstone/blocks/sockets/DefaultSocketFactory.java
Index: DefaultSocketFactory.java
===================================================================
RCS file: /home/cvs/jakarta-avalon-cornerstone/src/java/org/apache/cornerstone/blocks/sockets/DefaultSocketFactory.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- DefaultSocketFactory.java 2001/02/22 07:52:17 1.1
+++ DefaultSocketFactory.java 2001/03/16 13:16:45 1.2
@@ -45,8 +45,10 @@
* @return the socket
* @exception IOException if an error occurs
*/
- public Socket createSocket( final InetAddress address, final int port,
- final InetAddress localAddress, final int localPort )
+ public Socket createSocket( final InetAddress address,
+ final int port,
+ final InetAddress localAddress,
+ final int localPort )
throws IOException
{
return new Socket( address, port, localAddress, localPort );
1.1 jakarta-avalon-cornerstone/src/java/org/apache/cornerstone/demos/xcommander/CommandHandler.java
Index: CommandHandler.java
===================================================================
/*
* Copyright (C) The Apache Software Foundation. All rights reserved.
*
* This software is published under the terms of the Apache Software License
* version 1.1, a copy of which has been included with this distribution in
* the LICENSE file.
*/
package org.apache.cornerstone.demos.xcommander;
import java.io.CharArrayWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import org.xml.sax.Attributes;
import org.xml.sax.ErrorHandler;
import org.xml.sax.InputSource;
import org.xml.sax.Locator;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;
import org.xml.sax.helpers.XMLReaderFactory;
/**
* Contains callback methods for handling requests and results of XCommands.
*
* @author <a href="mailto:mail@leosimons.com">Leo Simons</a>
*/
public interface CommandHandler
{
/** Provides the result returned from an XCommand. */
void handleCommand( String type, String identifier, Object results );
/** Asks for the class implementing the specified XCommand.
* You are encouraged to follow the java naming standard for
* XCommand names.
* @return the relevant Class, or null if the specified XCommand is not known. */
Class getCommand( String commandName );
}
1.1 jakarta-avalon-cornerstone/src/java/org/apache/cornerstone/demos/xcommander/DefaultSAXHandler.java
Index: DefaultSAXHandler.java
===================================================================
/*
* Copyright (C) The Apache Software Foundation. All rights reserved.
*
* This software is published under the terms of the Apache Software License
* version 1.1, a copy of which has been included with this distribution in
* the LICENSE file.
*/
package org.apache.cornerstone.demos.xcommander;
import java.io.CharArrayWriter;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;
import org.apache.cornerstone.demos.xcommander.saxhandlers.AbstractElementHandler;
import org.apache.cornerstone.demos.xcommander.saxhandlers.ArgumentsElementHandler;
import org.apache.cornerstone.demos.xcommander.saxhandlers.ArraylistElementHandler;
import org.apache.cornerstone.demos.xcommander.saxhandlers.BooleanElementHandler;
import org.apache.cornerstone.demos.xcommander.saxhandlers.CommandElementHandler;
import org.apache.cornerstone.demos.xcommander.saxhandlers.ConstructorElementHandler;
import org.apache.cornerstone.demos.xcommander.saxhandlers.DoubleElementHandler;
import org.apache.cornerstone.demos.xcommander.saxhandlers.EmptyElementHandler;
import org.apache.cornerstone.demos.xcommander.saxhandlers.FloatElementHandler;
import org.apache.cornerstone.demos.xcommander.saxhandlers.IntElementHandler;
import org.apache.cornerstone.demos.xcommander.saxhandlers.MethodElementHandler;
import org.apache.cornerstone.demos.xcommander.saxhandlers.ObjectElementHandler;
import org.apache.cornerstone.demos.xcommander.saxhandlers.ReferenceElementHandler;
import org.apache.cornerstone.demos.xcommander.saxhandlers.StringElementHandler;
import org.apache.cornerstone.demos.xcommander.saxhandlers.TypeElementHandler;
import org.xml.sax.Attributes;
import org.xml.sax.ErrorHandler;
import org.xml.sax.InputSource;
import org.xml.sax.Locator;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;
import org.xml.sax.helpers.XMLReaderFactory;
/**
* <p>A SAX2 ContentHandler and ErrorHandler which parses an xml command
* document and then executes it. DefaultSAXHandler returns the results of
* the method called by the xml command document to it's parent by calling
* the parent's handleCommand( Object o ) method.</p>
* <p>A sample xml command can be found <a href="doc-files/sample-command.xml">here</a>.<br />
*
* @author <a href="mailto:mail@leosimons.com">Leo Simons</a>
*/
public class DefaultSAXHandler
extends DefaultHandler
implements ErrorHandler
{
// stack object containing element handlers in use
private ElementHandlerStack stack = new ElementHandlerStack();
// list containing element handlers
private ArrayList handlers = new ArrayList();
// container used for character data
private CharArrayWriter contents;
// method called to provide results of xml command
private CommandHandler parent;
/**
* @param ch The supplied CommandHandler will receive the results of all XCommands.
*/
DefaultSAXHandler( CommandHandler ch )
{
parent = ch;
contents = new CharArrayWriter();
handlers.add( CommandElementHandler.class );
handlers.add( ConstructorElementHandler.class );
handlers.add( MethodElementHandler.class );
handlers.add( ArgumentsElementHandler.class );
handlers.add( ObjectElementHandler.class );
handlers.add( ReferenceElementHandler.class );
handlers.add( ArraylistElementHandler.class );
handlers.add( StringElementHandler.class );
handlers.add( IntElementHandler.class );
handlers.add( BooleanElementHandler.class );
handlers.add( FloatElementHandler.class );
handlers.add( DoubleElementHandler.class );
}
// SAX2 ContentHandler Functions
public void startDocument()
throws SAXException
{
// the root handler is created...
stack.push( new DocumentHandler() );
}
public void endDocument()
throws SAXException
{
final DocumentHandler documentHandler = (DocumentHandler)stack.pop();
documentHandler.executeCommand( parent );
}
public void startElement( final String namespaceURI,
final String localName,
final String qName,
final Attributes atts )
throws SAXException
{
ElementHandler el = null;
// try to find the correct handler class and create an instance from it...
Iterator it = handlers.iterator();
while( it.hasNext() )
{
Class currClass = (Class)it.next();
Object currObj;
try
{
currObj = currClass.newInstance();
}
catch ( Exception e )
{
throw new
SAXException( "Could not instantiate a class in DefaultSAXHandler.handlers",
e );
}
ElementHandler eh = (ElementHandler)currObj;
if( eh.getLocalName().equals(localName) )
{
// we've got a correct ElementHandler!
el = eh;
break;
}
}
if( null == el )
{
// couldn't find an element anywhere.....
throw new SAXException( "No handler for xml element <" + localName + "> found." );
}
try
{
stack.peek().addChild( el );
stack.push( el );
el.start( atts );
}
catch( final IllegalArgumentException iae )
{
// the parent will not accept elements of this type.
// we will insert an 'empty' child, so further child
// elements will be added to the empty child which
// does nothing.
EmptyElementHandler eeh = new EmptyElementHandler();
stack.peek().addEmptyChild( eeh );
stack.push( eeh );
eeh.start( atts );
}
}
public void endElement( final String namespaceURI,
final String localName,
final String qName )
throws SAXException
{
stack.peek().end( contents.toString() );
contents.reset();
stack.pop();
}
public void characters( final char[] ch, final int start, final int length )
throws SAXException
{
contents.write( ch, start, length );
}
// SAX2 ErrorHandler Functions
public void warning( final SAXParseException spe )
throws SAXException
{
}
public void error( final SAXParseException spe )
throws SAXException
{
}
public void fatalError( final SAXParseException spe )
throws SAXException
{
}
/** This is simply a safe wrapper for a LinkedList that implements a simple stack. */
public class ElementHandlerStack
{
private LinkedList m_list = new LinkedList();
public boolean isEmpty()
{
return m_list.isEmpty();
}
public void push( final ElementHandler elementHandler )
{
m_list.addLast( elementHandler );
}
public ElementHandler pop()
{
return (ElementHandler)m_list.removeLast();
}
public ElementHandler peek()
{
return (ElementHandler)m_list.getLast();
}
}
}
1.1 jakarta-avalon-cornerstone/src/java/org/apache/cornerstone/demos/xcommander/DocumentHandler.java
Index: DocumentHandler.java
===================================================================
/*
* Copyright (C) The Apache Software Foundation. All rights reserved.
*
* This software is published under the terms of the Apache Software License
* version 1.1, a copy of which has been included with this distribution in
* the LICENSE file.
*/
package org.apache.cornerstone.demos.xcommander;
import java.io.CharArrayWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import org.apache.cornerstone.demos.xcommander.saxhandlers.CommandElementHandler;
import org.apache.cornerstone.demos.xcommander.saxhandlers.EmptyElementHandler;
import org.xml.sax.Attributes;
import org.xml.sax.ErrorHandler;
import org.xml.sax.InputSource;
import org.xml.sax.Locator;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;
import org.xml.sax.helpers.XMLReaderFactory;
/**
* This is not a real element handler, but rather it represents the start of
* an xml document.
*
* @author <a href="mailto:mail@leosimons.com">Leo Simons</a>
*/
public class DocumentHandler
implements ElementHandler
{
protected ArrayList children = new ArrayList();
/** does not do anything */
public void start( final Attributes attributes )
{
}
/** does not do anything */
public void end( final String contents )
{
}
/** a DocumentHandler takes CommandElementHandlers only. */
public void addChild( final ElementHandler elementHandler )
throws IllegalArgumentException
{
if( elementHandler instanceof CommandElementHandler )
{
children.add( elementHandler );
}
else
{
throw new IllegalArgumentException();
}
}
public void addEmptyChild( final EmptyElementHandler emptyElementHandler )
{
children.add( emptyElementHandler );
}
public boolean removeChild( ElementHandler elementHandler )
{
if( children.contains( elementHandler ) )
{
return children.remove( elementHandler );
}
return false;
}
public ArrayList getChildren()
{
return children;
}
/** This method checks whether two elements are of an equal type. It
* checks for similar namespaceURIs and localNames, and also
* if the type attributes (when available) are equal. */
public boolean equalTypes( final ElementHandler elementHandler )
{
if((elementHandler.getNamespaceURI() == this.getNamespaceURI()) &&
(elementHandler.getLocalName() == this.getLocalName()))
{
return true;
}
return false;
}
/** calls equalTypes */
public boolean equals( final Object object )
{
if( object instanceof ElementHandler )
{
return equalTypes( (ElementHandler)object );
}
return false;
}
/** @return the string "DocumentHandler" */
public String getNamespaceURI()
{
return "DocumentHandler";
}
/** @return the string "DocumentHandler" */
public String getLocalName()
{
return "DocumentHandler";
}
/** Calls {@link org.apache.cornerstone.demos.xcommander.saxhandlers.CommandElementHandler#executeCommand(CommandHandler) executeCommand()} on every CommandElementHandler.
*
* @param ch The CommandHandler which is to receive the results of this document's XCommands.
*/
public void executeCommand( final CommandHandler commandHandler )
throws SAXException
{
final Iterator it = children.iterator();
while( it.hasNext() )
{
final ElementHandler elementHandler = (ElementHandler)it.next();
if( elementHandler instanceof CommandElementHandler )
{
final CommandElementHandler commandElementHandler =
(CommandElementHandler)elementHandler;
commandElementHandler.executeCommand( commandHandler );
}
}
}
}
1.1 jakarta-avalon-cornerstone/src/java/org/apache/cornerstone/demos/xcommander/ElementHandler.java
Index: ElementHandler.java
===================================================================
/*
* Copyright (C) The Apache Software Foundation. All rights reserved.
*
* This software is published under the terms of the Apache Software License
* version 1.1, a copy of which has been included with this distribution in
* the LICENSE file.
*/
package org.apache.cornerstone.demos.xcommander;
import java.util.ArrayList;
import org.apache.cornerstone.demos.xcommander.saxhandlers.EmptyElementHandler;
import org.xml.sax.Attributes;
/**
* Defines the interface to be implemented by a class that is capable of receiving
* events fired by the DefaultSAXHandler.
*
* @author <a href="mailto:mail@leosimons.com">Leo Simons</a>
*/
public interface ElementHandler
{
/** This method is called by the eh its parent after the eh is constructed
* and validated to be of the right type, but before addChild() is called.
*/
void start( Attributes attributes );
/** This method is called by the eh its parent after all child eh have been
* added, started and stopped. */
void end( String contents );
/** This method is called by the eh its parent when a child element is found.
*/
void addChild( ElementHandler elementHandler )
throws IllegalArgumentException;
/** This method is called by the eh its parent when addChild() throws its
* exception, providing a special eh - an EmptyElementHandler.
*/
void addEmptyChild( EmptyElementHandler emptyElementHandler );
/** This method is probably redundant... */
boolean removeChild( ElementHandler elementHandler );
ArrayList getChildren();
/** This method checks whether two elements are of an equal type. It
* checks for similar namespaceURIs and localNames, and also
* if the type attributes (when available) are equal.
*/
boolean equalTypes( ElementHandler elementHandler );
/* calls equalTypes */
boolean equals( Object object );
/** @return At the moment, this does not return the namespace URI, but rather the name of the element handled.*/
String getNamespaceURI();
/** @return At the moment, this does not return the localname, but rather the name of the element handled.*/
String getLocalName();
}
1.1 jakarta-avalon-cornerstone/src/java/org/apache/cornerstone/demos/xcommander/GlobalResult.java
Index: GlobalResult.java
===================================================================
/*
* Copyright (C) The Apache Software Foundation. All rights reserved.
*
* This software is published under the terms of the Apache Software License
* version 1.1, a copy of which has been included with this distribution in
* the LICENSE file.
*/
package org.apache.cornerstone.demos.xcommander;
/**
* If the results of an XCommand implement this interface, they're sent to
* all the current clients.
*
* @author <a href="mailto:mail@leosimons.com">Leo Simons</a>
*/
public interface GlobalResult
{
}
1.1 jakarta-avalon-cornerstone/src/java/org/apache/cornerstone/demos/xcommander/XCommand.java
Index: XCommand.java
===================================================================
/*
* Copyright (C) The Apache Software Foundation. All rights reserved.
*
* This software is published under the terms of the Apache Software License
* version 1.1, a copy of which has been included with this distribution in
* the LICENSE file.
*/
package org.apache.cornerstone.demos.xcommander;
/**
* This empty interface identifies classes which can be called using XCommander.
* Results returned from methods which are called on XCommands (and any public
* method can be called) should always be objects whose toString() method returns
* valid xml (though the xml declaration may be omitted).
*
* @author <a href="mailto:mail@leosimons.com">Leo Simons</a>
*/
public interface XCommand
{
}
1.1 jakarta-avalon-cornerstone/src/java/org/apache/cornerstone/demos/xcommander/XCommanderHandler.java
Index: XCommanderHandler.java
===================================================================
/*
* Copyright (C) The Apache Software Foundation. All rights reserved.
*
* This software is published under the terms of the Apache Software License
* version 1.1, a copy of which has been included with this distribution in
* the LICENSE file.
*/
package org.apache.cornerstone.demos.xcommander;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.InterruptedIOException;
import java.io.OutputStreamWriter;
import java.io.StringReader;
import java.net.Socket;
import java.net.SocketException;
import org.apache.avalon.AbstractLoggable;
import org.apache.avalon.Component;
import org.apache.avalon.Initializable;
import org.apache.cornerstone.services.connection.ConnectionHandler;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.XMLReaderFactory;
/**
* This handles an individual incoming XCommander request.
*
* @author <a href="mailto:mail@leosimons.com">Leo Simons</a>
*/
public class XCommanderHandler
extends AbstractLoggable
implements Component, Initializable, ConnectionHandler, CommandHandler
{
protected final static String DEFAULT_PARSER = "org.apache.xerces.parsers.SAXParser";
protected final static String PARSER =
System.getProperty("org.xml.sax.parser", DEFAULT_PARSER );
protected XMLReader m_parser;
protected XCommanderServer m_parent;
protected BufferedReader m_in = null;
protected BufferedWriter m_out = null;
public XCommanderHandler( XCommanderServer parentServer )
{
m_parent = parentServer;
}
public void init() throws Exception
{
// create a SAX2 Parser from the org.xml.sax.parser system property,
// or else from the DEFUALT_PARSER.
try
{
m_parser = XMLReaderFactory.createXMLReader( PARSER );
//m_parser.setFeature("http://xml.org/sax/features/namespace-prefixes", true);
DefaultSAXHandler saxHandler = new DefaultSAXHandler( this );
m_parser.setContentHandler( saxHandler );
m_parser.setErrorHandler( saxHandler );
}
catch( final SAXException se )
{
getLogger().error( "Unable to setup SAX parser: " + se );
}
}
public void handleCommand( String type, String identifier, Object result )
{
if(result instanceof GlobalResult)
{
System.out.println( "Sending result " + result + " to main server." );
m_parent.handleCommand( type, identifier, result );
}
else
{
getLogger().info( "handling command: " + identifier +
" - sending results: " + result.toString() );
if( m_out != null )
{
String results = "<?xml version=\"1.0\" ?>\n";
results +=
"<results type=\"" + type + "\" identifier=\"" + identifier + "\">\n";
results += result.toString() + "\n</results>";
try
{
final char[] end = new char[1];
end[0] = '\u0000';
m_out.write( results ); // should be valid xml
m_out.write( end );
m_out.flush();
}
catch( final SocketException se )
{
getLogger().warn( "Socket closed remotely.", se );
}
catch( final InterruptedIOException iioe )
{
getLogger().warn( "Socket timeout.", iioe );
}
catch( IOException ioe )
{
getLogger().warn( "Exception handling socket:" + ioe.getMessage(), ioe );
}
catch( final Exception e )
{
getLogger().warn( "Exception on socket: " + e.getMessage(), e );
}
}
else
{
getLogger().info( "Exception: Socket not available." );
}
}
}
public Class getCommand( final String commandName )
{
return m_parent.getCommand( commandName );
}
/**
* Handle a connection.
* This handler is responsible for processing connections as they occur.
*
* @param socket the connection
* @exception IOException if an error reading from socket occurs
*/
public void handleConnection( final Socket socket )
throws IOException
{
String remoteHost = null;
String remoteIP = null;
try
{
m_in =
new BufferedReader( new InputStreamReader( socket.getInputStream() ), 1024 );
m_out =
new BufferedWriter( new OutputStreamWriter( socket.getOutputStream() ) );
remoteHost = socket.getInetAddress().getHostName();
remoteIP = socket.getInetAddress().getHostAddress();
getLogger().info( "Connection from " + remoteHost + " ( " + remoteIP + " )" );
// notify XCommanderServer of the new client
m_parent.addClient(this);
// read the input. When a zero byte is encountered,
// pass the input to the SAX parser.
String inputLine;
int streamResult;
char buf[] = new char[1];
do
{
inputLine = "";
do
{
streamResult = m_in.read( buf, 0, 1 );
inputLine += buf[0];
}
while( buf[0] != '\u0000' );
inputLine = inputLine.substring(0,(inputLine.length()-1));
if( -1 != inputLine.indexOf( "<command" ) )
{
if( !inputLine.startsWith( "<?xml " ) )
{
inputLine = "<?xml version=\"1.0\" ?> " + inputLine;
}
m_parser.parse( new InputSource( new StringReader( inputLine ) ) );
}
}
while( streamResult != -1 );
//Finish
m_out.flush();
// we lost the client; notify the server
m_parent.removeClient( this );
m_out.close();
m_in.close();
socket.close();
}
catch( final SocketException se )
{
getLogger().info( "Socket to " + remoteHost + " closed remotely." );
}
catch( final InterruptedIOException iioe )
{
getLogger().info( "Socket to " + remoteHost + " timeout." );
}
catch( IOException ioe )
{
getLogger().info( "Exception handling socket to " + remoteHost + ":" +
ioe.getMessage() );
}
catch( final Exception e )
{
getLogger().info( "Exception on socket: " + e.getMessage() );
}
finally
{
try { socket.close(); }
catch( final IOException ioe )
{
getLogger().error( "Exception closing socket: " + ioe.getMessage() );
}
try { m_parent.removeClient( this ); }
catch( final Exception e )
{
}
}
}
}
1.1 jakarta-avalon-cornerstone/src/java/org/apache/cornerstone/demos/xcommander/XCommanderServer.java
Index: XCommanderServer.java
===================================================================
/*
* Copyright (C) The Apache Software Foundation. All rights reserved.
*
* This software is published under the terms of the Apache Software License
* version 1.1, a copy of which has been included with this distribution in
* the LICENSE file.
*/
package org.apache.cornerstone.demos.xcommander;
import java.net.ProtocolException;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import org.apache.avalon.AbstractLoggable;
import org.apache.avalon.Component;
import org.apache.avalon.ComponentManager;
import org.apache.avalon.ComponentManagerException;
import org.apache.avalon.Composer;
import org.apache.avalon.Configurable;
import org.apache.avalon.Configuration;
import org.apache.avalon.ConfigurationException;
import org.apache.avalon.Initializable;
import org.apache.cornerstone.demos.xcommander.xcommands.Chat;
import org.apache.cornerstone.demos.xcommander.xcommands.Echo;
import org.apache.cornerstone.services.connection.ConnectionHandler;
import org.apache.cornerstone.services.connection.ConnectionHandlerFactory;
import org.apache.cornerstone.services.connection.ConnectionManager;
import org.apache.cornerstone.services.sockets.ServerSocketFactory;
import org.apache.cornerstone.services.sockets.SocketManager;
import org.apache.phoenix.Block;
/**
* A socket server which listens for XCommander requests.
*
* Based on SimpleServer & helloworldserver. this Server is the entry-point
* for XCommander. It listens on a port specified in the block's configuration,
* and delegates incoming requests to an {@link XCommanderHandler XCommanderHandler}.
*
* @author <a href="mailto:mail@leosimons.com">Leo Simons</a>
*/
public class XCommanderServer
extends AbstractLoggable
implements Block, Composer, Configurable, Initializable,
ConnectionHandlerFactory, XCommanderService, CommandHandler
{
// block stuff
protected Configuration m_configuration;
protected SocketManager m_socketManager;
protected ConnectionManager m_connectionManager;
protected ArrayList m_clients;
protected HashMap m_xcommands;
// BLOCK METHODS
public void compose( final ComponentManager componentManager )
throws ComponentManagerException
{
m_socketManager = (SocketManager)componentManager.
lookup( "org.apache.cornerstone.services.sockets.SocketManager" );
m_connectionManager = (ConnectionManager)componentManager.
lookup( "org.apache.cornerstone.services.connection.ConnectionManager" );
}
public void configure( final Configuration configuration )
throws ConfigurationException
{
m_configuration = configuration;
}
public void init()
throws Exception
{
m_clients = new ArrayList();
m_xcommands = new HashMap();
try
{
this.addCommand("xcommander.Echo", Echo.class );
this.addCommand("org.apache.cornerstone.demos.xcommander.roles.Echo", Echo.class );
this.addCommand("xcommander.Chat", Chat.class );
this.addCommand("org.apache.cornerstone.demos.xcommander.roles.Chat", Chat.class );
}
catch( final Exception e )
{
// never happens...
}
getLogger().info( "init XCommanderServer ..." );
final int port = m_configuration.getChild( "port" ).getValueAsInt();
getLogger().info( "Want to open port on:" + port );
final ServerSocketFactory factory =
m_socketManager.getServerSocketFactory( "plain" );
final ServerSocket serverSocket = factory.createServerSocket( port );
m_connectionManager.connect( "XCommanderListener", serverSocket, this );
getLogger().info( "Got socket" );
getLogger().info( "...XCommanderServer init" );
}
/**
* Construct an appropriate ConnectionHandler.
*
* @return the new ConnectionHandler
* @exception Exception if an error occurs
*/
public ConnectionHandler createConnectionHandler()
throws Exception
{
final XCommanderHandler handler = new XCommanderHandler( this );
setupLogger( handler );
handler.init();
return handler;
}
void addClient( final XCommanderHandler handler )
{
m_clients.add( handler );
}
void removeClient( final XCommanderHandler handler )
{
m_clients.remove( handler );
}
public void handleCommand( final String type,
final String identifier,
final Object result )
{
if( result instanceof GlobalResult )
{
Iterator it = m_clients.iterator();
while(it.hasNext())
{
((XCommanderHandler)it.next()).
handleCommand( type, identifier, result.toString());
}
}
}
public Class getCommand( final String commandName )
{
return (Class)m_xcommands.get( commandName );
}
/**
* Use this method to add an XCommand. Make sure you watch security
* concerns: all methods of an added class can be called.
*
* @throws IllegalArgumentException if the specified command is not
* an instance of XCommand.
*/
public Object addCommand ( final String commandName,
final Class command )
throws IllegalArgumentException
{
if( XCommand.class.isAssignableFrom( command ) )
{
return m_xcommands.put( commandName, command );
}
else
{
throw new IllegalArgumentException();
}
}
}
1.1 jakarta-avalon-cornerstone/src/java/org/apache/cornerstone/demos/xcommander/XCommanderServer.xinfo
Index: XCommanderServer.xinfo
===================================================================
<?xml version="1.0"?>
<blockinfo>
<meta>
<contributors>
<author name="Leo Simons" email="mail@leosimons.com"/>
</contributors>
</meta>
<!-- services that are offered by this block -->
<services>
<service name="org.apache.cornerstone.demos.xcommander.XCommanderService" version="1.0" />
</services>
<dependencies>
<dependency>
<role>org.apache.cornerstone.services.connection.ConnectionManager</role>
<service name="org.apache.cornerstone.services.connection.ConnectionManager"
version="1.0"/>
</dependency>
<dependency>
<role>org.apache.cornerstone.services.sockets.SocketManager</role>
<service name="org.apache.cornerstone.services.sockets.SocketManager" version="1.0"/>
</dependency>
</dependencies>
</blockinfo>
1.1 jakarta-avalon-cornerstone/src/java/org/apache/cornerstone/demos/xcommander/XCommanderService.java
Index: XCommanderService.java
===================================================================
/*
* Copyright (C) The Apache Software Foundation. All rights reserved.
*
* This software is published under the terms of the Apache Software License
* version 1.1, a copy of which has been included with this distribution in
* the LICENSE file.
*/
package org.apache.cornerstone.demos.xcommander;
import org.apache.phoenix.Service;
/**
* This is an empty service interface for the XCommander demo block.
*
* @author <a href="mailto:mail@leosimons.com">Leo Simons</a>
*/
public interface XCommanderService
extends Service
{
}
1.1 jakarta-avalon-cornerstone/src/java/org/apache/cornerstone/demos/xcommander/package.html
Index: package.html
===================================================================
<html>
<body>
A rather elaborate demo: a one-to-many socket server which is compatible with the flash 5 xml plugin.
<p>XCommander is a server program which is used to listen for and execute
java commands which are given in xml format
(here's a <a href="doc-files/sample-command.xml">sample command</a>).
It is possible to use XCommander to give any command which can be given
using standard code, using its lightweight xml over TCP/IP protocol.</p>
<p>Internally, XCommander uses a delegating SAX handler together with
the Xerces xml parser (which comes with Avalon) and simple RMI to
convert the xml into Java code.</p>
<p>At the moment, XCommander does not provide security. It simply uses
the current classloader for the block to find classes and execute methods
on them. This means anyone with knowledge of Avalon can wreck the server!
This will be changed: an addRole() and removeRole() option will be added
to the server, and the model Avalon uses for Roles will be implemented.</p>
</body>
</html>
1.1 jakarta-avalon-cornerstone/src/java/org/apache/cornerstone/demos/xcommander/doc-files/sample-command.xml
Index: sample-command.xml
===================================================================
<?xml version="1.0">
<command type="com.leosimons.flash.menu" identifier="getFlashMenu031" static="false">
<constructor>
<arguments>
<reference>com.leosimons.flash.MenuBar.DEFAULT_LOOK</reference>
</arguments>
</constructor>
<!-- if <method> is ommited, toXml() is used instead. ->
<method name="toHtml">
<arguments>
<string>Leo</string>
<int>1</int>
<float>2.4</float>
<double>4.5</double>
<boolean>false</boolean>
<reference>com.leosimons.flash.Menu.DEFAULT_LOOK</reference>
<object class="com.leosimons.flash.Menu">
<constructor>
<arguments>
<arraylist>
<object class="com.leosimons.flash.MenuItem">
<constructor><arguments>
<string >Menu Item1</string>
<arraylist>
<string>item 1</string>
<string>item 2</string>
<string>item 3</string>
<string>item 4</string>
<string>item 5</string>
</arraylist>
</arguments></constructor>
</object>
<object class="com.leosimons.flash.MenuItem">
<constructor><arguments>
<string>Menu Item1</string>
<arraylist>
<string>item 1</string>
<string>item 2</string>
<string>item 3</string>
<string>item 4</string>
<string>item 5</string>
</arraylist>
</arguments></constructor>
</object>
<object" class="com.leosimons.flash.MenuItem">
<constructor><arguments>
<string>Menu Item1</string>
<arraylist>
<string>item 1</string>
<string>item 2</string>
<string>item 3</string>
<string>item 4</string>
<string>item 5</string>
</arraylist>
</arguments></constructor>
</object>
</arraylist>
</arguments>
</constructor>
</object>
</arguments>
</method>
</command>
1.1 jakarta-avalon-cornerstone/src/java/org/apache/cornerstone/demos/xcommander/saxhandlers/AbstractElementHandler.java
Index: AbstractElementHandler.java
===================================================================
/*
* Copyright (C) The Apache Software Foundation. All rights reserved.
*
* This software is published under the terms of the Apache Software License
* version 1.1, a copy of which has been included with this distribution in
* the LICENSE file.
*/
package org.apache.cornerstone.demos.xcommander.saxhandlers;
import java.util.ArrayList;
import org.apache.cornerstone.demos.xcommander.*;
import org.xml.sax.Attributes;
/**
* Provides default implementations of the functions in ElementHandler.
* Define the start() and end() methods to create basic functionality.
*
* @author <a href="mailto:mail@leosimons.com">Leo Simons</a>
*/
public abstract class AbstractElementHandler
implements ElementHandler
{
protected ArrayList m_children = new ArrayList();
public void start( final Attributes attributes )
{
}
public void end( final String contents )
{
}
/** Override this method if you wish to be able to refuse the addition
* of an ElementHandler. */
public void addChild( final ElementHandler elementHandler )
throws IllegalArgumentException
{
m_children.add( elementHandler );
}
public void addEmptyChild( final EmptyElementHandler emptyElementHandler )
{
m_children.add( emptyElementHandler );
}
public boolean removeChild( final ElementHandler elementHandler )
{
if( m_children.contains( elementHandler ) )
{
return m_children.remove( elementHandler );
}
return false;
}
public ArrayList getChildren()
{
return m_children;
}
/** This method checks whether two elements are of an equal type. It
* checks for similar namespaceURIs and localNames, and also
* if the type attributes (when available) are equal. */
public boolean equalTypes( final ElementHandler elementHandler )
{
if((elementHandler.getNamespaceURI() == this.getNamespaceURI()) &&
(elementHandler.getLocalName() == this.getLocalName()))
{
return true;
}
return false;
}
/** calls equalTypes */
public boolean equals( final Object object )
{
if( object instanceof ElementHandler )
return equalTypes( (ElementHandler)object );
return false;
}
public String getNamespaceURI()
{
return "default";
}
public String getLocalName()
{
return "default";
}
}
1.1 jakarta-avalon-cornerstone/src/java/org/apache/cornerstone/demos/xcommander/saxhandlers/ArgumentsElementHandler.java
Index: ArgumentsElementHandler.java
===================================================================
/*
* Copyright (C) The Apache Software Foundation. All rights reserved.
*
* This software is published under the terms of the Apache Software License
* version 1.1, a copy of which has been included with this distribution in
* the LICENSE file.
*/
package org.apache.cornerstone.demos.xcommander.saxhandlers;
import org.apache.cornerstone.demos.xcommander.*;
import org.xml.sax.Attributes;
/**
*
*
* @author <a href="mailto:mail@leosimons.com">Leo Simons</a>
*/
public class ArgumentsElementHandler
extends AbstractElementHandler
{
public void addChild( final ElementHandler elementHandler )
throws IllegalArgumentException
{
if( elementHandler instanceof TypeElementHandler )
{
m_children.add( elementHandler );
}
else
{
throw new IllegalArgumentException();
}
}
public String getNamespaceURI()
{
return "arguments";
}
public String getLocalName()
{
return "arguments";
}
}
1.1 jakarta-avalon-cornerstone/src/java/org/apache/cornerstone/demos/xcommander/saxhandlers/ArraylistElementHandler.java
Index: ArraylistElementHandler.java
===================================================================
/*
* Copyright (C) The Apache Software Foundation. All rights reserved.
*
* This software is published under the terms of the Apache Software License
* version 1.1, a copy of which has been included with this distribution in
* the LICENSE file.
*/
package org.apache.cornerstone.demos.xcommander.saxhandlers;
import java.util.ArrayList;
import java.util.Iterator;
import org.apache.cornerstone.demos.xcommander.*;
import org.xml.sax.Attributes;
/**
*
*
* @author <a href="mailto:mail@leosimons.com">Leo Simons</a>
*/
public class ArraylistElementHandler
extends AbstractElementHandler
implements TypeElementHandler
{
private ArrayList values = new ArrayList();
public void addChild( final ElementHandler elementHandler )
throws IllegalArgumentException
{
if( elementHandler instanceof TypeElementHandler )
{
m_children.add( elementHandler );
}
else
{
throw new IllegalArgumentException();
}
}
public void end( final String contents )
{
Iterator it = m_children.iterator();
while ( it.hasNext() )
{
TypeElementHandler typeElementHandler = (TypeElementHandler)it.next();
values.add( typeElementHandler.getTypeValue() );
}
}
public Class getTypeClass()
{
return values.getClass();
}
public Object getTypeValue()
{
return values;
}
public String getNamespaceURI()
{
return "arraylist";
}
public String getLocalName()
{
return "arraylist";
}
}
1.1 jakarta-avalon-cornerstone/src/java/org/apache/cornerstone/demos/xcommander/saxhandlers/BooleanElementHandler.java
Index: BooleanElementHandler.java
===================================================================
/*
* Copyright (C) The Apache Software Foundation. All rights reserved.
*
* This software is published under the terms of the Apache Software License
* version 1.1, a copy of which has been included with this distribution in
* the LICENSE file.
*/
package org.apache.cornerstone.demos.xcommander.saxhandlers;
import org.apache.cornerstone.demos.xcommander.*;
import org.xml.sax.Attributes;
/**
*
*
* @author <a href="mailto:mail@leosimons.com">Leo Simons</a>
*/
public class BooleanElementHandler
extends AbstractElementHandler
implements TypeElementHandler
{
private Boolean value;
public void addChild( final ElementHandler elementHandler )
throws IllegalArgumentException
{
throw new IllegalArgumentException();
}
public void end( final String contents )
{
value = new Boolean( contents );
}
public Class getTypeClass()
{
return value.getClass();
}
public Object getTypeValue()
{
return value;
}
public String getNamespaceURI()
{
return "boolean";
}
public String getLocalName()
{
return "boolean";
}
}
1.1 jakarta-avalon-cornerstone/src/java/org/apache/cornerstone/demos/xcommander/saxhandlers/CommandElementHandler.java
Index: CommandElementHandler.java
===================================================================
/*
* Copyright (C) The Apache Software Foundation. All rights reserved.
*
* This software is published under the terms of the Apache Software License
* version 1.1, a copy of which has been included with this distribution in
* the LICENSE file.
*/
package org.apache.cornerstone.demos.xcommander.saxhandlers;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import org.apache.cornerstone.demos.xcommander.*;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
/**
* This is the entrypoint into the ElementHandler web.
*
* This eh listens for <command> elements. It accepts
* a single <constructor> and a single <method> as
* children.
*
* @author <a href="mailto:mail@leosimons.com">Leo Simons</a>
*/
public class CommandElementHandler
extends AbstractElementHandler
{
/** This is the method we try to call if none is specified */
public final static String DEFAULT_METHODNAME = "toString";
private String type;
private String identifier;
private ConstructorElementHandler ceh;
private MethodElementHandler meh;
public void addChild( final ElementHandler elementHandler )
throws IllegalArgumentException
{
if( elementHandler instanceof ConstructorElementHandler )
{
ceh = (ConstructorElementHandler)elementHandler;
}
else if( elementHandler instanceof MethodElementHandler )
{
meh = (MethodElementHandler)elementHandler;
}
else
{
throw new IllegalArgumentException();
}
}
public void start( final Attributes attributes )
{
type = attributes.getValue( "","type" );
identifier = attributes.getValue( "","identifier" );
}
/**
* This method is called when xml parsing of the XCommand is complete.
* It uses the supplied CommandHandler to get. After that,
* some RMI is used to try and call the specified method.
*
* @param ch This receives the results of the XCommand.
*/
public void executeCommand( final CommandHandler commandHandler )
throws SAXException
{
// try to find the specified role
// handlerClass = Class.forName( role );
Class handlerClass = commandHandler.getCommand( type );
if( handlerClass == null )
{
throw new SAXException( "the <command> " + identifier +
" could not be executed - unable to " +
"find the specified role: " + type + "!" );
}
Object o = null;
Object result = null;
if( null != ceh ) // otherwise, we assume a static method
{
try
{
// try to get an object from the constructor specified
// by ceh
o = handlerClass.getConstructor( ceh.getArgumentTypes() ).
newInstance( ceh.getArguments() );
}
catch( final Exception e )
{
// we'll try to use a static method
}
}
if( null != meh ) // otherwise, we assume the default method
{
try
{
final Method m = handlerClass.getMethod( meh.getName(), meh.getArgumentTypes() );
result = m.invoke( o, meh.getArguments() );
}
catch( final Exception e )
{
throw new SAXException( "the <command> " + identifier +
" could not be executed - unable " +
"to find the specified method!", e );
}
}
else
{
Class[] arr = new Class[0];
try
{
final Method m = handlerClass.getMethod( DEFAULT_METHODNAME, arr );
result = m.invoke( o, null );
}
catch( final Exception e )
{
throw new SAXException( "the <command> " + identifier +
" could not be executed - unable to " +
"find the default method!", e );
}
}
commandHandler.handleCommand( type, identifier, result );
}
public String getNamespaceURI()
{
return "command";
}
public String getLocalName()
{
return "command";
}
}
1.1 jakarta-avalon-cornerstone/src/java/org/apache/cornerstone/demos/xcommander/saxhandlers/ConstructorElementHandler.java
Index: ConstructorElementHandler.java
===================================================================
/*
* Copyright (C) The Apache Software Foundation. All rights reserved.
*
* This software is published under the terms of the Apache Software License
* version 1.1, a copy of which has been included with this distribution in
* the LICENSE file.
*/
package org.apache.cornerstone.demos.xcommander.saxhandlers;
import java.util.ArrayList;
import java.util.Iterator;
import org.apache.cornerstone.demos.xcommander.*;
import org.xml.sax.Attributes;
/**
*
*
* @author <a href="mailto:mail@leosimons.com">Leo Simons</a>
*/
public class ConstructorElementHandler
extends MethodElementHandler
{
public void start( final Attributes attributes )
{
name = "Constructor";
}
public String getNamespaceURI()
{
return "constructor";
}
public String getLocalName()
{
return "constructor";
}
}
1.1 jakarta-avalon-cornerstone/src/java/org/apache/cornerstone/demos/xcommander/saxhandlers/DoubleElementHandler.java
Index: DoubleElementHandler.java
===================================================================
/*
* Copyright (C) The Apache Software Foundation. All rights reserved.
*
* This software is published under the terms of the Apache Software License
* version 1.1, a copy of which has been included with this distribution in
* the LICENSE file.
*/
package org.apache.cornerstone.demos.xcommander.saxhandlers;
import org.apache.cornerstone.demos.xcommander.*;
import org.xml.sax.Attributes;
/**
*
*
* @author <a href="mailto:mail@leosimons.com">Leo Simons</a>
*/
public class DoubleElementHandler
extends AbstractElementHandler
implements TypeElementHandler
{
private Double value;
public void addChild( final ElementHandler elementHandler )
throws IllegalArgumentException
{
throw new IllegalArgumentException();
}
public void end( final String contents )
{
value = new Double( contents );
}
public Class getTypeClass()
{
return value.getClass();
}
public Object getTypeValue()
{
return value;
}
public String getNamespaceURI()
{
return "double";
}
public String getLocalName()
{
return "double";
}
}
1.1 jakarta-avalon-cornerstone/src/java/org/apache/cornerstone/demos/xcommander/saxhandlers/EmptyElementHandler.java
Index: EmptyElementHandler.java
===================================================================
/*
* Copyright (C) The Apache Software Foundation. All rights reserved.
*
* This software is published under the terms of the Apache Software License
* version 1.1, a copy of which has been included with this distribution in
* the LICENSE file.
*/
package org.apache.cornerstone.demos.xcommander.saxhandlers;
/**
*
*
* @author <a href="mailto:mail@leosimons.com">Leo Simons</a>
*/
public class EmptyElementHandler
extends AbstractElementHandler
{
}
1.1 jakarta-avalon-cornerstone/src/java/org/apache/cornerstone/demos/xcommander/saxhandlers/FloatElementHandler.java
Index: FloatElementHandler.java
===================================================================
/*
* Copyright (C) The Apache Software Foundation. All rights reserved.
*
* This software is published under the terms of the Apache Software License
* version 1.1, a copy of which has been included with this distribution in
* the LICENSE file.
*/
package org.apache.cornerstone.demos.xcommander.saxhandlers;
import org.apache.cornerstone.demos.xcommander.*;
import org.xml.sax.Attributes;
/**
*
*
* @author <a href="mailto:mail@leosimons.com">Leo Simons</a>
*/
public class FloatElementHandler
extends AbstractElementHandler
implements TypeElementHandler
{
private Float value;
public void addChild( final ElementHandler elementHandler )
throws IllegalArgumentException
{
throw new IllegalArgumentException();
}
public void end( final String contents )
{
value = new Float( contents );
}
public Class getTypeClass()
{
return value.getClass();
}
public Object getTypeValue()
{
return value;
}
public String getNamespaceURI()
{
return "float";
}
public String getLocalName()
{
return "float";
}
}
1.1 jakarta-avalon-cornerstone/src/java/org/apache/cornerstone/demos/xcommander/saxhandlers/IntElementHandler.java
Index: IntElementHandler.java
===================================================================
/*
* Copyright (C) The Apache Software Foundation. All rights reserved.
*
* This software is published under the terms of the Apache Software License
* version 1.1, a copy of which has been included with this distribution in
* the LICENSE file.
*/
package org.apache.cornerstone.demos.xcommander.saxhandlers;
import org.apache.cornerstone.demos.xcommander.*;
import org.xml.sax.Attributes;
/**
*
*
* @author <a href="mailto:mail@leosimons.com">Leo Simons</a>
*/
public class IntElementHandler
extends AbstractElementHandler
implements TypeElementHandler
{
private Integer value;
public void addChild( final ElementHandler elementHandler )
throws IllegalArgumentException
{
throw new IllegalArgumentException();
}
public void end( final String contents )
{
value = new Integer( contents );
}
public Class getTypeClass()
{
return value.getClass();
}
public Object getTypeValue()
{
return value;
}
public String getNamespaceURI()
{
return "int";
}
public String getLocalName()
{
return "int";
}
}
1.1 jakarta-avalon-cornerstone/src/java/org/apache/cornerstone/demos/xcommander/saxhandlers/MethodElementHandler.java
Index: MethodElementHandler.java
===================================================================
/*
* Copyright (C) The Apache Software Foundation. All rights reserved.
*
* This software is published under the terms of the Apache Software License
* version 1.1, a copy of which has been included with this distribution in
* the LICENSE file.
*/
package org.apache.cornerstone.demos.xcommander.saxhandlers;
import java.util.ArrayList;
import java.util.Iterator;
import org.apache.cornerstone.demos.xcommander.*;
import org.xml.sax.Attributes;
/**
*
*
* @author <a href="mailto:mail@leosimons.com">Leo Simons</a>
*/
public class MethodElementHandler
extends AbstractElementHandler
{
protected ArgumentsElementHandler aeh;
protected String name;
public void addChild( final ElementHandler elementHandler )
throws IllegalArgumentException
{
if( elementHandler instanceof ArgumentsElementHandler )
{
aeh = (ArgumentsElementHandler)elementHandler;
} else
{
throw new IllegalArgumentException();
}
}
public void start( final Attributes attributes )
{
name = attributes.getValue( "", "name" );
}
public String getName()
{
return name;
}
public Class[] getArgumentTypes()
{
if ( aeh != null )
{
ArrayList types = new ArrayList();
Iterator it = aeh.getChildren().iterator();
while( it.hasNext() )
{
TypeElementHandler teh = (TypeElementHandler)it.next();
types.add(teh.getTypeClass());
}
return (Class[])types.toArray(new Class[0]);
}
else
{
return new Class[0];
}
}
public Object[] getArguments()
{
if ( aeh != null )
{
ArrayList values = new ArrayList();
Iterator it = aeh.getChildren().iterator();
while( it.hasNext() )
{
TypeElementHandler teh = (TypeElementHandler)it.next();
values.add(teh.getTypeValue());
}
return values.toArray();
}
else
{
return new Object[0];
}
}
public String getNamespaceURI()
{
return "method";
}
public String getLocalName()
{
return "method";
}
}
1.1 jakarta-avalon-cornerstone/src/java/org/apache/cornerstone/demos/xcommander/saxhandlers/ObjectElementHandler.java
Index: ObjectElementHandler.java
===================================================================
/*
* Copyright (C) The Apache Software Foundation. All rights reserved.
*
* This software is published under the terms of the Apache Software License
* version 1.1, a copy of which has been included with this distribution in
* the LICENSE file.
*/
package org.apache.cornerstone.demos.xcommander.saxhandlers;
import org.apache.cornerstone.demos.xcommander.*;
import org.xml.sax.Attributes;
/**
*
*
* @author <a href="mailto:mail@leosimons.com">Leo Simons</a>
*/
public class ObjectElementHandler
extends AbstractElementHandler
implements TypeElementHandler
{
private ConstructorElementHandler ceh;
private Object obj;
private String type;
public void addChild( final ElementHandler elementHandler )
throws IllegalArgumentException
{
if( elementHandler instanceof ConstructorElementHandler )
{
ceh = (ConstructorElementHandler)elementHandler;
}
else
{
throw new IllegalArgumentException();
}
}
public void start( final Attributes attributes )
{
type = attributes.getValue( "", "class" );
}
public void end( final String contents )
{
try
{
// try to find the specified class
Class handlerClass = Class.forName( type );
if( ceh != null )
{
// create an object from the settings supplied by ceh
obj = handlerClass.getConstructor( ceh.getArgumentTypes() ).
newInstance( ceh.getArguments() );
}
}
catch( final Exception e )
{
}
}
public Class getTypeClass()
{
return obj.getClass();
}
public Object getTypeValue()
{
return obj;
}
public String getNamespaceURI()
{
return "object";
}
public String getLocalName()
{
return "object";
}
}
1.1 jakarta-avalon-cornerstone/src/java/org/apache/cornerstone/demos/xcommander/saxhandlers/ReferenceElementHandler.java
Index: ReferenceElementHandler.java
===================================================================
/*
* Copyright (C) The Apache Software Foundation. All rights reserved.
*
* This software is published under the terms of the Apache Software License
* version 1.1, a copy of which has been included with this distribution in
* the LICENSE file.
*/
package org.apache.cornerstone.demos.xcommander.saxhandlers;
import org.xml.sax.Attributes;
import org.apache.cornerstone.demos.xcommander.*;
/**
*
*
* @author <a href="mailto:mail@leosimons.com">Leo Simons</a>
*/
public class ReferenceElementHandler
extends AbstractElementHandler
implements TypeElementHandler
{
private Object value = null;
public void addChild( ElementHandler eh )
throws IllegalArgumentException
{
throw new IllegalArgumentException();
}
public void end( String contents )
{
ClassLoader cl = getClass().getClassLoader();
try
{
value = cl.loadClass( contents );
}
catch( final ClassNotFoundException cnfe )
{
value = cl.getResource( contents );
}
}
public Class getTypeClass()
{
return value.getClass();
}
public Object getTypeValue()
{
return value;
}
public String getNamespaceURI()
{
return "reference";
}
public String getLocalName()
{
return "reference";
}
}
1.1 jakarta-avalon-cornerstone/src/java/org/apache/cornerstone/demos/xcommander/saxhandlers/StringElementHandler.java
Index: StringElementHandler.java
===================================================================
/*
* Copyright (C) The Apache Software Foundation. All rights reserved.
*
* This software is published under the terms of the Apache Software License
* version 1.1, a copy of which has been included with this distribution in
* the LICENSE file.
*/
package org.apache.cornerstone.demos.xcommander.saxhandlers;
import org.apache.cornerstone.demos.xcommander.*;
import org.xml.sax.Attributes;
/**
*
*
* @author <a href="mailto:mail@leosimons.com">Leo Simons</a>
*/
public class StringElementHandler
extends AbstractElementHandler
implements TypeElementHandler
{
private String value;
public void addChild( final ElementHandler elementHandler )
throws IllegalArgumentException
{
throw new IllegalArgumentException();
}
public void end( final String contents )
{
value = new String( contents );
}
public Class getTypeClass()
{
return value.getClass();
}
public Object getTypeValue()
{
return value;
}
public String getNamespaceURI()
{
return "string";
}
public String getLocalName()
{
return "string";
}
}
1.1 jakarta-avalon-cornerstone/src/java/org/apache/cornerstone/demos/xcommander/saxhandlers/TypeElementHandler.java
Index: TypeElementHandler.java
===================================================================
/*
* Copyright (C) The Apache Software Foundation. All rights reserved.
*
* This software is published under the terms of the Apache Software License
* version 1.1, a copy of which has been included with this distribution in
* the LICENSE file.
*/
package org.apache.cornerstone.demos.xcommander.saxhandlers;
import org.apache.cornerstone.demos.xcommander.ElementHandler;
/**
* Classes that implement this interface can be subelements
* of <constructor>s and <method>s.
*
* @author <a href="mailto:mail@leosimons.com">Leo Simons</a>
*/
public interface TypeElementHandler
extends ElementHandler
{
Class getTypeClass();
Object getTypeValue();
}
1.1 jakarta-avalon-cornerstone/src/java/org/apache/cornerstone/demos/xcommander/saxhandlers/package.html
Index: package.html
===================================================================
<html>
<body>
Contains the ElementHandlers used in parsing XCommands.
</body>
</html>
1.1 jakarta-avalon-cornerstone/src/java/org/apache/cornerstone/demos/xcommander/xcommands/Chat.java
Index: Chat.java
===================================================================
/*
* Copyright (C) The Apache Software Foundation. All rights reserved.
*
* This software is published under the terms of the Apache Software License
* version 1.1, a copy of which has been included with this distribution in
* the LICENSE file.
*/
package org.apache.cornerstone.demos.xcommander.xcommands;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import org.apache.cornerstone.demos.xcommander.GlobalResult;
import org.apache.cornerstone.demos.xcommander.XCommand;
/**
* This contains very basic Chat program functionality. All messages are sent
* to all users.
*
* @author <a href="mailto:mail@leosimons.com">Leo Simons</a>
*/
public class Chat
implements XCommand
{
public GlobalResult addUser( String user )
{
return new AddUser( user );
}
public GlobalResult removeUser( String user )
{
return new RemoveUser( user );
}
public GlobalResult message( String msg )
{
return new Message( msg );
}
private class AddUser
implements GlobalResult
{
String m_name;
AddUser( String name )
{
m_name = name;
}
public String toString()
{
return "<chat action=\"addUser\">"+m_name+"</chat>";
}
}
private class RemoveUser
implements GlobalResult
{
String m_name;
RemoveUser( String name )
{
m_name = name;
}
public String toString()
{
return "<chat action=\"removeUser\">"+m_name+"</chat>";
}
}
private class Message
implements GlobalResult
{
String m_msg;
Message( String msg )
{
m_msg = msg;
}
public String toString()
{
return "<chat action=\"message\">"+m_msg+"</chat>";
}
}
}
1.1 jakarta-avalon-cornerstone/src/java/org/apache/cornerstone/demos/xcommander/xcommands/Echo.java
Index: Echo.java
===================================================================
/*
* Copyright (C) The Apache Software Foundation. All rights reserved.
*
* This software is published under the terms of the Apache Software License
* version 1.1, a copy of which has been included with this distribution in
* the LICENSE file.
*/
package org.apache.cornerstone.demos.xcommander.xcommands;
import org.apache.cornerstone.demos.xcommander.XCommand;
/**
* This simply returns the string passed to it in the constructor.
*
* @author <a href="mailto:mail@leosimons.com">Leo Simons</a>
*/
public class Echo
implements XCommand
{
private String m_string;
public Echo()
{
this( "Echo!" );
}
public Echo( Object o )
{
m_string = (String)o;
}
public Echo( String s )
{
m_string = s;
}
public String toString()
{
return "<string>" + m_string + "</string>";
}
public String toXml()
{
return this.toString();
}
}
1.1 jakarta-avalon-cornerstone/src/java/org/apache/cornerstone/demos/xcommander/xcommands/package.html
Index: package.html
===================================================================
<html>
<body>
Contains the Roles which XCommander can run by default.
<p>This package contains some simple classes that all implement the
Role interface. These are available to XCommander by default.</p>
</body>
</html>
---------------------------------------------------------------------
To unsubscribe, e-mail: avalon-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: avalon-dev-help@jakarta.apache.org