james-server-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dona...@apache.org
Subject cvs commit: jakarta-james/src/org/apache/james/remotemanager RemoteManagerHandler.java RemoteManager.java
Date Tue, 06 Mar 2001 03:16:28 GMT
donaldp     01/03/05 19:16:28

  Modified:    src/org/apache/james/remotemanager RemoteManager.java
  Added:       src/org/apache/james/remotemanager RemoteManagerHandler.java
  Log:
  Updated RemoteManager to not use deprecated code.
  
  Also made it work in a similar way to other server/handlers and separated handler code into
new class.
  
  Revision  Changes    Path
  1.24      +36 -216   jakarta-james/src/org/apache/james/remotemanager/RemoteManager.java
  
  Index: RemoteManager.java
  ===================================================================
  RCS file: /home/cvs/jakarta-james/src/org/apache/james/remotemanager/RemoteManager.java,v
  retrieving revision 1.23
  retrieving revision 1.24
  diff -u -r1.23 -r1.24
  --- RemoteManager.java	2001/03/05 15:18:38	1.23
  +++ RemoteManager.java	2001/03/06 03:16:28	1.24
  @@ -1,27 +1,19 @@
  -/*****************************************************************************
  - * 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.                                                         *
  - *****************************************************************************/
  -
  +/*
  + * 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.james.remotemanager;
   
  -import java.io.*;
  -import java.net.*;
  -import java.util.*;
  -import org.apache.avalon.AbstractLoggable;
  -import org.apache.avalon.Composer;
  -import org.apache.avalon.ComponentManager;
  -import org.apache.avalon.configuration.Configurable;
  +import java.net.InetAddress;
  +import java.net.UnknownHostException;
   import org.apache.avalon.configuration.Configuration;
   import org.apache.avalon.configuration.ConfigurationException;
  -import org.apache.avalon.Component;
  -import org.apache.cornerstone.services.Scheduler;
  -import org.apache.cornerstone.services.SocketServer;
  -import org.apache.james.*;
  -import org.apache.james.services.*;
  +import org.apache.cornerstone.services.connection.AbstractService;
  +import org.apache.cornerstone.services.connection.ConnectionHandlerFactory;
  +import org.apache.cornerstone.services.connection.DefaultHandlerFactory;
   
   /**
    * Provides a really rude network interface to administer James.
  @@ -31,218 +23,46 @@
    *       -much more...
    * @version 1.0.0, 24/04/1999
    * @author  Federico Barbieri <scoobie@pop.systemy.it>
  + * @author <a href="mailto:donaldp@apache.org">Peter Donald</a>
    */
   public class RemoteManager 
  -    extends AbstractLoggable
  -    implements SocketServer.SocketHandler, Component, Scheduler.Target, Composer, Configurable
{
  -
  -    private ComponentManager compMgr;
  -    private Configuration conf;
  -    private UsersRepository users;
  -    private Scheduler scheduler;
  -    private MailServer mailServer;
  -
  -    private BufferedReader in;
  -    private InputStream socketIn;
  -    private PrintWriter out;
  -    private OutputStream r_out;
  -    private HashMap admaccount;
  -    private Socket socket;
  -    private long timeout;
  -
  -    public RemoteManager() {
  -    }
  +    extends AbstractService {
   
  -    public void configure(Configuration conf)  throws ConfigurationException {
  -        this.conf = conf;
  -        timeout = conf.getChild("connectiontimeout").getValueAsLong(120000);
  +    protected ConnectionHandlerFactory createFactory()
  +    {
  +        return new DefaultHandlerFactory( RemoteManagerHandler.class );
       }
   
  -    public void compose(ComponentManager compMgr) {
  -        this.compMgr = compMgr;
  -    }
  +    public void configure( final Configuration configuration )
  +        throws ConfigurationException {
   
  -    public void init() throws Exception {
  +        m_port = configuration.getChild( "port" ).getValueAsInt( 4554 );
   
  -        getLogger().info("RemoteManager init...");
  -        scheduler = (Scheduler) compMgr.lookup("org.apache.cornerstone.services.Scheduler");
  -        mailServer = (MailServer) compMgr.lookup("org.apache.james.services.MailServer");
  -        SocketServer socketServer = (SocketServer) compMgr.lookup("org.apache.cornerstone.services.SocketServer");
  -        int port = conf.getChild("port").getValueAsInt(4554);
  -        InetAddress bind = null;
  -        try {
  -            String bindTo = conf.getChild("bind").getValue();
  -            if (bindTo.length() > 0) {
  -                bind = InetAddress.getByName(bindTo);
  +        try 
  +        { 
  +            final String bindAddress = configuration.getChild( "bind" ).getValue( null
);
  +            if( null != bindAddress )
  +            {
  +                m_bindTo = InetAddress.getByName( bindAddress ); 
               }
  -        } catch (ConfigurationException e) {
           }
  -
  -        String type = SocketServer.DEFAULT;
  -        try {
  -            if (conf.getChild("useTLS").getValue().equals("TRUE")) type = SocketServer.TLS;
  -        } catch (ConfigurationException e) {
  -        }
  -        
  -        getLogger().info("RemoteManager using " + type + " on port " + port);
  -        
  -        Configuration adm = conf.getChild("administrator_accounts");
  -        admaccount = new HashMap();
  -        final Configuration[] accountConfs = adm.getChildren( "account" );
  -        for ( int i = 0; i < accountConfs.length; i++ )
  +        catch( final UnknownHostException unhe ) 
           {
  -            Configuration c = accountConfs[i];
  -            admaccount.put(c.getAttribute("login"), c.getAttribute("password"));
  -        }
  -        if (admaccount.isEmpty()) {
  -            getLogger().warn("No Administrative account defined");
  -            getLogger().warn("RemoteManager failed to init");
  -            return;
  -        } else {
  -            socketServer.openListener("JAMESRemoteControlListener",type, port, bind, this);
  -            users = (UsersRepository) compMgr.lookup("org.apache.james.services.UsersRepository");
  -            getLogger().info("RemoteManager ...init end");
  +            throw new ConfigurationException( "Malformed bind parameter", unhe );
           }
  -    }
  -
   
  -    public void parseRequest(Socket s) {
  +        final String useTLS = configuration.getChild( "useTLS" ).getValue( "" );
  +        if( useTLS.equals( "TRUE" ) ) m_serverSocketType = "ssl";
   
  -        scheduler.setAlarm("RemoteManager", new Scheduler.Alarm(timeout), this);
  -        socket = s;
  -        String remoteHost = s.getInetAddress().getHostName();
  -        String remoteIP = s.getInetAddress().getHostAddress();
  -        try {
  -            socketIn = s.getInputStream();
  -            in = new BufferedReader(new InputStreamReader(socketIn));
  -            r_out = s.getOutputStream();
  -            out = new PrintWriter(r_out, true);
  -            getLogger().info("Access from " + remoteHost + "(" + remoteIP + ")");
  -            out.println("JAMES RemoteAdministration Tool " + Constants.SOFTWARE_VERSION);
  -            out.println("Please enter your login and password");
  -            String login = in.readLine();
  -            String password = in.readLine();
  -            while (!password.equals(admaccount.get(login)) || password.length() == 0) {
  -                out.println("Login failed for " + login);
  -                getLogger().info("Login for " + login + " failed");
  -                login = in.readLine();
  -                password = in.readLine();
  -            }
  -            scheduler.resetAlarm("RemoteManager");
  -            out.println("Welcome " + login + ". HELP for a list of commands");
  -            getLogger().info("Login for " + login + " succesful");
  -            while (parseCommand(in.readLine())) {
  -                scheduler.resetAlarm("RemoteManager");
  -            }
  -            getLogger().info("Logout for " + login + ".");
  -            s.close();
  -        } catch (IOException e) {
  -            out.println("Error. Closing connection");
  -            out.flush();
  -            getLogger().error("Exception during connection from " + remoteHost + " (" +
remoteIP + ")");
  -        }
  -        scheduler.removeAlarm("RemoteManager");
  +        super.configure( configuration );
       }
   
  -    public void wake(String name, Scheduler.Event event) {
  -        getLogger().error("Connection timeout on socket");
  -        try {
  -            out.println("Connection timeout. Closing connection");
  -            socket.close();
  -        } catch (IOException e) {
  -        }
  -    }
  +    public void init() throws Exception {
   
  -    private boolean parseCommand(String command) {
  -        if (command == null) return false;
  -        StringTokenizer commandLine = new StringTokenizer(command.trim(), " ");
  -        int arguments = commandLine.countTokens();
  -        if (arguments == 0) {
  -            return true;
  -        } else if(arguments > 0) {
  -            command = commandLine.nextToken();
  -        }
  -        String argument = (String) null;
  -        if(arguments > 1) {
  -            argument = commandLine.nextToken();
  -        }
  -        String argument1 = (String) null;
  -        if(arguments > 2) {
  -            argument1 = commandLine.nextToken();
  -        }
  -        if (command.equalsIgnoreCase("ADDUSER")) {
  -            String user = argument;
  -            String passwd = argument1;
  -            try {
  -                if (user.equals("") || passwd.equals("")) {
  -                    out.println("usage: adduser [username] [password]");
  -                    return true;
  -                }
  -            } catch (NullPointerException e) {
  -                out.println("usage: adduser [username] [password]");
  -                return true;
  -            }
  -            if (users.contains(user)) {
  -                out.println("user " + user + " already exist");
  -            } else {
  -                if(mailServer.addUser(user, passwd)) {
  -                    out.println("User " + user + " added");
  -                    getLogger().info("User " + user + " added");
  -                } else {
  -                    out.println("Error adding user " + user);
  -                    getLogger().info("Error adding user " + user);
  -                }
  -            }
  -            out.flush();
  -        } else if (command.equalsIgnoreCase("DELUSER")) {
  -            String user = argument;
  -            if (user.equals("")) {
  -                out.println("usage: deluser [username]");
  -                return true;
  -            }
  -            try {
  -                users.removeUser(user);
  -            } catch (Exception e) {
  -                out.println("Error deleting user " + user + " : " + e.getMessage());
  -                return true;
  -            }
  -            out.println("User " + user + " deleted");
  -            getLogger().info("User " + user + " deleted");
  -        } else if (command.equalsIgnoreCase("LISTUSERS")) {
  -            out.println("Existing accounts " + users.countUsers());
  -            for (Iterator it = users.list(); it.hasNext();) {
  -                out.println("user: " + (String) it.next());
  -            }
  -        } else if (command.equalsIgnoreCase("COUNTUSERS")) {
  -            out.println("Existing accounts " + users.countUsers());
  -        } else if (command.equalsIgnoreCase("VERIFY")) {
  -            String user = argument;
  -            if (user.equals("")) {
  -                out.println("usage: verify [username]");
  -                return true;
  -            }
  -            if (users.contains(user)) {
  -                out.println("User " + user + " exist");
  -            } else {
  -                out.println("User " + user + " does not exist");
  -            }
  -        } else if (command.equalsIgnoreCase("HELP")) {
  -            out.println("Currently implemented commans:");
  -            out.println("help                            display this help");
  -            out.println("adduser [username] [password]   add a new user");
  -            out.println("deluser [username]              delete existing user");
  -            out.println("listusers                       display existing accounts");
  -            out.println("countusers                      display the number of existing
accounts");
  -            out.println("verify [username]               verify if specified user exist");
  -            out.println("quit                            close connection");
  -            out.flush();
  -        } else if (command.equalsIgnoreCase("QUIT")) {
  -            out.println("bye");
  -            return false;
  -        } else {
  -            out.println("unknown command " + command);
  -        }
  -        return true;
  +        getLogger().info( "RemoteManager init..." );
  +        getLogger().info( "RemoteManager using " + m_serverSocketType + " on port " + m_port
);
  +        super.init();
  +        getLogger().info("RemoteManager ...init end");
       }
   }
   
  
  
  
  1.1                  jakarta-james/src/org/apache/james/remotemanager/RemoteManagerHandler.java
  
  Index: RemoteManagerHandler.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.james.remotemanager;
  
  import org.apache.james.Constants;
  import java.io.*;
  import java.net.*;
  import java.util.*;
  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.configuration.Configurable;
  import org.apache.avalon.configuration.Configuration;
  import org.apache.avalon.configuration.ConfigurationException;
  import org.apache.cornerstone.services.connection.ConnectionHandler;
  import org.apache.cornerstone.services.scheduler.TimeScheduler;
  import org.apache.cornerstone.services.scheduler.PeriodicTimeTrigger;
  import org.apache.cornerstone.services.scheduler.Target;
  import org.apache.james.services.MailServer;
  import org.apache.james.services.UsersRepository;
  
  /**
   * Provides a really rude network interface to administer James.
   * Allow to add accounts.
   * TODO: -improve protocol
   *       -add remove user
   *       -much more...
   * @version 1.0.0, 24/04/1999
   * @author  Federico Barbieri <scoobie@pop.systemy.it>
   * @author <a href="mailto:donaldp@apache.org">Peter Donald</a>
   */
  public class RemoteManagerHandler 
      extends AbstractLoggable
      implements ConnectionHandler, Composer, Configurable, Target {
  
      private UsersRepository users;
      private TimeScheduler scheduler;
      private MailServer mailServer;
  
      private BufferedReader in;
      private PrintWriter out;
      private HashMap admaccount = new HashMap();
      private Socket socket;
      private int timeout;
  
      public void configure( final Configuration configuration )
          throws ConfigurationException {
  
          timeout = configuration.getChild( "connectiontimeout" ).getValueAsInt( 120000 );
  
          final Configuration admin = configuration.getChild( "administrator_accounts" );
          final Configuration[] accounts = admin.getChildren( "account" );
          for ( int i = 0; i < accounts.length; i++ )
          {
              admaccount.put( accounts[ i ].getAttribute( "login" ), 
                              accounts[ i ].getAttribute( "password" ) );
          }
      }
  
      public void compose( final ComponentManager componentManager )
          throws ComponentManagerException {
  
          scheduler = (TimeScheduler)componentManager.
              lookup( "org.apache.cornerstone.services.scheduler.TimeScheduler" );
          mailServer = (MailServer)componentManager.
              lookup( "org.apache.james.services.MailServer" );
          users = (UsersRepository)componentManager.
              lookup( "org.apache.james.services.UsersRepository" );
      }
  
      /**
       * Handle a connection.
       * This handler is responsible for processing connections as they occur.
       *
       * @param connection the connection
       * @exception IOException if an error reading from socket occurs
       * @exception ProtocolException if an error handling connection occurs
       */
      public void handleConnection( final Socket connection ) 
          throws IOException {
  
          /*
          if( admaccount.isEmpty() ) {
              getLogger().warn("No Administrative account defined");
              getLogger().warn("RemoteManager failed to be handled");
              return;
          } 
          */
  
          final PeriodicTimeTrigger trigger = new PeriodicTimeTrigger( timeout, -1 );
          scheduler.addTrigger( this.toString(), trigger, this );
          socket = connection;
          String remoteHost = socket.getInetAddress().getHostName();
          String remoteIP = socket.getInetAddress().getHostAddress();
  
          try {
              in = new BufferedReader(new InputStreamReader( socket.getInputStream() ));
              out = new PrintWriter( socket.getOutputStream(), true);
              getLogger().info("Access from " + remoteHost + "(" + remoteIP + ")");
              out.println("JAMES RemoteAdministration Tool " + Constants.SOFTWARE_VERSION);
              out.println("Please enter your login and password");
              String login = in.readLine();
              String password = in.readLine();
  
              while (!password.equals(admaccount.get(login)) || password.length() == 0) {
                  scheduler.resetTrigger(this.toString());
                  out.println("Login failed for " + login);
                  getLogger().info("Login for " + login + " failed");
                  login = in.readLine();
                  password = in.readLine();
              }
  
              scheduler.resetTrigger(this.toString());
  
              out.println("Welcome " + login + ". HELP for a list of commands");
              getLogger().info("Login for " + login + " succesful");
  
              while (parseCommand(in.readLine())) {
                  scheduler.resetTrigger(this.toString());
              }
              getLogger().info("Logout for " + login + ".");
              socket.close();
  
          } catch ( final IOException e ) {
              out.println("Error. Closing connection");
              out.flush();
              getLogger().error("Exception during connection from " + remoteHost + " (" +
remoteIP + ")");
          }
  
          scheduler.removeTrigger(this.toString());
      }
  
      public void targetTriggered( final String triggerName ) {
          getLogger().error("Connection timeout on socket");
          try {
              out.println("Connection timeout. Closing connection");
              socket.close();
          } catch ( final IOException ioe ) {
          }
      }
  
      private boolean parseCommand( String command ) {
          if (command == null) return false;
          StringTokenizer commandLine = new StringTokenizer(command.trim(), " ");
          int arguments = commandLine.countTokens();
          if (arguments == 0) {
              return true;
          } else if(arguments > 0) {
              command = commandLine.nextToken();
          }
          String argument = (String) null;
          if(arguments > 1) {
              argument = commandLine.nextToken();
          }
          String argument1 = (String) null;
          if(arguments > 2) {
              argument1 = commandLine.nextToken();
          }
          if (command.equalsIgnoreCase("ADDUSER")) {
              String user = argument;
              String passwd = argument1;
              try {
                  if (user.equals("") || passwd.equals("")) {
                      out.println("usage: adduser [username] [password]");
                      return true;
                  }
              } catch (NullPointerException e) {
                  out.println("usage: adduser [username] [password]");
                  return true;
              }
              if (users.contains(user)) {
                  out.println("user " + user + " already exist");
              } else {
                  if(mailServer.addUser(user, passwd)) {
                      out.println("User " + user + " added");
                      getLogger().info("User " + user + " added");
                  } else {
                      out.println("Error adding user " + user);
                      getLogger().info("Error adding user " + user);
                  }
              }
              out.flush();
          } else if (command.equalsIgnoreCase("DELUSER")) {
              String user = argument;
              if (user.equals("")) {
                  out.println("usage: deluser [username]");
                  return true;
              }
              try {
                  users.removeUser(user);
              } catch (Exception e) {
                  out.println("Error deleting user " + user + " : " + e.getMessage());
                  return true;
              }
              out.println("User " + user + " deleted");
              getLogger().info("User " + user + " deleted");
          } else if (command.equalsIgnoreCase("LISTUSERS")) {
              out.println("Existing accounts " + users.countUsers());
              for (Iterator it = users.list(); it.hasNext();) {
                  out.println("user: " + (String) it.next());
              }
          } else if (command.equalsIgnoreCase("COUNTUSERS")) {
              out.println("Existing accounts " + users.countUsers());
          } else if (command.equalsIgnoreCase("VERIFY")) {
              String user = argument;
              if (user.equals("")) {
                  out.println("usage: verify [username]");
                  return true;
              }
              if (users.contains(user)) {
                  out.println("User " + user + " exist");
              } else {
                  out.println("User " + user + " does not exist");
              }
          } else if (command.equalsIgnoreCase("HELP")) {
              out.println("Currently implemented commans:");
              out.println("help                            display this help");
              out.println("adduser [username] [password]   add a new user");
              out.println("deluser [username]              delete existing user");
              out.println("listusers                       display existing accounts");
              out.println("countusers                      display the number of existing
accounts");
              out.println("verify [username]               verify if specified user exist");
              out.println("quit                            close connection");
              out.flush();
          } else if (command.equalsIgnoreCase("QUIT")) {
              out.println("bye");
              return false;
          } else {
              out.println("unknown command " + command);
          }
          return true;
      }
  }
  
  
  
  

Mime
View raw message