Skip to Content.
Sympa Menu

notify-dpml - svn commit: r2818 - in development/planet/components/console: . api/src/main/net/dpml/console impl/src/main/net/dpml/console/impl socket socket/src socket/src/main socket/src/main/net socket/src/main/net/dpml socket/src/main/net/dpml/console socket/src/main/net/dpml/console/socket xmpp xmpp/src xmpp/src/main xmpp/src/main/net xmpp/src/main/net/dpml xmpp/src/main/net/dpml/console xmpp/src/main/net/dpml/console/xmpp

notify-dpml AT lists.ibiblio.org

Subject: DPML Notify

List archive

Chronological Thread  
  • From: niclas AT hedhman.org
  • To: notify-dpml AT lists.ibiblio.org
  • Subject: svn commit: r2818 - in development/planet/components/console: . api/src/main/net/dpml/console impl/src/main/net/dpml/console/impl socket socket/src socket/src/main socket/src/main/net socket/src/main/net/dpml socket/src/main/net/dpml/console socket/src/main/net/dpml/console/socket xmpp xmpp/src xmpp/src/main xmpp/src/main/net xmpp/src/main/net/dpml xmpp/src/main/net/dpml/console xmpp/src/main/net/dpml/console/xmpp
  • Date: Fri, 10 Jun 2005 10:10:40 -0400

Author: niclas AT hedhman.org
Date: Fri Jun 10 10:10:38 2005
New Revision: 2818

Added:

development/planet/components/console/api/src/main/net/dpml/console/CommandConnection.java
(contents, props changed)

development/planet/components/console/api/src/main/net/dpml/console/CommandConnectionListener.java
(contents, props changed)

development/planet/components/console/api/src/main/net/dpml/console/CommandConnectionProvider.java
(contents, props changed)
development/planet/components/console/socket/
development/planet/components/console/socket/build.xml (contents, props
changed)
development/planet/components/console/socket/src/
development/planet/components/console/socket/src/main/
development/planet/components/console/socket/src/main/net/
development/planet/components/console/socket/src/main/net/dpml/
development/planet/components/console/socket/src/main/net/dpml/console/

development/planet/components/console/socket/src/main/net/dpml/console/socket/

development/planet/components/console/socket/src/main/net/dpml/console/socket/DeniedHostException.java
(contents, props changed)

development/planet/components/console/socket/src/main/net/dpml/console/socket/SocketCommandConnection.java
(contents, props changed)

development/planet/components/console/socket/src/main/net/dpml/console/socket/SocketProvider.java
(contents, props changed)
development/planet/components/console/xmpp/
development/planet/components/console/xmpp/build.xml (contents, props
changed)
development/planet/components/console/xmpp/src/
development/planet/components/console/xmpp/src/main/
development/planet/components/console/xmpp/src/main/net/
development/planet/components/console/xmpp/src/main/net/dpml/
development/planet/components/console/xmpp/src/main/net/dpml/console/
development/planet/components/console/xmpp/src/main/net/dpml/console/xmpp/

development/planet/components/console/xmpp/src/main/net/dpml/console/xmpp/XmppCommandConnection.java
(contents, props changed)

development/planet/components/console/xmpp/src/main/net/dpml/console/xmpp/XmppProvider.java
(contents, props changed)

development/planet/components/console/xmpp/src/main/net/dpml/console/xmpp/XmppReader.java
(contents, props changed)

development/planet/components/console/xmpp/src/main/net/dpml/console/xmpp/XmppWriter.java
(contents, props changed)
Removed:

development/planet/components/console/impl/src/main/net/dpml/console/impl/DeniedHostException.java
Modified:

development/planet/components/console/api/src/main/net/dpml/console/CommandInterpreter.java

development/planet/components/console/impl/src/main/net/dpml/console/impl/CommandInterpreterImpl.java

development/planet/components/console/impl/src/main/net/dpml/console/impl/ConsoleImpl.java

development/planet/components/console/impl/src/main/net/dpml/console/impl/LoginCmd.java
development/planet/components/console/index.xml
Log:
Refactored out the dependency on CommandConnectionProvider, so one can plug
in any provider of Reader/Writer pair, and introduced a XMPP provider (i.e.
Jabber) along side the Socket provider. Others to follow.

Added:
development/planet/components/console/api/src/main/net/dpml/console/CommandConnection.java
==============================================================================
--- (empty file)
+++
development/planet/components/console/api/src/main/net/dpml/console/CommandConnection.java
Fri Jun 10 10:10:38 2005
@@ -0,0 +1,37 @@
+/*
+ * Copyright 2005 Niclas Hedman.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package net.dpml.console;
+
+import java.io.BufferedReader;
+import java.io.BufferedWriter;
+import java.io.IOException;
+
+public interface CommandConnection
+{
+ String getUser();
+
+ BufferedReader getInputReader()
+ throws IOException;
+
+ BufferedWriter getOutputWriter()
+ throws IOException;
+
+ void close();
+
+ void busy( String message );
+
+ void available( String message );
+}

Added:
development/planet/components/console/api/src/main/net/dpml/console/CommandConnectionListener.java
==============================================================================
--- (empty file)
+++
development/planet/components/console/api/src/main/net/dpml/console/CommandConnectionListener.java
Fri Jun 10 10:10:38 2005
@@ -0,0 +1,23 @@
+/*
+ * Copyright 2005 Niclas Hedman.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package net.dpml.console;
+
+public interface CommandConnectionListener
+{
+ void connected( CommandConnection connection );
+
+ void connectionLost( CommandConnection connection );
+}
\ No newline at end of file

Added:
development/planet/components/console/api/src/main/net/dpml/console/CommandConnectionProvider.java
==============================================================================
--- (empty file)
+++
development/planet/components/console/api/src/main/net/dpml/console/CommandConnectionProvider.java
Fri Jun 10 10:10:38 2005
@@ -0,0 +1,32 @@
+/*
+ * Copyright 2005 Niclas Hedman.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package net.dpml.console;
+
+/** Interface for the Connection Provider.
+ *
+ * @metro.service name="net.dpml.console.CommandConnectionProvider"
+ */
+public interface CommandConnectionProvider
+{
+ void busy( String message );
+
+ void available( String message );
+
+ void addCommandConnectionListener( CommandConnectionListener listener );
+
+ void removeCommandConnectionListener( CommandConnectionListener listener
);
+}
+

Modified:
development/planet/components/console/api/src/main/net/dpml/console/CommandInterpreter.java
==============================================================================
---
development/planet/components/console/api/src/main/net/dpml/console/CommandInterpreter.java
(original)
+++
development/planet/components/console/api/src/main/net/dpml/console/CommandInterpreter.java
Fri Jun 10 10:10:38 2005
@@ -23,6 +23,8 @@
void removeCommand( ConsoleCommand cmd );
void removeCommand( String commandname );

+ void close();
+
ContainmentModel getCurrentContainer();

void setCurrentContainer( ContainmentModel node );

