How to access important filesystem paths of ant media server from an application or plugin?

"Only a life lived for others is a life worthwhile." – Albert Einstein

How to access important filesystem paths of ant media server from an application or plugin?

While working on Ant Media Server projects, reading & writing data from the filesystem can be a very frequent task in the workflow. It is therefore important to know how to access the various directories where configuration & other useful data is commonly stored. In this article, we will explore different ways to access important filesystem paths in Ant Media Server.

Accessing the AntMediaApplicationAdapter instance

One of the key components to having access to is the core application instance which is also represented by the AntMediaApplicationAdapter class. This instance will represent the context of the current application in question. In a webapp this can easily be accessed using the this operator of object-oriented programming. However, when your logic is inside a plugin you can access it through the plugin instance as shown below.

    AntMediaApplicationAdapter app = plugin.getApplication();

The application instance will help us gain access to various resources that are stored & retrieved in the context of the current application (for example the LiveApp).

Resolving the application’s root directory

Once you have the application instance you can use it to get the absolute application’s folder path in the server with some help from the Spring framework Java API.

    Resource resource = app.getContext().getApplicationContext().getResource(File.separator);
    String app_root = resource.getFile().getAbsolutePath();

Example: /home/user/Downloads/ant-media-server/webapps/LiveApp

Resolving the application’s WEB-INF directory

The webapp’s WEB-INF directory is the most commonly used location for application configuration and data storage. so if you have a localized database or a .properties configuration file that is applicable to the current application only, then you store it in the WEB-INF folder.

    File web_inf_folder = new File(resource.getFile().getAbsoluteFile() + File.separator + "WEB-INF");
    String web_inf_folder_path = web_inf_folder.getAbsolutePath();

Example: /home/user/Downloads/ant-media-server/webapps/LiveApp/WEB-INF

Resolving the Server’s Root Directory

The root of Ant Media Server is the top-level folder of the software dist and also the installation directory. You can find the start/shutdown scripts, the configuration directory, the plugin directory, webapps directory and more. The ant media server is based on the tomcat webserver so we can use tomcat environment variables in our ant media server java code.

To locate the server root we can use catalina.base environment variable as shown below:

    File file = new File(System.getProperty("catalina.base"));
    String catalina_root_path = file.getAbsolutePath();

Example: /home/user/Downloads/ant-media-server

Resolving the Webapps Directory

The webapps directory hosts all the ant media server applications (webapps). We can use the technique we saw previously to locate the webapps directory relative to the server root.

    File file = new File(System.getProperty("catalina.base"));    
    File webapps_folder = new File(file.getAbsoluteFile() + File.separator + "webapps");
    String webapps_folder_path = webapps_folder.getAbsolutePath();

Example: /home/user/Downloads/ant-media-server/webapps