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.

More interesting cases where catching Errors is ok:
http://blog.igorminar.com/2008/05/catching-stackoverflowerror-and-bug-in.html
http://programmers.stackexchange.com/questions/209099/is-it-ever-okay-to-catch-stackoverflowerror-in-java

Smb in code could use Errors http://docs.oracle.com/javase/7/docs/api/java/lang/AssertionError.html :) as a validation of some sort, .....


One more case of Error that is ok finish gently application  - is
java.lang.NoClassDefFoundError: com.mycompany.ISqlRowMapper
....
Caused by: java.lang.ClassNotFoundException: com.mycompany.ISqlRowMapper
it is completely to ok to catch it log it!! Yes it was terrible misconfiguration in maven level, but UTs were passed and problem appear only in fully deployed application  and in the middle of execution (not at start).

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.

Setup static web site on AWS EC2

Allow HTTP 80 port redirection from public IP to your private server, by default only 22 port is setup:


sudo yum install mc


desable SElinux
/etc/selinux/config
SELINUX=disabled


restart SERVER:
reboot


vi /etc/sysconfig/iptables
-A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT
service iptables restart


http://dev.antoinesolutions.com/apache-server

How to install Apache Server on CentOS, RedHat, Linux

Install Apache HTTP Server
yum install httpd
Note: This is typically installed with CentOS by default

How to configure Apache Server on CentOS, RedHat, Linux

Set the apache service to start on boot
chkconfig --levels 235 httpd on
Enable name-based virtual hosting on port 80
Open the httpd configuration file located at /etc/httpd/conf/httpd.conf

ErrorLog /www/logs/error_log
CustomLog /www/logs/access_log combined
ServerName 54.164.21.75:80
DocumentRoot "/www"

Save the file
Restart the Apache HTTP Server daemon
service httpd restart



 In Browser :
http://54.164.21.75/ should show apapche welcome page


To make Authentication for one user by password:
http://www.cyberciti.biz/faq/howto-setup-apache-password-protect-directory-with-htaccess-file/
http://weavervsworld.com/docs/other/passprotect.html

What is better inversion in method names of inversion operator in usage ?

What is better inversion in method names of in usage ? What criteria we could use measure that ?

For private methods it might be not a problem, but mostly affects API and public methods

@Override
public void visitToken(DetailAST aAst) {
DetailAST conditionExpressionAst = aAst.findFirstToken(TokenTypes.EXPR);
switch (aAst.getType()) {
case TokenTypes.LITERAL_RETURN:
if (isNonEmptyReturn(aAst)) {
DetailAST inversionAst = getInversion(conditionExpressionAst);
if (isAvoidableInversion(inversionAst)) {
log(inversionAst);
}
}
break;
case TokenTypes.LITERAL_WHILE:
case TokenTypes.LITERAL_DO:
case TokenTypes.LITERAL_IF:
DetailAST inversionAst = getInversion(conditionExpressionAst);
if (isAvoidableInversion(inversionAst)) {
log(inversionAst);
}
break;
case TokenTypes.FOR_CONDITION:
if (isNonEmptyForCondition(aAst)) {
inversionAst = getInversion(conditionExpressionAst);
if (isAvoidableInversion(inversionAst)) {
log(inversionAst);
}
}
break;
default:
final String exceptionMsg = "Unexpected Token Type - "
+ TokenTypes.getTokenName(aAst.getType());
throw new IllegalStateException(exceptionMsg);
}
}
code:
/**
* Checks if return statement is not empty
* @param aReturnAst
*/
private static boolean isNonEmptyReturn(DetailAST aReturnAst) {
return aReturnAst.findFirstToken(TokenTypes.EXPR) != null;
}
/**
* Checks if condition in for-loop is not empty
* @param aForConditionAst
*/
private static boolean isNonEmptyForCondition(DetailAST aForConditionAst) {
return aForConditionAst.getFirstChild() != null;
}

Some code could not be written without "NOT"/"NON" , Example:

Syntactically parallel elements in indentation


Why it is matter ?

https://google-styleguide.googlecode.com/svn-history/r130/trunk/javaguide.html#s4.5.1-line-wrapping-where-to-break
"The prime directive of line-wrapping is: prefer to break at a higher syntactic level."

https://google-styleguide.googlecode.com/svn-history/r130/trunk/javaguide.html#s4.5.2-line-wrapping-indent
"When there are multiple continuation lines, indentation may be varied beyond +4 as desired. In general, two continuation lines use the same indentation level if and only if they begin with syntactically parallel elements."

Cases from real code with bad indentation:
1)

 if ((rcurly != null) && !level.accept(rcurlyPos)
           && (rcurlyMustStart() || startsLine(rcurly))
      && !areOnSameLine(rcurly, lcurly))


2)
details.append("Focused : ").append(file.getPath())
.append(", start date :")
.append(", end date : ");

