Information
This explains basic usage of the Sakai ToolManager Service. This service is used to find out things like the current location of the current user (Site, etc.) and information about Sakai tools.
- Trunk javadocs:
- Trunk source location: https://source.sakaiproject.org/svn/user/trunk/
Accessing the ToolManager
- You can use Spring Framework to inject the service or use the cover
- Using Spring to get the service for your class (e.g. YourAppClass) (recommended)
- Add the ToolManager bean to the bean for YourAppClass
<bean id="org.sakaiproject.yourapp.logic.YourAppClass" class="org.sakaiproject.yourapp.logic.impl.YourAppClassImpl"> <property name="toolManager" ref="org.sakaiproject.tool.api.ToolManager" /> </bean>
- Add a variable and setter to YourAppClass to use the service in like so:
private ToolManager toolManager; public void setToolManager(ToolManager toolManager) { this.toolManager = toolManager; }
- Add the ToolManager bean to the bean for YourAppClass
- Using the cover to get the service
- Note: This is not the recommended method, you should be using Spring to inject the service
- Setup a variable to hold the instance from the cover
private ToolManager toolManager;
- Get access to the service using the cover
toolManager = org.sakaiproject.tool.cover.ToolManager.getInstance();
Getting the current siteId for the current thread (current user most likely)
- Use the service variable to access the service and get the current Placement
Site site = null; try { site = siteService.getSite(toolManager.getCurrentPlacement().getContext()); } catch (Exception e) { // assume we are not in a site then throw new RuntimeException("Cannot get the current site placement"); } return site.getId();
- Note: If you just need the current Placement reference (which may be the current site reference which is probably the current site Id) then you can do this:
String currentPlacementRef = toolManager.getCurrentPlacement().getContext();
- Note: If you just need the current Placement reference (which may be the current site reference which is probably the current site Id) then you can do this: