001: package com.sun.portal.community.mc.test.provider;
002:
003: import com.sun.portal.providers.*;
004: import com.sun.portal.community.mc.*;
005: import com.sun.portal.community.mc.ConfigTable.ConfigKey;
006: import com.sun.portal.community.mc.test.*;
007: import java.util.*;
008: import javax.naming.*;
009: import javax.servlet.http.*;
010: import java.sql.*;
011: import javax.sql.*;
012: import java.io.*;
013:
014: public class CommunityTestProvider extends ProfileProviderAdapter {
015: private ConfigTable lastRead = new ConfigTable();
016:
017: public StringBuffer getContent(HttpServletRequest request,
018: HttpServletResponse response) throws ProviderException {
019: StringBuffer b = new StringBuffer();
020: String cmd = request.getParameter("cmd");
021:
022: try {
023: if (cmd == null) {
024: throw new Exception("null cmd");
025: } else if (cmd.equals("jdbc")) {
026: testJdbc(b);
027: } else if (cmd.equals("getuser")) {
028: String uid = request.getParameter("uid");
029: testGetUser(b, uid);
030: } else if (cmd.equals("getmembership")) {
031: String uid = request.getParameter("uid");
032: CMCUser cu = testGetUser(b, uid);
033: testGetMembership(b, cu);
034: } else if (cmd.equals("create")) {
035: String s = request.getParameter("n");
036: int n = Integer.parseInt(s);
037: testCreate(b, n);
038: } else if (cmd.equals("adduser")) {
039: String number = request.getParameter("n");
040: int n = Integer.parseInt(number);
041: String uid = request.getParameter("uid");
042:
043: testAddUser(b, n, uid);
044: } else if (cmd.equals("removeuser")) {
045: String number = request.getParameter("n");
046: int n = Integer.parseInt(number);
047: String uid = request.getParameter("uid");
048:
049: testRemoveUser(b, n, uid);
050: } else if (cmd.equals("setdps")) {
051: String number = request.getParameter("n");
052: int n = Integer.parseInt(number);
053: String dpFilename = request.getParameter("dp");
054: if (dpFilename == null) {
055: throw new Exception("must specify dp");
056: }
057: testSetDp(b, n, dpFilename);
058: } else if (cmd.equals("getuserdps")) {
059: String uid = request.getParameter("uid");
060: CMCUser cu = testGetUser(b, uid);
061: testGetDps(b, cu);
062: } else if (cmd.equals("getusermodifieddps")) {
063: String uid = request.getParameter("uid");
064: CMCUser cu = testGetUser(b, uid);
065: testGetModifiedDps(b, cu);
066: } else if (cmd.equals("remove")) {
067: String number = request.getParameter("n");
068: int n = Integer.parseInt(number);
069:
070: testRemove(b, n);
071: } else if (cmd.equals("dumpuserdps")) {
072: String uid = request.getParameter("uid");
073: CMCUser cu = testGetUser(b, uid);
074: testDumpDps(b, cu);
075: } else if (cmd.equals("nop")) {
076: // nothing
077: } else {
078: throw new Exception("unknown command: " + cmd);
079: }
080: } catch (Throwable t) {
081: b.append("<pre>\n");
082: b.append(t.getMessage());
083: t.printStackTrace();
084: b.append("\n</pre>\n");
085: }
086:
087: return b;
088: }
089:
090: private void testAddUser(StringBuffer b, int n, String uid)
091: throws CMCException {
092: CMCFactory cf = CMCFactory.getInstance();
093: b.append("got community factory ...<br/>\n");
094:
095: if (uid == null) {
096: uid = getProviderContext().getUserID();
097: }
098: for (int i = 0; i < n; i++) {
099: CMCPrincipal cp = new CMCPrincipal(
100: com.sun.portal.community.mc.impl.jdo.CMCNodeImpl
101: .getStaticType(), "community" + i);
102: CMCNode cn = cf.getCMCNode(cp);
103: cn.addUsers(Collections.singleton(uid),
104: CMCRolePrincipal.MEMBER_ROLE);
105: }
106:
107: b.append("added users: n=" + n + ", uid=" + uid + "<br/>\n");
108: }
109:
110: private void testRemoveUser(StringBuffer b, int n, String uid)
111: throws CMCException {
112: CMCFactory cf = CMCFactory.getInstance();
113: b.append("got community factory ...<br/>\n");
114:
115: if (uid == null) {
116: uid = getProviderContext().getUserID();
117: }
118: for (int i = 0; i < n; i++) {
119: CMCPrincipal cp = new CMCPrincipal(
120: com.sun.portal.community.mc.impl.jdo.CMCNodeImpl
121: .getStaticType(), "community" + i);
122: CMCNode cn = cf.getCMCNode(cp);
123: cn.removeUsers(Collections.singleton(uid), Collections
124: .singleton(CMCRolePrincipal.MEMBER_ROLE));
125: }
126:
127: b.append("removed users: n=" + n + ", uid=" + uid + "<br/>\n");
128: }
129:
130: protected String readDp(File f) throws IOException {
131: BufferedReader br = null;
132: StringBuffer buf = new StringBuffer();
133:
134: try {
135: InputStream is = new FileInputStream(f);
136: InputStreamReader r = new InputStreamReader(is);
137: br = new BufferedReader(r);
138:
139: String line = null;
140: while ((line = br.readLine()) != null) {
141: buf.append(line);
142: }
143: } finally {
144: if (br != null) {
145: br.close();
146: }
147: }
148:
149: return buf.toString();
150: }
151:
152: private void testSetDp(StringBuffer b, int n, String dpFilename)
153: throws CMCException, IOException {
154: CMCFactory cf = CMCFactory.getInstance();
155: b.append("got community factory ...<br/>\n");
156:
157: String dpString = readDp(new File(dpFilename));
158: byte[] dp = dpString.getBytes("UTF-8");
159:
160: for (int i = 0; i < n; i++) {
161: CMCPrincipal cp = new CMCPrincipal(
162: com.sun.portal.community.mc.impl.jdo.CMCNodeImpl
163: .getStaticType(), "community" + i);
164: CMCNode cn = cf.getCMCNode(cp);
165: ConfigTable ct = new ConfigTable();
166: ConfigKey ck = new ConfigKey(cp,
167: CMCRolePrincipal.MEMBER_ROLE);
168: ct.put(ck, dp);
169: cn.setDPDocuments(ct);
170: }
171:
172: b.append("set DPs: n=" + n + "<br/>\n");
173: }
174:
175: private void testGetDps(StringBuffer b, CMCUser cu)
176: throws CMCException, IOException {
177: ConfigTable ct = cu.getDPDocuments(new ConfigTable());
178:
179: b.append("got DPs: " + ct.getConfigKeys() + "<br/>\n");
180: }
181:
182: private void testDumpDps(StringBuffer b, CMCUser cu)
183: throws CMCException, IOException {
184: ConfigTable ct = cu.getDPDocuments(new ConfigTable());
185:
186: Set keys = ct.getConfigKeys();
187: b.append("got DPs: " + keys + "<br/><br/>\n\n");
188:
189: String line = null;
190: for (Iterator i = keys.iterator(); i.hasNext();) {
191: ConfigKey key = (ConfigKey) i.next();
192: b.append(key + " (view HTML page source) : ");
193:
194: ByteArrayInputStream is = new ByteArrayInputStream(
195: (byte[]) ct.get(key));
196: InputStreamReader isr = new InputStreamReader(is);
197: BufferedReader br = new BufferedReader(isr);
198: while ((line = br.readLine()) != null) {
199: b.append(line + "\n");
200: }
201:
202: b.append("<br/><br/>\n");
203: }
204: }
205:
206: private void testGetModifiedDps(StringBuffer b, CMCUser cu)
207: throws CMCException, IOException {
208: ConfigTable ct = cu.getDPDocuments(lastRead);
209:
210: b.append("got DPs: " + ct.getConfigKeys() + "<br/>\n");
211: }
212:
213: private void testCreate(StringBuffer b, int n) throws CMCException {
214: CMCFactory cf = CMCFactory.getInstance();
215: b.append("got community factory ...<br/>\n");
216:
217: for (int i = 0; i < n; i++) {
218: CMCPrincipal cp = new CMCPrincipal(
219: com.sun.portal.community.mc.impl.jdo.CMCNodeImpl
220: .getStaticType(), "community" + i);
221: CMCNode cn = cf.getCMCNode(cp);
222: cn.create();
223: }
224:
225: b.append("created communites: n=" + n + "<br/>\n");
226: }
227:
228: private void testRemove(StringBuffer b, int n) throws CMCException {
229: CMCFactory cf = CMCFactory.getInstance();
230: b.append("got community factory ...<br/>\n");
231:
232: for (int i = 0; i < n; i++) {
233: CMCPrincipal cp = new CMCPrincipal(
234: com.sun.portal.community.mc.impl.jdo.CMCNodeImpl
235: .getStaticType(), "community" + i);
236: CMCNode cn = cf.getCMCNode(cp);
237: cn.remove();
238: }
239:
240: b.append("removed communites: n=" + n + "<br/>\n");
241: }
242:
243: private void testGetMembership(StringBuffer b, CMCUser cu)
244: throws CMCException {
245: Set membership = cu.getMembership();
246: b.append("got membership: ");
247: b.append(membership);
248: b.append("<br/>\n");
249: }
250:
251: private CMCUser testGetUser(StringBuffer b, String uid)
252: throws CMCException {
253: CMCFactory cf = CMCFactory.getInstance();
254: b.append("got community factory ...<br/>\n");
255: if (uid == null) {
256: uid = getProviderContext().getUserID();
257: }
258: CMCUser cu = cf.getCMCUser(uid);
259: b.append("got community user for uid: ");
260: b.append(uid);
261: b.append("<br/>\n");
262:
263: return cu;
264: }
265:
266: private void testJdbc(StringBuffer b) throws NamingException,
267: SQLException {
268: Context ctx = new InitialContext();
269: DataSource s = (DataSource) ctx
270: .lookup("java:comp/env/jdbc/communitymc");
271: //DataSource s = (DataSource)ctx.lookup("jdbc/communitymc");
272: //DataSource s = (DataSource)ctx.lookup("communitymc");
273: Connection c = s.getConnection();
274: b.append("got JDBC connection ...<br/>\n");
275: }
276: }
|