Saturday, April 11, 2015

Should logger be private static or not

Just read - http://wiki.apache.org/commons/Logging/StaticLog

decision is full of trade off but, that article is the best, so I am referencing it in my blog to let me quickly find it, as that question re-appear again and again.

My position:
Logger should never be public or protected and even share-able with or accessible by others classes, eventually will be enforced by Checkstyle - https://github.com/sevntu-checkstyle/sevntu.checkstyle/issues/286

Tuesday, March 24, 2015

Oracle listener is stopped unexpectedly on Xubuntu 14.04

Oracle listener is always launched on startup, I did not nothing special to stop it, but during work I found that listener is not started ....

15:01 $ lsnrctl status

LSNRCTL for Linux: Version 11.2.0.2.0 - Production on 24-MAR-2015 15:01:40

Copyright (c) 1991, 2011, Oracle.  All rights reserved.

Connecting to (DESCRIPTION=(ADDRESS=(PROTOCOL=IPC)(KEY=EXTPROC_FOR_XE)))
TNS-12541: TNS:no listener
 TNS-12560: TNS:protocol adapter error
  TNS-00511: No listener
   Linux Error: 111: Connection refused
Connecting to (DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=rivanov)(PORT=1521)))
TNS-12541: TNS:no listener
 TNS-12560: TNS:protocol adapter error
  TNS-00511: No listener
   Linux Error: 111: Connection refused



15:01 $ lsnrctl start

LSNRCTL for Linux: Version 11.2.0.2.0 - Production on 24-MAR-2015 15:12:35

Copyright (c) 1991, 2011, Oracle.  All rights reserved.

Connecting to (DESCRIPTION=(ADDRESS=(PROTOCOL=IPC)(KEY=EXTPROC_FOR_XE)))
STATUS of the LISTENER
------------------------
Alias                     LISTENER
Version                   TNSLSNR for Linux: Version 11.2.0.2.0 - Production
Start Date                24-MAR-2015 15:02:20
Uptime                    0 days 0 hr. 10 min. 15 sec
Trace Level               off
Security                  ON: Local OS Authentication
SNMP                      OFF
Default Service           XE
Listener Parameter File   /u01/app/oracle/product/11.2.0/xe/network/admin/listener.ora
Listener Log File         /u01/app/oracle/product/11.2.0/xe/log/diag/tnslsnr/vdu-rivanov/listener/alert/log.xml
Listening Endpoints Summary...
  (DESCRIPTION=(ADDRESS=(PROTOCOL=ipc)(KEY=EXTPROC_FOR_XE)))
  (DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=rivanov)(PORT=1521)))
  (DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=rivanov)(PORT=8088))(Presentation=HTTP)(Session=RAW))
Services Summary...
Service "PLSExtProc" has 1 instance(s).
  Instance "PLSExtProc", status UNKNOWN, has 1 handler(s) for this service...
Service "XE" has 1 instance(s).
  Instance "XE", status READY, has 1 handler(s) for this service...
Service "XEXDB" has 1 instance(s).
  Instance "XE", status READY, has 1 handler(s) for this service...
The command completed successfully


BUT SQLDeveloper still have no ability to connect to local instance.
Java still cannot connect to ORACLE XE on "mvn clean test"


15:10 $ sudo /etc/init.d/oracle-xe restart

after that command all works fine

Thursday, March 12, 2015

Neutral Filters and Evaluator ordering for Logback SMTPAppender

Example of config with nuance of NEUTRAL usage for filters,
miss of at least one filter that do deny by well distinctive parameter (level, marker) will result in sending mail with all events from begging of logs till event required in Evaluator.
:

<appender name="MY_MAIL" class="ch.qos.logback.classic.net.SMTPAppender">
<smtpHost>mailer.server.com</smtpHost>
<to>botlog@server.com</to>
<from>noreply@server.com</from>

<asynchronousSending>false</asynchronousSending>

<if condition="property(&quot;MY_ENV_VAR&quot;) == &quot;&quot; || property(&quot;MY_ENV_VAR&quot;) == null">
<then>
<subject>[%marker] [%p] from ${HOSTNAME}</subject>
</then>
<else>
<subject>[${MY_ENV_VAR}] [%marker] [%p] from ${HOSTNAME}</subject>
</else>
</if>

<!-- the way SMTPAppender works makes him only apply filters firsly 
    and then apply evaluator to actually trigger 
e-mail sending, filters are also executed in order in 
which they are defined in configuration -->
<!-- Due to usage of NEUTRAL at EvaluatorFilter(see below) without 
    this filter, mail will contain all events from beggining 
    till MY_MARKER -->
<filter class="ch.qos.logback.core.filter.EvaluatorFilter">
<evaluator class="ch.qos.logback.classic.boolex.OnMarkerEvaluator">
<marker>MY_MARKER</marker>
</evaluator>
<OnMatch>NEUTRAL</OnMatch>
<OnMismatch>DENY</OnMismatch>
</filter>

<!-- perform more precise filtering, 
    you can suppress particular messages here -->
