December 14, 2015

Example on SLF4J with RollingFile

pom.xml

<!-- slf4j Logger -->
<dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>1.7.13</version>
</dependency>
<dependency>
        <groupId>ch.qos.logback</groupId>
        <artifactId>logback-classic</artifactId>
        <version>1.1.3</version>
</dependency>
<dependency>
        <groupId>ch.qos.logback</groupId>
        <artifactId>logback-core</artifactId>
        <version>1.1.3</version>
</dependency>

logback.xml file

<?xml version="1.0" encoding="UTF-8"?>
<configuration>

 <!-- Send debug messages to System.out -->
 <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
  <!-- By default, encoders are assigned the type ch.qos.logback.classic.encoder.PatternLayoutEncoder -->
  <encoder>
   <Pattern>
    [%-5level] %logger:%line - %message%n
   </Pattern>
  </encoder>
 </appender>

 <!-- Send debug messages to a file at "/logs/test.log" -->
 <appender name="FILE"
  class="ch.qos.logback.core.rolling.RollingFileAppender">
  <file>logs/test.log</file>
  <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
   <Pattern>
    [%-5level] %d{yyyy-MM-dd_HH:mm:ss.SSS} [%thread] %logger{36}:%line - %msg%n
   </Pattern>
  </encoder>

  <rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
   <FileNamePattern>logs/test.%i.log.zip</FileNamePattern>
   <MinIndex>1</MinIndex>
   <MaxIndex>10</MaxIndex>
  </rollingPolicy>

  <triggeringPolicy
   class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
   <MaxFileSize>2MB</MaxFileSize>
  </triggeringPolicy>
 </appender>

 <logger name="com.javacodegeeks.examples.logbackexample.beans"
  level="INFO" additivity="false">
  <appender-ref ref="STDOUT" />
 </logger>

 <!-- By default, the level of the root level is set to DEBUG -->
 <root level="TRACE">
  <appender-ref ref="STDOUT" />
  <appender-ref ref="FILE" />
 </root>
</configuration>

Test Class

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.Marker;
import org.slf4j.MarkerFactory;

public class TestLog {

    final static Logger log = LoggerFactory.getLogger(TestLog.class.getName());

    public static void main(String[] args) {

        log.info("info");
        log.error("error");
        log.warn("warning");
        log.debug("debug");
        log.trace("trace");

        log.info("info {}", "one param");
        log.error("error {}", "one param");
        log.warn("warning {}", "one param");
        log.debug("debug {}", "one param");
        log.trace("trace {}", "one param");

        log.info("info {} and {}", "param-1", "param-2");
        log.error("error {} and {}", "param-1", "param-2");
        log.warn("warning {} and {}", "param-1", "param-2");
        log.debug("debug {} and {}", "param-1", "param-2");
        log.trace("trace {} and {}", "param-1", "param-2");

        log.error("Custome Exception ", new Exception("cusotome error msg"));
        log.error("ERROR in {} with {}", "className", "inParam", new Exception("custome error msg"));

        String s = "Hello";
        try {
            Integer.valueOf(s);
        } catch (NumberFormatException e) {
            log.error("Failed to format {}", s, e);
        }

        Marker fatal = MarkerFactory.getMarker("FATAL");
        Logger logger = LoggerFactory.getLogger("aLogger");
        try {
            Integer i = Integer.valueOf(s);
        } catch (NumberFormatException e) {
            logger.error(fatal, "Fatal Error", e);
        }

        logger.debug("Set {1,2} differs from {}", "3");
        logger.debug("Set {1,2} differs from {{}}", "3");
        logger.debug("Set \\{} differs from {}", "3");
        logger.debug("File name is C:\\\\{}.", "file.zip");
    }

}

November 2, 2015

Extending OutputStream

It was very helpful when using: ((ChannelExec) channel).setErrStream(new Log4JOutputStream());

Review JSch - Examples - Exec.java

Example 1

package sawalha.poc.sch;

import java.io.IOException;
import java.io.OutputStream;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

public class Log4JOutputStream extends OutputStream {

    static Logger log = LogManager.getLogger(ExecCli.class.getName());
    StringBuffer str = new StringBuffer();

    @Override
    public void write(int b) throws IOException {
        char current = (char) b;
        if (current == '\n') {
            log.error(str.toString());
            // Reset it
            str.setLength(0);
        } else {
            str.append(current);
        }
    }
}

Example 2

package sawalha.poc.sch;

import java.io.IOException;
import java.io.OutputStream;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

public class Log4JOutputStream extends OutputStream {

