/layers-store/src/main/java/org/ala/layers/pid/PidGenerator.java

http://alageospatialportal.googlecode.com/ · 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. ***************************************************************************/
  15. package org.ala.layers.pid;
  16. import au.csiro.pidclient.AndsPidClient;
  17. import au.csiro.pidclient.AndsPidClient.HandleType;
  18. import au.csiro.pidclient.AndsPidResponse;
  19. import au.csiro.pidclient.business.AndsPidIdentity;
  20. import java.sql.Connection;
  21. import java.sql.DriverManager;
  22. import java.sql.ResultSet;
  23. import java.sql.Statement;
  24. import java.util.concurrent.LinkedBlockingQueue;
  25. import java.util.logging.Level;
  26. import java.util.logging.Logger;
  27. /**
  28. *
  29. * @author ajay
  30. */
  31. public class PidGenerator {
  32. private String DB_DRIVER_DEV = "org.postgresql.Driver";
  33. private String DB_URL_DEV = "jdbc:postgresql://ala-devmaps-db.vm.csiro.au:5432/layersdb";
  34. private String DB_USERNAME_DEV = "postgres";
  35. private String DB_PASSWORD_DEV = "postgres";
  36. private String DB_DRIVER_PROD = "org.postgresql.Driver";
  37. private String DB_URL_PROD = "jdbc:postgresql://ala-maps-db.vic.csiro.au:5432/layersdb";
  38. private String DB_USERNAME_PROD = "postgres";
  39. private String DB_PASSWORD_PROD = "postgres";
  40. private static String ANDS_APPID_TEST = "2c6ed180e966774eee8409f7152b0cc885d07f71";
  41. private static String ANDS_AUTH_DOMAIN_TEST = "csiro.au";
  42. private static String ANDS_IDENTIFIER_TEST = "ALA";
  43. private static String ANDS_HOST_TEST = "test.ands.org.au";
  44. private static String ANDS_APPID_PROD = "2c6ed180e966774eee8409f7152b0cc885d07f71";
  45. private static String ANDS_AUTH_DOMAIN_PROD = "csiro.au";
  46. private static String ANDS_IDENTIFIER_PROD = "ALA";
  47. private static String ANDS_HOST_PROD = "services.ands.org.au";
  48. private static boolean isProduction = false;
  49. private Connection getConnection() {
  50. Connection conn = null;
  51. String db_driver = DB_DRIVER_DEV;
  52. String db_url = DB_URL_DEV;
  53. String db_user = DB_USERNAME_DEV;
  54. String db_pass = DB_PASSWORD_DEV;
  55. if (isProduction) {
  56. db_driver = DB_DRIVER_PROD;
  57. db_url = DB_URL_PROD;
  58. db_user = DB_USERNAME_PROD;
  59. db_pass = DB_PASSWORD_PROD;
  60. }
  61. try {
  62. Class.forName(db_driver);
  63. String url = db_url;
  64. //String url = "jdbc:postgresql://localhost:5432/layersdb";
  65. conn = DriverManager.getConnection(url, db_user, db_pass);
  66. } catch (Exception e) {
  67. System.out.println("Unable to create Connection");
  68. e.printStackTrace(System.out);
  69. }
  70. return conn;
  71. }
  72. private void testPidGeneration() {
  73. try {
  74. AndsPidIdentity andsid = new AndsPidIdentity();
  75. andsid.setAppId(ANDS_APPID_TEST);
  76. andsid.setAuthDomain(ANDS_AUTH_DOMAIN_TEST);
  77. andsid.setIdentifier(ANDS_IDENTIFIER_TEST);
  78. AndsPidClient ands = new AndsPidClient();
  79. ands.setPidServiceHost(ANDS_HOST_TEST);
  80. ands.setPidServicePath("/pids");
  81. ands.setPidServicePort(8443);
  82. ands.setRequestorIdentity(andsid);
  83. AndsPidResponse mintHandleFormattedResponse = ands.mintHandleFormattedResponse(AndsPidClient.HandleType.DESC, "test");
  84. System.out.println("handle creation status: " + mintHandleFormattedResponse.isSuccess());
  85. System.out.println(mintHandleFormattedResponse.getXmlResponse());
  86. } catch (Exception e) {
  87. System.out.println("Unable to generate PID");
  88. e.printStackTrace(System.out);
  89. }
  90. }
  91. public static String mintLayerPid(HandleType handleType, String value) {
  92. try {
  93. String ands_appid = ANDS_APPID_TEST;
  94. String ands_auth = ANDS_AUTH_DOMAIN_TEST;
  95. String ands_ident = ANDS_IDENTIFIER_TEST;
  96. String ands_host = ANDS_HOST_TEST;
  97. if (isProduction) {
  98. ands_appid = ANDS_APPID_PROD;
  99. ands_auth = ANDS_AUTH_DOMAIN_PROD;
  100. ands_ident = ANDS_IDENTIFIER_PROD;
  101. ands_host = ANDS_HOST_PROD;
  102. }
  103. AndsPidIdentity andsid = new AndsPidIdentity();
  104. andsid.setAppId(ands_appid);
  105. andsid.setAuthDomain(ands_auth);
  106. andsid.setIdentifier(ands_ident);
  107. AndsPidClient ands = new AndsPidClient();
  108. ands.setPidServiceHost(ands_host);
  109. ands.setPidServicePath("/pids");
  110. ands.setPidServicePort(8443);
  111. ands.setRequestorIdentity(andsid);
  112. AndsPidResponse mintHandleFormattedResponse = ands.mintHandleFormattedResponse(handleType, value);
  113. //System.out.println("handle creation status: " + mintHandleFormattedResponse.isSuccess());
  114. //System.out.println(mintHandleFormattedResponse.getXmlResponse());
  115. return mintHandleFormattedResponse.getHandle();
  116. } catch (Exception e) {
  117. System.out.println("Unable to generate PID");
  118. e.printStackTrace(System.out);
  119. }
  120. return null;
  121. }
  122. private void startGeneration() {
  123. System.out.println("starting PID generation...");
  124. try {
  125. Connection conn = getConnection();
  126. String sql = "SELECT id FROM layerpids WHERE pid IS NULL";
  127. Statement s1 = conn.createStatement();
  128. ResultSet rs1 = s1.executeQuery(sql);
  129. LinkedBlockingQueue<Statement> statements = new LinkedBlockingQueue<Statement>();
  130. int CONCURRENT_THREADS = 50;
  131. for (int j = 0; j < CONCURRENT_THREADS; j++) {
  132. statements.add(conn.createStatement());
  133. }
  134. long start = System.currentTimeMillis();
  135. int i = 0;
  136. while (rs1.next()) {
  137. Statement s2 = statements.take();
  138. new PidThread(rs1.getString("id"), s2, statements).start();
  139. if (++i == 100) {
  140. break;
  141. }
  142. i++;
  143. if (i % 100 == 0) {
  144. System.out.println("processed: " + i + " at " + (100 / ((System.currentTimeMillis() - start) / 1000.0)) + " records/s");
  145. start = System.currentTimeMillis();
  146. }
  147. }
  148. while (statements.size() > 0) {
  149. statements.take().close();
  150. }
  151. } catch (Exception ex) {
  152. Logger.getLogger(PidGenerator.class.getName()).log(Level.SEVERE, null, ex);
  153. }
  154. System.out.println("Completed PID threading");
  155. }
  156. public static void main(String[] args) {
  157. if (args.length > 0) {
  158. if (args[0].trim().toLowerCase().equals("production")) {
  159. isProduction = true;
  160. }
  161. }
  162. PidGenerator pg = new PidGenerator();
  163. pg.startGeneration();
  164. }
  165. }
  166. class PidThread extends Thread {
  167. String id = "";
  168. Statement s;
  169. LinkedBlockingQueue<Statement> lbq;
  170. public PidThread(String id, Statement s, LinkedBlockingQueue<Statement> lbq) {
  171. this.id = id;
  172. this.s = s;
  173. this.lbq = lbq;
  174. }
  175. public void run() {
  176. try {
  177. String handle = PidGenerator.mintLayerPid(HandleType.DESC, id);
  178. String sql = "UPDATE layerpids SET pid = '" + handle + "' WHERE id = '" + id + "'";
  179. int update = s.executeUpdate(sql);
  180. lbq.put(s);
  181. } catch (Exception ex) {
  182. Logger.getLogger(PidThread.class.getName()).log(Level.SEVERE, null, ex);
  183. }
  184. }
  185. }