/layers-store/src/main/java/org/ala/layers/pid/PidGenerator.java
Java | 228 lines | 164 code | 43 blank | 21 comment | 11 complexity | 2b271d13f1c6d9e3409ffeda3576e45b MD5 | raw file
1/************************************************************************** 2 * Copyright (C) 2010 Atlas of Living Australia 3 * All Rights Reserved. 4 * 5 * The contents of this file are subject to the Mozilla Public 6 * License Version 1.1 (the "License"); you may not use this file 7 * except in compliance with the License. You may obtain a copy of 8 * the License at http://www.mozilla.org/MPL/ 9 * 10 * Software distributed under the License is distributed on an "AS 11 * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or 12 * implied. See the License for the specific language governing 13 * rights and limitations under the License. 14 ***************************************************************************/ 15package org.ala.layers.pid; 16 17import au.csiro.pidclient.AndsPidClient; 18import au.csiro.pidclient.AndsPidClient.HandleType; 19import au.csiro.pidclient.AndsPidResponse; 20import au.csiro.pidclient.business.AndsPidIdentity; 21import java.sql.Connection; 22import java.sql.DriverManager; 23import java.sql.ResultSet; 24import java.sql.Statement; 25import java.util.concurrent.LinkedBlockingQueue; 26import java.util.logging.Level; 27import java.util.logging.Logger; 28 29/** 30 * 31 * @author ajay 32 */ 33public class PidGenerator { 34 35 private String DB_DRIVER_DEV = "org.postgresql.Driver"; 36 private String DB_URL_DEV = "jdbc:postgresql://ala-devmaps-db.vm.csiro.au:5432/layersdb"; 37 private String DB_USERNAME_DEV = "postgres"; 38 private String DB_PASSWORD_DEV = "postgres"; 39 40 private String DB_DRIVER_PROD = "org.postgresql.Driver"; 41 private String DB_URL_PROD = "jdbc:postgresql://ala-maps-db.vic.csiro.au:5432/layersdb"; 42 private String DB_USERNAME_PROD = "postgres"; 43 private String DB_PASSWORD_PROD = "postgres"; 44 45 private static String ANDS_APPID_TEST = "2c6ed180e966774eee8409f7152b0cc885d07f71"; 46 private static String ANDS_AUTH_DOMAIN_TEST = "csiro.au"; 47 private static String ANDS_IDENTIFIER_TEST = "ALA"; 48 private static String ANDS_HOST_TEST = "test.ands.org.au"; 49 50 private static String ANDS_APPID_PROD = "2c6ed180e966774eee8409f7152b0cc885d07f71"; 51 private static String ANDS_AUTH_DOMAIN_PROD = "csiro.au"; 52 private static String ANDS_IDENTIFIER_PROD = "ALA"; 53 private static String ANDS_HOST_PROD = "services.ands.org.au"; 54 55 private static boolean isProduction = false; 56 57 private Connection getConnection() { 58 Connection conn = null; 59 String db_driver = DB_DRIVER_DEV; 60 String db_url = DB_URL_DEV; 61 String db_user = DB_USERNAME_DEV; 62 String db_pass = DB_PASSWORD_DEV; 63 if (isProduction) { 64 db_driver = DB_DRIVER_PROD; 65 db_url = DB_URL_PROD; 66 db_user = DB_USERNAME_PROD; 67 db_pass = DB_PASSWORD_PROD; 68 } 69 70 try { 71 Class.forName(db_driver); 72 String url = db_url; 73 //String url = "jdbc:postgresql://localhost:5432/layersdb"; 74 conn = DriverManager.getConnection(url, db_user, db_pass); 75 76 } catch (Exception e) { 77 System.out.println("Unable to create Connection"); 78 e.printStackTrace(System.out); 79 } 80 81 return conn; 82 } 83 84 private void testPidGeneration() { 85 try { 86 AndsPidIdentity andsid = new AndsPidIdentity(); 87 andsid.setAppId(ANDS_APPID_TEST); 88 andsid.setAuthDomain(ANDS_AUTH_DOMAIN_TEST); 89 andsid.setIdentifier(ANDS_IDENTIFIER_TEST); 90 91 AndsPidClient ands = new AndsPidClient(); 92 ands.setPidServiceHost(ANDS_HOST_TEST); 93 ands.setPidServicePath("/pids"); 94 ands.setPidServicePort(8443); 95 ands.setRequestorIdentity(andsid); 96 AndsPidResponse mintHandleFormattedResponse = ands.mintHandleFormattedResponse(AndsPidClient.HandleType.DESC, "test"); 97 98 System.out.println("handle creation status: " + mintHandleFormattedResponse.isSuccess()); 99 System.out.println(mintHandleFormattedResponse.getXmlResponse()); 100 101 } catch (Exception e) { 102 System.out.println("Unable to generate PID"); 103 e.printStackTrace(System.out); 104 } 105 106 } 107 108 public static String mintLayerPid(HandleType handleType, String value) { 109 try { 110 String ands_appid = ANDS_APPID_TEST; 111 String ands_auth = ANDS_AUTH_DOMAIN_TEST; 112 String ands_ident = ANDS_IDENTIFIER_TEST; 113 String ands_host = ANDS_HOST_TEST; 114 if (isProduction) { 115 ands_appid = ANDS_APPID_PROD; 116 ands_auth = ANDS_AUTH_DOMAIN_PROD; 117 ands_ident = ANDS_IDENTIFIER_PROD; 118 ands_host = ANDS_HOST_PROD; 119 } 120 121 AndsPidIdentity andsid = new AndsPidIdentity(); 122 andsid.setAppId(ands_appid); 123 andsid.setAuthDomain(ands_auth); 124 andsid.setIdentifier(ands_ident); 125 126 AndsPidClient ands = new AndsPidClient(); 127 ands.setPidServiceHost(ands_host); 128 ands.setPidServicePath("/pids"); 129 ands.setPidServicePort(8443); 130 ands.setRequestorIdentity(andsid); 131 AndsPidResponse mintHandleFormattedResponse = ands.mintHandleFormattedResponse(handleType, value); 132 133 //System.out.println("handle creation status: " + mintHandleFormattedResponse.isSuccess()); 134 //System.out.println(mintHandleFormattedResponse.getXmlResponse()); 135 136 return mintHandleFormattedResponse.getHandle(); 137 138 } catch (Exception e) { 139 System.out.println("Unable to generate PID"); 140 e.printStackTrace(System.out); 141 } 142 143 return null; 144 145 } 146 147 private void startGeneration() { 148 System.out.println("starting PID generation..."); 149 150 try { 151 Connection conn = getConnection(); 152 String sql = "SELECT id FROM layerpids WHERE pid IS NULL"; 153 Statement s1 = conn.createStatement(); 154 ResultSet rs1 = s1.executeQuery(sql); 155 156 LinkedBlockingQueue<Statement> statements = new LinkedBlockingQueue<Statement>(); 157 int CONCURRENT_THREADS = 50; 158 for (int j = 0; j < CONCURRENT_THREADS; j++) { 159 statements.add(conn.createStatement()); 160 } 161 long start = System.currentTimeMillis(); 162 163 int i = 0; 164 while (rs1.next()) { 165 Statement s2 = statements.take(); 166 167 new PidThread(rs1.getString("id"), s2, statements).start(); 168 169 if (++i == 100) { 170 break; 171 } 172 i++; 173 174 if (i % 100 == 0) { 175 System.out.println("processed: " + i + " at " + (100 / ((System.currentTimeMillis() - start) / 1000.0)) + " records/s"); 176 start = System.currentTimeMillis(); 177 } 178 } 179 180 while (statements.size() > 0) { 181 statements.take().close(); 182 } 183 } catch (Exception ex) { 184 Logger.getLogger(PidGenerator.class.getName()).log(Level.SEVERE, null, ex); 185 } 186 187 188 189 System.out.println("Completed PID threading"); 190 } 191 192 public static void main(String[] args) { 193 194 if (args.length > 0) { 195 if (args[0].trim().toLowerCase().equals("production")) { 196 isProduction = true; 197 } 198 } 199 200 PidGenerator pg = new PidGenerator(); 201 pg.startGeneration(); 202 } 203} 204 205class PidThread extends Thread { 206 207 String id = ""; 208 Statement s; 209 LinkedBlockingQueue<Statement> lbq; 210 211 public PidThread(String id, Statement s, LinkedBlockingQueue<Statement> lbq) { 212 this.id = id; 213 this.s = s; 214 this.lbq = lbq; 215 } 216 217 public void run() { 218 try { 219 String handle = PidGenerator.mintLayerPid(HandleType.DESC, id); 220 String sql = "UPDATE layerpids SET pid = '" + handle + "' WHERE id = '" + id + "'"; 221 int update = s.executeUpdate(sql); 222 223 lbq.put(s); 224 } catch (Exception ex) { 225 Logger.getLogger(PidThread.class.getName()).log(Level.SEVERE, null, ex); 226 } 227 } 228}