26 Ekim 2021 Salı

DbUtils

Giriş
Bu kütüphanede az sınıf var. Kullanması kolay. Tasarım açısından 2 çeşit API sağlıyor
1. JDBC kaynaklarını DbUtils'in yönettiği API. Bu kullanımda Connection, PreparedStatement ve ResultSet'i kütüphane kapatır.
2. Connection'ın dışarıdan verildiği API. Bu kullanımda Connection nesnesini bizim kapatmamız gerekir.

Kötü Tasarım
Bir seferinde Connection, PreparedStatement ve ResultSet'i sarmalayıp dışarı veren bir kütüphane görmüştüm. Bu sarmalanan nesneyi kapatmayı unutunca bir sürü resource leak oluyor ve "ORA-01000: Maximum Open Cursors Exceeded" hatası geliyordu.

Yani en güzeli, kaynakları kapatma işini kütüphanenin kendisinin yapması

Maven
Şu satırı dahil ederiz
<dependency>
  <groupId>commons-dbutils</groupId>
  <artifactId>commons-dbutils</artifactId>
  <version>1.6</version>
</dependency>
QueryRunner Sınıfı
insert metodu
Örnek
Şöyle yaparız
QueryRunner runner = new QueryRunner();
    String insertSQL
      = "INSERT INTO employee (firstname,lastname,salary, hireddate) "
        + "VALUES (?, ?, ?, ?)";

int numRowsInserted = runner.update(connection, insertSQL, "Leia", "Kane", 60000.60,
  new Date());
query metodu - String sql, ResultSetHandler<T> rsh, Object... params
Şöyle yaparız
// Create a ResultSetHandler implementation to convert the first row into an Object[].
ResultSetHandler<Object[]> h = new ResultSetHandler<>() {
  public Object[] handle(ResultSet rs) throws SQLException {
    ...
  }
};

// Create a QueryRunner that will use connections from the given DataSource
QueryRunner run = new QueryRunner(dataSource);

// Execute the query and get the results back from the handler
Object[] result = run.query("SELECT * FROM Person WHERE name=?", h, "John Doe");
query metodu - Connection + String sql, ResultSetHandler<T> rsh, Object... params
Şöyle yaparız
ResultSetHandler<Object[]> h = ... // Define a handler the same as above example

// No DataSource so we must handle Connections manually
QueryRunner run = new QueryRunner();
Connection conn = ... // open a connection try { Object[] result = run.query(conn, "SELECT * FROM Person WHERE name=?", h, "John Doe");
// do something with the result } finally { // Use this helper method so we don't have to check for null DbUtils.close(conn); }
update metodu
Örnek
Şöyle yaparız
double salary = 35000;

QueryRunner runner = new QueryRunner();
String updateSQL = "UPDATE employee SET salary = salary * 1.1 WHERE salary <= ?";
int numRowsUpdated = runner.update(connection, updateSQL, salary);
BeanListHandler Sınıfı
Şu satırı dahil ederiz
import org.apache.commons.dbutils.handlers.BeanListHandler;
Örnek
Elimizde şöyle bir kod olsun
public class EmployeeHandler extends BeanListHandler<Employee> {

  public EmployeeHandler() {
    super(Employee.class,
          new BasicRowProcessor(new BeanProcessor(getColumnsToFieldsMap())));
    // ...
  }
  protected Map<String, String> getColumnsToFieldsMap() {
    Map<String, String> columnsToFieldsMap = new HashMap<>();
    columnsToFieldsMap.put("FIRST_NAME", "firstName");
    columnsToFieldsMap.put("LAST_NAME", "lastName");
    columnsToFieldsMap.put("HIRED_DATE", "hiredDate");
    return columnsToFieldsMap;
  }

}
Şöyle yaparız
EmployeeHandler employeeHandler = new EmployeeHandler();

