Showing posts with label Liferay 7 OSGi. Show all posts
Showing posts with label Liferay 7 OSGi. Show all posts

Saturday, October 7, 2017

Apache Blueprint Introduction

Apache Blueprint is container specification, which defines the dependency injection for OSGi. It is a dependency injection framework for OSGi bundle development.

Liferay 7/DXP came up with OSGi support. We can use Apache Blueprint framework to develop Liferay Application OSGi bundles. We can use either OSGi Declarative Services (DS) or Apache Blueprint to develop Liferay bundles. This article gives basic idea of Apache Blueprint framework.

We already know Declarative Services in OSGi. Apache Blueprint is similar to DS. Blueprint is enable component services make it available and unavailable at any point of time. Apache Blueprint based on OSGi Compendium R4.2 specifications.


Apache Blueprint is container specification based on OSGi extender pattern. Extender pattern is very used pattern in OSGi environment to extend semantics of bundle by adding new MANIFEST headers. To add addition features to existing bundle nature, we will use OSGi extender pattern. Best example is OSGi Declarative Services. OSGi Declarative Services used extender pattern so that it will provide dynamic nature to the components and it services.

Apache Blueprint is used extender pattern as well.  Apache Blue print extender bundles monitor the bundles state in the framework and perform the action on behalf of the bundle based on their actual state.

Extender bundle is responsible to identify the bundle is blueprint bundle or not, once the bundle activated the OSGi container. Extender bundle will look for the Blueprint xml files in the bundle. If any bundle have Blueprint xml files then it will treat bundle as blue print framework bundle. Usually blueprint xml files located in the fixed location in the place OSGI-INF/blueprint/ directory or are specified explicitly in the Bundle-Blueprint manifest header.

Once bundle is blueprint bundle, then it will create the Blueprint container for the bundle and it is responsible for following actions. These actions are similar to Spring Framework Dependency Injection. If we are familiar with Spring framework then it will be easy to understand OSGi dependency Injection through Blueprint framework.

XML file parsing
Instantiate the components
Wiring the component together
Registering the services
Looking up service references

Blueprint XML Configuration

Blueprint framework uses the XML configurations to declare beans and its wiring. Blueprint container read this xml configuration file to instantiate and wiring the beans. Blueprint is top-level xml tag.

The following is basic declaration which specify the blueprint configuration


<?xml version="1.0" encoding="UTF-8"?>
<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0">
...
</
blueprint>


The XML namespace identifies the document as conforming to the Blueprint version 1.0.0. The top-level blueprint element identifies the document as a blueprint module definition.

Bean Declaration

Blueprint uses the bean tag to declare beans. Bean tag have id attribute, which is unique identification for bean.

The following is example of bean declaration.


<?xml version="1.0" encoding="UTF-8"?>
  <blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0">
  <bean id="helloServiceOne" 
class="com.liferaysavvy.blueprint.HelloService" />
</blueprint>


Bean Constriction

Blueprint container will look for right constrictor to instantiate the beans. This information provided as bean attributes or its child tags. Instantiation can be happen with the constrictor or factory method. By default, the Blueprint Container uses the number and order of the argument elements in XML to find the right constructor or method.

To help the Blueprint Container pick the right constructor, method, or parameter arrangement, additional attributes, such as index or type, can be specified on the argument element. For example, the type attribute specifies a class name used to match the argument element to a parameter by the exact type.

Beans instantiation with constructor

The following example for bean instantiation with constructor

Java Class


public class HelloService {
    public HelloService(long number) {
    }
}


Blueprint XML configuration


<bean id=" helloServiceOne" 
class="com.liferaysavvy.blueprint.HelloService">
  <argument value="1"/>
</bean>


Beans instantiation with factory method.

Bean construction can be happen through the static factory method as well. We will use bean factory-method attribute so that Blueprint container identify it and it will instantiate bean by using factory method.

The following is example for Beans instantiation with factory method.

Java Class

 
public class StaticHelloServiceFactory {
    public static HelloService createHelloService(long number) {
        return new HelloService(number);
    }
}
 

Blueprint XML configuration


<bean id="helloServiceTwo" 
class="com.liferaysavvy.blueprint.StaticHelloServiceFactory" 
factory-method="createHelloService">
  <argument value="2"/>
</bean>
 

Bean Properties

Blueprint have property tag to provide additional configuration to the bean and the container will use it whenever bean required.

The following is example to access and declare the properties for the beans.

Java class


public class HelloService {
    public HelloService(long number) {
    }
    public String getMessage() {
    }
}


Blueprint XML configuration


<bean id="helloServiceOne" 
class="com.liferaysavvy.blueprint.HelloService">
  <argument value="1"/>
  <property name="message" value="Hi I am Hello Service"/>
</bean>


Bean Wiring

Sometimes one bean dependence on other bean. We should wire the bean into the current bean. Property injection is used for wiring beans together. In the following example, helloServiceOne is injected with a DependencyService bean.

Java Classes


public class HelloService {
    public HelloService() {
    }
    public void setDependencyService (Currency currency) {
    }
}

public class DependencyService  {
    public DependencyService () {
    }
}


Blueprint XML configuration


<bean id="helloServiceOne" 
class="com.liferaysavvy.blueprint.HelloService">
  <property name="dependencyService" ref="dependencyService" />
  </bean>
  <bean id="dependencyService" 
class="com.liferaysavvy.blueprint.DependencyService" />


Services

Blueprint framework provide the configuration to register service in OSGi service registry. Blueprint xml have service tag it will register the declared services. Service implementation will be referred through ref attribute.

Service Interface

Service Interface container set of services and its simple java interface.

 
public interface HelloService {
    public String sayHello();
}


Service Implementation

Service implementation provided the implementation to the service interface.


public class HelloServiceImpl implements HelloService {
    public HelloServiceImpl () {
    }
    public void sayHello() {
    }
}


Blueprint XML configuration


<service id="helloServiceOne" ref="helloServiceImpl" 
interface="com.liferaysavvy.blueprint.api.HelloService"/>
<bean id="helloServiceImpl" 
class="com.liferaysavvy.blueprint.impl.HelloServiceImpl"/>


Without using ref attribute we can also declared service implementation as inline to the service declaration


<service id="helloServiceTwo"  
interface="com.liferaysavvy.blueprint.api.HelloService">
<bean class="com.liferaysavvy.blueprint.impl.HelloServiceImpl" />
</service>


The interfaces under which a service is registered can be determined by Blueprint using auto-export. The following registers the service under all the bean's interfaces


<service id="helloServiceOne" ref="helloServiceImpl" 
auto-export="interfaces" />
<bean id="helloServiceImpl" 
class="com.liferaysavvy.blueprint.impl.HelloServiceImpl"/>


Note:

Other values for auto-export are disabled (the default) class-hierarchy and all-classes.

Service Properties

We also configure requires properties for the service using service-properties tag. This service-properties tag contains multiple entry tags to declared properties and it has key and type as attributes. The service property values can be of different types, but only OSGi service property types are permitted: primitives, primitive wrapper classes, collections, or arrays of primitive types.

The following is example for property declaration


<service id="helloService" ref="helloServiceImpl" 
autoExport="all-classes">
  <service-properties>
    <entry key="active">
        <value type="java.lang.Boolean">true</value>
    </entry>
    <entry key="message" value="say hi"/>
  </service-properties>
</service>


Service Ranking

When one service have multiple implementation then service ranking used to choose the matching service implementation. The default ranking value is 0. Service ranking is specified using the ranking attributes as follows:

   
<service id="serviceFive" ref="account" auto-export="all-classes"
  ranking="3" />


Service Reference
Services are found in the OSGi service registry using the reference element from the Blueprint xml file. The following configuration reference tag referencing the HelloService. If this service found in the OSGi registry then it will be set in the HelloServiceClient.

Blueprint Service Reference XML configuration


<bean id="helloworldClient" 
class="com.liferaysavvy.blueprint.client.HelloServiceClient">
  <property name="helloService" ref="helloServiceRef" />
</bean>
<reference id="helloServiceRef" 
interface="com.liferaysavvy.blueprint.api.HelloService"/>


Blueprint Implementation Example

Usually OSGi service implementation have their basic bundles. Service API, Service API Provider and Service Client.

Follow below articles


Service API

Service API consist set of services. It means simple interfaces and its abstract methods. These we will make it as bundle. Usually we use export mechanism to make these interfaces available to other bundles.

Hello Service Interface

 
public interface HelloService {
    public String sayHello();
}


Service API Provider

Service Provider bundle usually implement the services defined in the Service API bundle.

Hello Service Implementation


public class HelloServiceImpl implements HelloService {
    public String sayHello(){ 
    }
}


XML configuration


<?xml version="1.0" encoding="UTF-8"?>
<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0">
<service id="helloServiceOne" ref="helloServiceImpl" 
interface="com.liferaysavvy.blueprint.api.HelloService"/>
<bean id="helloServiceImpl" 
class="com.liferaysavvy.blueprint.impl.HelloServiceImpl"/>
</blueprint>


Service Client

Service Client is other bundle, which use the services provided by Service Provider bundles.
 
public class HelloServiceClient {
    HelloService helloService = null;
    public HelloService getHelloService() {
        return helloService
    }
    public HelloService setHelloService(HelloService helloService) {
        this.helloService = helloService;
    }
}


XML configuration

 
<?xml version="1.0" encoding="UTF-8"?>
<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0">
<bean id="helloworldClient" 
class="com.liferaysavvy.blueprint.client.HelloServiceClient">
<property name="helloService" ref="helloServiceRef" />
</bean>
<reference id="helloServiceRef" 
interface="com.liferaysavvy.blueprint.api.HelloService"/>
</blueprint>



Author

Thursday, October 5, 2017

Differences between Liferay 7 / DXP and Liferay 6.2

Liferay 7 / DXP have many new features when compare with its previous versions. The following are some of differences between Liferay 7/DXP and Liferay 6.2. There are many changes can find, following are major changes we can find in Liferay 7 / DXP




Liferay 7 support modularity development using OSGi framework.


Liferay 6.2 partially support and it’s an experiment and it need more effort to achieve the modularity


Liferay 7 support Bootstrap 3 UI framework.


Support Bootstrap 2 UI framework.


AUI 3.x components are used.


AUI 2.x components are used.


AUI components built using JQuery Framework


AUI components built using YUI framework


Liferay Application Development based on OSGi bundles


Liferay Application Development based on Plugins


Liferay 7 EE called Liferay DXP


We simply call Liferay 6.2 EE


Liferay Applications packaged as OSGi bundle jar files and deployed in OSGi container. It support WAR deployment as well.


Liferay Applications packaged as war files and deployed in traditional application server.


User interface improved more and portal look and feel completely changed.


Liferay 6.2 look and feel improved when compare with its previous versions


Liferay 7 separate core portal and its default portlets. Core portal only have core features rest of features made it as portlets and packaged as bundles. Core portal files in ROOT directory deployed in Application Server. All portlet bundles available in data/OSGI directory. It means bundles deployed in OSGi container.


Liferay 6.2 core portal and its default portlets all available in ROOT directory and deployed in Application Server.


We can easily deactivate default portlets if we are not using. We simply deactivate portlet bundle.


We cannot deactivate without change in the code.


Liferay 7 Applications call it as OSGi bundles. Customization and adding new features developed using bundles.


Liferay 6.2 Application Development using Plugins and we have different types of plugins portlet, hook, theme, layout and webs.


Liferay 7 inbuilt search engine Elastic Search. It support SOLR search as well.


Liferay 6.2 in built search engine Apache Lucene. Liferay 6.2 have SOLR web to enable SOLR search.


Liferay 7 removed inbuilt database configuration for Proprietary Databases like Oracle. It support Community databases like MySQL, Hypersonic SQL and MariaDB.


Its support all types of databases like Oracle, MySQL, DB2 and Sybase.


Liferay 7 have Single Page Application inbuilt support using Senna.js


Liferay 6.2 does not have inbuilt Single Page Application support but we can integrate SPA framework to achieve it.


Liferay 7 have introduced Clay and Lexicon language to design UI. Lexicon is a design language that provides a common framework for building interfaces. Clay is web implementation of Lexicon.It's built with HTML, CSS, and Javascript with Bootstrap as a foundation


Liferay 6.2 does not have these design languages.


Liferay 7 form feature improved lot and we can design tow column forms and multiple page forms.


Liferay 6.2 forms are very basic and we cannot design two column forms.


Liferay 7 portlet can break into multiple bundles and deployed in OSGi container.


Liferay 6.2 we should put all files and configuration in one Plugin.


Liferay 7 support MAVEN, GRADLE build tools.


Liferay 6.2 support MAVEN, ANT.


Liferay IDE for Liferay 7 Application Development based on OSGi bundle development features using Bndtools framework. Bundles use MAVEN or GRADLE build tool.


Liferay IDE for Liferay 6.2 Application Development based on Plugins and we can develop different types of plugins like portlet, hook, theme, layout and web. It uses the MAVEN or ANT build tool.


Liferay 7 introduce new tool called BLADE CLI to develop Liferay 7 Application as bundles. Internally its uses the Bndtools GRADLE flavor.


Liferay 6.2 have Plugins SDK Command Line Interface to create portlet, hook, theme and layout.


Class load issues resolved before bundle activate or available in the container. We cannot see Class Not Found run time issues. All these will be resolved before bundle available for use.


We can experience many times Class Not Found issues in Liferay 6.2 Environment.


We can activate/deactivate  Liferay Application Bundles simply using Apache Gogo shell Command Line Interface without un-deploy the bundle from the container.


Liferay 6.2 it is not possible. We should un-deploy the plugins from the server so that we can remove the unused features.


Author

Recent Posts

Recent Posts Widget

Popular Posts