1 16 17 package org.apache.jetspeed.services.portaltoolkit; 18 19 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 84 public class JetspeedPortalToolkitService 85 extends TurbineBaseService 86 implements PortalToolkitService 87 { 88 91 private static final JetspeedLogger logger = JetspeedLogFactoryService.getLogger(JetspeedPortalToolkitService.class.getName()); 92 93 94 private String defaultControl = null; 95 96 97 private String defaultController = null; 98 99 100 private String defaultSkin = null; 101 102 103 private String defaultUserSecurityRef = null; 104 105 106 private String defaultAnonSecurityRef = null; 107 108 109 private String defaultRoleSecurityRef = null; 110 111 112 private String defaultGroupSecurityRef = null; 113 114 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 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 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 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 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 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 if (entry == null) 346 { 347 entry = (SkinEntry) Registry.getEntry(Registry.SKIN, this.defaultSkin); 348 } 349 350 if (entry != null) 351 { 352 result.setName(entry.getName()); 354 result.putAll(entry.getParameterMap()); 355 } 356 357 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 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 result = getSkin(name); 385 386 result.putAll(getParameters(skin)); 388 389 } 390 391 return result; 392 } 393 394 400 public PortletSet getSet(Portlets portlets) 401 { 402 VariableInteger lastID = new VariableInteger(0); 403 return getSet(portlets, new VariableInteger(0)); 404 } 405 406 414 protected PortletSet getSet(Portlets portlets, VariableInteger theCount) 415 { 416 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 set.setPortletConfig(getPortletConfig(portlets)); 437 438 442 for (Iterator it = portlets.getPortletsIterator(); it.hasNext();) 443 { 444 Portlets subset = (Portlets) it.next(); 445 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 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 if (portlets.getControl() != null) 505 { 506 PortletControl control = getControl(portlets.getControl()); 507 return initControl(control, set); 508 } 509 510 set.sortPortletSet(); 511 return set; 513 } 514 515 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 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 pc.setPortletSkin(getSkin(findSkin(portlets))); 558 559 pc.setSecurityRef(portlets.getSecurityRef()); 560 pc.setMetainfo(getMetaData(portlets)); 561 562 return pc; 563 } 564 565 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 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 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 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 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 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 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 } 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 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 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 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 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 } 908 | Popular Tags |