Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
158 changes: 148 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ JavaFF: Java Facade/Factories

We all know the Golden Object Oriented rule **Don't talk to strangers**

We all know that you will never find the API/frameworks defects or magic or limitations the begging of the development.
We all know that you will never find the API/frameworks defects or magic or limitations the at begging of the development.

So imagine you are using an API in all your projects and after spending months or years that API you got a production issue because of it even if it so famous and mature,
changing the that API would be soo hard and costy !!!
Imagine yourself using API in all your projects for years. Everybody else is using it as well, it is famous and mature. Suddenly you have a huge production issue. Changing that API is too hard and costly at this stage!

What now?

Examples for famous bugs in very famous frameworks
---------------------------------------------------
Expand All @@ -25,16 +26,153 @@ Performance
Deadlocks
- [(Oracle JDK) deadlock in SSLSocketImpl between between write and close](http://bugs.java.com/view_bug.do?bug_id=8013809)

So, we should always protect our project and noy use a framwork or API directly and this is the main idea here
So, we should always protect our project and noy use a framework or API directly and this is the main idea here

Main Features
--------------
- This project offers a standard/clear API for the most used API's in the Java Applications like : Exceptions, Locale, Beans, Formatter's, Json Handlers, Loggers, ReflectionHelpers ...etc
- **You can control the implementation's through the class path without changing line of code**
- Smart Exception handling mechanism
- Default Implementations
- Many Utilises

- This project offers a standard/clear API for the most used API's in the Java Applications like : Exceptions, Locale, Beans, Formatter's, Json Handlers, Loggers, ReflectionHelpers, etc.

- **You can control the implementations through the class path without changing line of code**
The below example shows how the implementation will be changed without changing the code:
Now I have the below dependencies in my pom.xml
```xml
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-jdk14</artifactId>
<version>${slf4.version}</version>
<scope>provided</scope>
</dependency>
```
So when I call
```javascript
LogFactory.getLogger(LogFactory.class).info("LOCALIZED_MSG", "Kiswani");
output:
Nov 22, 2016 6:54:07 PM io.github.rhkiswani.javaff.log.Slf4jLog info
INFO: this is localized msg from messages_en.properties thanks for Mr Kiswani
```

When I remove the dependencies from the pom.xml and run the same code I will get

```javascript
LogFactory.getLogger(LogFactory.class).info("LOCALIZED_MSG", "Kiswani");
output:
Nov 22, 2016 7:00:15 PM io.github.rhkiswani.javaff.log.DefaultLog info
INFO: this is localized msg from messages_en.properties thanks for Mr Kiswani
```

- Transparent localization for logs, strings, exceptions

- Centralized and Configurable Exception Handling by the class type below a full example
```java

package io.github.rhkiswani.javaff;

import io.github.rhkiswani.javaff.exceptions.ExceptionHandler;
import io.github.rhkiswani.javaff.exceptions.ExceptionHandlersFactory;
import io.github.rhkiswani.javaff.exceptions.ExceptionUtil;

public class TestMain {

public static void main(String[] args) {
//Any class is instance of ConsoleException will be handled here
ExceptionHandlersFactory.instance().add(ConsoleException.class, new ExceptionHandler() {
@Override
public void handle(Throwable t) {
System.out.println("ConsoleException handler");
}
});

//Any class is instance of MailException will be handled here
ExceptionHandlersFactory.instance().add(MailException.class, new ExceptionHandler() {
@Override
public void handle(Throwable t) {
System.out.println("MailException handler");
}
});


ExceptionUtil.handle(new ConsoleException());
ExceptionUtil.handle(new SubConsoleException());
ExceptionUtil.handle(new MailException());
ExceptionUtil.handle(new SubMailException());

//Null pointer is not related to the perilous class's it will be handled by default handler for Throwable.class
//which is painting the stack trace by default
ExceptionUtil.handle(new NullPointerException());

//We decided to override the default implantation for Throwable.class
ExceptionHandlersFactory.instance().overrideImp(Throwable.class, new ExceptionHandler() {
@Override
public void handle(Throwable t) {
System.out.println("Overridden handler");
}
});

ExceptionUtil.handle(new NullPointerException());
}

private static class ConsoleException extends RuntimeException{

}

private static class SubConsoleException extends ConsoleException{

}

private static class MailException extends RuntimeException{

}

private static class SubMailException extends MailException {

}

}

```

- Logging with localization out of the box

```java
LogFactory.getLogger(LogFactory.class).info("LOCALIZED_MSG", "Kiswani");
output : INFO: this is localized msg from messages_en.properties thanks for Mr Kiswani
```

```java
LogFactory.getLogger(LogFactory.class).info("normal msg num {0} date {1}", Integer.MAX_VALUE, new Date());
INFO: normal msg num 2,147,483,647 date 11/22/16 6:06 PM
```

- Many Utilities, below is just examples
- Formatter's
```java
System.out.println(FormatUtil.format("Mr {0} {1}", "Mohamed", "Kiswani"));
System.out.println(FormatUtil.format(new Date()));
System.out.println(FormatUtil.format(new Date(), "yyyy-MM-dd"));
System.out.println(FormatUtil.format(Integer.MAX_VALUE));

Mr Mohamed Kiswani
11/22/16 6:16 PM
2016-11-22
2,147,483,647
```

- Json Handlers
```java
System.out.println(JsonHandlerFactory.getJsonHandler(TestMain.class).toJson(new Employee(1000)));
output : {"id":0,"name":null,"empId":1000}
```
```java
System.out.println(JsonHandlerFactory.getJsonHandler(TestMain.class).fromJson("{\"id\":100,\"name\":null,\"empId\":1000}", Employee.class));
output: Employee[id=100]
```

Prerequisites
-------------
Requires JDK 1.7 or higher.
Expand Down
9 changes: 8 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,14 @@
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.21</version>
<version>${slf4.version}</version>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-jdk14</artifactId>
<version>${slf4.version}</version>
<scope>provided</scope>
</dependency>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;

/**
* @author Mohamed Kiswani
Expand Down Expand Up @@ -57,7 +58,10 @@ protected IMP_TYPE create(Class targetClass){
if (userDefaultImpl != null){
return userDefaultImpl;
}
for (Class aClass : map.keySet()) {
Set<Class> classSet = map.keySet();
Class[] keys = classSet.toArray(new Class[classSet.size()]);
for (int i = keys.length - 1 ; i >=0 ; i--){
Class aClass = keys[i];
if (aClass.isAssignableFrom(targetClass) || targetClass.isAnnotationPresent(aClass)){
return map.get(aClass);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,6 @@ public static <T>T format(Object obj, Object... params){
if (obj == null){
return null;
}
return (T) FormatFactory.getFormatter(obj.getClass()).format(String.valueOf(obj), params);
return (T) FormatFactory.getFormatter(obj.getClass()).format(obj, params);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ public class LocaleWorkersFactory extends AbstractFactory<LocaleWorker>{
private static LocaleWorkersFactory instance = new LocaleWorkersFactory();

private LocaleWorkersFactory(){
add(Throwable.class, new LocaleWorkerBuilder().path("exceptions/").build());
add(Object.class, new LocaleWorkerBuilder().path("app/").build());
add(Throwable.class, new LocaleWorkerBuilder().path("exceptions/").build());
}

public static LocaleWorkersFactory instance(){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,6 @@ protected Log getDefault(Class targetClazz) {
}

public static Log getLogger(Class aClass) {
return instance.create(aClass);
return new LogWrapper(instance.create(aClass));
}
}
58 changes: 58 additions & 0 deletions src/main/java/io/github/rhkiswani/javaff/log/LogWrapper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* Copyright 2016 Mohamed Kiswani.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.github.rhkiswani.javaff.log;

import io.github.rhkiswani.javaff.locale.LocaleUtil;

/**
* @author Mohamed Kiswani
* @since 0.0.17
*
*/
public class LogWrapper implements Log{

private final Log log;

public LogWrapper(Log log) {
this.log = log;
}

@Override
public void debug(String message, Object... params) {
log.debug(LocaleUtil.getString(message, params));
}

@Override
public void info(String message, Object... params) {
log.info(LocaleUtil.getString(message, params));
}

@Override
public void warn(String message, Object... params) {
log.warn(LocaleUtil.getString(message, params));
}

@Override
public void error(String message, Object... params) {
log.error(LocaleUtil.getString(message, params));
}

@Override
public void error(String message, Exception e, Object... params) {
log.error(LocaleUtil.getString(message, params), e);
}
}
1 change: 1 addition & 0 deletions src/main/resources/app/messages_en.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
LOCALIZED_MSG=this is localized msg from messages_en.properties thanks for Mr {0}
11 changes: 11 additions & 0 deletions src/test/java/io/github/rhkiswani/javaff/TestMain.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package io.github.rhkiswani.javaff;

public class TestMain {

public static void main(String[] args) {

}



}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,15 @@ public class Employee extends Person<Employee>{

private int empId;

public Employee() {

}

public Employee(int empId) {
super();
this.empId = empId;
}

public int getEmpId() {
return empId;
}
Expand Down
Loading