Coverage Report - com.jcabi.mysql.maven.plugin.Instances
 
Classes in this File Line Coverage Branch Coverage Complexity
Instances
83%
89/106
60%
34/56
3.417
Instances$1
66%
2/3
N/A
3.417
Instances$2
100%
3/3
N/A
3.417
Instances$AjcClosure1
100%
1/1
N/A
3.417
Instances$AjcClosure3
100%
1/1
N/A
3.417
Instances$AjcClosure5
100%
1/1
N/A
3.417
 
 1  6
 /**
 2  
  * Copyright (c) 2012-2014, jcabi.com
 3  
  * All rights reserved.
 4  
  *
 5  
  * Redistribution and use in source and binary forms, with or without
 6  
  * modification, are permitted provided that the following conditions
 7  
  * are met: 1) Redistributions of source code must retain the above
 8  
  * copyright notice, this list of conditions and the following
 9  
  * disclaimer. 2) Redistributions in binary form must reproduce the above
 10  
  * copyright notice, this list of conditions and the following
 11  
  * disclaimer in the documentation and/or other materials provided
 12  
  * with the distribution. 3) Neither the name of the jcabi.com nor
 13  
  * the names of its contributors may be used to endorse or promote
 14  
  * products derived from this software without specific prior written
 15  
  * permission.
 16  
  *
 17  
  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 18  
  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT
 19  
  * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
 20  
  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
 21  
  * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
 22  
  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 23  
  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 24  
  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 25  
  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 26  
  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 27  
  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 28  
  * OF THE POSSIBILITY OF SUCH DAMAGE.
 29  
  */
 30  
 package com.jcabi.mysql.maven.plugin;
 31  
 
 32  
 import com.jcabi.aspects.Loggable;
 33  
 import com.jcabi.aspects.Tv;
 34  
 import com.jcabi.log.Logger;
 35  
 import com.jcabi.log.VerboseProcess;
 36  
 import com.jcabi.log.VerboseRunnable;
 37  
 import java.io.File;
 38  
 import java.io.IOException;
 39  
 import java.io.OutputStreamWriter;
 40  
 import java.io.PrintWriter;
 41  
 import java.util.Arrays;
 42  
 import java.util.Collection;
 43  
 import java.util.LinkedList;
 44  
 import java.util.concurrent.Callable;
 45  
 import java.util.concurrent.ConcurrentHashMap;
 46  
 import java.util.concurrent.ConcurrentMap;
 47  
 import java.util.concurrent.TimeUnit;
 48  
 import javax.validation.constraints.NotNull;
 49  
 import lombok.EqualsAndHashCode;
 50  
 import lombok.ToString;
 51  
 import org.apache.commons.io.FileUtils;
 52  
 import org.apache.commons.lang3.CharEncoding;
 53  
 import org.apache.commons.lang3.StringUtils;
 54  
 
 55  
 /**
 56  
  * Running instances of MySQL.
 57  
  *
 58  
  * <p>The class is thread-safe.
 59  
  * @author Yegor Bugayenko (yegor@tpc2.com)
 60  
  * @version $Id$
 61  
  * @checkstyle ClassDataAbstractionCoupling (500 lines)
 62  
  * @checkstyle MultipleStringLiterals (500 lines)
 63  
  * @since 0.1
 64  
  */
 65  0
 @ToString
 66  0
 @EqualsAndHashCode(of = "processes")
 67  
 @Loggable(Loggable.INFO)
 68  
 @SuppressWarnings({ "PMD.DoNotUseThreads", "PMD.TooManyMethods" })
 69  8
 public final class Instances {
 70  
 
 71  
     /**
 72  
      * Directory of the actual database relative to the target.
 73  
      */
 74  
     private static final String DATA_SUB_DIR = "data";
 75  
 
 76  
     /**
 77  
      * No defaults.
 78  
      */
 79  
     private static final String NO_DEFAULTS = "--no-defaults";
 80  
 
 81  
     /**
 82  
      * Default user.
 83  
      */
 84  
     private static final String DEFAULT_USER = "root";
 85  
 
 86  
     /**
 87  
      * Default password.
 88  
      */
 89  
     private static final String DEFAULT_PASSWORD = "root";
 90  
 
 91  
     /**
 92  
      * Default host.
 93  
      */
 94  
     @SuppressWarnings("PMD.AvoidUsingHardCodedIP")
 95  
     private static final String DEFAULT_HOST = "127.0.0.1";
 96  
 
 97  
     /**
 98  
      * Running processes.
 99  
      */
 100  8
     private final transient ConcurrentMap<Integer, Process> processes =
 101  
         new ConcurrentHashMap<Integer, Process>(0);
 102  
 
 103  
     /**
 104  
      * If true, always create a new database. If false, check if there is an
 105  
      * existing database at the target location and try to use that if
 106  
      * possible, otherwise create a new one anyway.
 107  
      */
 108  8
     private transient boolean clean = true;
 109  
 
 110  
     /**
 111  
      * Start a new one at this port.
 112  
      * @param config Instance configuration
 113  
      * @param dist Path to MySQL distribution
 114  
      * @param target Where to keep temp data
 115  
      * @param deldir If existing DB should be deleted
 116  
      * @param socket Alternative socket location for mysql (may be null)
 117  
      * @throws IOException If fails to start
 118  
      * @checkstyle ParameterNumberCheck (10 lines)
 119  
      */
 120  
     public void start(@NotNull final Config config, @NotNull final File dist,
 121  
         @NotNull final File target, final boolean deldir, final File socket)
 122  
         throws IOException {
 123  16
         this.setClean(target, deldir);
 124  8
         synchronized (this.processes) {
 125  8
             if (this.processes.containsKey(config.port())) {
 126  0
                 throw new IllegalArgumentException(
 127  
                     String.format("port %d is already busy", config.port())
 128  
                 );
 129  
             }
 130  8
             final Process proc = this.process(config, dist, target, socket);
 131  8
             this.processes.put(config.port(), proc);
 132  8
             Runtime.getRuntime().addShutdownHook(
 133  
                 new Thread(
 134  8
                     new Runnable() {
 135  
                         @Override
 136  
                         public void run() {
 137  4
                             Instances.this.stop(config.port());
 138  0
                         }
 139  
                     }
 140  
                 )
 141  
             );
 142  8
         }
 143  8
     }
 144  
 
 145  
     /**
 146  
      * Stop a running one at this port.
 147  
      * @param port The port to stop at
 148  
      */
 149  
     public void stop(final int port) {
 150  24
         synchronized (this.processes) {
 151  8
             final Process proc = this.processes.remove(port);
 152  8
             if (proc != null) {
 153  8
                 proc.destroy();
 154  
             }
 155  8
         }
 156  8
     }
 157  
 
 158  
     /**
 159  
      * Returns if a clean database had to be created. Note that this must be
 160  
      * called after {@link Instances#start(Config, File, File, boolean)}.
 161  
      * @return If this is a clean database or could have been reused
 162  
      */
 163  
     public boolean reusedExistingDatabase() {
 164  6
         return !this.clean;
 165  
     }
 166  
 
 167  
     /**
 168  
      * Start a new process.
 169  
      * @param config Instance configuration
 170  
      * @param dist Path to MySQL distribution
 171  
      * @param target Where to keep temp data
 172  
      * @param socketfile Alternative socket location for mysql (may be null)
 173  
      * @return Process started
 174  
      * @throws IOException If fails to start
 175  
      * @checkstyle ParameterNumberCheck (10 lines)
 176  
      */
 177  
     private Process process(@NotNull final Config config,
 178  
         final File dist, final File target, final File socketfile)
 179  
         throws IOException {
 180  8
         final File temp = this.prepareFolders(target);
 181  
         final File socket;
 182  8
         if (socketfile == null) {
 183  8
             socket = new File(target, "mysql.sock");
 184  
         } else {
 185  0
             socket = socketfile;
 186  
         }
 187  8
         final ProcessBuilder builder = this.builder(
 188  
             dist,
 189  
             "bin/mysqld",
 190  
             Instances.NO_DEFAULTS,
 191  
             String.format("--user=%s", System.getProperty("user.name")),
 192  
             "--general_log",
 193  
             "--console",
 194  
             "--innodb_buffer_pool_size=64M",
 195  
             "--innodb_log_file_size=64M",
 196  
             "--log_warnings",
 197  
             "--innodb_use_native_aio=0",
 198  
             String.format("--binlog-ignore-db=%s", config.dbname()),
 199  
             String.format("--basedir=%s", dist),
 200  
             String.format("--lc-messages-dir=%s", new File(dist, "share")),
 201  
             String.format("--datadir=%s", this.data(dist, target)),
 202  
             String.format("--tmpdir=%s", temp),
 203  
             String.format("--socket=%s", socket),
 204  
             String.format("--pid-file=%s", new File(target, "mysql.pid")),
 205  
             String.format("--port=%d", config.port())
 206  
         ).redirectErrorStream(true);
 207  8
         builder.environment().put("MYSQL_HOME", dist.getAbsolutePath());
 208  8
         for (final String option : config.options()) {
 209  1
             if (!StringUtils.isBlank(option)) {
 210  1
                 builder.command().add(String.format("--%s", option));
 211  
             }
 212  1
         }
 213  8
         final Process proc = builder.start();
 214  8
         final Thread thread = new Thread(
 215  
             new VerboseRunnable(
 216  16
                 new Callable<Void>() {
 217  
                     @Override
 218  
                     public Void call() throws Exception {
 219  8
                         new VerboseProcess(proc).stdoutQuietly();
 220  7
                         return null;
 221  
                     }
 222  
                 }
 223  
             )
 224  
         );
 225  8
         thread.setDaemon(true);
 226  8
         thread.start();
 227  8
         this.waitFor(socket, config.port());
 228  8
         if (this.clean) {
 229  7
             this.configure(config, dist, socket);
 230  
         }
 231  8
         return proc;
 232  
     }
 233  
 
 234  
     /**
 235  
      * Prepare the folder structure for the database if necessary.
 236  
      * @param target Location of the database
 237  
      * @return The location of the temp directory
 238  
      * @throws IOException If fails to create temp directory
 239  
      */
 240  
     private File prepareFolders(final File target) throws IOException {
 241  8
         if (this.clean && target.exists()) {
 242  7
             FileUtils.deleteDirectory(target);
 243  7
             Logger.info(this, "deleted %s directory", target);
 244  
         }
 245  8
         if (!target.exists() && target.mkdirs()) {
 246  7
             Logger.info(this, "created %s directory", target);
 247  
         }
 248  8
         final File temp = new File(target, "temp");
 249  8
         if (!temp.exists() && !temp.mkdirs()) {
 250  0
             throw new IllegalStateException(
 251  
                 "Error during temporary folder creation"
 252  
             );
 253  
         }
 254  8
         return temp;
 255  
     }
 256  
 
 257  
     /**
 258  
      * Prepare and return data directory.
 259  
      * @param dist Path to MySQL distribution
 260  
      * @param target Where to create it
 261  
      * @return Directory created
 262  
      * @throws IOException If fails
 263  
      */
 264  
     private File data(final File dist, final File target) throws IOException {
 265  8
         final File dir = new File(target, DATA_SUB_DIR);
 266  8
         if (!dir.exists()) {
 267  7
             final File cnf = new File(target, "my-default.cnf");
 268  7
             FileUtils.writeStringToFile(
 269  
                 cnf,
 270  
                 "[mysql]\n# no defaults..."
 271  
             );
 272  7
             new VerboseProcess(
 273  
                 this.builder(
 274  
                     dist,
 275  
                     "scripts/mysql_install_db",
 276  
                     String.format("--defaults-file=%s", cnf),
 277  
                     "--force",
 278  
                     "--innodb_use_native_aio=0",
 279  
                     String.format("--datadir=%s", dir),
 280  
                     String.format("--basedir=%s", dist)
 281  
                 )
 282  
             ).stdoutQuietly();
 283  
         }
 284  8
         return dir;
 285  
     }
 286  
 
 287  
     /**
 288  
      * Wait for this file to become available.
 289  
      * @param socket The file to wait for
 290  
      * @param port Port to wait for
 291  
      * @return The same socket, but ready for usage
 292  
      * @throws IOException If fails
 293  
      */
 294  
     private File waitFor(final File socket, final int port) throws IOException {
 295  8
         final long start = System.currentTimeMillis();
 296  8
         long age = 0L;
 297  
         while (true) {
 298  29
             if (socket.exists()) {
 299  8
                 Logger.info(
 300  
                     this,
 301  
                     "socket %s is available after %[ms]s of waiting",
 302  
                     socket, age
 303  
                 );
 304  8
                 break;
 305  
             }
 306  21
             if (SocketHelper.isOpen(port)) {
 307  0
                 Logger.info(
 308  
                     this,
 309  
                     "port %s is available after %[ms]s of waiting",
 310  
                     port, age
 311  
                 );
 312  0
                 break;
 313  
             }
 314  
             try {
 315  21
                 TimeUnit.SECONDS.sleep(1L);
 316  0
             } catch (final InterruptedException ex) {
 317  0
                 Thread.currentThread().interrupt();
 318  0
                 throw new IllegalStateException(ex);
 319  21
             }
 320  21
             age = System.currentTimeMillis() - start;
 321  21
             if (age > TimeUnit.MINUTES.toMillis((long) Tv.FIVE)) {
 322  0
                 throw new IOException(
 323  
                     Logger.format(
 324  
                         "socket %s is not available after %[ms]s of waiting",
 325  
                         socket, age
 326  
                     )
 327  
                 );
 328  
             }
 329  
         }
 330  8
         return socket;
 331  
     }
 332  
 
 333  
     /**
 334  
      * Configure the running MySQL server.
 335  
      * @param config Instance configuration
 336  
      * @param dist Directory with MySQL distribution
 337  
      * @param socket Socket of it
 338  
      * @throws IOException If fails
 339  
      */
 340  
     private void configure(@NotNull final Config config,
 341  
         final File dist, final File socket)
 342  
         throws IOException {
 343  7
         new VerboseProcess(
 344  
             this.builder(
 345  
                 dist,
 346  
                 "bin/mysqladmin",
 347  
                 Instances.NO_DEFAULTS,
 348  
                 String.format("--port=%d", config.port()),
 349  
                 String.format("--user=%s", Instances.DEFAULT_USER),
 350  
                 String.format("--socket=%s", socket),
 351  
                 String.format("--host=%s", Instances.DEFAULT_HOST),
 352  
                 "password",
 353  
                 Instances.DEFAULT_PASSWORD
 354  
             )
 355  
         ).stdoutQuietly();
 356  7
         final Process process =
 357  
             this.builder(
 358  
                 dist,
 359  
                 "bin/mysql",
 360  
                 String.format("--port=%d", config.port()),
 361  
                 String.format("--user=%s", Instances.DEFAULT_USER),
 362  
                 String.format("--password=%s", Instances.DEFAULT_PASSWORD),
 363  
                 String.format("--socket=%s", socket)
 364  
             ).start();
 365  7
         final PrintWriter writer = new PrintWriter(
 366  
             new OutputStreamWriter(
 367  
                 process.getOutputStream(), CharEncoding.UTF_8
 368  
             )
 369  
         );
 370  7
         writer.print("CREATE DATABASE ");
 371  7
         writer.print(config.dbname());
 372  7
         writer.println(";");
 373  7
         if (!Instances.DEFAULT_USER.equals(config.user())) {
 374  2
             writer.println(
 375  
                 String.format(
 376  
                     "CREATE USER '%s'@'%s' IDENTIFIED BY '%s';",
 377  
                     config.user(),
 378  
                     Instances.DEFAULT_HOST,
 379  
                     config.password()
 380  
                 )
 381  
             );
 382  2
             writer.println(
 383  
                 String.format(
 384  
                     "GRANT ALL ON %s.* TO '%s'@'%s';",
 385  
                     config.dbname(),
 386  
                     config.user(),
 387  
                     Instances.DEFAULT_HOST
 388  
                 )
 389  
             );
 390  
         }
 391  7
         writer.close();
 392  7
         new VerboseProcess(process).stdoutQuietly();
 393  7
     }
 394  
 
 395  
     /**
 396  
      * Make process builder with this commands.
 397  
      * @param dist Distribution directory
 398  
      * @param name Name of the cmd to run
 399  
      * @param cmds Commands
 400  
      * @return Process builder
 401  
      */
 402  
     private ProcessBuilder builder(final File dist, final String name,
 403  
         final String... cmds) {
 404  29
         String label = name;
 405  29
         final Collection<String> commands = new LinkedList<String>();
 406  29
         final File exec = new File(dist, label);
 407  29
         if (exec.exists()) {
 408  
             try {
 409  29
                 exec.setExecutable(true);
 410  0
             } catch (final SecurityException sex) {
 411  0
                 throw new IllegalStateException(sex);
 412  29
             }
 413  
         } else {
 414  0
             label = String.format("%s.exe", name);
 415  0
             if (!new File(dist, label).exists()) {
 416  0
                 label = String.format("%s.pl", name);
 417  0
                 commands.add("perl");
 418  
             }
 419  
         }
 420  29
         commands.add(new File(dist, label).getAbsolutePath());
 421  29
         commands.addAll(Arrays.asList(cmds));
 422  29
         Logger.info(this, "$ %s", StringUtils.join(commands, " "));
 423  29
         return new ProcessBuilder()
 424  
             .command(commands.toArray(new String[commands.size()]))
 425  
             .directory(dist);
 426  
     }
 427  
 
 428  
     /**
 429  
      * Will set the {@link Instances#clean} flag, indicating if the database
 430  
      * can be reused or if it should be deleted and recreated.
 431  
      * @param target Location of database
 432  
      * @param deldir Should database always be cleared
 433  
      */
 434  
     private void setClean(final File target, final boolean deldir) {
 435  8
         if (new File(target, DATA_SUB_DIR).exists() && !deldir) {
 436  1
             Logger.info(this, "reuse existing database %s", target);
 437  1
             this.clean = false;
 438  
         } else {
 439  7
             this.clean = true;
 440  
         }
 441  8
         Logger.info(this, "reuse existing database %s", !this.clean);
 442  8
     }
 443  
 
 444  
 }