    static Logger log = LogManager.getLogger(ExecCli.class.getName());
    StringBuffer str = new StringBuffer();

    @Override
    public void close() throws IOException {
        super.close();
        log.error(str.toString());
    }

    @Override
    public void write(int b) throws IOException {
        int[] bytes = { b };
        write(bytes, 0, bytes.length);
    }

    public void write(int[] bytes, int offset, int length) {
        String s = new String(bytes, offset, length);
        str.append(s);
    }

}

Copy Files between Linux & Windows using Java

Maven Dependency

print 'hello w<!-- www.jcraft.com/jsch/ -->
<dependency>
 <groupId>com.jcraft</groupId>
 <artifactId>jsch</artifactId>
 <version>0.1.53</version>
</dependency>orld!'

FileCopierOverNetwork example class

package sawalha.poc.sch.trans;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
import com.jcraft.jsch.SftpException;
import com.jcraft.jsch.SftpProgressMonitor;

public class FileCopierOverNetwork {

    static Logger log = LogManager.getLogger(FileCopierOverNetwork.class.getName());

    final static SftpProgressMonitor monitor = new SftpProgressMonitor() {
        public void init(final int op, final String source, final String target, final long max) {
            log.info("sftp start uploading file from:" + source + " to:" + target);
        }

        public boolean count(final long count) {
            log.debug("sftp sending bytes: " + count);
            return true;
        }

        public void end() {
            log.info("sftp uploading is done.");
        }
    };

    public static void main(String args[]) {
        String hostname = "10.100.102.37";
        String username = "thin";
        String password = "thin123";
        String copyWinFrom = createTestFile("fromWindows.del").getAbsolutePath();
        // String copyWinFrom = "d:/fromWindows.del";
        String copyWinTo = "/home/thin/ibrahim-scripts/fromWindows.del";

        String copyLinuxFrom = "/home/thin/ibrahim-scripts/fromLinux.del";
        String copyLinuxTo = "fromLinux.del";

        try {
            putFile(hostname, username, password, copyWinFrom, copyWinTo);
            getFile(hostname, username, password, copyLinuxFrom, copyLinuxTo);
        } catch (JSchException e1) {
            e1.printStackTrace();
        } catch (SftpException e1) {
            e1.printStackTrace();
        }
        log.info("Done !!");
    }

    public static void putFile(String hostname, String username, String password, String copyFrom, String copyTo)
            throws JSchException, SftpException {
        log.info("Initiate sending file to Linux Server...");
        JSch jsch = new JSch();
        Session session = null;
        log.info("Trying to connect.....");
        session = jsch.getSession(username, hostname, 22);
        session.setConfig("StrictHostKeyChecking", "no");
        session.setPassword(password);
        session.connect();
        log.info("is server connected? " + session.isConnected());

        Channel channel = session.openChannel("sftp");
        channel.connect();
        ChannelSftp sftpChannel = (ChannelSftp) channel;
        log.info("Server's home directory: " + sftpChannel.getHome());
        try {
            sftpChannel.put(copyFrom, copyTo, monitor, ChannelSftp.OVERWRITE);
        } catch (SftpException e) {
            log.error("file was not found: " + copyFrom);
        }

        sftpChannel.exit();
        session.disconnect();
        log.info("Finished sending file to Linux Server...");
    }

    public static void getFile(String hostname, String username, String password, String copyFrom, String copyTo)
            throws JSchException {
        log.info("Initiate getting file from Linux Server...");
        JSch jsch = new JSch();
        Session session = null;
        log.info("Trying to connect.....");
        session = jsch.getSession(username, hostname, 22);
        session.setConfig("StrictHostKeyChecking", "no");
        session.setPassword(password);
        session.connect();
        log.info("is server connected? " + session.isConnected());

        Channel channel = session.openChannel("sftp");
        channel.connect();
        ChannelSftp sftpChannel = (ChannelSftp) channel;
        try {
            log.info(sftpChannel.getHome());
        } catch (SftpException e1) {
            e1.printStackTrace();
        }
        try {
            sftpChannel.get(copyFrom, copyTo, monitor, ChannelSftp.OVERWRITE);
        } catch (SftpException e) {
            log.error("file was not found: " + copyFrom);
        }

        sftpChannel.exit();
        session.disconnect();
        log.info("Finished getting file from Linux Server...");
    }

    public static File createTestFile(String fileName) {
        File file = new File(fileName);
        if (file.exists()) {
            return file;
        }
        try {
            FileWriter fileWriter = new FileWriter(file);
            fileWriter.write("This is ");
            fileWriter.write("a test");
            fileWriter.write("\n");
            fileWriter.write("Ibrahim Sawalha.");
            fileWriter.flush();
            fileWriter.close();
            return file;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}

Example on Log4j2 with RollingFile

pom.xml file

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 <modelVersion>4.0.0</modelVersion>
 <groupId>log4j2-example</groupId>
 <artifactId>log4j2-example</artifactId>
 <version>0.0.1-SNAPSHOT</version>

 <dependencies>
  <!-- Log4J 2 : required -->
  <dependency>
   <groupId>org.apache.logging.log4j</groupId>
   <artifactId>log4j-core</artifactId>
   <version>2.3</version>
  </dependency>
 </dependencies>
</project>

log4j2.xml file

Note: the log4j2.xml file is located under PORJECT_HOME\src\main\resources\log4j2.xml

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
 <Properties>
  <Property name="log-path">logs</Property>
 </Properties>
 <Appenders>
  <Console name="console-log" target="SYSTEM_OUT">
   <PatternLayout pattern="[%-5level][%t] %c{1}:%L- %msg%n" />
  </Console>
  <RollingFile name="trace-log" fileName="${log-path}/myexample.log"
   filePattern="${log-path}/myexample-%d{yyyy-MM-dd}-%i.log">
   <PatternLayout>
    <pattern>[%-5level] %d [%t] %c{1}:%L- %msg%n</pattern>
   </PatternLayout>
   <Policies>
    <SizeBasedTriggeringPolicy size="1 KB" />
   </Policies>
   <DefaultRolloverStrategy max="4" />
  </RollingFile>
  <RollingFile name="error-log" fileName="${log-path}/myexample-error.log"
   filePattern="${log-path}/ics-print-info-error-%d{yyyy-MM-dd}.log">
   <PatternLayout>
    <!-- %-5level %d [%t] %c:%M(%L): %m%n -->
    <pattern>[%-5level] %d [%t] %c:%M:%L- %m%n</pattern>
   </PatternLayout>
   <Policies>
    <SizeBasedTriggeringPolicy size="1 KB" />
   </Policies>
   <DefaultRolloverStrategy max="4" />
  </RollingFile>
 </Appenders>
 <Loggers>
  <Logger name="sawalha" level="debug" additivity="false"
   includeLocation="true">
   <appender-ref ref="trace-log" level="debug" />
   <appender-ref ref="error-log" level="error" />
   <appender-ref ref="console-log" level="info" />
  </Logger>
  <Root level="debug" additivity="false">
   <AppenderRef ref="console-log" />
  </Root>
 </Loggers>
</Configuration>

Log4j2Example class file

package sawalha.log4j2;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

public class Log4j2Example {
    static Logger log = LogManager.getLogger(Log4j2Example.class.getName());

    public static void main(String[] args) throws IOException {
        System.out.println("===> Please enter a number:\n===>");
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int number = Integer.valueOf(br.readLine());
        log.info("Info : number is " + number);
        log.warn("Warning : number is " + number);
        log.debug("Debug : number is " + number);
        log.error("Error : number is " + number);
        log.fatal("Fatal : number is " + number);

        if (number > 100) {
            log.info("Info : You chose a number > 100 ");
            log.warn("Warning : You chose a number > 100 ");
            log.debug("Debug : You chose a number > 100 ");
            log.error("Error : You chose a number > 100 ");
            log.fatal("Fatal : You chose a number > 100 ");

        } else {
            log.info("Info : You chose a number < 100 ");
            log.warn("Warning : You chose a number < 100 ");
            log.debug("Debug : You chose a number < 100 ");
            log.error("Error : You chose a number < 100 ");
            log.fatal("Fatal : You chose a number < 100 ");
        }
        String numberStr = String.valueOf(number);
        for (int i = 0; i <= 10; i++) {
            if (numberStr.contains(String.valueOf(i))) {
                log.info("Info : Your number has the digit " + i);
                log.warn("Warning : Your number has the digit " + i);
                log.debug("Debug : Your number has the digit " + i);
                log.error("Error : Your number has the digit " + i);
                log.fatal("Fatal : Your number has the digit " + i);
            }
        }
    }
}

October 26, 2015

Obtain a connection to DataSource in Weblogic Server

In other word "How to obtain a remote connection to a DataSource in Weblogic Server"

1. Standard way to obtain a connection from a local server on web appliation

1.1 Define the DataSource name in web.xml file as follow:

<resource-ref>
    <description>Oracle Datasource example</description>
    <res-ref-name>jdbc/testDS</res-ref-name>
    <res-type>javax.sql.DataSource</res-type>
    <res-auth>Container</res-auth>
</resource-ref>

1.2 In Java class you lookup for the DataSource as follow:

String dataSourceName = "jdbc/testDS"
Context initialContext = new InitialContext();
DataSource dataSource = (DataSource) initialContext.lookup("java:comp/env/" + dataSourceName);
connection = dataSource.getConnection();

2. Obtain a REMOTE connection from a local server on web application

2.1. No need to define anything on web.xml file

2.2. In Java class you look for the DataSource as follow:

String dataSourceName = "jdbc/testDS"
Context initialContext = new InitialContext();
Properties env = new Properties();
env.put("java.naming.factory.initial", "weblogic.jndi.WLInitialContextFactory");
env.put("java.naming.provider.url", "t3://localhost:7001");
DataSource dataSource = (DataSource) initialContext.lookup(dataSourceName);
connection = dataSource.getConnection();

September 22, 2015

How to pass arguments to a Bash-script

1. Using getopts

1. Create new Shell Script

$ vi test.sh

2. Copy the following line into test.sh file

while getopts p:u:f:n:r: option
do
  case "${option}" in
    p) weblogic_password=${OPTARG};;
    u) weblogic_username=${OPTARG};;
    f) war_file=${OPTARG};;
    n) ws_name=${OPTARG};;
    r) ROLE_NAME=${OPTARG};;
  esac
done

echo ${weblogic_password}
echo $weblogic_username
echo $war_file
echo $ws_name
echo $ROLE_NAME

3.Run the script

$ chmod +x test.sh
$ ./test.sh -p weblogic_password -u weblogic_username -f war_file -n web_name -r role_name

2. Using environment variables

See the example below:
#!/bin/sh

OIFS=$IFS;
IFS=",";

usage() {
    cat <<EOF
Usage: $0 [options] [--] [file...]

Arguments:

  -h, --help
    Display this usage message and exit.

  -f <val>, --foo <val>, --foo=<val>
    Documentation goes here.

  -b <val>, --bar <val>, --bar=<val>
    Documentation goes here.

  --
    Treat the remaining arguments as file names.  Useful if the first
    file name might begin with '-'.

  file...
    Optional list of file names.  If the first file name in the list
    begins with '-', it will be treated as an option unless it comes
    after the '--' option.
EOF
}

# handy logging and error handling functions
log() { printf '%s\n' "$*"; }
error() { log "ERROR: $*" >&2; }
fatal() { error "$*"; exit 1; }
usage_fatal() { error "$*"; usage >&2; exit 1; }

# parse options
foo="foo default value goes here"
bar="bar,default,value,goes,here"
barArray=($bar)

while [ "$#" -gt 0 ]; do
    arg=$1
    case $1 in
        # convert "--opt=the value" to --opt "the value".
        # the quotes around the equals sign is to work around a
        # bug in emacs' syntax parsing
        --*'='*) shift; set -- "${arg%%=*}" "${arg#*=}" "$@"; continue;;
        -f|--foo) shift; foo=$1;;
        -b|--bar) shift;
                  bar=$1;
                  barArray=($bar);
                  ;;
        -h|--help) usage; exit 0;;
        --) shift; break;;
        -*) usage_fatal "unknown option: '$1'";;
        *) break;; # reached the list of file names
    esac
    shift || usage_fatal "option '${arg}' requires a value"
done
#arguments are now the file names


