-
Notifications
You must be signed in to change notification settings - Fork 18
Working with filesystem
Red5 media server, other than being a media server is also a standard java (JEE) based server. Hence Red5 follows the behavioural traits of a JEE server for common server side activities including file system handling. Just as any JEE server you can work with absolute and relative paths when handling files in Red5.
Each Red5 web application by standards manages its resources within its own context scope. Mostly application resources are located either in the application’s directory or somewhere inside the WEB-INF directory. The code snippet given below shows how to resolve the Path to the application’s root directory and to WEB-INF directory.
try
{
Resource resource = this.getContext().getApplicationContext().getResource(File.separator);
System.out.println(“Application root => ” + resource.getFile().getAbsolutePath());
File web_inf_folder = new File(resource.getFile().getAbsoluteFile() + File.separator + “WEB-INF”);
System.out.println(“WEB-INF folder => ” + web_inf_folder.getAbsolutePath());
}
catch (IOException e)
{
e.printStackTrace();
}
Once the path to the application directory is resolved you can then access any resource from there-on. This is a platform-independent approach and is recommended as opposed to using hard coded paths strings in your application code.
Another entry point in path resolutions in a java (JEE) server is to make use of environment variables. Since Red5 is a tomcat based server, you can use the following snippet to locate the root of the server and from there on you can easily locate the webapps root directory which contains all your applications.
Note: This is true when you have a single tomcat / red5 installation on your system.
try
{
File file = new File(System.getProperty(“catalina.base”));
System.out.println(“Server root => ” + file.getAbsolutePath());
File webapps_folder = new File(file.getAbsoluteFile() + File.separator + “webapps”);
System.out.println(“Webapps root => ” + webapps_folder.getAbsolutePath());
}
catch (Exception e)
{
e.printStackTrace();
}
If in case you have defined the path of your red5 installation in an environment variable called RED5_HOME , you can directly access that value in your application as well.
String red5Path= System.getenv(“RED5_HOME”);
File folder = new File(red5Path);
The following snippet assumes that there is a file called sample.txt in the WEB-INF directory of the application’s root. We simply open the file for writing and overwrite the existing content with the new content and close the file.
try
{
Resource resource = this.getContext().getApplicationContext().getResource(File.separator);
System.out.println(“Application root => ” + System.getProperty(“catalina.base”));
File web_inf_folder = new File(resource.getFile().getAbsoluteFile() + File.separator + “WEB-INF”);
System.out.println(“WEB-INF folder => ” + web_inf_folder.getAbsolutePath());
String content = “My Red5 File”;
File myFile = new File(web_inf_folder.getAbsoluteFile() + File.separator + “sample.txt”);
FileWriter fw = new FileWriter(myFile.getAbsoluteFile());
BufferedWriter bw = new BufferedWriter(fw);
bw.write(content);
bw.close();
}
catch (IOException e)
{
e.printStackTrace();
}
You can write any kind of data to any kind of file extension as long as you can read it. The file type and content is not restricted to text only. For example, you can write a file with an unknown extension with text content inside it to make it a little more interesting.
The following snippet assumes that there is a file called “sample.txt” in your application’s WEB-INF directory. We first resolve the file to the application’s WEB-INF directory and then to the sample.txt file. Once the file is located we load it and display the content of the file after reading it line by line.
try
{
Resource resource = this.getContext().getApplicationContext().getResource(File.separator);
System.out.println(“Application root => ” + System.getProperty(“catalina.base”));
File web_inf_folder = new File(resource.getFile().getAbsoluteFile() + File.separator + “WEB-INF”);
System.out.println(“WEB-INF folder => ” + web_inf_folder.getAbsolutePath());
File myFile = new File(web_inf_folder.getAbsoluteFile() + File.separator + “sample.txt”);
BufferedReader br = new BufferedReader(new FileReader(myFile.getAbsoluteFile()));
try
{
StringBuilder sb = new StringBuilder();
String line = br.readLine();
while (line != null) {
sb.append(line);
sb.append(System.lineSeparator());
line = br.readLine();
}
String everything = sb.toString();
System.out.print(“=> ” + everything);
}
finally
{
br.close();
}
}
catch (IOException e)
{
e.printStackTrace();
}
While test files are good for storing simple information, it is not an ideal format for storing configuration information. Complex information can be stored in a text file as JSON with an extension of .json) or as a URL encoded string for the better. However, a better and recommended approach for configuration information is to use the properties file.
Properties file enable you to store java as key value pairs and make it easy for you to read the information into an enumerable Properties object. The snippets below will demonstrate how to efficiently store and read information such as database connection data using the properties file.
Properties prop = new Properties();
OutputStream output = null;
try
{
Resource resource = this.getContext().getApplicationContext().getResource(File.separator);
System.out.println(“Application root => ” + System.getProperty(“catalina.base”));
File web_inf_folder = new File(resource.getFile().getAbsoluteFile() + File.separator + “WEB-INF”);
System.out.println(“WEB-INF folder => ” + web_inf_folder.getAbsolutePath());
File props = new File(web_inf_folder.getAbsolutePath() + File.separator + “config.properties”);
output = new FileOutputStream(props.getAbsolutePath());
// set the properties value
prop.setProperty(“database”, “localhost”);
prop.setProperty(“dbuser”, “red5user”);
prop.setProperty(“dbpassword”, “red5pass”);
prop.store(output, null);
}
catch (IOException e)
{
e.printStackTrace();
}
finally
{
if (output != null) {
try {
output.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Properties prop = new Properties();
InputStream input = null;
try
{
Resource resource = this.getContext().getApplicationContext().getResource(File.separator);
System.out.println(“Application root => ” + System.getProperty(“catalina.base”));
File web_inf_folder = new File(resource.getFile().getAbsoluteFile() + File.separator + “WEB-INF”);
System.out.println(“WEB-INF folder => ” + web_inf_folder.getAbsolutePath());
File props = new File(web_inf_folder.getAbsolutePath() + File.separator + “config.properties”);
input = new FileInputStream(props.getAbsolutePath());
// load a properties file
prop.load(input);
// get the property value and print it out
System.out.println(prop.getProperty(“database”));
System.out.println(prop.getProperty(“dbuser”));
System.out.println(prop.getProperty(“dbpassword”));
}
catch (IOException e)
{
e.printStackTrace();
}
finally
{
if (input != null) {
try {
input.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Using the properties file for storing and fetching configuration data is recommended and matter of best practice.
You can extend and apply the concepts of file handling that we learned in this section to loading SQLLite database files, Writing images, database configurations, json files and much more. The implementation of it is a food for the curious minds.