001 /*
002 * file SimpleQuery.java
003 *
004 * Licensed Materials - Property of IBM
005 * Restricted Materials of IBM - you are allowed to copy, modify and
006 * redistribute this file as part of any program that interfaces with
007 * IBM Rational CM API.
008 *
009 * com.ibm.rational.stp.client.samples.SimpleQuery
010 *
011 * © Copyright IBM Corporation 2004, 2008. All Rights Reserved.
012 * Note to U.S. Government Users Restricted Rights: Use, duplication or
013 * disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
014 */
015
016 package com.ibm.rational.stp.client.samples;
017
018 import static javax.swing.JOptionPane.showInputDialog;
019
020 import java.util.List;
021
022 import javax.swing.JFrame;
023 import javax.swing.JOptionPane;
024 import javax.swing.JScrollPane;
025 import javax.swing.JTextArea;
026 import javax.wvcm.ProviderFactory;
027 import javax.wvcm.WvcmException;
028
029 import com.ibm.rational.wvcm.stp.StpProvider;
030 import com.ibm.rational.wvcm.stp.StpProvider.Domain;
031 import com.ibm.rational.wvcm.stp.cc.CcProvider;
032 import com.ibm.rational.wvcm.stp.cq.CqProvider;
033 import com.ibm.rational.wvcm.stp.cq.CqQuery;
034 import com.ibm.rational.wvcm.stp.cq.CqResultSet;
035
036 /**
037 * Sample CM API Application that executes a query named by the user and
038 * displays the results in a passive window.
039 */
040 public class SimpleQuery
041 {
042
043 /**
044 * @param args
045 */
046 public static void main(String[] args) throws Exception
047 {
048 CqProvider provider = (CqProvider)
049 ProviderFactory.createProvider(CqProvider.CQ_ONLY_PROVIDER_CLASS, g_callback);
050 CqQuery query = provider.buildProxy(CqQuery.class,
051 showInputDialog("Enter Name of Query to Run (use indicated format)",
052 "<query-pathname>@<db-set>/<user-db>"));
053 CqResultSet results = query.doExecute(1, Integer.MAX_VALUE, null);
054 JTextArea text = new JTextArea(640, 480);
055 JFrame frame = new JFrame(query.location().toString());
056
057 while (results.hasNext()) {
058 for (String r: results.next().getStrings()) text.append(r + "\t");
059 text.append("\n");
060 }
061
062 provider.terminate();
063 frame.setContentPane(new JScrollPane(text));
064 frame.setBounds(500, 400, 600, 300);
065 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
066 frame.setVisible(true);
067 }
068
069 static StpProvider.StpCallback g_callback = new StpProvider.StpCallback() {
070 public Authentication getAuthenticationEx(Domain domain,
071 String realm,
072 int retryCount,
073 StpProvider provider,
074 WvcmException failure)
075 throws WvcmException
076 {
077 // Try to reuse last credentials on each new repository
078 if (m_unpw != null && retryCount == 0)
079 return m_unpw;
080
081 String title = "Enter " + domain + " Username '+' Password for "
082 + realm + " [" + retryCount + "]";
083
084 if (failure != null)
085 title = "Login failed: " + failure + "\n" + title;
086
087 String unpw = JOptionPane.showInputDialog(title, "admin+");
088
089 if (unpw == null || unpw.length() == 0)
090 throw new IllegalAccessError("User canceled request");
091
092 return m_unpw = unpw.equals("anonymous")? null: new UnPw(unpw);
093 }
094
095 public Authentication getAuthentication(String r, int c)
096 { return null; /* Will not be called */ }
097
098 private UnPw m_unpw;
099 };
100
101 /**
102 * A simple Authentication object in which the username and password
103 * obtained from the user is cached for use by the CM API.
104 */
105 static class UnPw implements CcProvider.CcAuthentication {
106 /**
107 * Constructs an Authentication object
108 *
109 * @param unpw A String containing username+password.
110 */
111 UnPw(String unpw) { m_data = unpw.split("\\+", -2); }
112
113 public String loginName() { return m_data[0]; }
114 public String password() { return m_data.length > 1 ? m_data[1] : ""; };
115 public String getPrimaryGroupName() { return m_data.length > 2 ? m_data[2] : ""; }
116
117 /* (non-Javadoc)
118 * @see com.ibm.rational.wvcm.stp.cc.CcProvider.CcAuthentication#getGroupList()
119 */
120 public List<String> getGroupList()
121 {
122 return null;
123 }
124
125 /** The cached credentials */
126 private String[] m_data;
127
128 }
129 }