The Util API contains common utility classes and methods that most of the time requires single call to achieve its goal.
General
To use smart-cloud-util components, create Maven project with the pom such as the following example:
Unresolved include directive in modules/6.x.x/pages/frameworks/jk-framework/jk-framework-core/tutorial.adoc - include::inc/pom.xml[]
Logging
This section includes the logging description in the framework.
Always consider adding logging to classes that could require debugging or monitoring. |
Basic Usage
You can use logging by
public class YourClass{
JKLogger logger=JKLoggerFactory.getLogger(getClass());
...
public void method(String param1){
logger.info("Calling method1() with param {}",param1);
....
...
logger.debug("End of method1()");
}
Note that the "{}" braces is used as placeholders for variables, and they are served by order
Recommendations
It is recommended to use logging as follows:
-
Public method: logger.info(); only once on every method
-
Non-public methods: logger.debug()
-
Most of the time you will not need to use logger.error() since it will be used internally by the framework in case of exceptions or failure.
-
It could be a good idea to have logger.debug at the end of each public methods for later performance tuning.
Logging Config File
A default logging configuration as available out of the box in the framework, and includes the following contents:
Unresolved include directive in modules/6.x.x/pages/frameworks/jk-framework/jk-framework-core/logging.adoc - include::../_inc/logback.xml[]
For having custom logging configuration, create src/main/resources/logback.xml file which shall contain your custom configurations.
For unit testing, the logback file name should be named logback-test.xml. |
Exception Handling
Exception handling in Smart-Cloud framework is implemented based on my article Exception Handling in Real-Life Java Applications published on DZone.com.
Exception Handing Bullet Points:
-
Always have only one catch statement
-
Never catch Runtime exception
-
In the catch statement just call JK.handle(e); the framework will handle the rest.
-
If you are handling an exception inside method that should return value, it is safe to return null, check the following example.
Example 1:
package com.app;
import com.jk.util.JK;
public class ExceptionExample1 {
public static void main(String[] args) {
//Throw nullpointer exception and handle with default handler, which log the error then through runtime exception
try {
String name = null;
name.length();
} catch (Exception e) {
JK.handle(e);
}
}
}
Custom Exceptions Handling
-
Create Custom Handler
package com.app; import javax.swing.JOptionPane; import com.jk.util.exceptions.handler.ExceptionHandler; import com.jk.util.exceptions.handler.JKExceptionHandler; @ExceptionHandler public class ArrayIndexExceptionHandler implements JKExceptionHandler<ArrayIndexOutOfBoundsException> { @Override public void handle(ArrayIndexOutOfBoundsException throwable, boolean throwRuntimeException) { JOptionPane.showMessageDialog(null, "My ArrayIndexException Handler"); } }
-
Register Handler:
package com.app;
import com.jk.util.JK;
import com.jk.util.exceptions.handler.JKExceptionHandlerFactory;
public class ExceptionExample2 {
public static void main(String[] args) {
JKExceptionHandlerFactory.getInstance().registerHandlers("com.app");
//Throw nullpointer exception and handle with custom handler, which show the message in JOptionPane
try {
int arr[]=new int[0];
int x=arr[1];
} catch (Exception e) {
JK.handle(e);
}
}
}
For more details, check the DZone article at: https://dzone.com/articles/exception-handling-in-real-life-applications |
Caching
Unresolved include directive in modules/6.x.x/pages/frameworks/jk-framework/jk-framework-core/cache.adoc - include::inc/src/main/java/com/app/cache/JKCacheExample.java[]
Code Refactoring
-
JK.fixMe(); to remember back to code block that need to be fixed later.
-
JK.implementMe(); to throw an exception when called reminding you to fix ASAP.
Validation
-
JKValidationUtil
-
isBoolean
-
isDouble
-
isEmpty
-
isFloat
-
isInt
-
isInteger.
-
isUpperCase
-
-
JK.validateNull("Variable Name", variable);
Remote Reflection
Remote Reflection is a Java API which make it easier to call remote methods on remote JVM in very simple way without the need for any special configurations or common contract between client and server.
Test Service
package com.app.server;
public class TestService {
public String hello(String name) {
return "Hello " + name + " from server";
}
}
Server
In the server application(the application which you want to expose its method remotely) , add the following after at the end of you main method:
package com.app.server;
import com.jk.util.reflection.server.ReflectionServer;
public class ServiceServer {
public static void main(String[] args) {
int port=7125;
ReflectionServer server = new ReflectionServer(port);
server.start();
}
}
Thats it , now you can expose any method inside this application VM to your application client.
Client
In the client application(the application which should consume the remote method):
package com.app.client;
import com.jk.util.reflection.client.ReflectionClient;
import com.jk.util.reflection.common.MethodCallInfo;
public class ServiceClient {
public static void main(String[] args) {
ReflectionClient client=new ReflectionClient("localhost", 7125);
MethodCallInfo info=new MethodCallInfo("com.app.server.TestService", "hello", "Jalal");
client.callMethod(info);
String result = (String) info.getResult();
System.out.println(result);
}
}
Git Integration
JKGitWrapper is a full wrapper that enables full communication with a Git repository.
Full example can be found below.
String url = "Remote Repository URL";
String userName = "UserName";
String password = "Password";
String localPath = "local-source-path";
JKGitWrapper gw = new JKGitWrapper();
gw.url(url).user(userName).password(password).localPath(localPath);
Git git = gw.cloneRepo();
JKIOUtil.createDirectory(gw.getLocalPath(), "dir1");
JKIOUtil.createDirectory(gw.getLocalPath(), "dir2");
JKIOUtil.createDirectory(gw.getLocalPath(), "dir2/dir2-1");
JKIOUtil.createDirectory(gw.getLocalPath(), "dir3");
JKIOUtil.writeDataToFile("Hello from uncle Jalal0", new File(gw.getLocalPath(), "dir1/test.txt"));
JKIOUtil.writeDataToFile("Hello from uncle Jalal1", new File(gw.getLocalPath(), "dir2/test.txt"));
JKIOUtil.writeDataToFile("Hello from uncle Jalal2", new File(gw.getLocalPath(), "dir2/dir2-1/test.txt"));
JKIOUtil.writeDataToFile("Hello from uncle Jalal3", new File(gw.getLocalPath(), "dir3/test.txt"));
gw.add(git);
gw.commit(git);
gw.push(git);