Modified:
development/planet/components/console/impl/src/main/net/dpml/console/impl/CommandInterpreterImpl.java
==============================================================================
---
development/planet/components/console/impl/src/main/net/dpml/console/impl/CommandInterpreterImpl.java
(original)
+++
development/planet/components/console/impl/src/main/net/dpml/console/impl/CommandInterpreterImpl.java
Fri Jun 10 10:10:38 2005
@@ -18,36 +18,62 @@
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
-import java.io.InputStreamReader;
-import java.io.OutputStreamWriter;
-import java.net.Socket;
import java.util.Map;
import java.util.StringTokenizer;

import net.dpml.composition.model.ContainmentModel;
+
import net.dpml.console.CommandInterpreter;
import net.dpml.console.ConsoleCommand;
import net.dpml.console.LoginException;

-class CommandInterpreterImpl extends Thread
- implements CommandInterpreter
+import net.dpml.logging.Logger;
+
+class CommandInterpreterImpl
+ implements CommandInterpreter, Runnable
{
- private Socket m_Socket;
private boolean m_Login;
private BufferedWriter m_Output;
private BufferedReader m_Input;
private Map m_Commands;
private ContainmentModel m_CurrentNode;
+ private String m_Name;
+ private Thread m_Thread;
+ private Logger m_Logger;

- CommandInterpreterImpl( Socket socket, String welcome, Map commands,
ContainmentModel root )
+ CommandInterpreterImpl( Logger logger, String name, BufferedReader
input, BufferedWriter output, Map commands, ContainmentModel root )
throws IOException
{
- m_Socket = socket;
+ m_Logger = logger.getChildLogger( name );
+ m_Name = name;
+ m_Input = input;
+ m_Output = output;
m_Commands = commands;
- welcomeMessage( welcome );
m_CurrentNode = root;
+ m_Thread = new Thread( this, "Console-" + name );
+ m_Thread.start();
}

+ public void close()
+ {
+ m_Thread.interrupt();
+ try
+ {
+ m_Output.close();
+ } catch( IOException e )
+ {
+ m_Logger.warn( "Can not close the Writer: " + m_Name );
+ }
+ try
+ {
+ m_Input.close();
+ } catch( IOException e )
+ {
+ m_Logger.warn( "Can not close the Reader: " + m_Name );
+ }
+ m_Login = false;
+ }
+
public void addCommand( ConsoleCommand cmd )
{
m_Commands.put( cmd.getName(), cmd );
@@ -99,12 +125,6 @@
e.printStackTrace();
}
}
- try
- {
- m_Socket.close();
- } catch( IOException e )
- {
- }
}

private void execute( String cmdline )
@@ -133,7 +153,7 @@
String cmdline = "";
while( cmdline.length() == 0 )
{
- getOutput().write( "[" + m_Socket.getLocalAddress() + " " +
m_CurrentNode.getQualifiedName() + "]$ " );
+ getOutput().write( "[" + m_Name + " " +
m_CurrentNode.getQualifiedName() + "]$ " );
getOutput().flush();
cmdline = m_Input.readLine().trim();
}
@@ -148,20 +168,6 @@
return args;
}

- private void welcomeMessage( String message )
- {
- try
- {
- getOutput().newLine();
- getOutput().write( message );
- getOutput().newLine();
- getOutput().flush();
- } catch( IOException e )
- {
- e.printStackTrace( System.out );
- }
- }
-
private void errorMessage( String message )
{
try
@@ -178,30 +184,13 @@
private BufferedWriter getOutput()
throws IOException
{
- if( m_Output == null )
- openOut();
return m_Output;
}

private BufferedReader getInput()
throws IOException
{
- if( m_Input == null )
- openIn();
return m_Input;
}
-
- private void openOut()
- throws IOException
- {
- m_Output = new BufferedWriter( new OutputStreamWriter(
m_Socket.getOutputStream() ) );
- }
-
- private void openIn()
- throws IOException
- {
- m_Input = new BufferedReader( new InputStreamReader(
m_Socket.getInputStream() ) );
- }
-
}


Modified:
development/planet/components/console/impl/src/main/net/dpml/console/impl/ConsoleImpl.java
==============================================================================
---
development/planet/components/console/impl/src/main/net/dpml/console/impl/ConsoleImpl.java
(original)
+++
development/planet/components/console/impl/src/main/net/dpml/console/impl/ConsoleImpl.java
Fri Jun 10 10:10:38 2005
@@ -15,255 +15,133 @@
*/
package net.dpml.console.impl;

+import java.io.BufferedReader;
+import java.io.BufferedWriter;
import java.io.IOException;
-import java.net.InetAddress;
-import java.net.ServerSocket;
-import java.net.Socket;
-import java.net.UnknownHostException;
+
import java.util.ArrayList;
import java.util.Collection;
+import java.util.HashMap;
import java.util.HashSet;
import java.util.Hashtable;
import java.util.Iterator;

-import net.dpml.activity.Initializable;
-import net.dpml.activity.Startable;
+import net.dpml.activity.Disposable;
+
import net.dpml.composition.model.ContainmentModel;
+
import net.dpml.configuration.Configuration;
import net.dpml.configuration.ConfigurationException;
+
+import net.dpml.console.CommandConnection;
+import net.dpml.console.CommandConnectionListener;
+import net.dpml.console.CommandConnectionProvider;
import net.dpml.console.CommandInterpreter;
import net.dpml.console.Console;
import net.dpml.console.ConsoleCommand;
+
import net.dpml.context.Context;
import net.dpml.context.ContextException;
-import net.dpml.context.Contextualizable;
-import net.dpml.logging.AbstractLogEnabled;
+
+import net.dpml.logging.Logger;
+
import net.dpml.parameters.ParameterException;
-import net.dpml.parameters.Parameterizable;
import net.dpml.parameters.Parameters;

+import net.dpml.service.ServiceException;
+import net.dpml.service.ServiceManager;

