Liferay

Create a Custom gogo shell command in Liferay

Prishita Rojivadiya
Prishita RojivadiyaJun 18, 2024
Create a Custom gogo shell command in Liferay

Introduction:

Gogo Shell is a powerful command-line interface that facilitates the management of OSGi bundles and interaction with the runtime environment. In this blog post, we'll delve into creating a custom command for the Gogo Shell in Liferay. This allows us to extend its functionality to meet specific requirements.

Prerequisites:

  • Liferay 7.4
  • Java 11.0.18
  • Eclipse (Version 2022-03(4.23.0))
  • Liferay IDE 3.0.0 Plugin

Step 1: Project Setup

1. Create a new Liferay workspace in Eclipse IDE.

2. Create a Liferay module project of type API.

Blog Image

Now add the dependency below in the build.gradle file of this module and build it.

1compileOnly group: "org.osgi", name: "org.osgi.service.component.annotations", version: "1.3.0"
2compileOnly group: "org.apache.felix", name: "org.apache.felix.gogo.runtime", version: "1.0.2"
3compileOnly group: "com.liferay.portal", name: "com.liferay.portal.kernel", version: "2.0.0"
Blog Image

3. In this module, create a Liferay component class. Choose the Gogo command as the component class template.

Blog Image
Blog Image
  1. Package Declaration:
    1. ‘package gogo.shell.api;’ : Specifies the package to which the class belongs.
  2. Import Statements:
1import org.osgi.service.component.annotations.Reference;
2import com.liferay.portal.kernel.service.UserLocalService;
3import org.osgi.service.component.annotations.Component;
  1. Reference: For dependency injection.
  2. UserLocalService: For user-related operations.
  3. Component: For declaring the class as an OSGi component.

3. Class Declaration:

  1. ‘@Component’ : Annotates the class as an OSGi component with specified properties.
    1. immediate = true: Specifies immediate component activation.
    2. property: Declares Gogo Shell properties, such as the command scope (blade) and the function (usercount) that we create in our class.
    3. service = Object.class: Specifies the class as an OSGi service.

4. Class Members:

  1. getUserLocalService(): Getter method for accessing the injected UserLocalService.
  2. setUserLocalService(UserLocalService _userLocalService): Method annotated with @Reference for injecting UserLocalService.
  3. usercount(): Custom Gogo Shell command function that prints the number of users using getUserLocalService().getUsersCount().

Now, you have a custom command template. If you wish to use it as is, build your module and deploy it to your Liferay instance.

4. If you want to customize it, create a public Java class and write the below code in it.

1package gogo.shell.api;
2
3import org.osgi.service.component.annotations.Component;
4
5@Component(
6	immediate = true,
7	property = {
8		"osgi.command.scope=blade",
9		"osgi.command.function=greeting"
10	},
11	service = Object.class
12)
13
14public class Greetings {
15	public void greeting() {
16		System.out.println("Hello !");
17	}
18}

1. Package Declaration:

  1. package gogo.shell.api;: Specifies the package to which the class belongs.

2. Import Statements:

1import org.osgi.service.component.annotations.Component;
  1. Component : For declaring the class as an OSGi component.

3. Class Declaration:

  1. @Component: Annotates the class as an OSGi component with specified properties.
    1. immediate = true: Specifies immediate component activation.
    2. property: Declares Gogo Shell properties, such as the command scope (blade) and the function (greeting).
    3. service = Object.class: Specifies the class as an OSGi service.

4. Class Method:

  1. greeting(): Custom Gogo Shell command function that prints "Hello !" to the console.
Blog Image

Build your module and deploy it to your Liferay instance.

Step 2: Configure Gogo Shell

1. Add the following lines to Liferay’s ‘portal-ext.properties’ file and restart your server.

1module.framework.properties.osgi.console=0.0.0.0:11311
2module.framework.properties.osgi.console.use.ip=false

2. Open your terminal and execute the following commands:

1telnet localhost 11311
Blog Image

3. Now, run custom commands in the Gogo Shell:

1blade:UserCount
2blade:Greeting
Blog Image

Conclusion:

In this blog post, we have demonstrated how to create a custom command for the Gogo Shell in Liferay 7.4. By following the detailed steps, you can set up your project, create a Liferay module, and define a custom Gogo Shell command to meet your specific requirements. The ability to extend the functionality of Gogo Shell commands allows for better management of OSGi bundles and interaction with the runtime environment. With your custom command now in place, you can efficiently execute tasks and streamline your Liferay development process. Happy coding!

© 2026 IGNEK. All rights reserved.

Ignek on LinkedInIgnek on InstagramIgnek on FacebookIgnek on YouTubeIgnek on X