1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.jetspeed.services.portaltoolkit;
18
19
20 import org.apache.jetspeed.portal.PortletControl;
21 import org.apache.jetspeed.portal.PortletController;
22 import org.apache.jetspeed.portal.PortletSkin;
23 import org.apache.jetspeed.portal.PortletSet;
24 import org.apache.jetspeed.portal.Portlet;
25 import org.apache.jetspeed.portal.PortletConfig;
26 import org.apache.jetspeed.portal.BasePortletConfig;
27 import org.apache.jetspeed.portal.PortletControlConfig;
28 import org.apache.jetspeed.portal.BasePortletControlConfig;
29 import org.apache.jetspeed.portal.PortletControllerConfig;
30 import org.apache.jetspeed.portal.BasePortletControllerConfig;
31 import org.apache.jetspeed.portal.BasePortletSkin;
32 import org.apache.jetspeed.portal.BasePortletSet;
33 import org.apache.jetspeed.om.profile.Control;
34 import org.apache.jetspeed.om.profile.Controller;
35 import org.apache.jetspeed.om.profile.Skin;
36 import org.apache.jetspeed.om.profile.Portlets;
37 import org.apache.jetspeed.om.profile.Layout;
38 import org.apache.jetspeed.om.profile.Profile;
39 import org.apache.jetspeed.om.profile.Parameter;
40 import org.apache.jetspeed.om.profile.MetaInfo;
41 import org.apache.jetspeed.om.profile.Entry;
42 import org.apache.jetspeed.om.profile.ProfileLocator;
43 import org.apache.jetspeed.om.profile.PSMLDocument;
44 import org.apache.jetspeed.services.Profiler;
45 import org.apache.jetspeed.services.rundata.JetspeedRunData;
46 import org.apache.jetspeed.services.rundata.JetspeedRunDataService;
47
48 import org.apache.jetspeed.services.Registry;
49 import org.apache.jetspeed.services.PortletFactory;
50 import org.apache.jetspeed.services.logging.JetspeedLogFactoryService;
51 import org.apache.jetspeed.services.logging.JetspeedLogger;
52 import org.apache.jetspeed.om.registry.PortletEntry;
53 import org.apache.jetspeed.om.registry.PortletControlEntry;
54 import org.apache.jetspeed.om.registry.PortletControllerEntry;
55 import org.apache.jetspeed.om.registry.SkinEntry;
56 import org.apache.jetspeed.util.MetaData;
57 import org.apache.jetspeed.util.JetspeedException;
58 import org.apache.jetspeed.util.ServiceUtil;
59 import org.apache.jetspeed.om.BaseSecurityReference;
60 import org.apache.jetspeed.om.SecurityReference;
61 import org.apache.jetspeed.om.registry.SecurityEntry;
62
63 import org.apache.turbine.services.TurbineServices;
64 import org.apache.turbine.services.TurbineBaseService;
65 import org.apache.turbine.services.InitializationException;
66 import org.apache.turbine.services.resources.ResourceService;
67 import org.apache.turbine.services.rundata.RunDataService;
68
69 import java.util.Iterator;
70
71 import java.util.Hashtable;
72 import java.util.Map;
73 import javax.servlet.ServletConfig;
74
75 /***
76 * Simple implementation of the PortalFactoryService.
77 *
78 * @author <a href="mailto:raphael@apache.org">Raphaël Luta</a>
79 * @author <a href="mailto:taylor@apache.org">David Sean Taylor</a>
80 * @author <a href="mailto:morciuch@apache.org">Mark Orciuch</a>
81 *
82 * @version $Id: JetspeedPortalToolkitService.java,v 1.33 2004/03/29 21:02:29 taylor Exp $
83 */
84 public class JetspeedPortalToolkitService
85 extends TurbineBaseService
86 implements PortalToolkitService
87 {
88 /***
89 * Static initialization of the logger for this class
90 */
91 private static final JetspeedLogger logger = JetspeedLogFactoryService.getLogger(JetspeedPortalToolkitService.class.getName());
92
93 /*** The default control to use when none is specified */
94 private String defaultControl = null;
95
96 /*** The default controller to use when none is specified */
97 private String defaultController = null;
98
99 /*** The default skin to use when none is specified */
100 private String defaultSkin = null;
101
102 /*** The default user security ref to use when none is specified */
103 private String defaultUserSecurityRef = null;
104
105 /*** The default anonymous user security ref to use when none is specified */
106 private String defaultAnonSecurityRef = null;
107
108 /*** The default role security ref to use when none is specified */
109 private String defaultRoleSecurityRef = null;
110
111 /*** The default group security ref to use when none is specified */
112 private String defaultGroupSecurityRef = null;
113
114 /***
115 * This is the early initialization method called by the
116 * Turbine <code>Service</code> framework
117 */
118 public void init(ServletConfig conf) throws InitializationException
119 {
120
121 ResourceService serviceConf =
122 ((TurbineServices) TurbineServices.getInstance()).getResources(
123 PortalToolkitService.SERVICE_NAME);
124
125 this.defaultControl = serviceConf.getString("default.control");
126 this.defaultController = serviceConf.getString("default.controller");
127 this.defaultSkin = serviceConf.getString("default.skin");
128 this.defaultUserSecurityRef = serviceConf.getString("default.user.security.ref");
129 this.defaultAnonSecurityRef = serviceConf.getString("default.anon.security.ref");
130 this.defaultRoleSecurityRef = serviceConf.getString("default.role.security.ref");
131 this.defaultGroupSecurityRef = serviceConf.getString("default.group.security.ref");
132 setInit(true);
133
134 }
135
136 /***
137 * Instanciates a PortletControl based on a Registry entry, if available
138 * or directly from a classname.
139 *
140 * @param name a PortletControl name available in the registry or a classname
141 * @return the created PortletControl
142 */
143 public PortletControl getControl(String name)
144 {
145 PortletControl pc = null;
146 PortletControlEntry entry = null;
147
148 if (name != null)
149 {
150 entry = (PortletControlEntry) Registry.getEntry(Registry.PORTLET_CONTROL, name);
151 }
152
153 Map params = null;
154
155 try
156 {
157 if (entry == null)
158 {
159 if (name != null)
160 {
161 pc = (PortletControl) Class.forName(name).newInstance();
162 params = new Hashtable();
163 }
164 }
165 else
166 {
167 pc = (PortletControl) Class.forName(entry.getClassname()).newInstance();
168 params = entry.getParameterMap();
169 }
170 }
171 catch (Exception e)
172 {
173 logger.error("Unable to instanciate control " + name + ", using default", e);
174 }
175
176 if ((pc == null) && (defaultControl != null) && (!defaultControl.equals(name)))
177 {
178 return getControl(defaultControl);
179 }
180
181 PortletControlConfig pcConf = new BasePortletControlConfig();
182 pcConf.setName(name);
183 pcConf.setInitParameters(params);
184 pc.setConfig(pcConf);
185
186 return pc;
187 }
188
189 /***
190 * Instanciates a PortletControl based on a PSML Control object
191 *
192 * @param control the PSML control object
193 * @return the created PortletControl
194 */
195 public PortletControl getControl(Control control)
196 {
197 PortletControl pc = null;
198
199 if (control != null)
200 {
201 pc = getControl(control.getName());
202 pc.getConfig().getInitParameters().putAll(getParameters(control));
203 }
204 else
205 {
206 if (defaultControl != null)
207 {
208 pc = getControl(this.defaultControl);
209 }
210 }
211
212 return pc;
213 }
214
215 protected PortletControl getControl(Control control, PortletEntry entry)
216 {
217 PortletControl pc = null;
218
219 if (control != null)
220 {
221 pc = getControl(control.getName());
222 pc.getConfig().getInitParameters().putAll(getParameters(control));
223 }
224 else
225 {
226 org.apache.jetspeed.om.registry.Parameter dftPortletCtrl =
227 entry.getParameter("_control");
228
229 if (dftPortletCtrl != null)
230 {
231 pc = getControl(dftPortletCtrl.getValue());
232 }
233 else if (defaultControl != null)
234 {
235 pc = getControl(this.defaultControl);
236 }
237 }
238
239 return pc;
240 }
241
242 /***
243 * Instanciates a PortletController based on a Registry entry, if available
244 * or directly from a classname.
245 *
246 * @param name a PortletController name available in the registry or a classname
247 * @return the created PortletController
248 */
249 public PortletController getController(String name)
250 {
251 PortletController pc = null;
252 PortletControllerEntry entry = null;
253
254 if (name != null)
255 {
256 entry = (PortletControllerEntry) Registry.getEntry(Registry.PORTLET_CONTROLLER, name);
257 }
258
259 Map params = null;
260
261 try
262 {
263 if (entry == null)
264 {
265 if (name != null)
266 {
267 pc = (PortletController) Class.forName(name).newInstance();
268 params = new Hashtable();
269 }
270 }
271 else
272 {
273 pc = (PortletController) Class.forName(entry.getClassname()).newInstance();
274 params = entry.getParameterMap();
275 }
276 }
277 catch (Exception e)
278 {
279 logger.error("Unable to instanciate controller " + name + ", using default");
280 }
281
282 if ((pc == null) && (defaultController != null) && (!defaultController.equals(name)))
283 {
284 return getController(defaultController);
285 }
286
287 PortletControllerConfig pcConf = new BasePortletControllerConfig();
288 pcConf.setName(name);
289 pcConf.setInitParameters(params);
290 pc.setConfig(pcConf);
291 pc.init();
292
293 return pc;
294 }
295
296 /***
297 * Instantiates a PortletController based on a PSML Controller object
298 *
299 * @param controller the PSML controller object
300 * @return the created PortletController
301 */
302 public PortletController getController(Controller controller)
303 {
304
305 PortletController pc = null;
306
307 if (controller != null)
308 {
309 pc = getController(controller.getName());
310 pc.getConfig().getInitParameters().putAll(getParameters(controller));
311 }
312 else
313 {
314 if (defaultController != null)
315 {
316 pc = getController(this.defaultController);
317 }
318 }
319
320 pc.init();
321
322 return pc;
323 }
324
325 /***
326 * Create a PortletSkin object based on a Registry skin name
327 *
328 * @param name the registry SkinEntry name
329 * @return the new PortletSkin object
330 */
331 public PortletSkin getSkin(String name)
332 {
333 BasePortletSkin result = new BasePortletSkin();
334
335 SkinEntry entry = null;
336
337 if (name != null)
338 {
339 entry = (SkinEntry) Registry.getEntry(Registry.SKIN, name);
340 }
341
342
343
344
345 if (entry == null)
346 {
347 entry = (SkinEntry) Registry.getEntry(Registry.SKIN, this.defaultSkin);
348 }
349
350 if (entry != null)
351 {
352
353 result.setName(entry.getName());
354 result.putAll(entry.getParameterMap());
355 }
356
357
358 JetspeedRunDataService jrds =
359 (JetspeedRunDataService) ServiceUtil.getServiceByName(RunDataService.SERVICE_NAME);
360 JetspeedRunData jData = jrds.getCurrentRunData();
361 if(jData != null)
362 {
363 result.setCapabilityMap(jData.getCapability());
364 }
365 return result;
366 }
367
368 /***
369 * Create a PortletSkin object based on PSML skin description
370 *
371 * @param skin the PSML Skin object
372 * @return the new PortletSkin object
373 */
374 public PortletSkin getSkin(Skin skin)
375 {
376 PortletSkin result = null;
377 String name = null;
378
379 if (skin != null)
380 {
381 name = skin.getName();
382
383
384 result = getSkin(name);
385
386
387 result.putAll(getParameters(skin));
388
389 }
390
391 return result;
392 }
393
394 /***
395 * Creates a PortletSet from a PSML portlets description
396 *
397 * @param portlets the PSML portlet set description
398 * @return a new instance of PortletSet
399 */
400 public PortletSet getSet(Portlets portlets)
401 {
402 VariableInteger lastID = new VariableInteger(0);
403 return getSet(portlets, new VariableInteger(0));
404 }
405
406 /***
407 * Creates a PortletSet from a PSML portlets description, updating
408 * the portletset name based on its position within the tree
409 *
410 * @param portlets the PSML portlet set description
411 * @param count the portletset number within the complete tree
412 * @return a new instance of PortletSet
413 */
414 protected PortletSet getSet(Portlets portlets, VariableInteger theCount)
415 {
416
417 BasePortletSet set = new BasePortletSet();
418 PortletController controller = getController(portlets.getController());
419 set.setController(controller);
420 String name = portlets.getName();
421 if (name != null)
422 {
423 set.setName(name);
424 }
425 else
426 set.setName(String.valueOf(theCount.getValue()));
427
428 set.setID(portlets.getId());
429
430 theCount.setValue(theCount.getValue() + 1);
431
432
433
434
435
436 set.setPortletConfig(getPortletConfig(portlets));
437
438
439
440
441
442 for (Iterator it = portlets.getPortletsIterator(); it.hasNext();)
443 {
444 Portlets subset = (Portlets) it.next();
445
446 subset.setParentPortlets(portlets);
447
448 Map constraints = getParameters(subset.getLayout());
449 int position = getPosition(subset.getLayout());
450 set.addPortlet(
451 getSet(subset, theCount),
452 controller.getConstraints(constraints),
453 position);
454 }
455
456
457
458
459
460 for (Iterator eit = portlets.getEntriesIterator(); eit.hasNext();)
461 {
462 try
463 {
464
465 Entry psmlEntry = (Entry) eit.next();
466 PortletEntry entry =
467 (PortletEntry) Registry.getEntry(Registry.PORTLET, psmlEntry.getParent());
468
469 if (entry != null)
470 {
471 Portlet p = PortletFactory.getPortlet(psmlEntry);
472
473 if (p != null)
474 {
475 Map constraints = getParameters(psmlEntry.getLayout());
476 int position = getPosition(psmlEntry.getLayout());
477
478 PortletControl control = getControl(psmlEntry.getControl(), entry);
479
480 set.addPortlet(
481 initControl(control, p),
482 controller.getConstraints(constraints),
483 position);
484 }
485 }
486 else
487 {
488 logger.error(
489 " The portlet "
490 + psmlEntry.getParent()
491 + " does not exist in the Registry ");
492 continue;
493 }
494 }
495 catch (JetspeedException e)
496 {
497 logger.error("Exception", e);
498 continue;
499 }
500
501 }
502
503
504 if (portlets.getControl() != null)
505 {
506 PortletControl control = getControl(portlets.getControl());
507 return initControl(control, set);
508 }
509
510 set.sortPortletSet();
511
512 return set;
513 }
514
515 /***
516 * Associates a PortletControl with an existing Portlet and
517 * returns the Control
518 *
519 * @param pc the existing PortletControl
520 * @param portlet the existing Portlet to be associated with the control
521 * @return first PortletControl associated with the portlet
522 */
523 protected PortletControl initControl(PortletControl pc, Portlet portlet)
524 {
525
526 if (portlet == null)
527 {
528 throw new IllegalArgumentException("Portlet not specified");
529 }
530
531 if (pc == null)
532 {
533 throw new IllegalArgumentException("PortletControl not specified");
534 }
535
536 pc.init(portlet);
537
538 return pc;
539
540 }
541
542 /***
543 Given a PSML Portlets, get the value of what its PortletConfig would be.
544
545 @param entry the Portlets containing the config
546 @return the newly created PortletConfig object
547 */
548 protected PortletConfig getPortletConfig(Portlets portlets)
549 {
550
551 PortletConfig pc = new BasePortletConfig();
552
553 pc.setName(portlets.getName());
554 pc.setInitParameters(getParameters(portlets));
555
556
557 pc.setPortletSkin(getSkin(findSkin(portlets)));
558
559 pc.setSecurityRef(portlets.getSecurityRef());
560 pc.setMetainfo(getMetaData(portlets));
561
562 return pc;
563 }
564
565 /***
566 * Fetches the parameters out of a PSML Portlets entry
567 *
568 * @param portlets the Portlets entry to check for parameters
569 * @return a Map containing the parameters names/values, an empty Dictionary
570 * is returned if there are no parameters
571 */
572 protected static Map getParameters(Portlets portlets)
573 {
574 Hashtable hash = new Hashtable();
575
576 if (portlets != null)
577 {
578 Parameter[] props = portlets.getParameter();
579
580 for (int i = 0; i < props.length; ++i)
581 {
582 hash.put(props[i].getName(), props[i].getValue());
583 }
584 }
585
586 return hash;
587 }
588
589 /***
590 * Retrieves the parameters from a PSML Control object
591 *
592 * @param control the PSML object to explore
593 * @return a Map of the existing control parameters or an empty map
594 */
595 protected static Map getParameters(Control control)
596 {
597 Hashtable hash = new Hashtable();
598
599 if (control != null)
600 {
601 Parameter[] params = control.getParameter();
602
603 for (int i = 0; i < params.length; i++)
604 {
605 hash.put(params[i].getName(), params[i].getValue());
606 }
607 }
608 return hash;
609 }
610
611 /***
612 * Retrieves the parameters from a PSML Controller object
613 *
614 * @param controller the PSML object to explore
615 * @return a Map of the existing controller parameters or an empty map
616 */
617 protected static Map getParameters(Controller controller)
618 {
619 Hashtable hash = new Hashtable();
620
621 if (controller != null)
622 {
623 Parameter[] params = controller.getParameter();
624
625 for (int i = 0; i < params.length; i++)
626 {
627 hash.put(params[i].getName(), params[i].getValue());
628 }
629 }
630 return hash;
631 }
632
633 /***
634 * Retrieves a parameter Map from an array of PSML Layout object
635 *
636 * @param layout the Layout object to use
637 * @return a Map containing the names/values, an empty map
638 * is returned if there are no properties
639 */
640 protected static Map getParameters(Layout layout)
641 {
642 Hashtable hash = new Hashtable();
643
644 if (layout != null)
645 {
646 Parameter[] props = layout.getParameter();
647
648 for (int i = 0; i < props.length; ++i)
649 {
650 hash.put(props[i].getName(), props[i].getValue());
651 }
652 }
653
654 return hash;
655 }
656
657 /***
658 * Retrieves a parameter Map from a PSML skin object
659 *
660 * @param skin the Skin object to use
661 * @return a Map containing the names/values, an empty map
662 * is returned if there are no properties
663 */
664 protected static Map getParameters(Skin skin)
665 {
666 Hashtable hash = new Hashtable();
667
668 if (skin != null)
669 {
670 Parameter[] props = skin.getParameter();
671
672 for (int i = 0; i < props.length; ++i)
673 {
674 hash.put(props[i].getName(), props[i].getValue());
675 }
676 }
677
678 return hash;
679 }
680
681 /***
682 Create a MetaData object from a PSML Metainfo object
683
684 @param meta the Metainfo to copy
685
686 @return the new MetaData object, empty if meta is null
687 */
688 protected static MetaData getMetaData(Portlets portlets)
689 {
690 MetaData data = new MetaData();
691 MetaInfo meta = portlets.getMetaInfo();
692
693 if (meta != null)
694 {
695 if (meta.getTitle() != null)
696 {
697 data.setTitle(meta.getTitle());
698 }
699
700 if (meta.getDescription() != null)
701 {
702 data.setDescription(meta.getDescription());
703 }
704
705 if (meta.getImage() != null)
706 {
707 data.setImage(meta.getImage());
708 }
709 }
710
711 return data;
712
713 }
714
715 /***
716 * Get the position value in a Layout object
717 *
718 * @param layout the Layout object to use
719 *
720 * @return the defined position or -1 if undefined
721 */
722 protected static int getPosition(Layout layout)
723 {
724 int pos = -1;
725
726 try
727 {
728 pos = (int) layout.getPosition();
729 }
730 catch (RuntimeException e)
731 {
732
733
734 }
735
736 return pos;
737 }
738
739 protected static class VariableInteger
740 {
741 int value;
742
743 public VariableInteger(int value)
744 {
745 this.value = value;
746 }
747
748 public int getValue()
749 {
750 return this.value;
751 }
752
753 public void setValue(int value)
754 {
755 this.value = value;
756 }
757 }
758
759 /***
760 * Given a locator String path, returns a Portlets collecton
761 *
762 * @param locatorPath ProfileLocator resource path identifier
763 * @return a portlets collection from the PSML resource
764 */
765 public Portlets getReference(String locatorPath)
766 {
767 ProfileLocator locator = Profiler.createLocator();
768 locator.createFromPath(locatorPath);
769 String id = locator.getId();
770
771 try
772 {
773 Profile profile = Profiler.getProfile(locator);
774 PSMLDocument doc = profile.getDocument();
775 if (doc == null)
776 {
777 return null;
778 }
779 Portlets portlets = doc.getPortlets();
780 return portlets;
781 }
782 catch (Exception e)
783 {
784 logger.error("Exception", e);
785 return null;
786 }
787 }
788
789 /***
790 * Helps locate a skin, recursively if neccesary.
791 * <ul>
792 * <li>First: return the name of the skin defined for this <code>Portlets</code>
793 * collection.</li>
794 * <li> If the this <code>Portlets</code> collection has no skin defined, it's
795 * parent is checked, then it's parent's parent and so on until either a skin
796 * is found.</li>
797 * <li> If the previous two attempts fail the, the system default skin is used</li>
798 * @param Portlets portlets Portlets collection whose skin needs to be located.
799 */
800 protected String findSkin(Portlets portlets)
801 {
802 if (portlets.getSkin() != null)
803 {
804 return portlets.getSkin().getName();
805 }
806 else if (portlets.getParentPortlets() != null)
807 {
808 return findSkin(portlets.getParentPortlets());
809 }
810 else
811 {
812 return this.defaultSkin;
813 }
814 }
815
816 /***
817 * Gets default security ref based on the profile type (user|role|group). Returns
818 * null if no default is defined.
819 *
820 * @param profile
821 * @return default security reference
822 */
823 public SecurityReference getDefaultSecurityRef(Profile profile)
824 {
825 String type = null;
826 if (profile.getUserName() != null)
827 {
828 if (profile.getAnonymous())
829 {
830 type = Profiler.PARAM_ANON;
831 }
832 else
833 {
834 type = Profiler.PARAM_USER;
835 }
836 }
837 else if (profile.getRoleName() != null)
838 {
839 type = Profiler.PARAM_ROLE;
840 }
841 else if (profile.getGroupName() != null)
842 {
843 type = Profiler.PARAM_GROUP;
844 }
845
846 return getDefaultSecurityRef(type);
847
848 }
849
850 /***
851 * Gets default security ref based on the profile type (user|role|group). Returns
852 * null if no default is defined.
853 *
854 * @param type of entity to return default security ref for
855 * @return default security reference
856 */
857 public SecurityReference getDefaultSecurityRef(String type)
858 {
859 BaseSecurityReference result = null;
860
861 SecurityEntry entry = null;
862
863 String defaultRef = null;
864 if (type.equals(Profiler.PARAM_USER))
865 {
866 defaultRef = this.defaultUserSecurityRef;
867 }
868 else if (type.equals(Profiler.PARAM_ANON))
869 {
870 defaultRef = this.defaultAnonSecurityRef;
871 }
872 else if (type.equals(Profiler.PARAM_ROLE))
873 {
874 defaultRef = this.defaultRoleSecurityRef;
875 }
876 else if (type.equals(Profiler.PARAM_GROUP))
877 {
878 defaultRef = this.defaultGroupSecurityRef;
879 }
880
881 if (defaultRef != null)
882 {
883 entry = (SecurityEntry) Registry.getEntry(Registry.SECURITY, defaultRef);
884 if (logger.isDebugEnabled())
885 {
886 logger.debug(
887 "JetspeedPortalToolkit: default security for type: " + type + " is " + defaultRef);
888 }
889 if (entry != null)
890 {
891 result = new BaseSecurityReference();
892 result.setParent(entry.getName());
893 if (logger.isDebugEnabled())
894 {
895 logger.debug(
896 "JetspeedPortalToolkit: default security for type: "
897 + type
898 + " was set to "
899 + entry.getName());
900 }
901 }
902 }
903
904 return result;
905
906 }
907 }