Activities

February 14
Russell Bohl updated LPS-65950.
Russell Bohl commented on LPS-65950.
Russell Bohl updated LPS-65950.
Russell Bohl commented on LPS-65950.
1:54 PM h1. Changing Default Portal Properties from a Module The easiest way to override portal properties is using a {{portal-ext.properties}} file. However, there might be a case where you want to change the default portal property value, while retaining the ability to override it through the {{portal-ext.properties}} mechanism. To override default portal properties, use a portal properties swapper module. Keep in mind that overriding default values won’t change any values that have previously been initialized. Here's how you'd override {{company.default.name}} and {{image.default.company.logo}}: 1. Create an OSGi module with the proper metadata in the {{bnd.bnd}} file: {code:java} Bundle-Name: Liferay Portal Properties Swapper Bundle-SymbolicName: com.liferay.portal.properties.swapper Bundle-Version: 1.0.0 Export-Package: com.liferay.portal.properties.swapper {code} 2. Create a java class for each of the properties you're overriding. For example: {code:java} com.liferay.portal.properties.swapper.internal.DefaultCompanyLogoSwapper.java {code} and {code:java} com.liferay.portal.properties.swapper.internal.DefaultCompanyNameSwapper.java {code} 3. Make your java classes components using the DS {{@Component}} annotation. {code:java} @Component(enabled = false, immediate = true) {code} 4. Set the appropriate property values in the activate method of each component. Since you specified {{true}} for the immediate property, activation will happen as soon as your component is introduced to the OSGi runtime (as long as your specified dependencies can be resolved): Here's the logo swapping code: {code:java} public class DefaultCompanyLogoSwapper { @Activate public void activate(BundleContext bundleContext) { Bundle bundle = bundleContext.getBundle(); String bundleIdString = String.valueOf(bundle.getBundleId()); PropsUtil.set( PropsKeys.IMAGE_DEFAULT_COMPANY_LOGO, bundleIdString + ";com/liferay/portal/properties/swapper/internal/" + "company_dxp_logo.png"); } } {code} Here's the code for changing the default company name: {code:java} public class DefaultCompanyNameSwapper { @Activate public void activate() { PropsValues.COMPANY_DEFAULT_NAME = "Liferay DXP"; } } {code} Note: Notice that these examples override the respective portal properties in two different ways: {{PropsValues}} for the default company name and {{PropsUtil.set(PropsKeys....)}} for the default company logo (note that this {{PropsUtil}} method is only available from the {{PropsUtil}} class found in the {{portal-impl}} module). If you use this technique to override some other portal property, there's no definitive way to tell which method you'll need to use without digging into the Liferay source code, because it depends on how and when the property is read. 5. Now you just need to make sure your new components are tracked and applied. In short, you need to register a {{PortalProfile}} service. Create a java class that extends {{com.liferay.portal.profile.BaseDSModulePortalProfile}}. Make it an immediate component and specify that it implements {{PortalProfile}}: {code:java} @Component(immediate = true, service = PortalProfile.class) public class ModulePortalProfile extends BaseDSModulePortalProfile { {code} 6. In the activation method of your new component class, call the {{init}} method inherited from {{BaseDSModulePortalProfile}}, and pass it the {{componentContext}}, supported portal profiles, and the components you created to swap portal properties values: {code:java} @Activate public void activate(ComponentContext componentContext) { init( componentContext, Collections.singleton(PortalProfile.PORTAL_PROFILE_NAME_DXP), DefaultCompanyLogoSwapper.class.getName(), DefaultCompanyNameSwapper.class.getName()); } {code} By registering a {{PortalProfile}} service that references the components you developed above you ensure that they're tracked and that they automatically take effect in Liferay: 7. Make sure you declare the necessary dependencies. Here's what the {{build.gradle}} for the project described here would look like: {code:java} dependencies { compileOnly group: "com.liferay.portal", name: "com.liferay.portal.impl", version: "2.0.0" compileOnly group: "com.liferay.portal", name: "com.liferay.portal.kernel", version: "2.0.0" compileOnly group: "org.osgi", name: "org.osgi.core", version: "5.0.0" compileOnly group: "org.osgi", name: "org.osgi.service.component", version: "1.3.0" compileOnly group: "org.osgi", name: "org.osgi.service.component.annotations", version: "1.3.0" compileOnly group: "com.liferay", name: "com.liferay.portal.profile", version: "1.0.2" } {code} All of the dependencies needed are available in Liferay, so you can use the {{compileOnly}} scope in grade. {{PropsKeys}} was used in setting the path to the new company logo, and comes from the {{com.liferay.portal.kernel}} dependency. {{PropsValues was used in overriding the default company name, and comes from the {{com.liferay.portal.impl}} dependency, along with {{PropsUtil}}. When you registered the {{PortalProfile}}, you subclassed {{BaseDSModulePortalProfile}} from {{com.liferay.portal.profile}}. All the annotations you used came from {{org.osgi.service.component.annotations}}, and the {{ComponentContext}} that was a parameter of the {{activate}} method in your {{PortalProfile}} service came from {{org.osgi.service.component}}. To reference the {{BundleContext}} and {{Bundle}} objects, you needed {{org.osgi.core}}.
February 8
Russell Bohl updated LPS-66721.
Russell Bohl updated LPS-66721.
Russell Bohl updated LPS-66721.
Russell Bohl updated LPS-66721.
October 13
Russell Bohl commented on LPS-65950.
Russell Bohl updated LPS-65950.
Subscribe to Russell Bohl's activities. (Opens New Window)