<filter class="ch.qos.logback.core.filter.EvaluatorFilter">
<evaluator>
<expression><![CDATA[
// reduce noise of job for some problems
if (level == WARN && logger.contains("ConstraintService") 
&& message.contains("violates constraint")) {
return false;
}
return true;
]]></expression>
</evaluator>
<!-- NEUTRAL as we may want to add more filters later -->
<OnMatch>NEUTRAL</OnMatch> 
<OnMismatch>DENY</OnMismatch>
</filter>

<!-- This evaluator triggers e-mail sending. As we only filter 
    events with appropriate
marker, e-mail would always have only one logEvent -->
<evaluator class="ch.qos.logback.classic.boolex.OnMarkerEvaluator">
<marker>MY_MARKER</marker>
</evaluator>
</appender>

----

Tuesday, March 10, 2015

Catch Exception or catch Throwable when you need to log anything


When you ask yourself a question what to catch in main method Exception and Throwable - use Throwable to catch all unexpected problems for sure.

if you choose to catch Exception you will lose Errors ,and Errors are not only system problems.

interesting Error that could happen in business logic + misconfiguration
http://docs.oracle.com/javase/7/docs/api/java/lang/ExceptionInInitializerError.html

initialization of non static field

Configuration.getBean(FileToCsvExtractor.class);

declaration of Configuration

public enum Configuration {
INSTANCE;

private AbstractApplicationContext context;

private Configuration() {
this.context = new AnnotationConfigApplicationContext("com.mycompany");
}

public static <T> T getBean(Class<T> clazz) {
return INSTANCE.context.getBean(clazz);
}

public void destroy() {
context.close();
}

}


If you somewhow did a mistake in from and Configuration will fail to initialize then getBean is failed with ExceptionInInitializerError, but it have a cause Exception that could be like:

Exception in thread "main" java.lang.ExceptionInInitializerError
at com.mycompany.ExecutableImpl.execute(LdLogRenaissanceDeployJob.java:23)
at com.revere.xfeed.customclients.renaissance.MyTestClass.main(MyTestClass.java:8)
Caused by: org.springframework.beans.factory.BeanInitializationException: Could not load properties; nested exception is java.io.FileNotFoundException: myproperties.properties (No such file or directory)
at org.springframework.beans.factory.config.PropertyResourceConfigurer.postProcessBeanFactory(PropertyResourceConfigurer.java:87)
at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:694)
at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:669)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:461)
at org.springframework.context.annotation.AnnotationConfigApplicationContext.<init>(AnnotationConfigApplicationContext.java:84)


so you have to catch Thowable in main method to log all problems before exiting a application.

interface Executable {
void execute(Object args) throws Throwable;
}

class ExecutableImpl implements Executable {

public void doAll() throws IOException, FTPException {
                        Configuration.getBean(FileToCsvExtractor.class);
throw new IOException();
}

@Override
public void execute(Object args) throws Throwable {
try {
doAll();
} catch (Throwable th) {
throw th;
}
}

}

or do "throws Exception" in interface , no other changes are required,  but that will kind if lie to developers as you know that code could throw ExceptionInInitializerError :) even it mean to be RuntimeException.

Functional naming guides is not good for OOP

As you pass in parameter object - it have to be named as noun. Library method expect function. Function is a noun, so it is object.
Object Oriented languages have very good methodology for naming of all items, that let read code easily, mixing in different methodology that conflicts - it is not good approach.

-  names = FluentIterable.from(zipFiles).transformAndConcat(getTableNamesForZipFile).toSet();
+ names = FluentIterable.from(zipFiles).transformAndConcat(tableNamesForZipFileMaker).toSet();

  public <T> FluentIterable<T> transformAndConcat(
      Function<? super E, ? extends Iterable<? extends T>> function) {
    return from(Iterables.concat(transform(function)));
  }

Name should be:
tableNamesForZipFileGetter,
tableNamesForZipFileMaker,
tableNamesForZipFilePuller,
tableNamesForZipFileAlter,
tableNamesForZipFileResolver, ...

It is ok to have no standard prefix/suffix for name, as all functions are different by type (different functional libraries).

Economizing on 2-5 letter in comparison  to use "get" prefix does not cost that.

Interesting compromise for naming function objects : column2Name, file2name, zipFile2Name

Naming patterns for filtering methods

from http://martinfowler.com/articles/collection-pipeline/#op-catalog

Good naming pattern for filters as "filter" term does not not describe where required object will be - removed from collection or stay in collection:

rejectXXXXX - Inverse of filter, returning elements that do not match the predicate.

selectXXXXX - Runs a boolean function on each element and only puts those that pass into the output..

Why JavaDoc is matter in checkstyle code ?

As a mentor I do a lot of code reviews ...

My moto: Good code with good naming does not need any comment and JavaDoc

but ..... in Checkstyle we demand javadoc for all elements of class

Why we need that ?


1)
to validate method logic I need to know author intentions

2) catch problems in naming like
    /**
     * Method makes recurse DetailAST and save types that it returns. Its an
.........
     */
    private void findAllMethodReturnTypes(DetailAST processingSiblingAST) {

    /**
     * Method checks if inner private defined type (class, interface or enumeration)
     * is private and does not implement(extend)
     * something, if so it adds type's name to the set of private types.......
     */
    private void getPrivateType(DetailAST innerTypeDefAST) {


3)
It is better for reading of newly created code.