/** This is the main component for the Console facility.
*
* <p>Parameters, default values are shown below.</p>
* <pre>
* &lt;parameters&gt;
- * &lt;parameter name="port" value="3333" /&gt;
- * &lt;parameter name="allow-deny" value="false" /&gt;
- * &lt;parameter name="local-allow" value="true" /&gt;
* &lt;parameter name="welcome-text" value="type 'help' form more info."
/&gt;
* &lt;/parameters&gt;
* </pre>
- * <p> If <i>allow-deny</i> is set to true, then the connection is allowed
- * if the host does not exist in the deny list, otherwise it is not allowed
- * unless the host exist in the allow list.
- * </p>
- * <p>If <i>local-allow</i> is true, the localhost/127.0.0.1 is added to the
- * allow list. This is default, so one can connect without further
parameterization
- * or configuration.</p>
- * <P>Configuration</P>
- * <pre>
- * &lt;configuration&gt;
- * &lt;connections&gt;
- * &lt;allow&gt;127.0.0.1&gt;
- * &lt;allow&gt;10.0.0.23&gt;
- * &lt;allow&gt;10.0.0.24&gt;
- * &lt;/connections&gt;
- * &lt;/configuration&gt;
- * </pre>
- * <p>or in some circumstances, one sets the <i>allow-deny</i> parameter to
- * true, and have a deny list in the configuration.</p>
- * <pre>
- * &lt;configuration&gt;
- * &lt;connections&gt;
- * &lt;deny&gt;211.24.132.24&gt;
- * &lt;deny&gt;211.24.132.29&gt;
- * &lt;/connections&gt;
- * &lt;/configuration&gt;
- * </pre>
- *
* @metro.component name="console-impl" lifestyle="singleton"
* @metro.service type="net.dpml.console.Console"
*/
-public class ConsoleImpl extends AbstractLogEnabled
- implements Console, Runnable, Startable, Parameterizable,
- Initializable, Contextualizable
+public class ConsoleImpl
+ implements Console, CommandConnectionListener, Disposable
{
- private int m_Port;
- private ServerSocket m_ServerSocket;
- private Thread m_Thread;
private Hashtable m_Commands;
private ArrayList m_Interpreters;
private String m_Welcome;
private ContainmentModel m_RootModel;
-
- private boolean m_Allow;
- private HashSet m_Allows;
- private HashSet m_Denies;
-
- public ConsoleImpl()
- {
- m_Allows = new HashSet();
- m_Denies = new HashSet();
- }
-
- /**
- * Contextulaization of the listener by the container during
- * which we are supplied with the root composition model for
- * the application.
- *
- * @param ctx the supplied listener context
- *
- * @exception ContextException if a contextualization error occurs
- *
- * @metro.entry key="urn:composition:containment.model"
- * type="net.dpml.composition.model.ContainmentModel"
- *
- */
- public void contextualize( Context ctx )
- throws ContextException
+ private CommandConnectionProvider m_ConnectionProvider;
+ private HashMap m_Connections;
+ private Logger m_Logger;
+
+ /**
+ * @param ctx the supplied listener context
+ * @metro.entry key="urn:composition:containment.model"
+ * type="net.dpml.composition.model.ContainmentModel"
+ * @metro.dependency type="net.dpml.console.CommandConnectionProvider"
+ * key="connection-provider"
+ */
+ public ConsoleImpl( Logger logger, Context ctx, Parameters params,
ServiceManager man )
+ throws ContextException, ServiceException
{
+ m_Logger = logger;
m_RootModel = (ContainmentModel) ctx.get(
"urn:composition:containment.model" );
- }
-
- /**
- * Parameterization of the component by the container.
- *
- * <p>Parameters, default values are shown below.</p>
- * <pre>
- * &lt;parameters&gt;
- * &lt;parameter name="port" value="3333" /&gt;
- * &lt;parameter name="allow-deny" value="false" /&gt;
- * &lt;parameter name="local-allow" value="true" /&gt;
- * &lt;parameter name="welcome-text" value="type 'help' form more
info." /&gt;
- * &lt;/parameters&gt;
- * </pre>
- */
- public void parameterize( Parameters params )
- throws ParameterException
- {
- m_Port = params.getParameterAsInteger( "port", 3333 );
m_Welcome = params.getParameter( "welcome-text", "type 'help' form
more info." );
- m_Allow = params.getParameterAsBoolean( "deny-allow", false );
-
- boolean localAllow = params.getParameterAsBoolean( "allow-local",
true );
- if( localAllow )
- {
- try
- {
- InetAddress address = InetAddress.getByName( "127.0.0.1" );
- m_Allows.add( address );
- } catch( UnknownHostException e )
- {} // should not be possible, and if so, never mind.
- }
- }
-
- /**
- * Configuration of the component by the container.
- *
- * <p>Configuration:</p>
- * <pre>
- * &lt;configuration&gt;
- * &lt;connections&gt;
- * &lt;allow&gt;127.0.0.1&gt;
- * &lt;allow&gt;10.0.0.23&gt;
- * &lt;allow&gt;10.0.0.24&gt;
- * &lt;/connections&gt;
- * &lt;/configuration&gt;
- * </pre>
- * <p>or in some circumstances, one sets the <i>allow-deny</i> parameter
to
- * true, and have a deny list in the configuration.</p>
- * <pre>
- * &lt;configuration&gt;
- * &lt;connections&gt;
- * &lt;deny&gt;211.24.132.24&gt;
- * &lt;deny&gt;211.24.132.29&gt;
- * &lt;/connections&gt;
- * &lt;/configuration&gt;
- * </pre>
- *
- */
- public void configure( Configuration conf )
- throws ConfigurationException
- {
- Configuration child = conf.getChild( "connections" );
- configureConnections( child );
- }
-
- private void configureConnections( Configuration conf )
- throws ConfigurationException
- {
- Configuration[] allows = conf.getChildren( "allow" );
- for( int i=0 ; i < allows.length ; i++ )
- configureAllow( allows[i] );
-
- Configuration[] denies = conf.getChildren( "deny" );
- for( int i=0 ; i < denies.length ; i++ )
- configureDeny( denies[i] );
+ m_Interpreters = new ArrayList();
+ m_Commands = new Hashtable();
+ m_ConnectionProvider = (CommandConnectionProvider) man.lookup(
"connection-provider" );
+ m_ConnectionProvider.addCommandConnectionListener( this );
}

- private void configureAllow( Configuration allow )
- throws ConfigurationException
+ public void dispose()
{
- String address = allow.getValue();
- try
- {
- InetAddress allowAddr = InetAddress.getByName( address );
- m_Allows.add( allowAddr );
- } catch( UnknownHostException e )
+ m_ConnectionProvider.removeCommandConnectionListener( this );
+ Iterator list = m_Interpreters.iterator();
+ while( list.hasNext() )
{
- throw new ConfigurationException( "Unknown host: " + address,
allow, e );
+ CommandInterpreter intp = (CommandInterpreter) list.next();
+ intp.close();
}
}

- private void configureDeny( Configuration deny )
- throws ConfigurationException
+ public void connected( CommandConnection connection )
{
- String address = deny.getValue();
+ String name = connection.getUser();
try
{
- InetAddress denyAddr = InetAddress.getByName( address );
- m_Denies.add( denyAddr );
- } catch( UnknownHostException e )
+ BufferedReader input = connection.getInputReader();
+ BufferedWriter output = connection.getOutputWriter();
+ welcomeMessage( output, name );
+
+ synchronized( m_Connections )
+ {
+ CommandInterpreter intp = new CommandInterpreterImpl(
+ m_Logger,
+ name,
+ input,
+ output,
+ m_Commands,
+ m_RootModel
+ );
+ m_Connections.put( connection, intp );
+ }
+ } catch( IOException e )
{
- throw new ConfigurationException( "Unknown host: " + address,
deny, e );
+ m_Logger.warn( "Unable to establish connection: " + name +
"\nAborting," , e );
}
}

- public void initialize()
- throws Exception
+ public void connectionLost( CommandConnection connection )
{
- m_Interpreters = new ArrayList();
- m_Commands = new Hashtable();
- m_ServerSocket = new ServerSocket( m_Port );
- }
-
- public void start()
- {
- /* start new thread for monitoring and updating. */
- getLogger().info( "Console started." );
- m_Thread = new Thread( this );
- m_Thread.start();
- }
-
- public void stop()
- {
- try
+ synchronized( m_Connections )
{
- m_Thread.interrupt();
- m_ServerSocket.close();
- } catch( IOException e )
- {}
- }
-
- public void dispose()
- {
- getLogger().info("Console disposed.");
+ CommandInterpreter intp = (CommandInterpreter)
m_Connections.get( connection );
+ intp.close();
+ }
}
-
+
public void addCommand( ConsoleCommand cmd )
{
- getLogger().info( "Added " + cmd.getName() + " command." );
+ m_Logger.info( "Added " + cmd.getName() + " command." );
String name = cmd.getName();
m_Commands.put( name, cmd );
synchronized( m_Interpreters )
@@ -314,66 +192,20 @@
{
return m_Commands.keySet();
}
-
- public void run()
- {
- boolean running = true;
- while( running )
- {
- Socket socket = null;
- try
- {
- socket = m_ServerSocket.accept();
- validateHost( socket.getInetAddress() );
- CommandInterpreterImpl intp = new CommandInterpreterImpl(
socket, m_Welcome, m_Commands, m_RootModel );
- synchronized( m_Interpreters )
- {
- m_Interpreters.add( intp );
- }
- intp.start();
- } catch( IOException e )
- {
- running = false;
- } catch( DeniedHostException e )
- {
- getLogger().warn( e.getMessage() );
- try
- {
- socket.close();
- } catch( IOException ioe )
- {}
- } catch( Exception e )
- {
- getLogger().warn( "", e );
- try
- {
- Thread.sleep( 60000 );
- } catch( InterruptedException f )
- {
- running = false;
- }
- }
- }
- Iterator list = m_Interpreters.iterator();
- while( list.hasNext() )
- {
- CommandInterpreterImpl intp = (CommandInterpreterImpl)
list.next();
- intp.interrupt();
- }
- }
-
- private void validateHost( InetAddress host )
- throws DeniedHostException
+
+ private void welcomeMessage( BufferedWriter output, String name )
+ throws IOException
{
- if( m_Allow )
+ try
{
- if( m_Denies.contains( host ) )
- throw new DeniedHostException( host.toString() );
- }
- else
+ output.newLine();
+ output.write( m_Welcome );
+ output.newLine();
+ output.flush();
+ } catch( IOException e )
{
- if( ! m_Allows.contains( host ) )
- throw new DeniedHostException( host.toString() );
+ m_Logger.warn( "Could not send Welcome message to " + name +
"\nAborting the connection.", e );
+ throw e;
}
- }
+ }
}