echo "foo=" $foo
echo "bar=" $bar
echo "barArray size=" ${#barArray[@]}
for ((i=0; i<${#barArray[@]}; ++i)); do
  echo "bar $i: ${barArray[$i]}";
done

IFS=$OIFS;

September 17, 2015

Creating WebLogic DataSource Using WLST and Properties File

How to Configure WebLogic DataSource using WLST?

Step 1. Create working directory and Server properties file

Create a directory at the machine that has the Weblogic Server installed on.
Example: (Linux OS) create directory called /home/create_DS
place a properties file like below in this directory with name "server.properties"

domain.name=7001_Domain
admin.url=t3://localhost:7001
admin.userName=weblogic
admin.password=weblogicPassword

datasource.name=jdbc/DS_Name
datasource.database.name=demo

datasource.targets_server=AdminServer
datasource.targets_cluster=cluster_server_1,cluster_server_2

datasource.filename=jdbc_DS_Name.xml
datasource.jndiname=DS_Name
datasource.driver.class=oracle.jdbc.OracleDriver
datasource.url=jdbc:oracle:thin:@192.168.1.1:1521/DB_NAME
datasource.username=DB_Username
datasource.password=DB_Password

datasource.test.query=SQL SELECT * FROM DUAL

Step 2. Create createDataSource.py

Now in the same directory write the following WLST Script “createDataSource.py” like following:

from java.io import FileInputStream

propInputStream = FileInputStream("server.properties")
configProps = Properties()
configProps.load(propInputStream)

domainName=configProps.get("domain.name")
adminURL=configProps.get("admin.url")
adminUserName=configProps.get("admin.userName")
adminPassword=configProps.get("admin.password")

dsName=configProps.get("datasource.name")
dsFileName=configProps.get("datasource.filename")
dsDatabaseName=configProps.get("datasource.database.name")

dsJNDIName=configProps.get("datasource.jndiname")
dsDriverName=configProps.get("datasource.driver.class")
dsURL=configProps.get("datasource.url")
dsUserName=configProps.get("datasource.username")
dsPassword=configProps.get("datasource.password")
dsTestQuery=configProps.get("datasource.test.query")

try:
  connect(adminUserName, adminPassword, adminURL)
  edit()
  startEdit()
  cd('/')
  cmo.createJDBCSystemResource(dsName)

  cd('/JDBCSystemResources/' + dsName + '/JDBCResource/' + dsName)
  cmo.setName(dsName)

  cd('/JDBCSystemResources/' + dsName + '/JDBCResource/' + dsName + '/JDBCDataSourceParams/' + dsName )
  set('JNDINames',jarray.array([String(dsName)], String))

  cd('/JDBCSystemResources/' + dsName + '/JDBCResource/' + dsName + '/JDBCDriverParams/' + dsName )
  cmo.setUrl(dsURL)
  cmo.setDriverName( dsDriverName )
  cmo.setPassword(dsPassword)

  cd('/JDBCSystemResources/' + dsName + '/JDBCResource/' + dsName + '/JDBCConnectionPoolParams/' + dsName )
  cmo.setTestTableName(dsTestQuery)
  cd('/JDBCSystemResources/' + dsName + '/JDBCResource/' + dsName + '/JDBCDriverParams/' + dsName + '/Properties/' + dsName )
  cmo.createProperty('user')

  cd('/JDBCSystemResources/' + dsName + '/JDBCResource/' + dsName + '/JDBCDriverParams/' + dsName + '/Properties/' + dsName + '/Properties/user')
  cmo.setValue(dsUserName)

  cd('/JDBCSystemResources/' + dsName + '/JDBCResource/' + dsName + '/JDBCDriverParams/' + dsName + '/Properties/' + dsName )
  cmo.createProperty('databaseName')

  #cd('/JDBCSystemResources/' + dsName + '/JDBCResource/' + dsName + '/JDBCDriverParams/' + dsName + '/Properties/' + dsName + '/Properties/databaseName')
  #cmo.setValue(dsDatabaseName)

  cd('/JDBCSystemResources/' + dsName + '/JDBCResource/' + dsName + '/JDBCDataSourceParams/' + dsName )
  cmo.setGlobalTransactionsProtocol('OnePhaseCommit')

  cd('/SystemResources/' + dsName )

  targets=PyList()
  datasource_targets_server=configProps.get("datasource.targets_server").split(",")
  datasource_targets_cluster=configProps.get("datasource.targets_cluster").split(",")

  for datasourceTarget in datasource_targets_server:
    targetStr='com.bea:Name='+datasourceTarget+',Type=Server'
    targets.append(ObjectName(targetStr))
  
  for datasourceTarget in datasource_targets_cluster:
    targetStr='com.bea:Name='+datasourceTarget+',Type=Cluster'
    targets.append(ObjectName(targetStr))

  set('Targets',jarray.array(targets,ObjectName))

  save()
  activate()
except Exception, e:
  print 'ERROR:'
  print e
  print 'Operation was canceled.'
  undo('true','y')
  cancelEdit('y')
  exit()

Step 3. Running setWLSEnv.sh script

Now Open a Command/Shell Prompt and then run the “setWLSEnv.sh” script to set the CLASSPATH and PATH environment variables. Run the “. ./setWLSEnv.sh” by adding two DOTs separated by a single space …..before the actual script like following : (use ‘cd’ command to move inside the /wlserver_10.3/server/bin) then run the following command…. . ./setWLSEnv.sh
Note: Here The first DOT represents that set the Environment in the current Shell, AND the second ./ represents execute the script from the current directory.

Step 4. Running the createDataSource.py script

Run the Above WLST Script like following:
java weblogic.WLST createDataSource.py

OR
java -cp /<WL_HOME>/wlserver/server/lib/weblogic.jar weblogic.WLST createDataSource.py

Make sure that the Database is running while you are creating the DataSource.