3)
if (toVisitAst != null
&& (toVisitAst.getParent().getParent().getNextSibling() == null
|| toVisitAst.getParent().getParent().getNextSibling().getType()
== TokenTypes.RCURLY)
&& toVisitAst.getType() == TokenTypes.LITERAL_RETURN
&& toVisitAst.getParent().getNextSibling()== null) { }


===========================


resulted in https://github.com/checkstyle/checkstyle/issues/270

Static Import Check for Checkstyle

Static import: http://docs.oracle.com/javase/1.5.0/docs/guide/language/static-import.html

Command to catch code:
 grep --include=*.java --exclude={*Test.java,Test*.java,*IT.java} -rw -m 1 . -e "import static"

we can not forbid static import usage at all, it is useful, we need to limit it, So we will forbid all static imports that are not applying to following options:

1) allow in target member is used more then X times.
see example in point "3)" and imagine there is a lot of such code blocks, that option could allow static import for "psvBuilder".

2) allow in Class of target member could brake Line-limit more then X times.
 that it is not required as will hide too much code that should be formatted appropriately

3) Allow in complicated statements with a lot of inner methods calls in arguments.

import static com.commons.domain.DsvFileDefinition.psvBuilder;
import static com.commons.domain.FeedDefinition.feedBuilder;
import static com.commons.domain.ZipFileDefinition.zipBuilder;

private static FeedDefinition getFeedDefinition() {
return feedBuilder("Premium", "/datafeeds/", "NAME", "NAME1").addFile(
zipBuilder("premium").addFiles(of(
psvBuilder("address").build(),
psvBuilder("entity").build(),public enum BetPosition {
    // ...
    STRAIGHT_32(96, ...),
    STRAIGHT_33(142, ...),
    STRAIGHT_34(52, ...),
    BET_1_2(53, ...),
    BET_4_5(55, ...),
    // ... long list of enum values

psvBuilder("entity_changes").build(),
psvBuilder("entity_identifiers").build(),
psvBuilder("entity_names").build(),
psvBuilder("entity_profiles").build(),
psvBuilder("entity_relationships").build(),
psvBuilder("entity_structure").build(),
psvBuilder("entity_map").build(),
psvBuilder("listing_map").build())).build()
).build();


4) Ignore list of classes/methods that team presume distinctive and whole team know.
"com.google.common.base.Strings,com.google.common.base.XXXXXXX"

import static com.google.common.base.Strings.isNullOrEmpty;
import static com.google.common.base.Strings.nullToEmpty;

this.schemaName = nullToEmpty(schemaName);
if (isNullOrEmpty(tableName)) {
throw new IllegalArgumentException("tableName could not be empty");
}
this.tableName = tableName;

5)
Invalid usage:
import static com.google.common.collect.Iterables.getFirst;
....

TreeSet<Name> names = Sets.newTreeSet(new ILifeCycleEntityComparator());
if (names.isEmpty()) {
// do smth 
} else {
Name firstName = getFirst(tradenames, null);public enum BetPosition {
    // ...
    STRAIGHT_32(96, ...),
    STRAIGHT_33(142, ...),
    STRAIGHT_34(52, ...),
    BET_1_2(53, ...),
    BET_4_5(55, ...),
    // ... long list of enum values

// do smth else
}

6)
Here looks like static import allow to keep lines short.

import static com.revere.det.core.domain.validation.ValidationUtils.formatPeriod;

details.append("Focused sector: ").append(focusSector.getPath())
.append(", start date of focus: ").append(formatPeriod(focus.getStartDate()))
.append(", end date of focus: ").append(formatPeriod(focus.getEndDate()))
.append(NEW_LINE);

7)
Inappropriate - used only one time in whole file.

import static org.joda.time.LocalDateTime.now;

public class Builder extends DataBuilder {

private static final java.sql.Date THREE_YEARS_AGO = new java.sql.Date(now().minusYears(3).toDate().getTime());

=======================

https://code.google.com/p/guava-libraries/wiki/FunctionalExplained

Even using static imports, even if the Function and the Predicate declarations are moved to a different file, the first implementation is less concise, less readable, and less efficient.   

=================

Here's a shortened version of a class where static import is suitable:

===============================
import static com.some.company.BetPosition.*;

public class NumberMapping {

    private final BetPosition[][] numberToPositions = new BetPosition[][] {
            /* 0 number */{ STRAIGHT_0, BET_0_1, BET_0_2, BET_0_3, BET_0_1_2, BET_0_2_3, BET_0_1_2_3 },                 
            // ... 1-35 ...
            /* 36 number */{ STRAIGHT_36, BET_35_36, BET_33_36, BET_34_35_36, BET_32_33_35_36, BET_31_32_33_34_35_36,
                    TOP_2_TO_1, DOZEN_3_RD_12, HIGH_19_TO_36, RED, EVEN } };

