001: package com.sun.portal.config;
002:
003: import com.sun.im.service.*;
004: import java.io.*;
005: import java.util.*;
006:
007: /*
008: * This class is responsible for populating the JES Instant Messaging
009: * Server with sample data for the JES Portal Server adventuresportscafe.com
010: * sample.
011: *
012: * It will populate the Contact Folder and List for the users:
013: * chris, ed, and mary
014: *
015: */
016: //public class PopulateInstantMessenger implements CollaborationSessionListener {
017: public class PopulateInstantMessenger {
018:
019: protected String serviceUrl = null;
020: protected String user = null;
021: protected String password = null;
022: protected boolean debug = false;
023: protected String domain = null;
024:
025: // known users
026: protected final static String CHRIS = "chris";
027: protected final static String ED = "ed";
028: protected final static String MARY = "mary";
029:
030: // contact folder name
031: protected final static String CONTACT_FOLDER_NAME = "My Contacts";
032:
033: /*
034: * Constructor
035: */
036: public PopulateInstantMessenger(String serviceUrl, String user,
037: String password, String domain, boolean debug) {
038:
039: this .serviceUrl = serviceUrl;
040: this .user = user;
041: this .password = password;
042: this .domain = domain;
043: this .debug = debug;
044:
045: }
046:
047: /*
048: * Retreive PersonalStoreSession
049: */
050: public PersonalStoreSession getSession(
051: CollaborationSessionFactory fac, String serviceUrl,
052: String user, String password) throws Exception {
053:
054: String uid = user;
055:
056: if ((domain != null) && (domain.length() > 0)) {
057: uid = user + "@" + domain;
058: }
059:
060: if (debug) {
061: System.out.println("serviceUrl: " + serviceUrl);
062: System.out.println("uid: " + uid);
063: }
064:
065: CollaborationSession cSession = fac.getSession(serviceUrl, uid,
066: password, null);
067: PersonalStoreSession psSession = (PersonalStoreSession) cSession
068: .accessService(CollaborationSessionFactory.PERSONALSTORE);
069: if (debug) {
070: System.out.println("retrieved PersonalStoreSession");
071: }
072: return psSession;
073: }
074:
075: public void generate() throws Exception {
076: CollaborationSessionFactory fac = null;
077: PersonalStoreSession session = null;
078:
079: if (debug) {
080: System.setProperty("com.sun.im.service.xmpp.log", "debug");
081: }
082: fac = new CollaborationSessionFactory();
083: session = getSession(fac, serviceUrl, user, password);
084: if (session == null) {
085: System.out.println("Session is null");
086: return;
087: }
088: // Get contact folder
089: PersonalStoreFolder folder = getContactFolder(session);
090: String c1 = MARY;
091: String c2 = ED;
092: if (user.equals(CHRIS)) {
093: c1 = MARY;
094: c2 = ED;
095: } else if (user.equals(ED)) {
096: c1 = MARY;
097: c2 = CHRIS;
098: } else if (user.equals(MARY)) {
099: c1 = CHRIS;
100: c2 = ED;
101: } else {
102: System.out.println("Invalid user: " + user);
103: return;
104: }
105: CollaborationPrincipal[] c1Array = session.searchPrincipals(
106: PersonalStoreSession.SEARCHTYPE_EQUALS, c1);
107: CollaborationPrincipal[] c2Array = session.searchPrincipals(
108: PersonalStoreSession.SEARCHTYPE_EQUALS, c2);
109: PersonalContact pc1, pc2;
110: try {
111: pc1 = (PersonalContact) session.createEntry(c1Array[0],
112: PersonalStoreEntry.CONTACT, c1);
113: pc2 = (PersonalContact) session.createEntry(c2Array[0],
114: PersonalStoreEntry.CONTACT, c2);
115: } catch (CollaborationException ce) {
116: pc1 = (PersonalContact) session.createEntry(
117: PersonalStoreEntry.CONTACT, c1);
118: pc2 = (PersonalContact) session.createEntry(
119: PersonalStoreEntry.CONTACT, c2);
120: }
121: addContactsToFolder(folder, pc1, pc2);
122: session.logout();
123: }
124:
125: /*
126: * This method is responsible for return the personal contact folder
127: * for the user, creating it if necessary.
128: *
129: */
130: public PersonalStoreFolder getContactFolder(
131: PersonalStoreSession session) throws CollaborationException {
132: Collection allFolders = session
133: .getFolders(PersonalStoreFolder.CONTACT_FOLDER);
134: if (allFolders != null) {
135: for (Iterator i = allFolders.iterator(); i.hasNext();) {
136: PersonalStoreFolder cFolder = (PersonalStoreFolder) i
137: .next();
138: if (cFolder.getDisplayName()
139: .equals(CONTACT_FOLDER_NAME)) {
140: if (debug)
141: System.out.println("Found folder "
142: + CONTACT_FOLDER_NAME);
143: return cFolder;
144: }
145: }
146: }
147: PersonalStoreEntry newFolder = session
148: .createEntry(PersonalStoreFolder.CONTACT_FOLDER,
149: CONTACT_FOLDER_NAME);
150: newFolder.save();
151: return (PersonalStoreFolder) newFolder;
152: }
153:
154: /*
155: * Add contacts to folder
156: *
157: */
158: public void addContactsToFolder(PersonalStoreFolder folder,
159: PersonalContact pc1, PersonalContact pc2) {
160: try {
161: if (debug) {
162: System.out.println("Detailed information of folder:");
163: System.out.println(" Display Name: "
164: + folder.getDisplayName());
165: System.out
166: .println(" Entry Id: " + folder.getEntryId());
167: System.out.println(" Type: " + folder.getType());
168: System.out.println("Saving pc1");
169: System.out
170: .println("Detailed information of contact pc1:");
171: System.out.println(" Display Name: "
172: + pc1.getDisplayName());
173: System.out.println(" Entry Id: " + pc1.getEntryId());
174: System.out.println(" Type: " + pc1.getType());
175: }
176: pc1.addToFolder(folder);
177: pc1.save();
178: if (debug) {
179: System.out.println("Saving pc2");
180: System.out
181: .println("Detailed information of contact pc2:");
182: System.out.println(" Display Name: "
183: + pc2.getDisplayName());
184: System.out.println(" Entry Id: " + pc2.getEntryId());
185: System.out.println(" Type: " + pc2.getType());
186: }
187: pc2.addToFolder(folder);
188: pc2.save();
189: if (debug) {
190: System.out.println("Saving folder");
191: }
192: folder.save();
193: } catch (CollaborationException ce) {
194: System.out.println("Error saving contacts to folder");
195: ce.printStackTrace();
196: }
197: }
198:
199: /*
200: *
201: *
202: */
203: private static void usage() {
204: System.out
205: .println("usage: java PopulateInstantMessenger "
206: + " <iim_mux.host:iim_mux.listenport> <user> <password> <domain>"
207: + "[true|false]");
208: System.out.println(" or java PopulateInstantMessenger "
209: + " <iim_mux.host:iim_mux.listenport> <domain>"
210: + "[true|false]" + " to populate all default users");
211: }
212:
213: // Set the user name
214: public void setUser(String user) {
215: this .user = user;
216: }
217:
218: //Set the password
219: public void setPassword(String password) {
220: this .password = password;
221: }
222:
223: /*
224: *
225: *
226: */
227: public static void main(String[] args) {
228: String l_serviceUrl = "";
229: String l_user = "chris";
230: String l_password = "chris";
231: String l_domain = "";
232: boolean l_debug = false;
233: boolean popAllUsers = false;
234: if (args.length < 4) {
235: // Allow for passing in just the host:port and l_debug value
236: if (args.length < 2) {
237: usage();
238: return;
239: }
240: popAllUsers = true;
241: l_serviceUrl = args[0];
242: l_domain = args[1];
243: if (args.length > 2) {
244: l_debug = Boolean.valueOf(args[2]).booleanValue();
245: }
246: } else {
247:
248: l_serviceUrl = args[0];
249: l_user = args[1];
250: l_password = args[2];
251: l_domain = args[3];
252:
253: if (args.length > 4) {
254: l_debug = Boolean.valueOf(args[4]).booleanValue();
255: }
256: }
257: PopulateInstantMessenger seed = new PopulateInstantMessenger(
258: l_serviceUrl, l_user, l_password, l_domain, l_debug);
259: try {
260: seed.generate();
261: if (popAllUsers) {
262: seed.setUser("mary");
263: seed.setPassword("mary");
264: seed.generate();
265: seed.setUser("ed");
266: seed.setPassword("ed");
267: seed.generate();
268: }
269: } catch (Exception e) {
270: e.printStackTrace();
271: return;
272: }
273: }
274:
275: }
|