001/**************************************************************** 002 * Licensed to the Apache Software Foundation (ASF) under one * 003 * or more contributor license agreements. See the NOTICE file * 004 * distributed with this work for additional information * 005 * regarding copyright ownership. The ASF licenses this file * 006 * to you under the Apache License, Version 2.0 (the * 007 * "License"); you may not use this file except in compliance * 008 * with the License. You may obtain a copy of the License at * 009 * * 010 * http://www.apache.org/licenses/LICENSE-2.0 * 011 * * 012 * Unless required by applicable law or agreed to in writing, * 013 * software distributed under the License is distributed on an * 014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * 015 * KIND, either express or implied. See the License for the * 016 * specific language governing permissions and limitations * 017 * under the License. * 018 ****************************************************************/ 019 020package org.apache.james.mpt.user; 021 022import java.io.Reader; 023import java.io.StringReader; 024 025import org.apache.james.mpt.Runner; 026import org.apache.james.mpt.api.Monitor; 027import org.apache.james.mpt.api.UserAdder; 028import org.apache.james.mpt.monitor.NullMonitor; 029import org.apache.james.mpt.protocol.ProtocolSessionBuilder; 030import org.apache.james.mpt.session.ExternalSessionFactory; 031 032/** 033 * Adds a user by executing a script at a port. 034 * The user name and password supplied will be substituted 035 * for the variables <code>${user}</code> and <code>${password}</code>. 036 */ 037public class ScriptedUserAdder implements UserAdder { 038 039 private static final String SCRIPT_NAME = "Add User Script"; 040 private static final String PASSWORD_VARIABLE_NAME = "password"; 041 private static final String USER_VARIABLE_NAME = "user"; 042 043 private final String host; 044 private final int port; 045 private final String script; 046 private final Monitor monitor; 047 048 /** 049 * Constructs an adder without a script. 050 * Note that {@link #addUser(String, String)} will not be available 051 * @param host connect to this host 052 * @param port connect to this port 053 */ 054 public ScriptedUserAdder(String host, int port) 055 { 056 this(host, port, (String) null); 057 } 058 059 public ScriptedUserAdder(String host, int port, String script) { 060 this(host, port, script, new NullMonitor()); 061 } 062 063 /** 064 * Note that {@link #addUser(String, String)} will not be available 065 * @param host connect to this host 066 * @param port connect to this port 067 * @param monitor not null 068 */ 069 public ScriptedUserAdder(String host, int port, Monitor monitor) { 070 this(host, port, null, monitor); 071 } 072 073 public ScriptedUserAdder(String host, int port, String script, Monitor monitor) { 074 this.host = host; 075 this.port = port; 076 this.script = script; 077 this.monitor = monitor; 078 } 079 080 /** 081 * Adds a user using the script read from the given input. 082 * @param user user name, not null 083 * @param password password to set, not null 084 * @throws Exception upon failure 085 * @throws NullPointerException when script has not been set 086 */ 087 public void addUser(String user, String password) throws Exception { 088 final StringReader reader = new StringReader(script); 089 addUser(user, password, reader); 090 } 091 092 /** 093 * Adds a user using the script read from the given input. 094 * @param user user name, not null 095 * @param password password to set, not null 096 * @param reader reader for script, not null 097 * @throws Exception upon failure 098 */ 099 public void addUser(String user, String password, Reader reader) throws Exception { 100 final ProtocolSessionBuilder builder = new ProtocolSessionBuilder(); 101 builder.setVariable(USER_VARIABLE_NAME, user); 102 builder.setVariable(PASSWORD_VARIABLE_NAME, password); 103 104 final Runner runner = new Runner(); 105 builder.addProtocolLines(SCRIPT_NAME, reader, runner.getTestElements()); 106 final ExternalSessionFactory factory = new ExternalSessionFactory(host, port, monitor, null); 107 runner.runSessions(factory); 108 } 109 110 /** 111 * Constructs a <code>String</code> with all attributes 112 * in name = value format. 113 * 114 * @return a <code>String</code> representation 115 * of this object. 116 */ 117 public String toString() 118 { 119 final String TAB = " "; 120 121 return "ScriptedUserAdder ( " 122 + super.toString() + TAB 123 + "host = " + this.host + TAB 124 + "port = " + this.port + TAB 125 + "script = " + this.script + TAB 126 + "monitor = " + this.monitor + TAB 127 + " )"; 128 } 129 130 131}