    public BetPosition[] getPositions(int number) {
        BetPosition[] positions = numberToPositions[number];
        // some logic
        return positions;
    }
}


Where BetPosition is an enum with such a content:

public enum BetPosition {
    // ...
    STRAIGHT_32(96, ...),
    STRAIGHT_33(142, ...),
    STRAIGHT_34(52, ...),
    BET_1_2(53, ...),
    BET_4_5(55, ...),
    // ... long list of enum values

====================================

TODO..... investigate Guava code to have be sure that their code could follow that Check.

Issue is registered:
https://github.com/sevntu-checkstyle/sevntu.checkstyle/issues/453 

Wrong link generation by maven javadoc plugin for org.xml.sax.helpers.DefaultHandler

Friday, March 6, 2015

Reason of checkstyle ideas rejection for GSoC 2015

Conversation between GSoC administration and Checkstyle :

<gsocbot> carols: Next in line is checkstyle with notice: checkstyle checkstyle https://github.com/checkstyle/checkstyle/wiki/Checkstyle-GSoC-2015-Project-Ideas
* kblin gives voice to checkstyle
* scorche|sh gives voice to checkstyle
<checkstyle> Hi Carol, please share your thoughts about our ideas page
<carols> actually, just looking it over, i think it looks great.
<carols> your application looks good too.
<checkstyle> Do you have any preference of size (number of sentences) of one idea description ? We had a challange to describe clearly and be not too wordy. 
<carols> unfortunately this was another numbers thing. :-(
<carols> i’m sorry that i don’t have a more satisfying answer than that
<carols> checkstyle: yes, my preference is always for more information over less
<carols> i would rather see you over-describe a topic than under-describe
<checkstyle> So we just unlucky this year , right ?
<carols> it helps the students
<carols> yes, unfortunately so :-(
<checkstyle> ok, thanks a lot !
<carols> i hope you’ll try again next year
<carols> you’re welcome



Some reason why many other organizations are failed to pass to GSoC 20015:
<carols> we got a lot of applications in a few specific topic areas where we had to reject a lot of orgs, and we also tried to make space for new organizations who have never participated before over accepting organizations who have been with us for a long time.
<carols> it’s always difficult, and unfortunately some orgs had wonderful ideas pages and applications and still didn’t make the cut.
<carols> yeah, ideas page looks good to me, and it looks like we just made a bunch of hard decisions this year.

Full log of IRC feedback meeting for rejected organizations for Google Summer of Code 2015. #gsoc on freenode.net.

Monday, March 2, 2015

Logback config to suppress event by stacktrace element

Log event in file:
[2015-03-02 09:03:14,418] [server] [user] [ajp-bio-8009-exec-33] [Session][] [ERROR] - An exception has been thrown while processing following requests: '[Request[oid=2021,name=updateSelection,args=[ [ [ 2, 3 ], [ 6, 6 ], [ 11, 11 ] ] ]], Request[oid=2019,name=handleEvent,args=[ popupBeforeOpen, beforePopupOpen, [  ] ]]]'.
java.lang.NullPointerException: null
at com.company.View.fillContextMenu(View.java:444) 
at com.company.View.access$500(View.java:107)
at com.company.View$2.beforePopupOpen(View.java:303)
at sun.reflect.GeneratedMethodAccessor791.invoke(Unknown Source) ~[na:na]


to suppress that event following logback configuration for ch.qos.logback.classic.boolex.JaninoEventEvaluator is required:
<included>

   <property name="common.log.date.format" value="{MM/dd HH:mm:ss:SSS}" />
   <property name="common.log.conversionpattern" value="[%d${common.log.date.format}] [%X{username}] [%c{0}] [%marker] [%p]: %m%n" />

   <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
      <target>System.out</target>
      <encoder>
         <pattern>${common.log.conversionpattern}</pattern>
      </encoder>
<!-- perform more precise filtering, you can suppress particular messages here -->
<filter class="ch.qos.logback.core.filter.EvaluatorFilter">
<evaluator>
<expression><![CDATA[
// suppress ERRORs from legacy code
if (level == ERROR
&& logger.contains("Session")
&& message.contains("handleEvent,args=[ popupBeforeOpen, beforePopupOpen, [  ] ]")
&& message.contains("updateSelection")
       && message.contains("An exception has been thrown while processing following requests")
        && throwableProxy != null && "java.lang.NullPointerException".equals(throwableProxy.getClassName())
&& throwableProxy.getStackTraceElementProxyArray()[0].getSTEAsString().contains("com.mycompany.View.fillContextMenu")
) {
return false;
}

return true;
]]></expression>
</evaluator>
<OnMatch>NEUTRAL</OnMatch>
<OnMismatch>DENY</OnMismatch>
</filter>
   </appender>

   <root level="INFO">
      <appender-ref ref="STDOUT" />
   </root>

</included>