SFTP ls command returns no files

I created a new user called phone for the client, and created a new directory /home/phone as his home directory. Next create some files for testing purpose.

Then modify /etc/ssh/sshd_config to set chroot configuration

But logging in with this new user and run ls command, no files are listed, it returns nothing.. But if I run ls in ssh, the files are shown.

After trying lots of time, the reason is found: It’s because there is no “x” permission for /home/phone directory..

What is “x” permission?

The “x” permission for directory is used to traverse directory. And the “x” permission for file is for file execution.

Then run following command to add “x” permission for /home/phone

And try running ls command in sftp again, the issue is fixed now, we can see files are listed now.

mvn add jar file to local repository

Did you ever want to put a local jar library file into your deployment jar file, but the library file is not available on public repository (or you don’t want to put it on public repository?)

One possible method is to add maven dependency with scope and systemPath property

<dependency>
<groupId>aeszip</groupId>
<artifactId>aeszip</artifactId>
<version>1.0</version>
<scope>system</scope>
<systemPath>${project.basedir}/WebContent/WEB-INF/lib/aeszip.jar</systemPath>
</dependency>

But using scope and systemPath has a problem, that is when I run mvn package to build a package, the dependency will not be included, so running the application on another machine will always complain NoClassDefFoundError or ClassNotFoundException

So a better method is to use mvn install:install-file plugin

This will add AesZip.jar to your local maven repository. When running mvn package to create package, the dependent libraries will be included into the generated package

Python No module named feedparser

I want to use following tool to download paper from arxiv

https://github.com/pvskand/arXiv_download

I installed it by pip

When trying to run python arXiv, following error happened.

Install feedparser using pip

Run python arXiv again, the issue is fixed

OpenCV findContours with RETR_EXTERNAL not working

Following code is trying to extracting bounding rectangles from contours and merge rectangles which is contained in other rectangles

 

In above found contours, one inner contour is contained within an outer contour, but is not merged with outer contour. the inner one’s border is 2 pixel away from outer one’s border.

I checked the code multiple times, and not found something wrong.

 

Then I tried changed the drawing rectangle border thickness to 2, and it works..

 

 

Checking it in PyCharm SciView Data View I found their border is connected. So I’m not sure why it’s working is caused by increased border width or connected shapes. (But in the past cv2.findContours with RETR_EXTERNAL will work for inner contours, so it’s likely caused by thin border)

 

 

 

SSH WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!

My cloud server moved to VPC from classic network just now, and connecting to it via SSH got following error

 

Solution

It seems the server host key is changed, one way to solve this prolem is to remove the line containing server IP address from /root/.ssh/known_hosts . Then connect to it by SSH again, it will ask you whether trust this new host key, like following message

Type yes and press Enter key, this new host key is added to .ssh/known_hosts  then.

 

Also you can obtain the host key from the remote server, and add it to .ssh/known_hosts  manually , but we won’t cover this method in detail.

Spring disable transaction for method in transactional class

Overview

In Spring @Transactional  annotation can be used to indicate method should be run in transaction.

When @Transactional  is placed on method, this method will be run with transaction. And if it’s placed on a class, all methods of this class will run within transaction.

 

In MVC style application, we usually wrap business logic into a separate service class annotated with @Service  and @Transactional , so every method in this class will run within transaction. But how to disable transaction management for a specific single method in class which is annotated with @Transactional ?

 

Solution

Add following code before the method which you want to disable transaction management for.

Above code will tell Spring PlatformTransactionManager that transaction is not supported for this method, and transaction manager won’t create a new transaction if no transaction is existed.

And if a current transaction existed, exception will be thrown (An IllegalTransactionStateException with message “Existing transaction found for transaction marked with propagation ‘never'”).

 

java.lang.NoClassDefFoundError: java/util/Base64

Problem

In the following code, Base64 is used to encode AES encrypted data bytes to plain text for transfering via URL query parameter.

 

But when running this code on my coworker’s machine, following exception is thrown

 

It’s because that my execution environment is Java 8, but my coworker is using Java 7, and java.util.Base64 is introduced in Java 8.

 

Solution

To fix this issue, we can use Base64 class from Apache Commons Codec.

First add commons codec dependency to maven pom.xml file.

Next import Base64 class from commons-codec

 

Finally replace  Base64.getUrlEncoder().encodeToString to Base64.encodeBase64URLSafeString .

So the code will become like following

 

 

Method.invoke throws java.lang.IllegalArgumentException: wrong number of arguments

Background

There is a private downloadFile method of ZhangyoobaoLeshuaMerchantRegister class, the method signature is like following

A unit test case for this method need be created, in this test case we need pass a null value as parameter to test whether this method can handle null parameter. But a private method cannot be accessed from the instance level. The solution is to use reflection to get the method, and set its accessiblity to public temporarily (by using the Method.setAccessible method).

 

First let’s see some code

Above Java code is the unit test case, it uses reflection to get the private method downloadFile of class ZhangyoobaoLeshuaMerchantRegister, and set the access level of this method to public, then call it with parameters. (Note that if setAccessible is not called to set method access level, Method.invoke will throw IllegalAccessException.)

But running this line  result = method.invoke(register, null);  will got following error

It’s because the second argument of Method.invoke() is Object...  (An Object[] array), its elements are arguments passed in to the method. Calling Method.invoke() with null as second argument is actually trying to call the method with no arguments, but downloadFile method requires one parameter, so it definitely raises IllegalArgumentException.

 

Solution

To fix this issue, change following line

to

This will pass null as first argument for downloadFile method. new Object[]{null}  is correspondding to the Object...  argument of Method.invoke(), it’s expanded like calling downloadFile(null) .

 

Hibernate Query unexpected token: INTERVAL

I’m trying to fetch inventories which is opened at last month, for traditional SQL DATE_SUB function should be used here for date calculation. So the HQL is like following

 

But following error appeared when running it in Hibernate QL Query Consonle in Intellij IDEA

 

Unfortunatelly the INTERVAL keyword is not supported in HQL.

 

Solution

An alternative is to calculate the date in Java and pass the result dates to HQL as parameters. The code is like following

 

Implementation code for DateUtil.getLastMonthFirstDay and DateUtil.getLastMonthLastDay

 

 

 

GitLab took too much memory

The gitlab execution on our company internal server eats lots of memory, the server has 32G memory, and gitlab took more than half of it.

 

I found there are lots of unicorn processes running, and each unicorn process is taking 1.5% memory
In top  command output it’s displaying as ruby process and took nearly 500MB resident memory (by default top is sorting by cpu usage, pressing < or > key to change sorting column)
Running above command outputs 34, that means at most 34 unicorn processes are running (some other processes’ name also contain unicorn)..
Assume there are 30 real unicorn process running, each takes 500MB memory, then they will take 30*500MB=15GB memory
To decrease unicorn processes count, edit /etc/gitlab/gitlab.rb, and uncomment following line

 

Next restart GitLab

Now the memory usage is decreased.

 

(Note that running gitlab-ctl reconfigure  will not kill already running unicorn processes)