Modified:
development/planet/components/console/impl/src/main/net/dpml/console/impl/LoginCmd.java
==============================================================================
---
development/planet/components/console/impl/src/main/net/dpml/console/impl/LoginCmd.java
(original)
+++
development/planet/components/console/impl/src/main/net/dpml/console/impl/LoginCmd.java
Fri Jun 10 10:10:38 2005
@@ -37,11 +37,31 @@
* @metro.service type="net.dpml.console.ConsoleCommand"
*/
public class LoginCmd
- implements ConsoleCommand, Parameterizable, Serviceable, Contextualizable
+ implements ConsoleCommand
{
private String m_Password;
private String m_Name;
-
+
+ /** Constructor for the Login Command.
+ * @param ctx the supplied listener context
+ *
+ * @exception ContextException if a contextualization error occurs
+ *
+ * @metro.dependency type="net.dpml.console.Console"
+ * key="console"
+ *
+ * @metro.entry key="urn:avalon:name"
+ * type="java.lang.String"
+ */
+ public LoginCmd( Context ctx, Parameters params, ServiceManager man )
+ throws ContextException, ParameterException, ServiceException
+ {
+ m_Name = (String) ctx.get( "urn:metro:name" );
+ m_Password = params.getParameter( "root-password" );
+ Console console = (Console) man.lookup( "console" );
+ console.addCommand( this );
+ }
+
public String getName()
{
return m_Name;
@@ -49,45 +69,10 @@

public String getDescription()
{
- String str = "usage: " + m_Name + "\n\nLogins to the system. This
command is executed automatically.";
+ String str = "usage: " + m_Name + "\n\nLogin to the system. This
command is executed automatically.";
return str;
}

- public void parameterize( Parameters params )
- throws ParameterException
- {
- m_Password = params.getParameter( "root-password" );
- }
-
- /**
- * Contextulaization of the listener by the container during
- * which we are supplied with the root composition model for
- * the application.
- *
- * @param ctx the supplied listener context
- *
- * @exception ContextException if a contextualization error occurs
- *
- * @metro.entry key="urn:avalon:name"
- * type="java.lang.String"
- */
- public void contextualize( Context ctx )
- throws ContextException
- {
- m_Name = (String) ctx.get( "urn:avalon:name" );
- }
-
- /**
- * @metro.dependency type="net.dpml.console.Console"
- * key="console"
- */
- public void service( ServiceManager man )
- throws ServiceException
- {
- Console console = (Console) man.lookup( "console" );
- console.addCommand( this );
- }
-
public void execute( CommandInterpreter intp, BufferedReader input,
BufferedWriter output, String[] arguments )
throws Exception
{

Modified: development/planet/components/console/index.xml
==============================================================================
--- development/planet/components/console/index.xml (original)
+++ development/planet/components/console/index.xml Fri Jun 10 10:10:38
2005
@@ -23,8 +23,6 @@
<info>
<group>dpml/planet/console</group>
<name>dpml-console</name>
- <version>1.0.0</version>
- <status>SNAPSHOT</status>
<type>module</type>
</info>
</project>
@@ -32,8 +30,6 @@
<info>
<group>dpml/planet/console/api</group>
<name>dpml-console-api</name>
- <version>1.0.0</version>
- <type>jar</type>
</info>
<dependencies>
<include key="dpml-transit-main"/>
@@ -48,11 +44,34 @@
<info>
<group>dpml/planet/console/impl</group>
<name>dpml-console-impl</name>
- <version>1.0.0</version>
- <type>jar</type>
</info>
<dependencies>
- <include key="dpml-console-api"/>
+ <include key="dpml-console-api"/>
+ </dependencies>
+ <plugins>
+ <include key="dpml-meta-tools"/>
+ </plugins>
+ </project>
+ <project basedir="xmpp">
+ <info>
+ <group>dpml/planet/console/xmpp</group>
+ <name>dpml-console-xmpp</name>
+ </info>
+ <dependencies>
+ <include key="dpml-console-api"/>
+ <include key="smack"/>
+ </dependencies>
+ <plugins>
+ <include key="dpml-meta-tools"/>
+ </plugins>
+ </project>
+ <project basedir="socket">
+ <info>
+ <group>dpml/planet/console/socket</group>
+ <name>dpml-console-socket</name>
+ </info>
+ <dependencies>
+ <include key="dpml-console-api"/>
</dependencies>
<plugins>
<include key="dpml-meta-tools"/>
@@ -62,11 +81,9 @@
<info>
<group>dpml/planet/console/commands</group>
<name>dpml-console-commands</name>
- <version>1.0.0</version>
- <type>jar</type>
</info>
<dependencies>
- <include key="dpml-console-impl"/>
+ <include key="dpml-console-impl"/>
</dependencies>
<plugins>
<include key="dpml-meta-tools"/>
@@ -76,7 +93,6 @@
<info>
<group>dpml/planet/console</group>
<name>dpml-console-default</name>
- <version>1.0.0</version>
<type>block</type>
</info>
<dependencies>
@@ -87,4 +103,21 @@
</plugins>
</project>

+ <resource>
+ <info>
+ <name>smack</name>
+ <group>smack</group>
+ <version>1.5.0</version>
+ </info>
+ <dependencies>
+ <include key="smackx"/>
+ </dependencies>
+ </resource>
+ <resource>
+ <info>
+ <name>smackx</name>
+ <group>smack</group>
+ <version>1.5.0</version>
+ </info>
+ </resource>
</index>

Added: development/planet/components/console/socket/build.xml
==============================================================================
--- (empty file)
+++ development/planet/components/console/socket/build.xml Fri Jun 10
10:10:38 2005
@@ -0,0 +1,24 @@
+<?xml version="1.0" encoding="UTF-8" ?>
+<!--
+ * Copyright 2004 Stephen J. McConnell.
+ * Copyright 2004 Niclas Hedman.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.dpml.net/central/about/legal/
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+-->
+
+<project name="dpml-console-socket" default="install" basedir="."
+ xmlns:transit="antlib:net.dpml.transit" >
+
+ <transit:import uri="artifact:template:dpml/magic/standard"/>
+
+</project>

Added:
development/planet/components/console/socket/src/main/net/dpml/console/socket/DeniedHostException.java
==============================================================================
--- (empty file)
+++
development/planet/components/console/socket/src/main/net/dpml/console/socket/DeniedHostException.java
Fri Jun 10 10:10:38 2005
@@ -0,0 +1,26 @@
+/*
+ * Copyright 2004 Niclas Hedman.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package net.dpml.console.socket;
+
+
+public class DeniedHostException extends Exception
+{
+ public DeniedHostException( String message )
+ {
+ super( message );
+ }
+}
+

Added:
development/planet/components/console/socket/src/main/net/dpml/console/socket/SocketCommandConnection.java
==============================================================================
--- (empty file)
+++
development/planet/components/console/socket/src/main/net/dpml/console/socket/SocketCommandConnection.java
Fri Jun 10 10:10:38 2005
@@ -0,0 +1,126 @@
+/*
+ * Copyright 2005 Niclas Hedman.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package net.dpml.console.socket;
+
+import java.io.BufferedReader;
+import java.io.BufferedWriter;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.OutputStream;
+import java.io.OutputStreamWriter;
+import java.io.IOException;
+
+import java.net.Socket;
+
+import net.dpml.logging.Logger;
+
+import net.dpml.console.CommandConnection;
+
+public class SocketCommandConnection
+ implements CommandConnection
+{
+ private Socket m_Socket;
+ private BufferedReader m_Input;
+ private BufferedWriter m_Output;
+ private Logger m_Logger;
+
+ SocketCommandConnection( Logger logger, Socket socket )
+ throws IOException
+ {
+ m_Socket = socket;
+
+ InputStream in = socket.getInputStream();
+ InputStreamReader isr = new InputStreamReader( in, "UTF-8" );
+ m_Input = new BufferedReader( isr );
+
+ OutputStream out = socket.getOutputStream();
+ OutputStreamWriter osr = new OutputStreamWriter( out );
+ m_Output = new BufferedWriter( osr );
+
+ m_Logger = logger;
+ }
+
+ public String getUser()
+ {
+ // TODO: Better User handling for Sockets.
+ return m_Socket.toString();
+ }
+
+ public BufferedReader getInputReader()
+ throws IOException
+ {
+ return m_Input;
+ }
+
+ public BufferedWriter getOutputWriter()
+ throws IOException
+ {
+ return m_Output;
+ }
+
+ public void close()
+ {
+ try
+ {
+ m_Input.close();
+ } catch( IOException e )
+ {
+ m_Logger.warn( "Unable to close Reader: " + m_Input, e );
+ }
+ try
+ {
+ m_Output.close();
+ } catch( IOException e )
+ {
+ m_Logger.warn( "Unable to close Writer: " + m_Output, e );
+ }
+ try
+ {
+ if( m_Socket.isClosed() == false )
+ {
+ m_Socket.close();
+ }
+ } catch( IOException e )
+ {
+ m_Logger.warn( "Unable to close Socket: " + m_Socket, e );
+ }
+ }
+
+ public void busy( String message )
+ {
+ try
+ {
+ m_Output.write( message );
+ m_Output.newLine();
+ } catch( IOException e )
+ {
+ m_Logger.warn( "[busy]: Can not write to connection: " +
m_Socket, e );
+ }
+ }
+
+ public void available( String message )
+ {
+ try
+ {
+ m_Output.write( message );
+ m_Output.newLine();
+ } catch( IOException e )
+ {
+ m_Logger.warn( "[available]: Can not write to connection: " +
m_Socket, e );
+ }
+ }
+}
+

Added:
development/planet/components/console/socket/src/main/net/dpml/console/socket/SocketProvider.java
==============================================================================
--- (empty file)
+++
development/planet/components/console/socket/src/main/net/dpml/console/socket/SocketProvider.java
Fri Jun 10 10:10:38 2005
@@ -0,0 +1,305 @@
+/*
+ * Copyright 2005 Niclas Hedman.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package net.dpml.console.socket;
+
+import java.io.BufferedReader;
+import java.io.BufferedWriter;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.IOException;
+import java.io.OutputStream;
+import java.io.OutputStreamWriter;
+
+import java.net.InetAddress;
+import java.net.ServerSocket;
+import java.net.Socket;
+import java.net.UnknownHostException;
+
+import java.util.ArrayList;
+import java.util.HashSet;
+import java.util.Iterator;
+
+import net.dpml.activity.Startable;
+
+import net.dpml.configuration.Configuration;
+import net.dpml.configuration.ConfigurationException;
+
+import net.dpml.console.CommandConnection;
+import net.dpml.console.CommandConnectionListener;
+import net.dpml.console.CommandConnectionProvider;
+
+import net.dpml.logging.Logger;
+
+import net.dpml.parameters.ParameterException;
+import net.dpml.parameters.Parameters;
+
+/** Server Socket provider.
+*
+* <p>Parameters, default values are shown below.</p>
+* <pre>
+* &lt;parameters&gt;
+* &lt;parameter name="port" value="3333" /&gt;
+* &lt;parameter name="allow-deny" value="false" /&gt;
+* &lt;parameter name="local-allow" value="true" /&gt;
+* &lt;parameter name="welcome-text" value="type 'help' form more info."
/&gt;
+* &lt;/parameters&gt;
+* </pre>
+* <p> If <i>allow-deny</i> is set to true, then the connection is allowed
+* if the host does not exist in the deny list, otherwise it is not allowed
+* unless the host exist in the allow list.
+* </p>
+* <p>If <i>local-allow</i> is true, the localhost/127.0.0.1 is added to the
+* allow list. This is default, so one can connect without further
parameterization
+* or configuration.</p>
+* <P>Configuration</P>
+* <pre>
+* &lt;configuration&gt;
+* &lt;connections&gt;
+* &lt;allow&gt;127.0.0.1&gt;
+* &lt;allow&gt;10.0.0.23&gt;
+* &lt;allow&gt;10.0.0.24&gt;
+* &lt;/connections&gt;
+* &lt;/configuration&gt;
+* </pre>
+* <p>or in some circumstances, one sets the <i>allow-deny</i> parameter to
+* true, and have a deny list in the configuration.</p>
+* <pre>
+* &lt;configuration&gt;
+* &lt;connections&gt;
+* &lt;deny&gt;211.24.132.24&gt;
+* &lt;deny&gt;211.24.132.29&gt;
+* &lt;/connections&gt;
+* &lt;/configuration&gt;
+* </pre>
+*
+*
+* @metro.component name="console-socket" lifestyle="singleton"
+* @metro.service type="net.dpml.console.CommandConnectionProvider"
+*/
+public class SocketProvider
+ implements CommandConnectionProvider, Startable, Runnable
+{
+ private ServerSocket m_ServerSocket;
+ private Thread m_Thread;
+ private int m_Port;
+
+ private boolean m_Allow;
+ private HashSet m_Allows;
+ private HashSet m_Denies;
+
+ private ArrayList m_Listeners;
+ private Logger m_Logger;
+
+ /** Constructor for Metro.
+ *
+ * Configuration of the component by the container.
+ *
+ * <p>Configuration:</p>
+ * <pre>
+ * &lt;configuration&gt;
+ * &lt;connections&gt;
+ * &lt;allow&gt;127.0.0.1&gt;
+ * &lt;allow&gt;10.0.0.23&gt;
+ * &lt;allow&gt;10.0.0.24&gt;
+ * &lt;/connections&gt;
+ * &lt;/configuration&gt;
+ * </pre>
+ * <p>or in some circumstances, one sets the <i>allow-deny</i> parameter
to
+ * true, and have a deny list in the configuration.</p>
+ * <pre>
+ * &lt;configuration&gt;
+ * &lt;connections&gt;
+ * &lt;deny&gt;211.24.132.24&gt;
+ * &lt;deny&gt;211.24.132.29&gt;
+ * &lt;/connections&gt;
+ * &lt;/configuration&gt;
+ * </pre>
+ *
+ */
+ public SocketProvider( Logger logger, Parameters params, Configuration
conf )
+ throws ParameterException, ConfigurationException, IOException
+ {
+ m_Logger = logger;
+ m_Allow = params.getParameterAsBoolean( "deny-allow", false );
+
+ boolean localAllow = params.getParameterAsBoolean( "allow-local",
true );
+ if( localAllow )
+ {
+ try
+ {
+ InetAddress address = InetAddress.getByName( "127.0.0.1" );
+ m_Allows.add( address );
+ } catch( UnknownHostException e )
+ {} // should not be possible, and if so, never mind.
+ }
+ Configuration child = conf.getChild( "connections" );
+ configureConnections( child );
+ m_ServerSocket = new ServerSocket( m_Port );
+ m_Listeners = new ArrayList();
+ }
+
+ public void start()
+ {
+ /* start new thread for monitoring and updating. */
+ m_Logger.info( "Console started." );
+ m_Thread = new Thread( this );
+ m_Thread.start();
+ }
+
+ public void stop()
+ {
+ try
+ {
+ m_Thread.interrupt();
+ m_ServerSocket.close();
+ } catch( IOException e )
+ {}
+ }
+
+ public void run()
+ {
+ boolean running = true;
+ while( running )
+ {
+ Socket socket = null;
+ try
+ {
+ socket = m_ServerSocket.accept();
+ validateHost( socket.getInetAddress() );
+ fireConnection( socket );
+ } catch( IOException e )
+ {
+ running = false;
+ } catch( DeniedHostException e )
+ {
+ m_Logger.warn( e.getMessage() );
+ try
+ {
+ socket.close();
+ } catch( IOException ioe )
+ {}
+ } catch( Exception e )
+ {
+ m_Logger.warn( "", e );
+ try
+ {
+ Thread.sleep( 60000 );
+ } catch( InterruptedException f )
+ {
+ running = false;
+ }
+ }
+ }
+ }
+
+ public void addCommandConnectionListener( CommandConnectionListener
listener )
+ {
+ synchronized( m_Listeners )
+ {
+ m_Listeners.add( listener );
+ }
+ }
+
+ public void removeCommandConnectionListener( CommandConnectionListener
listener )
+ {
+ synchronized( m_Listeners )
+ {
+ m_Listeners.add( listener );
+ }
+ }
+
+ public void busy( String message )
+ {
+ // TODO;
+ }
+
+ public void available( String message )
+ {
+ // TODO;
+ }
+
+ private void configureConnections( Configuration conf )
+ throws ConfigurationException
+ {
+ Configuration[] allows = conf.getChildren( "allow" );
+ for( int i=0 ; i < allows.length ; i++ )
+ configureAllow( allows[i] );
+
+ Configuration[] denies = conf.getChildren( "deny" );
+ for( int i=0 ; i < denies.length ; i++ )
+ configureDeny( denies[i] );
+ }
+
+ private void configureAllow( Configuration allow )
+ throws ConfigurationException
+ {
+ String address = allow.getValue();
+ try
+ {
+ InetAddress allowAddr = InetAddress.getByName( address );
+ m_Allows.add( allowAddr );
+ } catch( UnknownHostException e )
+ {
+ throw new ConfigurationException( "Unknown host: " + address,
allow, e );
+ }
+ }
+
+ private void configureDeny( Configuration deny )
+ throws ConfigurationException
+ {
+ String address = deny.getValue();
+ try
+ {
+ InetAddress denyAddr = InetAddress.getByName( address );
+ m_Denies.add( denyAddr );
+ } catch( UnknownHostException e )
+ {
+ throw new ConfigurationException( "Unknown host: " + address,
deny, e );
+ }
+ }
+
+ private void validateHost( InetAddress host )
+ throws DeniedHostException
+ {
+ if( m_Allow )
+ {
+ if( m_Denies.contains( host ) )
+ throw new DeniedHostException( host.toString() );
+ }
+ else
+ {
+ if( ! m_Allows.contains( host ) )
+ throw new DeniedHostException( host.toString() );
+ }
+ }
+
+ private void fireConnection( Socket socket )
+ throws IOException
+ {
+ Logger childLogger = m_Logger.getChildLogger( socket.toString() );
+ CommandConnection cc = new SocketCommandConnection( childLogger,
socket );
+ synchronized( m_Listeners )
+ {
+ Iterator list = m_Listeners.iterator();
+ while( list.hasNext() )
+ {
+ CommandConnectionListener listener =
(CommandConnectionListener) list.next();
+ listener.connected( cc );
+ }
+ }
+ }
+
+}

Added: development/planet/components/console/xmpp/build.xml
==============================================================================
--- (empty file)
+++ development/planet/components/console/xmpp/build.xml Fri Jun 10
10:10:38 2005
@@ -0,0 +1,24 @@
+<?xml version="1.0" encoding="UTF-8" ?>
+<!--
+ * Copyright 2004 Stephen J. McConnell.
+ * Copyright 2004 Niclas Hedman.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.dpml.net/central/about/legal/
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+-->
+
+<project name="dpml-console-xmpp" default="install" basedir="."
+ xmlns:transit="antlib:net.dpml.transit" >
+
+ <transit:import uri="artifact:template:dpml/magic/standard"/>
+
+</project>

Added:
development/planet/components/console/xmpp/src/main/net/dpml/console/xmpp/XmppCommandConnection.java
==============================================================================
--- (empty file)
+++
development/planet/components/console/xmpp/src/main/net/dpml/console/xmpp/XmppCommandConnection.java
Fri Jun 10 10:10:38 2005
@@ -0,0 +1,121 @@
+/*
+ * Copyright 2005 Niclas Hedman.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package net.dpml.console.xmpp;
+
+import java.io.BufferedReader;
+import java.io.BufferedWriter;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.OutputStream;
+import java.io.OutputStreamWriter;
+import java.io.IOException;
+
+import java.net.Socket;
+
+import net.dpml.logging.Logger;
+
+import net.dpml.console.CommandConnection;
+
+import org.jivesoftware.smack.Chat;
+import org.jivesoftware.smack.XMPPConnection;
+import org.jivesoftware.smack.packet.Presence;
+
+public class XmppCommandConnection
+ implements CommandConnection
+{
+ private Logger m_Logger;
+ private BufferedReader m_Input;
+ private BufferedWriter m_Output;
+ private XMPPConnection m_Connection;
+ private String m_User;
+
+ XmppCommandConnection( Logger logger, XMPPConnection connection, String
user )
+ throws IOException
+ {
+ m_Connection = connection;
+ m_User = user;
+ Chat tChat = m_Connection.createChat( m_User );
+
+ XmppReader in = new XmppReader( tChat );
+ m_Input = new BufferedReader( in );
+
+ XmppWriter out = new XmppWriter( tChat );
+ m_Output = new BufferedWriter( out );
+
+ m_Logger = logger;
+ }
+
+ public String getUser()
+ {
+ return m_User;
+ }
+
+ public BufferedReader getInputReader()
+ throws IOException
+ {
+ return m_Input;
+ }
+
+ public BufferedWriter getOutputWriter()
+ throws IOException
+ {
+ return m_Output;
+ }
+
+ public void close()
+ {
+ try
+ {
+ m_Input.close();
+ } catch( IOException e )
+ {
+ m_Logger.warn( "Unable to close Reader: " + m_Input, e );
+ }
+ try
+ {
+ m_Output.close();
+ } catch( IOException e )
+ {
+ m_Logger.warn( "Unable to close Writer: " + m_Output, e );
+ }
+ }
+
+ public void busy( String message )
+ {
+ if( m_Logger.isDebugEnabled() )
+ {
+ m_Logger.debug( "busy: " + message );
+ }
+ Presence.Type type = Presence.Type.AVAILABLE;
+ Presence.Mode mode = Presence.Mode.DO_NOT_DISTURB;
+ Presence presence = new Presence( type, message, 0, mode );
+ m_Connection.sendPacket( presence );
+ }
+
+ public void available( String message )
+ {
+ if( m_Logger.isDebugEnabled() )
+ {
+ m_Logger.debug( "available: " + message );
+ }
+ Presence.Type type = Presence.Type.AVAILABLE;
+ Presence.Mode mode = Presence.Mode.AVAILABLE;
+ Presence presence = new Presence( type, message, 0, mode );
+ m_Connection.sendPacket( presence );
+ }
+
+}
+

Added:
development/planet/components/console/xmpp/src/main/net/dpml/console/xmpp/XmppProvider.java
==============================================================================
--- (empty file)
+++
development/planet/components/console/xmpp/src/main/net/dpml/console/xmpp/XmppProvider.java
Fri Jun 10 10:10:38 2005
@@ -0,0 +1,229 @@
+/*
+ * Copyright 2005 Niclas Hedman.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package net.dpml.console.xmpp;
+
+import java.io.BufferedReader;
+import java.io.BufferedWriter;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.IOException;
+import java.io.OutputStream;
+import java.io.OutputStreamWriter;
+
+import java.net.InetAddress;
+import java.net.ServerSocket;
+import java.net.Socket;
+import java.net.UnknownHostException;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Iterator;
+
+import net.dpml.activity.Startable;
+
+import net.dpml.configuration.Configuration;
+import net.dpml.configuration.ConfigurationException;
+
+import net.dpml.console.CommandConnection;
+import net.dpml.console.CommandConnectionListener;
+import net.dpml.console.CommandConnectionProvider;
+
+import net.dpml.logging.Logger;
+
+import net.dpml.parameters.ParameterException;
+import net.dpml.parameters.Parameters;
+
+import org.jivesoftware.smack.PacketListener;
+import org.jivesoftware.smack.XMPPConnection;
+import org.jivesoftware.smack.XMPPException;
+
+import org.jivesoftware.smack.filter.PacketFilter;
+
+import org.jivesoftware.smack.packet.Message;
+import org.jivesoftware.smack.packet.Packet;
+import org.jivesoftware.smack.packet.Presence;
+
+/** Server Socket provider.
+*
+* <p>Parameters, with example values are shown below.</p>
+* <pre>
+* &lt;parameters&gt;
+* &lt;parameter name="jabber-server" value="jabber.dpml.net" /&gt;
+* &lt;parameter name="jabber-port" value="5222" /&gt;
+* &lt;parameter name="user" value="niclas" /&gt;
+* &lt;parameter name="password" value="NotThisOne" /&gt;
+* &lt;/parameters&gt;
+* </pre>
+*
+* @metro.component name="console-socket" lifestyle="singleton"
+* @metro.service type="net.dpml.console.CommandConnectionProvider"
+*/
+public class XmppProvider
+ implements CommandConnectionProvider, PacketListener
+{
+ private ArrayList m_Listeners;
+ private Logger m_Logger;
+
+ private String m_Server;
+ private int m_Port;
+ private String m_User;
+ private String m_Password;
+ private XMPPConnection m_Connection;
+
+ private HashMap m_ConnectedUsers;
+
+ /** Constructor for Metro.
+ *
+ */
+ public XmppProvider( Logger logger, Parameters params )
+ throws ParameterException, IOException, XMPPException
+ {
+ m_Logger = logger;
+ m_Listeners = new ArrayList();
+ m_Server = params.getParameter( "jabber-server" );
+ m_Port = params.getParameterAsInteger( "jabber-port" );
+ m_User = params.getParameter( "user" );
+ m_Password = params.getParameter( "password" );
+ connectToJabberServer();
+ }
+
+ public void addCommandConnectionListener( CommandConnectionListener
listener )
+ {
+ synchronized( m_Listeners )
+ {
+ m_Listeners.add( listener );
+ }
+ }
+
+ public void removeCommandConnectionListener( CommandConnectionListener
listener )
+ {
+ synchronized( m_Listeners )
+ {
+ m_Listeners.add( listener );
+ }
+ }
+
+ public void busy( String message )
+ {
+ synchronized( m_ConnectedUsers )
+ {
+ Iterator list = m_ConnectedUsers.values().iterator();
+ while( list.hasNext() )
+ {
+ CommandConnection cc = (CommandConnection) list.next();
+ cc.busy( message );
+ }
+ }
+ }
+
+ public void available( String message )
+ {
+ synchronized( m_ConnectedUsers )
+ {
+ Iterator list = m_ConnectedUsers.values().iterator();
+ while( list.hasNext() )
+ {
+ CommandConnection cc = (CommandConnection) list.next();
+ cc.available( message );
+ }
+ }
+ }
+
+ public void processPacket( Packet packet )
+ {
+ if( packet instanceof Message )
+ {
+ Message message = (Message) packet;
+ processPacket( message );
+ return;
+ }
+ if( packet instanceof Presence )
+ {
+ Presence presence = (Presence) packet;
+ processPacket( presence );
+ return;
+ }
+ }
+
+ private void processPacket( Message message )
+ {
+ String tCommand = message.getBody();
+ String user = message.getFrom();
+ try
+ {
+ synchronized( m_ConnectedUsers )
+ {
+ XmppCommandConnection cc = (XmppCommandConnection)
m_ConnectedUsers.get( user );
+ if( cc == null )
+ {
+ cc = fireConnection( user );
+ m_ConnectedUsers.put( user, cc );
+ } // else the connection has already been established, and
the
+ // XmppReader is listening in on the Chat instead.
+ }
+ } catch( IOException e )
+ {
+ m_Logger.warn( "Unable to communicate with user: " + user, e );
+ }
+ }
+
+ private void processPacket( Presence presence )
+ {
+ String user = presence.getFrom();
+ Presence.Type type = presence.getType();
+ if( type.equals( Presence.Type.UNAVAILABLE ) )
+ {
+ XmppCommandConnection cc = (XmppCommandConnection)
m_ConnectedUsers.remove( user );
+ cc.close();
+ }
+ }
+
+ private XmppCommandConnection fireConnection( String user )
+ throws IOException
+ {
+ Logger childLogger = m_Logger.getChildLogger( user );
+ XmppCommandConnection cc = new XmppCommandConnection( childLogger,
m_Connection, user );
+
+ synchronized( m_Listeners )
+ {
+ Iterator list = m_Listeners.iterator();
+ while( list.hasNext() )
+ {
+ CommandConnectionListener listener =
(CommandConnectionListener) list.next();
+ listener.connected( cc );
+ }
+ }
+ return cc;
+ }
+
+ private void connectToJabberServer()
+ throws XMPPException
+ {
+ m_Connection = new XMPPConnection( m_Server, m_Port );
+ m_Connection.login( m_User, m_Password );
+
+ PacketFilter filter = new PacketFilter()
+ {
+ public boolean accept( Packet packet )
+ {
+ return true;
+ }
+ };
+ m_Connection.addPacketListener( this, filter );
+ }
+
+}

Added:
development/planet/components/console/xmpp/src/main/net/dpml/console/xmpp/XmppReader.java
==============================================================================
--- (empty file)
+++
development/planet/components/console/xmpp/src/main/net/dpml/console/xmpp/XmppReader.java
Fri Jun 10 10:10:38 2005
@@ -0,0 +1,98 @@
+/*
+ * Copyright 2005 Niclas Hedman.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package net.dpml.console.xmpp;
+
+import java.io.Reader;
+
+import org.jivesoftware.smack.Chat;
+import org.jivesoftware.smack.PacketListener;
+
+import org.jivesoftware.smack.packet.Message;
+import org.jivesoftware.smack.packet.Packet;
+
+public class XmppReader extends Reader
+ implements PacketListener
+{
+ private Chat m_Chat;
+ private StringBuffer m_Buffer;
+
+ XmppReader( Chat chat )
+ {
+ m_Buffer = new StringBuffer();
+ m_Chat = chat;
+ String from = chat.getParticipant();
+ m_Chat.addMessageListener( this );
+ }
+
+ public void close()
+ {
+ m_Buffer = null;
+ }
+
+ public int read( char[] cbuf, int off, int len )
+ {
+ synchronized( m_Buffer )
+ {
+ if( m_Buffer == null )
+ {
+ return -1;
+ }
+
+ try
+ {
+ while( m_Buffer.length() == 0 )
+ {
+ m_Buffer.wait();
+ }
+ } catch( InterruptedException e )
+ {
+ return -1;
+ }
+
+ if( len > m_Buffer.length() )
+ {
+ len = m_Buffer.length();
+ }
+ if( off + len > cbuf.length )
+ {
+ len = cbuf.length - off;
+ }
+
+ m_Buffer.getChars( 0, len, cbuf, off );
+ m_Buffer.delete( 0, len );
+ return len;
+ }
+ }
+
+ public boolean ready()
+ {
+ synchronized( m_Buffer )
+ {
+ return m_Buffer.length() > 0;
+ }
+ }
+
+ public void processPacket( Packet packet )
+ {
+ Message message = (Message) packet;
+ String text = message.getBody();
+ synchronized( m_Buffer )
+ {
+ m_Buffer.append( text );
+ m_Buffer.notifyAll();
+ }
+ }
+}

Added:
development/planet/components/console/xmpp/src/main/net/dpml/console/xmpp/XmppWriter.java
==============================================================================
--- (empty file)
+++
development/planet/components/console/xmpp/src/main/net/dpml/console/xmpp/XmppWriter.java
Fri Jun 10 10:10:38 2005
@@ -0,0 +1,69 @@
+/*
+ * Copyright 2005 Niclas Hedman.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package net.dpml.console.xmpp;
+
+import java.io.IOException;
+import java.io.Writer;
+
+import org.jivesoftware.smack.Chat;
+import org.jivesoftware.smack.XMPPException;
+
+public class XmppWriter extends Writer
+{
+ private Chat m_Chat;
+ private StringBuffer m_Buffer;
+
+ XmppWriter( Chat chat )
+ {
+ m_Chat = chat;
+ }
+
+ public void newLine()
+ throws IOException
+ {
+ flush();
+ }
+
+ public void write( char[] ch, int offset, int length )
+ {
+ m_Buffer.append( ch, offset, length );
+ }
+
+ public void flush()
+ throws IOException
+ {
+ if( m_Buffer.length() == 0 )
+ {
+ return;
+ }
+ try
+ {
+ m_Chat.sendMessage( m_Buffer.toString() );
+ m_Buffer = new StringBuffer();
+ } catch( XMPPException e )
+ {
+ IOException problem = new IOException( "Can not send XMPP
message." );
+ problem.initCause( e );
+ throw problem;
+ }
+ }
+
+ public void close()
+ {
+ m_Chat = null;
+ System.gc();
+ }
+}



  • svn commit: r2818 - in development/planet/components/console: . api/src/main/net/dpml/console impl/src/main/net/dpml/console/impl socket socket/src socket/src/main socket/src/main/net socket/src/main/net/dpml socket/src/main/net/dpml/console socket/src/main/net/dpml/console/socket xmpp xmpp/src xmpp/src/main xmpp/src/main/net xmpp/src/main/net/dpml xmpp/src/main/net/dpml/console xmpp/src/main/net/dpml/console/xmpp, niclas, 06/10/2005

Archive powered by MHonArc 2.6.24.

Top of Page