Coverage Report - com.jcabi.ssl.maven.plugin.Keystore
 
Classes in this File Line Coverage Branch Coverage Complexity
Keystore
91%
32/35
41%
10/24
2.2
Keystore$AjcClosure1
100%
1/1
N/A
2.2
Keystore$AjcClosure3
100%
1/1
N/A
2.2
Keystore$AjcClosure5
100%
1/1
N/A
2.2
 
 1  4
 /**
 2  
  * Copyright (c) 2012-2017, 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.ssl.maven.plugin;
 31  
 
 32  
 import com.jcabi.aspects.Immutable;
 33  
 import com.jcabi.aspects.Loggable;
 34  
 import com.jcabi.log.Logger;
 35  
 import java.io.File;
 36  
 import java.io.IOException;
 37  
 import java.util.Properties;
 38  
 import javax.validation.constraints.NotNull;
 39  
 import lombok.EqualsAndHashCode;
 40  
 
 41  
 /**
 42  
  * Keystore abstraction.
 43  
  *
 44  
  * @author Yegor Bugayenko (yegor256@gmail.com)
 45  
  * @version $Id: 319ddbf1b62df5f722e06512ed1f7f69e3784b61 $
 46  
  * @since 0.5
 47  
  */
 48  
 @Immutable
 49  0
 @EqualsAndHashCode(of = "password")
 50  
 final class Keystore {
 51  
 
 52  
     /**
 53  
      * Constant {@code javax.net.ssl.keyStore}.
 54  
      */
 55  
     public static final String KEY = "javax.net.ssl.keyStore";
 56  
 
 57  
     /**
 58  
      * Constant {@code javax.net.ssl.keyStorePassword}.
 59  
      */
 60  
     public static final String KEY_PWD = "javax.net.ssl.keyStorePassword";
 61  
 
 62  
     /**
 63  
      * Unique password of it.
 64  
      */
 65  
     private final transient String password;
 66  
 
 67  
     /**
 68  
      * Public ctor.
 69  
      * @param pwd The password
 70  
      */
 71  4
     public Keystore(@NotNull final String pwd) {
 72  4
         this.password = pwd;
 73  4
     }
 74  
 
 75  
     /**
 76  
      * {@inheritDoc}
 77  
      */
 78  
     @Override
 79  
     public String toString() {
 80  1
         final String[] names = new String[] {
 81  
             Keystore.KEY,
 82  
             Keystore.KEY_PWD,
 83  
         };
 84  1
         final StringBuilder text = new StringBuilder();
 85  1
         text.append('[');
 86  3
         for (final String name : names) {
 87  2
             if (text.length() > 1) {
 88  1
                 text.append(", ");
 89  
             }
 90  2
             text.append(name).append('=');
 91  2
             final String value = System.getProperty(name);
 92  2
             if (name == null) {
 93  0
                 text.append("NULL");
 94  
             } else {
 95  2
                 text.append(value);
 96  
             }
 97  
         }
 98  1
         text.append(']');
 99  1
         return text.toString();
 100  
     }
 101  
 
 102  
     /**
 103  
      * Is it active now in the JVM?
 104  
      * @return TRUE if JVM is using our keystore
 105  
      */
 106  
     @Loggable(Loggable.DEBUG)
 107  
     public boolean isActive() {
 108  4
         final String pwd = System.getProperty(Keystore.KEY_PWD);
 109  2
         return pwd != null && pwd.equals(this.password);
 110  
     }
 111  
 
 112  
     /**
 113  
      * Activate it, in the given file.
 114  
      * @param file The file to use
 115  
      * @throws IOException If fails
 116  
      */
 117  
     @Loggable(Loggable.DEBUG)
 118  
     public void activate(final File file) throws IOException {
 119  6
         file.getParentFile().mkdirs();
 120  3
         file.delete();
 121  3
         new Keytool(file, this.password).genkey();
 122  3
         System.setProperty(Keystore.KEY, file.getAbsolutePath());
 123  3
         System.setProperty(Keystore.KEY_PWD, this.password);
 124  3
         new Keytool(file, this.password).list();
 125  3
     }
 126  
 
 127  
     /**
 128  
      * Populate given properties with this keystore's path and password.
 129  
      * @param props The properties
 130  
      */
 131  
     @Loggable(Loggable.DEBUG)
 132  
     public void populate(final Properties props) {
 133  2
         final String[] names = new String[] {
 134  
             Keystore.KEY,
 135  
             Keystore.KEY_PWD,
 136  
         };
 137  3
         for (final String name : names) {
 138  2
             final String value = System.getProperty(name);
 139  2
             if (value == null) {
 140  0
                 continue;
 141  
             }
 142  2
             props.put(name, value);
 143  2
             Logger.info(
 144  
                 this,
 145  
                 "Maven property ${%s} set to '%s'",
 146  
                 name,
 147  
                 System.getProperty(name)
 148  
             );
 149  
         }
 150  1
     }
 151  
 
 152  
 }