To convert from primitive arrays into object type arrays, we can use the Apache Commons Lang library. The Commons Lang provides an ArrayUtils class that does this conversion. To convert the other way just use the toPrimitive() method.
To check if a file, or a directory exists we can utilize java.io.File.exists() method. This method return either true or false. Before we can check whether its exists or not we need to create an instance of File that represent an abstract path name of the file or directory. After we have the File instance we can call the exists() method to validate it.
package org.kodejava.io;
import java.io.File;
import java.io.FileNotFoundException;
public class FileExists {
public static void main(String[] args) throws Exception {
// Create an abstract definition of configuration file to be
// read.
File file = new File("applicationContext-hibernate.xml");
// Print the exact location of the file in file system.
System.out.println("File = " + file.getAbsolutePath());
// If configuration file, applicationContext-hibernate.xml
// does not exist in the current path throws an exception.
if (!file.exists()) {
String message = "Cannot find configuration file!";
System.out.println(message);
throw new FileNotFoundException(message);
}
// Continue with application logic here!
}
}
In the code snippet below we are going to learn how to use the System.arraycopy() method to copy array values. This method will copy array from the specified source array, starting from the element specified by starting position. The array values will be copied to the target array, placed at the specified start position in the target array and will copy values for the specified number of elements.
The code snippet below will copy 3 elements from the letters array and place it in the results array. The copy start from the third elements which is the letter U and will be place at the beginning of the target array.
package org.kodejava.lang;
public class ArrayCopy {
public static void main(String[] args) {
// Creates letters array with 5 chars inside it.
String[] letters = {"A", "I", "U", "E", "O"};
// Create an array to where we are going to copy
// some elements of the previous array.
String[] results = new String[3];
// Copy 3 characters from letters starting from the
// third element and put it inside result array
// beginning at the first element
System.arraycopy(letters, 2, results, 0, 3);
// Just print out what were got copied, it will
// contains U, E, and O
for (String result : results) {
System.out.println("result = " + result);
}
}
}
In an application we sometimes want to terminate the execution of our application, for instance because it cannot find the required resource.
To terminate it we can use exit(status) method in java.lang.System class or in the java.lang.Runtime class. When terminating an application we need to provide a status code, a non-zero status assigned for any abnormal termination.
package org.kodejava.lang;
import java.io.File;
public class AppTerminate {
public static void main(String[] args) {
File file = new File("config.xml");
int errCode = 0;
if (!file.exists()) {
errCode = 1;
}
// When the error code is not zero go terminate
if (errCode > 0) {
System.exit(errCode);
}
}
}
The call to System.exit(status) is equals to Runtime.getRuntime().exit(status). Actually the System class will delegate the termination process to the Runtime class.
Running the code give the following output because it can’t find the config.xml file.
I have created a servlet based web application, and I want to used Spring beans in it. So how do I do this in my servlet. Well, it is simple enough to do this. First, I have to obtain the Spring’s WebApplicationContext, from where I can grab the required bean by my servlet.
Let’s see some lines of code on how to do it, here we go:
Inside the Java Servlet doGet() method above I get the ServletContext, next the WebApplicationContextUtils help me to get Spring’s WebApplicationContext. With this object in my hand I can get my UserDao implementation and do a query from database.
Spring Bean Servlet Example
Here are the supporting classes and the configurations.
UserDao.java
package org.kodejava.servlet.dao;
import org.kodejava.servlet.model.User;
public interface UserDao {
User getUser(Long userId);
}
UserDaoImpl.java
package org.kodejava.servlet.dao.impl;
import org.kodejava.servlet.dao.UserDao;
import org.kodejava.servlet.model.User;
public class UserDaoImpl implements UserDao {
@Override
public User getUser(Long userId) {
if (userId == 1L) {
return new User(1L, "jdoe", "John Doe");
}
return new User();
}
}
User.java
package org.kodejava.servlet.model;
import java.util.Objects;
public class User {
private Long id;
private String username;
private String realName;
public User() {
}
public User(Long id, String username, String realName) {
this.id = id;
this.username = username;
this.realName = realName;
}
// Getters and Setters
}