Java common - Library usage notes

Submitted by code_admin on Fri, 07/20/2018 - 12:57

metcarob.com.common.io.db
XML Connection Manager
Encrypt a password

Salt pot

To use the salt pot
1. Create a random saltpot and output it.
2. Copy and paste the byte creation and use it to instalise the salt pot.

For Step 1 use:

  1. saltPot sp = new saltPot(2048);
  2. sp.dumpContents();

For Step 2 use:

  1. saltPot sp = null;
  2. {byte[] salt = {
  3.         (byte)0x41, (byte)0xBB, (byte)0xF7, (byte)0xC8, (byte)0x06, (byte)0xF9, (byte)0x42, (byte)0xD8,
  4.         ...
  5.         (byte)0x21, (byte)0x13, (byte)0x74, (byte)0x34, (byte)0x94, (byte)0x7B, (byte)0x70, (byte)0x99
  6.     };     
  7. sp = new saltPot(salt);};

metcarob.com.common.security.saltPot

Secret Manager

  1. try {
  2.     secretManager sm = new secretManager(password.toCharArray(), mainfile, sp);
  3. } catch (Exception e) {
  4.     System.out.println("Error creating secret manager");
  5.     e.printStackTrace();
  6.     return;
  7. }

metcarob.com.common.security.secret.secretManager

DBFile

  1. public class LocalSettingsFile extends DBFile {
  2.     public LocalSettingsFile(String string, boolean b) throws IOException,
  3.                                                           ClassNotFoundException,
  4.                                                           SQLException {
  5.         super(string, b);
  6.     }
  7.  
  8.     protected void CreateDataStructureDerived() throws SQLException {
  9.         //SETTINGSFIELDLIST
  10.         String query = "create table systemSettings (" +
  11.             "id integer not null PRIMARY KEY," +
  12.             "name TEXT not null unique," +
  13.             "systype integer not null DEFAULT 0," +
  14.             "svnroot TEXT not null," +
  15.             "roaccountid TEXT," +
  16.             "ROJDBCUrl TEXT" +
  17.         ")";
  18.         this.executeUpdate(query);
  19.     }
  20.  
  21.     @Override
  22.     protected void UpdateDataStructureDerived(int p_fromVer,
  23.                                               int p_toVer) throws SQLException {
  24.         if ((p_fromVer==0) && (p_toVer==1)) {
  25.             String query = "ALTER TABLE systemSettings ADD COLUMN systype integer not null DEFAULT 0";
  26.             this.executeUpdate(query);
  27.         }
  28.     }
  29.  
  30.     @Override
  31.     protected int getCodeDBFileVersion() {
  32.         return 1;
  33.     }
  34. }

Usage of XMLUtilities class

Output XML Node:

  1.             DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
  2.             Document doc = dBuilder.parse(settingsFile);
  3.             XPath xPath = XPathFactory.newInstance().newXPath();
  4.             DataFile = xPath.evaluate("/SingleNodeServerSettings/datafile", doc.getDocumentElement());
  5.             String tmp = "";
  6.             tmp = xPath.evaluate("/SingleNodeServerSettings/port", doc.getDocumentElement());
  7.             ListenPort = Integer.parseInt(tmp);
  8.  
  9.             NodeList nodes = (NodeList)xPath.evaluate(
  10.                     "/SingleNodeServerSettings/masteradminuser/publickey",
  11.                     doc.getDocumentElement(), XPathConstants.NODESET
  12.             );
  13.             System.out.println(XMLUtilities.nodeToString(nodes.item(0)));
RJM Article Type
Quick Reference