Advertisement
Guest User

Example Code2

a guest
Aug 30th, 2018
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.85 KB | None | 0 0
  1. import oracle.search.admin.api.ws.client.AdminAPIRuntimeFault;
  2. import oracle.search.admin.api.ws.client.AdminAPIRuntimeFault_Exception;
  3. import oracle.search.admin.api.ws.client.AdminKeyPair;
  4. import oracle.search.admin.api.ws.client.AdminPortType;
  5. import oracle.search.admin.api.ws.client.AdminService;
  6. import oracle.search.admin.api.ws.client.Credentials;
  7. import oracle.search.admin.api.ws.client.ObjectKey;
  8. import oracle.search.admin.api.ws.client.ObjectOutput;
  9.  
  10. import java.util.List;
  11. import java.net.URL;
  12.  
  13. import javax.xml.ws.BindingProvider;
  14. import javax.xml.namespace.QName;
  15.  
  16. public class CreateWebSource
  17. {
  18. public static void main(String[] args) throws Exception
  19. {
  20. System.out.println( "" );
  21.  
  22. try
  23. {
  24. if ( args == null || args.length != 4 )
  25. {
  26. System.out.println(
  27. "Usage:\n CreateWebSource <webServiceURL> <userName> <password> <webSourceURL>"
  28. );
  29. }
  30. else
  31. {
  32. // Get web service URL from command-line arguments
  33. String webServiceURL = args[0];
  34. System.out.println( "Using web service URL \"" + webServiceURL + "\"\n" );
  35.  
  36. // Get username and password
  37. String userName = args[1];
  38. String password = args[2];
  39.  
  40. // Get stateless web service client
  41. AdminPortType adminPort =
  42. getStatelessWebServiceClient( webServiceURL );
  43.  
  44. // Create Credentials object for operations
  45. Credentials credentials = new Credentials();
  46. credentials.setUserName( userName );
  47. credentials.setPassword( password );
  48.  
  49. // 1. Create a simple web source
  50. String webSourceURL = args[3];
  51. String webSourceXML =
  52. "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
  53. "<search:config productVersion=\"11.1.2.0.0\" xmlns:search=\"http://xmlns.oracle.com/search\">" +
  54. " <search:sources>" +
  55. " <search:webSource>" +
  56. " <search:name>web1</search:name>" +
  57. " <search:startingUrls>" +
  58. " <search:startingUrl>" +
  59. " <search:url>" + webSourceURL + "</search:url>" +
  60. " </search:startingUrl>" +
  61. " </search:startingUrls>" +
  62. " </search:webSource>" +
  63. " </search:sources>" +
  64. "</search:config>";
  65.  
  66. adminPort.createAll(
  67. "source",
  68. webSourceXML,
  69. "password",
  70. credentials,
  71. null,
  72. null,
  73. "en"
  74. );
  75.  
  76. // 2. Export all sources to show the full definition
  77. ObjectOutput oo = adminPort.exportAll(
  78. "source",
  79. null,
  80. "password",
  81. credentials,
  82. null,
  83. "en"
  84. );
  85. System.out.println("Web Source XML = \n" + oo.getObjectXML() );
  86.  
  87. // 3. Create a source group for the source
  88. String sourceGroupXML =
  89. "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
  90. "<search:config productVersion=\"11.1.2.0.0\" xmlns:search=\"http://xmlns.oracle.com/search\">" +
  91. " <search:sourceGroups>" +
  92. " <search:sourceGroup>" +
  93. " <search:name>Web</search:name>" +
  94. " <search:assignedSources>" +
  95. " <search:assignedSource>web1</search:assignedSource>" +
  96. " </search:assignedSources>" +
  97. " </search:sourceGroup>" +
  98. " </search:sourceGroups>" +
  99. "</search:config>";
  100.  
  101. adminPort.createAll(
  102. "sourceGroup",
  103. sourceGroupXML,
  104. null,
  105. credentials,
  106. null,
  107. null,
  108. "en"
  109. );
  110.  
  111. System.out.println("Created source group...");
  112.  
  113. // 4. Create a schedule for the web source
  114. String scheduleXML =
  115. "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
  116. "<search:config productVersion=\"11.1.2.0.0\" xmlns:search=\"http://xmlns.oracle.com/search\">" +
  117. " <search:schedules>" +
  118. " <search:schedule>" +
  119. " <search:name>schedule1</search:name>" +
  120. " <search:crawlingMode>ACCEPT_ALL</search:crawlingMode>" +
  121. " <search:recrawlPolicy>PROCESS_CHANGED</search:recrawlPolicy>" +
  122. " <search:frequency>" +
  123. " <search:manual/>" +
  124. " </search:frequency>" +
  125. " <search:assignedSources>" +
  126. " <search:assignedSource>web1</search:assignedSource>" +
  127. " </search:assignedSources>" +
  128. " </search:schedule>" +
  129. " </search:schedules>" +
  130. "</search:config>";
  131.  
  132. adminPort.createAll(
  133. "schedule",
  134. scheduleXML,
  135. null,
  136. credentials,
  137. null,
  138. null,
  139. "en"
  140. );
  141.  
  142. System.out.println("Created schedule...");
  143.  
  144. // 5. Start the schedule
  145.  
  146. // Create object key for schedule name
  147. ObjectKey objectKey = new ObjectKey();
  148. AdminKeyPair keyPair = new AdminKeyPair();
  149. keyPair.setKeyName( "name" ); // schedules identified by name
  150. keyPair.setKeyValue( "schedule1" ); // schedule name
  151. objectKey.getAdminKeyPairs().add( keyPair );
  152.  
  153. adminPort.start(
  154. "schedule",
  155. objectKey,
  156. null,
  157. null,
  158. credentials,
  159. null,
  160. null,
  161. "en"
  162. );
  163.  
  164. System.out.println("Started schedule...");
  165. System.out.println("Waiting 30 seconds to get status...");
  166. Thread.sleep( 30000 );
  167.  
  168. // 6. Use object key above to get schedule state
  169. oo = adminPort.getState(
  170. "schedule",
  171. objectKey,
  172. null, // request all state properties
  173. credentials,
  174. null,
  175. "en"
  176. );
  177.  
  178. System.out.println("Schedule state XML = " + oo.getObjectXML() );
  179. }
  180. }
  181. catch (AdminAPIRuntimeFault_Exception e)
  182. {
  183. AdminAPIRuntimeFault runtimeFault = e.getFaultInfo();
  184. System.out.println("Caught AdminAPIRuntimeFault");
  185. System.out.println(" message = " + runtimeFault.getMessage() );
  186. System.out.println(" errorCode = " + runtimeFault.getErrorCode() );
  187. System.out.println(" causeMessage = " + runtimeFault.getCauseMessage() );
  188. System.out.println(" stackTrace = " );
  189. e.printStackTrace( System.out );
  190. System.out.println(" causeStackTrace = \n" + runtimeFault.getCauseStackTrace() );
  191. }
  192. catch (Throwable t)
  193. {
  194. System.out.println("Caught unexpected run-time exception");
  195. System.out.println(" message = " + t.getMessage() );
  196. System.out.println(" stackTrace = " );
  197. t.printStackTrace( System.out );
  198. }
  199. }
  200.  
  201. /**
  202. * Initializes and returns a stateless admin web service client.
  203. */
  204. private static AdminPortType getStatelessWebServiceClient(
  205. String webServiceURL) throws Exception
  206. {
  207. AdminService adminService = new AdminService(
  208. new URL( webServiceURL ),
  209. new QName(
  210. "http://search.oracle.com/Admin",
  211. "AdminService"
  212. )
  213. );
  214.  
  215. return adminService.getAdmin();
  216. }
  217. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement