Coverage Summary for Class: SQLAccessControlSystemInitializer (com.acciente.oacc.sql)

Class Class, % Method, % Line, %
SQLAccessControlSystemInitializer 100% (1/ 1) 42.9% (3/ 7) 27.8% (10/ 36)


1 /* 2  * Copyright 2009-2017, Acciente LLC 3  * 4  * Acciente LLC licenses this file to you under the 5  * Apache License, Version 2.0 (the "License"); you 6  * may not use this file except in compliance with the 7  * License. You may obtain a copy of the License at 8  * 9  * http://www.apache.org/licenses/LICENSE-2.0 10  * 11  * Unless required by applicable law or agreed to in 12  * writing, software distributed under the License is 13  * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES 14  * OR CONDITIONS OF ANY KIND, either express or implied. 15  * See the License for the specific language governing 16  * permissions and limitations under the License. 17  */ 18 package com.acciente.oacc.sql; 19  20 import com.acciente.oacc.encryptor.PasswordEncryptor; 21 import com.acciente.oacc.sql.internal.encryptor.PasswordEncryptors; 22  23 import java.io.IOException; 24 import java.io.StringReader; 25 import java.sql.Connection; 26 import java.sql.DriverManager; 27 import java.sql.SQLException; 28 import java.util.List; 29 import java.util.Properties; 30  31 public class SQLAccessControlSystemInitializer { 32  private static final String PROP_DbUrl = "-dburl"; 33  private static final String PROP_DbUser = "-dbuser"; 34  private static final String PROP_DbPwd = "-dbpwd"; 35  private static final String PROP_DbSchema = "-dbschema"; 36  private static final String PROP_PwdEncryptor = "-pwdencryptor"; 37  private static final String PROP_OACCRootPwd = "-oaccsystempwd"; 38  private static final String OPT_HELP_SHORT = "-h"; 39  private static final String OPT_HELP_LONG = "--help"; 40  private static final String OPT_HELP_QUESTION = "-?"; 41  private static final String USAGE = "Usage:" + 42  "\n java com.acciente.oacc.SQLAccessControlSystemInitializer " + 43  PROP_DbUrl + "=<db-url> " + 44  PROP_PwdEncryptor + "=(" + 45  join(" | ", PasswordEncryptors.getSupportedEncryptorNames()) + ") " + 46  PROP_OACCRootPwd + "=<OACC-system-password> " + 47  "[options]" + 48  "\n\nOptions:" + 49  "\n " + PROP_DbUser + "=<db-user> Database username." + 50  "\n " + PROP_DbPwd + "=<db-password> Database password." + 51  "\n " + PROP_DbSchema + "=<db-schema> Database schema." + 52  "\n\nOther:" + 53  "\n -h, --help, -? Shows usage info."; 54  55  public static void main(String args[]) throws SQLException, IOException { 56  // first read the command line args into a properties object 57  Properties optionArgs = new Properties(); 58  59  for (String arg : args) { 60  optionArgs.load(new StringReader(arg)); 61  } 62  63  // print usage info, if necessary 64  if (args.length == 0 65  || optionArgs.containsKey(OPT_HELP_SHORT) 66  || optionArgs.containsKey(OPT_HELP_LONG) 67  || optionArgs.containsKey(OPT_HELP_QUESTION)) { 68  System.out.println(USAGE); 69  return; 70  } 71  72  verifyOptionArgs(optionArgs); 73  74  initializeOACC(optionArgs.getProperty(PROP_DbUrl), 75  optionArgs.getProperty(PROP_DbUser), 76  optionArgs.getProperty(PROP_DbPwd), 77  optionArgs.getProperty(PROP_DbSchema), 78  optionArgs.getProperty(PROP_OACCRootPwd).toCharArray(), 79  PasswordEncryptors.getPasswordEncryptor(optionArgs.getProperty(PROP_PwdEncryptor))); 80  } 81  82  private static void verifyOptionArgs(Properties optionArgs) { 83  if (optionArgs.getProperty(PROP_DbUrl) == null) { 84  throw new IllegalArgumentException(PROP_DbUrl + " is required!\n" + USAGE); 85  } 86  if (optionArgs.getProperty(PROP_PwdEncryptor) == null) { 87  throw new IllegalArgumentException(PROP_PwdEncryptor + " is required!\n" + USAGE); 88  } 89  if (optionArgs.getProperty(PROP_OACCRootPwd) == null) { 90  throw new IllegalArgumentException(PROP_OACCRootPwd + " is required!\n" + USAGE); 91  } 92  } 93  94  public static void initializeOACC(String dbUrl, 95  String dbUser, 96  String dbPwd, 97  String dbSchema, 98  char[] oaccRootPwd, 99  PasswordEncryptor passwordEncryptor) throws SQLException { 100  System.out.println("Connecting to OACC database @ " + dbUrl); 101  102  try (Connection connection = DriverManager.getConnection(dbUrl, dbUser, dbPwd)) { 103  // delegate to internal handler 104  com.acciente.oacc.sql.internal.SQLAccessControlSystemInitializer.initializeOACC(connection, 105  dbSchema, 106  oaccRootPwd, 107  passwordEncryptor); 108  } 109  finally { 110  System.out.println("Disconnecting from OACC database @ " + dbUrl); 111  } 112  113  System.out.println("Initialize..OK!"); 114  } 115  116  public static void initializeOACC(Connection connection, 117  String dbSchema, 118  char[] oaccRootPwd, 119  PasswordEncryptor passwordEncryptor) throws SQLException { 120  // delegate to internal handler 121  com.acciente.oacc.sql.internal.SQLAccessControlSystemInitializer.initializeOACC(connection, 122  dbSchema, 123  oaccRootPwd, 124  passwordEncryptor); 125  } 126  127  //TODO remove this method and replace its usages with String.join() when OACC updates to Java 8 128  /** 129  * Provides identical functionality of the Strings.join() method in Java 8 130  * 131  * @param delimiter the delimiter to insert between elements 132  * @param elements the elements to concatenate 133  * @return a String of the specified elements joined by the specified delimiter 134  */ 135  private static String join(final String delimiter, final List<String> elements) { 136  if (elements == null || elements.size() == 0) { 137  return ""; 138  } 139  140  final StringBuilder result = new StringBuilder(elements.get(0)); 141  for (String element : elements.subList(1, elements.size())) { 142  result.append(delimiter); 143  result.append(element); 144  } 145  return result.toString(); 146  } 147 }