QueryRunner runner = new QueryRunner();
String query = "SELECT * FROM employee_legacy";
List<Employee> employees = runner.query(connection, query, employeeHandler);
MapListHandler Sınıfı
Şu satırı dahil ederiz
import org.apache.commons.dbutils.handlers.MapListHandler;
Örnek
Şöyle yaparız
MapListHandler beanListHandler = new MapListHandler();

QueryRunner runner = new QueryRunner();
List<Map<String, Object>> list = runner.query(connection, 
                                               "SELECT * FROM employee", 
                                               beanListHandler);


6 Ekim 2021 Çarşamba

Apache Shiro - Role-Based Access Control (RBAC) İçindir

Giriş
Açıklaması şöyle. Framework yani çatı olarak kullanılır.
Apache Shiro is a powerful and flexible open-source security framework that cleanly handles authentication, authorization, enterprise session management and cryptography.
Şeklen şöyle

Daha gelişmiş bir şekil şöyle

Bölümlerin amacı şöyle
Authentication: Sometimes referred to as ‘login’, this is the act of proving a user is who they say they are.
Authorization: The process of access control, i.e. determining ‘who’ has access to ‘what’.
Session Management: Managing user-specific sessions, even in non-web or EJB applications.
Cryptography: Keeping data secure using cryptographic algorithms while still being easy to use.
Maven
Spring ile kullanmak için şöyle yaparız
<dependency>
    <groupId>org.apache.shiro</groupId>
    <artifactId>shiro-spring-boot-starter</artifactId>
    <version>1.8.0</version>
</dependency>
Spring ile tümleşim için bir örnek burada


26 Eylül 2021 Pazar

CXF İle SOAP

Giriş
CFX sadece bir kütüphane değil. Yanında bir sürü araçla birlikte geliyor
JAX-RS ve JAX-WS (yani SOAP) için kullanılabilir.

Örnek
Maven ile şöyle yaparız
<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-web-services</artifactId>
</dependency>
       
<dependency>
  <groupId>org.glassfish.jaxb</groupId>
  <artifactId>jaxb-runtime</artifactId>
</dependency>
       
<dependency>
  <groupId>org.apache.cxf</groupId>
  <artifactId>cxf-spring-boot-starter-jaxws</artifactId>
  <version>${cxf.version}</version>
</dependency>
<dependency>
  <groupId>org.apache.cxf</groupId>
  <artifactId>cxf-rt-features-logging</artifactId>
  <version>${cxf.version}</version>
</dependency>
Elimizde şöyle bir kod olsun
@WebService
public interface HelloWorldWS {
    @WebMethod
    String createMessage(@WebParam(name = "createMessageRequest", mode = WebParam.Mode.IN)
String name);
}

@Component
public class HelloWorldWSImpl implements HelloWorldWS{
    @Override
    public String createMessage(String name){
        return "Hello "+name;
    }
}
Şöyle yaparız. Spring uygulamasını çalıştırınca "http://localhost:8080/ws/helloWorldWS?wsdl" adresinde WSDL çıktısı görülebilir.
import javax.xml.ws.Endpoint;
import org.apache.cxf.Bus;
import org.apache.cxf.jaxws.EndpointImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;
import com.cfxconsumer.soapcxfconsumer.ws.HelloWorldWS;

@Configuration
@ImportResource({ "classpath:META-INF/cxf/cxf.xml" })
public class CxfWebServiceConfig {
  @Autowired
  private Bus cxfBus;

  @Bean
  public ServletRegistrationBean cxfServlet() {
    org.apache.cxf.transport.servlet.CXFServlet cxfServlet =
new org.apache.cxf.transport.servlet.CXFServlet();
    ServletRegistrationBean def = new ServletRegistrationBean<>(cxfServlet, "/ws/*");
    def.setLoadOnStartup(1);
    return def;
  }

  @Bean
  public Endpoint helloWorldWebService(HelloWorldWS helloWorldWS) {
    EndpointImpl endpoint = new EndpointImpl(cxfBus, helloWorldWS);
    endpoint.setAddress("/helloWorldWS");
    endpoint.publish();
    return endpoint;
  }
}


19 Eylül 2021 Pazar

