Thursday, 5 January 2017

Overriding classes and properties using OSGi - DXP...

Here i have published all possible ways to override Liferay classes and properties for which we were using hook in older Liferay version.


1) Create Custom portlet data handler

 @Component(
        property = {
            "javax.portlet.name=com.sample.portlet"
        },
        service = PortletDataHandler.class
    )
public class SamplePortletDataHandler extends BasePortletDataHandler
{
// Override methods which you required to override

2) Create Custom Model Listener

 @Component(
        immediate = true,
        service = ModelListener.class
    )
public class LayoutModelListener extends BaseModelListener<Layout>
{
 // Override methods which you required
}

4) Custom Action

@Component(
        property = {
            "javax.portlet.name=" + PollsPortletKeys.POLLS,
            "javax.portlet.name=" + PollsPortletKeys.POLLS_DISPLAY,
            "mvc.command.name=/polls/edit_question"
        },
        service = MVCActionCommand.class
    )
public class CustomMVCActionCommand  extends BaseMVCActionCommand
{
 // Override methods which you required
}

5) Custom Render

@Component(
        property = {
            "javax.portlet.name=" + PollsPortletKeys.POLLS_DISPLAY,
            "mvc.command.name=/polls_display/view"
        },
        service = MVCRenderCommand.class
)
public class CustomActionMVCRenderCommand implements MVCRenderCommand
{
 // Override methods which you required
}

6) Override Liferay Language Properties

@Component(
        property = { "language.id=en_US" },
        service = ResourceBundle.class
    )
public class ENResourceBundleComponent  extends ResourceBundle {

    @Override
    protected Object handleGetObject(String key) {
        return _resourceBundle.getObject(key);
    }

    @Override
    public Enumeration<String> getKeys() {
        return _resourceBundle.getKeys();
    }

    private final ResourceBundle _resourceBundle = ResourceBundle.getBundle(
        "content.Language", UTF8Control.INSTANCE);
}

7) Override Liferay Module Language Properties

@Component(
    immediate = true,
    property = {
        "bundle.symbolic.name=com.liferay.polls.web",
        "resource.bundle.base.name=content.Language",
        "servlet.context.name=polls-web"
    }
)
public class ResourceBundleLoaderComponent  implements ResourceBundleLoader {

    @Override
    public ResourceBundle loadResourceBundle(String languageId) {
        return _resourceBundleLoader.loadResourceBundle(languageId);
    }

    @Reference(target = "(bundle.symbolic.name=com.liferay.polls.web)")
    public void setResourceBundleLoader(
        ResourceBundleLoader resourceBundleLoader) {

        _resourceBundleLoader = new AggregateResourceBundleLoader(
            new CacheResourceBundleLoader(
                new ClassResourceBundleLoader(
                    "content.Language",
                    ResourceBundleLoaderComponent.class.getClassLoader())),
            resourceBundleLoader);
    }

    private AggregateResourceBundleLoader _resourceBundleLoader;

}


8) Override Liferay services

@Component(service = ServiceWrapper.class)
public class CustomUserLocalService extends UserLocalServiceWrapper {
// Override methods which you require   
}

 Note:

Portal-ext.prioperties cannot be override through OSGi and you will have to manually update portal-ext.properties which we are keeping inside WEB-INF/classes