1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.jetspeed.om.profile;
18
19 import java.util.StringTokenizer;
20
21
22 import org.apache.jetspeed.om.security.JetspeedUser;
23 import org.apache.jetspeed.om.security.JetspeedUserFactory;
24 import org.apache.jetspeed.om.security.Role;
25 import org.apache.jetspeed.om.security.Group;
26
27 import org.apache.jetspeed.services.Profiler;
28 import org.apache.jetspeed.services.JetspeedSecurity;
29 import org.apache.jetspeed.services.logging.JetspeedLogFactoryService;
30 import org.apache.jetspeed.services.logging.JetspeedLogger;
31
32 /***
33 * Interface definition for a Profile Locator.
34 * Locators are used by the profiler to describe the parameters used to locate
35 * a resource in the persistent configuration store.
36 *
37 *
38 * @author <a href="mailto:taylor@apache.org">David Sean Taylor</a>
39 * @author <a href="mailto:adambalk@cisco.com">Atul Dambalkar</a>
40 * @version $Id: BaseProfileLocator.java,v 1.21 2004/02/23 03:05:01 jford Exp $
41 */
42
43 public class BaseProfileLocator implements ProfileLocator
44 {
45
46 private String name = null;
47 private String mediaType = null;
48 private String language = null;
49 private String country = null;
50 private JetspeedUser user = null;
51 private Role role = null;
52 private Group group = null;
53 private boolean anonymous = false;
54
55 private String roleName = null;
56 private String userName = null;
57 private String groupName = null;
58
59 private static final String DELIM = "/";
60
61 /***
62 * Static initialization of the logger for this class
63 */
64 private static final JetspeedLogger logger = JetspeedLogFactoryService.getLogger(BaseProfileLocator.class.getName());
65
66
67
68
69
70
71
72
73
74
75
76
77
78 public String getId()
79 {
80 StringBuffer id = new StringBuffer(128);
81
82 if (!anonymous && user != null)
83 {
84 id.append(Profiler.PARAM_USER).append(DELIM);
85 id.append(user.getUserName());
86 }
87 else if (group != null)
88 {
89 id.append(Profiler.PARAM_GROUP).append(DELIM);
90 id.append(group.getName());
91 }
92 else if (role != null)
93 {
94 id.append(Profiler.PARAM_ROLE).append(DELIM);
95 id.append(role.getName());
96 }
97 else
98 {
99 id.append(Profiler.PARAM_ANON);
100 }
101 if (language != null)
102 {
103 id.append(DELIM);
104 id.append(language);
105 }
106 if (country != null)
107 {
108 id.append(DELIM);
109 id.append(country);
110 }
111 if (mediaType != null)
112 {
113 id.append(DELIM);
114 id.append(mediaType);
115 }
116 if (name != null)
117 {
118 id.append(DELIM);
119 id.append(name);
120 }
121
122 return id.toString();
123 }
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140 public String getPath()
141 {
142 StringBuffer id = new StringBuffer(128);
143
144 if (!anonymous && user != null)
145 {
146 id.append(Profiler.PARAM_USER).append(DELIM);
147 id.append(user.getUserName()).append(DELIM);
148 }
149 else if (group != null)
150 {
151 id.append(Profiler.PARAM_GROUP).append(DELIM);
152 id.append(group.getName()).append(DELIM);
153 }
154 else if (role != null)
155 {
156 id.append(Profiler.PARAM_ROLE).append(DELIM);
157 id.append(role.getName()).append(DELIM);
158 }
159 else
160 {
161 id.append(Profiler.PARAM_USER).append(DELIM);
162 id.append(Profiler.PARAM_ANON).append(DELIM);
163 }
164
165 if (language != null)
166 {
167 id.append(Profiler.PARAM_LANGUAGE).append(DELIM);
168 id.append(language).append(DELIM);
169 }
170 if (country != null)
171 {
172 id.append(Profiler.PARAM_COUNTRY).append(DELIM);
173 id.append(country).append(DELIM);
174 }
175 if (mediaType != null)
176 {
177 id.append(Profiler.PARAM_MEDIA_TYPE).append(DELIM);
178 id.append(mediaType).append(DELIM);
179 }
180 if (name != null)
181 {
182 id.append(Profiler.PARAM_PAGE).append(DELIM);
183 id.append(name).append(DELIM);
184 }
185 id.deleteCharAt(id.length()-1);
186 return id.toString();
187 }
188
189
190
191
192
193
194
195
196
197
198
199
200
201 public void createFromPath(String path)
202 {
203 StringTokenizer tok = new StringTokenizer(path, "/");
204 while (tok.hasMoreTokens())
205 {
206 String name = (String)tok.nextToken();
207 if (name.equals(Profiler.PARAM_USER) && tok.hasMoreTokens())
208 {
209 try
210 {
211
212
213 this.userName = tok.nextToken();
214 this.setUser( JetspeedSecurity.getUser(this.userName) );
215 }
216 catch (Exception e)
217 {
218 logger.error("ProfileLocator: Failed to set User: ", e);
219 }
220 }
221 else if (name.equals(Profiler.PARAM_GROUP) && tok.hasMoreTokens())
222 {
223 try
224 {
225
226
227 this.groupName = tok.nextToken();
228 this.setGroup( JetspeedSecurity.getGroup(this.groupName) );
229 }
230 catch (Exception e)
231 {
232 logger.error("ProfileLocator: Failed to set Group: ", e);
233 }
234 }
235 else if (name.equals(Profiler.PARAM_ROLE) && tok.hasMoreTokens())
236 {
237 try
238 {
239
240
241 this.roleName = tok.nextToken();
242 this.setRole(JetspeedSecurity.getRole(this.roleName));
243 }
244 catch (Exception e)
245 {
246 logger.error("ProfileLocator: Failed to set Role: ", e);
247 }
248 }
249 else if (name.equals(Profiler.PARAM_PAGE) && tok.hasMoreTokens())
250 {
251 this.setName(tok.nextToken());
252 }
253 else if (name.equals(Profiler.PARAM_MEDIA_TYPE) && tok.hasMoreTokens())
254 {
255 this.setMediaType(tok.nextToken());
256 }
257 else if (name.equals(Profiler.PARAM_LANGUAGE) && tok.hasMoreTokens())
258 {
259 this.setLanguage(tok.nextToken());
260 }
261 else if (name.equals(Profiler.PARAM_COUNTRY) && tok.hasMoreTokens())
262 {
263 this.setCountry(tok.nextToken());
264 }
265
266 }
267 }
268
269 /***
270 * @see Object#clone
271 * @return an instance copy of this object
272 */
273 public Object clone() throws java.lang.CloneNotSupportedException
274 {
275 return super.clone();
276 }
277
278
279
280
281
282
283 public String getName()
284 {
285 return name;
286 }
287
288
289
290
291
292
293 public void setName(String name)
294 {
295 this.name = name;
296 }
297
298
299
300
301
302
303 public boolean getAnonymous()
304 {
305 return this.anonymous;
306 }
307
308
309
310
311
312
313
314 public void setAnonymous(boolean anonymous)
315 {
316 try
317 {
318 JetspeedUser user = JetspeedUserFactory.getInstance();
319 user.setUserName(JetspeedSecurity.getAnonymousUserName());
320 this.setUser(user);
321 }
322 catch (Exception e)
323 {
324 logger.error("Could not get Anonymous user", e);
325 }
326 finally
327 {
328 this.anonymous = anonymous;
329 }
330 }
331
332
333
334
335
336
337
338 public String getMediaType()
339 {
340 return mediaType;
341 }
342
343
344
345
346
347
348
349 public void setMediaType(String mediaType)
350 {
351 this.mediaType = mediaType;
352 }
353
354
355
356
357
358
359
360
361 public String getLanguage()
362 {
363 return language;
364 }
365
366
367
368
369
370
371
372
373 public void setLanguage(String language)
374 {
375 this.language = language;
376 }
377
378
379
380
381
382
383
384
385 public String getCountry()
386 {
387 return country;
388 }
389
390
391
392
393
394
395
396
397 public void setCountry(String country)
398 {
399 this.country = country;
400 }
401
402
403
404
405
406
407 public JetspeedUser getUser()
408 {
409 return user;
410 }
411
412 public String getUserName()
413 {
414 if (null == user)
415 return userName;
416
417 return user.getUserName();
418 }
419
420
421
422
423
424
425 public void setUser(JetspeedUser user)
426 {
427 this.user = user;
428 }
429
430
431
432
433
434
435 public Role getRole()
436 {
437 return role;
438 }
439
440 public String getRoleName()
441 {
442 if (null == role)
443 return roleName;
444
445 return role.getName();
446 }
447
448
449
450
451
452
453 public void setRole( Role role )
454 {
455 this.role = role;
456 }
457
458 public void setRoleByName( String roleName )
459 {
460 try
461 {
462 Role temp = JetspeedSecurity.getRole(roleName);
463 if (null != temp)
464 {
465 role = temp;
466 }
467 }
468 catch (Exception e)
469 {
470 logger.error("ProfileLocator: Failed to set Role " + roleName, e);
471 }
472 }
473
474
475
476
477
478
479 public Group getGroup()
480 {
481 return group;
482 }
483
484 public String getGroupName()
485 {
486 if (null == group)
487 return groupName;
488
489 return group.getName();
490 }
491
492
493
494
495
496
497 public void setGroup( Group group )
498 {
499 this.group = group;
500 }
501
502 public void setGroupByName( String groupName )
503 {
504 try
505 {
506 Group temp = JetspeedSecurity.getGroup(groupName);
507 if (null != temp)
508 {
509 group = temp;
510 }
511 }
512 catch (Exception e)
513 {
514 logger.error("ProfileLocator: Failed to set Group: " + e);
515 }
516 }
517
518
519
520
521
522
523 /***
524 * Define equality criteria for ProfileLocator objects.
525 * @param obj ProfileLocator object to be compared with.
526 */
527 public boolean equals(Object obj)
528 {
529 if( obj == null )
530 {
531 return false;
532 }
533 synchronized (obj)
534 {
535 if ( ! ( obj instanceof ProfileLocator ) )
536 {
537 return false;
538 }
539
540 ProfileLocator locator = (ProfileLocator)obj;
541
542 String name = locator.getName();
543 String mediaType = locator.getMediaType();
544 String language = locator.getLanguage();
545 String country = locator.getCountry();
546 Group group = locator.getGroup();
547 Role role = locator.getRole();
548
549 return nameEquals(name)
550
551 && mediaTypeEquals(mediaType)
552 && languageEquals(language)
553 && countryEquals(country)
554 && userEquals(locator)
555 && groupEquals(group)
556 && roleEquals(role);
557 }
558 }
559
560 /***
561 * Check equality for given User object with this ProfileLocator's User
562 * object.
563 */
564 private boolean userEquals(ProfileLocator locator)
565 {
566 JetspeedUser user = locator.getUser();
567
568 if (exclusiveOr(this.user, user))
569 {
570 return false;
571 }
572
573 if (assertNotNull(this.user) && assertNotNull(user))
574 {
575 return stringEquals(this.user.getUserName(), user.getUserName());
576 }
577
578 return this.anonymous == locator.getAnonymous();
579 }
580
581 /***
582 * Check equality for given Group object with this ProfileLocator's Group
583 * object.
584 */
585 private boolean groupEquals(Group group)
586 {
587
588 if (exclusiveOr(this.group, group))
589 {
590 return false;
591 }
592
593 if (assertNotNull(this.group) && assertNotNull(group))
594 {
595 return stringEquals( this.group.getName(), group.getName());
596 }
597
598 return true;
599 }
600
601 /***
602 * Check equality for given Role object with this ProfileLocator's Role
603 * object.
604 */
605 private boolean roleEquals(Role role)
606 {
607
608 if (exclusiveOr(this.role, role))
609 {
610 return false;
611 }
612
613 if (assertNotNull(this.role) && assertNotNull(role))
614 {
615 return stringEquals(this.role.getName(), role.getName());
616 }
617
618 return true;
619 }
620
621 /***
622 * Check equality for language object with this ProfileLocator's language
623 * object.
624 */
625 private boolean languageEquals(String language)
626 {
627 return stringEquals(this.language, language);
628 }
629
630 /***
631 * Check equality for country object with this ProfileLocator's country
632 * object.
633 */
634 private boolean countryEquals(String country)
635 {
636 return stringEquals(this.country, country);
637 }
638
639 /***
640 * Check equality for name object with this ProfileLocator's name
641 * object.
642 */
643 private boolean nameEquals(String name)
644 {
645 return stringEquals(this.name, name);
646 }
647
648 /***
649 * Check equality for name object with this ProfileLocator's name
650 * object.
651 */
652 private boolean mediaTypeEquals(String mediaType)
653 {
654 return stringEquals(this.mediaType, mediaType);
655 }
656
657 /***
658 * AssertNotNull the two String objects and then check the equality.
659 */
660 private boolean stringEquals(String str1, String str2)
661 {
662 if (exclusiveOr(str1, str2))
663 {
664 return false;
665 }
666 if (assertNotNull(str1) && assertNotNull(str2))
667 {
668 return str1.equals(str2);
669 }
670
671 return true;
672 }
673
674 /***
675 * AssertNotNull the given object.
676 */
677 private boolean assertNotNull(Object object)
678 {
679 return object != null;
680 }
681
682 /***
683 * Exclusive or the two objects fro their references null-ability.
684 */
685 private boolean exclusiveOr(Object obj1, Object obj2)
686 {
687 return (assertNotNull(obj1) && !assertNotNull(obj2))
688 || (!assertNotNull(obj1) && assertNotNull(obj2));
689 }
690 }