Tika

Maven
Şu satırı dahil ederiz
<dependency>
  <groupId>org.apache.tika</groupId>
  <artifactId>tika-core</artifactId>
  <version>1.26</version>
</dependency>
AutoDetectParser Sınıfı
parse metodu
Örnek
Şöyle yaparız
InputStream stream = ...;

FileOutputStream fileOutputStream = ...;
ContentHandler handler = new BodyContentHandler(new WriteOutContentHandler(fileOutputStream));

Metadata metadata = new Metadata();

ParseContext context = new ParseContext();

Parser parser = new AutoDetectParser();

parser.parse(stream, handler, metadata, context);

8 Eylül 2021 Çarşamba

Lang3 StringUtils strip metodları

Giriş
strip işlemi için eğer düz Java kullanıyorsak String sınıfının replaceFirst() metodu kullanılabilir.
Örnek
Şöyle yaparız
"00000050.43".replaceFirst("^0+(?!$)", "")
Guava'nın CharMatcher sınıfı kullanılabilir.
Örnek
Şöyle yaparız
CharMatcher.is('0').trimLeadingFrom("00000050.43")
Bence en kolayı halen StringUtils sınıfı

stripEnd metodu
Sağdaki belirtilen diziye uyan karakterleri siler.
Örnek
Şöyle yaparız. null boşluk anlamına gelir.
StringUtils.stripEnd("abc  ", null)    = "abc"
Örnek
Şöyle yaparız.
private String rTrim(String str) {
  return StringUtils.stripEnd(str, /*stripChars*/" ");
}
stripStart metodu
Soldaki whitespace karakterlerini siler
Örnek
Şöyle yaparız.
private String lTrim(String str) {
  return StringUtils.stripStart(str, /*stripChars*/" ");
}
Örnek
Şöyle yaparız
StringUtils.stripStart("00000050.43","0");

13 Ağustos 2021 Cuma

CLI Option Sınıfı

setArgs metodu
Örnek
Açıklaması şöyle. Eğer seçenek birden fazla değer alıyorsa kullanılır
You have to set maximum the number of argument values the option can take, otherwise it assumes the option only has 1 argument value
Şöyle yaparız
Options options = new Options();
Option option = new Option("c", "c desc");
// Set option c to take maximum of 10 arguments
option.setArgs(10);
options.addOption(option);

3 Ağustos 2021 Salı

Camel ve SpringBoot

Giriş
Spring ile kullanmak için şu satırı dahil ederiz
<dependency>
  <groupId>org.apache.camel.springboot</groupId>
  <artifactId>camel-spring-boot-starter</artifactId>
  <version>3.4.0</version>
</dependency>
Spring + JSON kullanmak için Jackson kütüphanesini dahil etmek gerekir. Şöyle yaparız
<dependency>
  <groupId>org.apache.camel.springboot</groupId>
  <artifactId>camel-jackson-starter</artifactId>
  <version>3.8.0</version>
</dependency>
Test
Bir yazı burada

application.properties
Tüm alanlar camel.springboot.XXX şeklinde başlar

main-run-controller Alanı
Örnek
Şöyle yaparız
camel.springboot.main-run-controller=true
Açıklaması şöyle
To ensure the Spring Boot application keeps running until being stopped or the JVM terminated, typically only need when running Spring Boot standalone, i.e. not with spring-boot-starter-web when the web container keeps the JVM running, set the camel.springboot.main-run-controller=true property in your configuration.
RouteBuilder
RouteBuilder yazısına bakabilirsiniz. RouterBuilder sınıfı bir Spring component'i dir.

Örnek
Şöyle yaparız
import org.apache.camel.builder.RouteBuilder;

@SpringBootApplication
public class SpringBootWithCamelApplication extends RouteBuilder {
  public static void main(String[] args) {
    SpringApplication.run(SpringBootWithCamelApplication.class);
  }
  @Override
  public void configure() throws Exception {
    from("file:C:/test1/test.txt").
    to("file:C/test2/test.txt")
  }
}