Showing posts with label java. Show all posts
Showing posts with label java. Show all posts

Friday, August 31, 2018

Java ffmpeg wrapper jave

Java ffmpeg wrapper

In the last months we did some major enhancements in the jave library which can be used from java to analyze/convert audio and video files with the use of ffmpeg.

The project homepage can be found here https://github.com/a-schild/jave2

The main changes as to the original package are:
- Support for Windwos 32+64 bit
- Support for Linux 32+64 bit
- Support for OS-X 64bit
- Upgraded to ffmpeg 4.x (From 3.x)
- Enhanced output parsing
- Added options for running the encoding/decoding as a separate thread
- Added to maven central for simpler usage
- Many smaller enhancements

The project was initially started by Carlo Pelliccia at http://www.sauronsoftware.it/projects/jave/

You are welcome to contribute to the project with ideas and code.
You can use the github page for this.

Wednesday, June 21, 2017

JSVC fails with error 11 after latest linux kernel Upgrades on debian/ubuntu

This morning, after doing some apt-get update/upgrades on various debian systems, we noticed that many of our java services did no longer work.
Since we use it to handle all our tomcat instances on many many servers, the impact is heavy.No single tomcat did run this morning...

Looking into the tomcat logs, we did see this message:
Service killed by signal 11

After some more investigations, it looks like the problem is related to the new kernel version installed in the upgrade process.

Google didn't find much about this, but the more important ones are here:
So the solution for now, is either switch to not using jsvc to start your services, or use a older or a "unstable" kernel.

The kernel causing the problems is
3.16.0-4-amd64 #1 SMP Debian 3.16.43-2+deb8u1 (2017-06-18) x86_64
 
With the older one it does work
3.16.0-4-amd64 #1 SMP Debian 3.16.43-2 (2017-04-30) x86_64
 
To switch back to the older kernel just do this, and reboot your system:
apt install linux-image-3.16.0-4-amd64=3.16.43-2  
 
Reverting back to a older kernel is discouraged, since this does not solve the security problem.

Fortunately there is a very simple work arround for it.
When you start jsvc, just specify it to use a larger stack.
For tomcat you can put this in your startup file, so the daemon.sh takes the new options for jsvc.
 
export JSVC_OPTS=-Xss1280k

Thanks to https://community.ubnt.com/t5/UniFi-Wireless/IMPORTANT-Debian-Ubuntu-users-MUST-READ-Updated-06-21/td-p/1968252

Thursday, March 30, 2017

Java API for nextCloud/ownCloud

Java API for nextCloud

Currently the nextCloud and ownCloud solutions have a very big drive in the market. One of the main reason is, that you have control over your data.

When you look at recent events and news, then we can confirm this.
In our company we have been using ownCloud/nextCloud since version 5.x and have a long positive history with the solution.
We also provide managed nextCloud solutions, for sharing data with your customers for example, as backup back end and many other use cases.

The use case

To integrate nextCloud in your business processes, you sometimes need to automate things a bit more than what is included out of the box.
If possible we do this with shell scripts, but for complexer work flows, this isn't enough.
In those cases we use the full power of server side java applications.
Unfortunately the API of nextCloud is not fully REST/Webdav, it has some parts (Mainly file sharing and provisioning) which work with a XML style interface.

The java integration

To be able to use these API also from java applications, we have created a API library which exposes the important parts for simple usage in java applications.
To give back something to the open source community, we have decided to publish the library under the GPL license, so it can be used by other applications.
You can find the library source on github, and feedback and additions to the api are appreciated.


Happy coding

Thursday, March 31, 2016

Installing Java 8 on Debian Jessie

Installation Oracle Java 8 on Debian Jessie

Debian 8 alias Jessie ships with OpenJDK 7 which is fine in many cases. But sometimes you need a more recent version.
In that case you can use the ubuntu ppa archives as install source.

Just type these commands in the console of your Debian system and it should install just fine, also providing automatic security upgrades as they become available.

echo "deb http://ppa.launchpad.net/webupd8team/java/ubuntu trusty main" > /etc/apt/sources.list.d/webupd8team-java.list
echo "deb-src http://ppa.launchpad.net/webupd8team/java/ubuntu trusty main" >> /etc/apt/sources.list.d/webupd8team-java.list
apt-key adv --keyserver keyserver.ubuntu.com --recv-keys EEA14886
apt-get update
apt-get install oracle-java8-installer
java -version

Thursday, September 25, 2014

Don't write boilerplate code for java objects any longer

When you write java classes, you usually have many properties you expose via getter/setter methods.
This gives a lot of code, which is not very interesting to write and maintain, but for the sake of java bean (and other reasons) you will do it correctly.

It's one of the features of the IDE that you can let them generate the proper getter/setter methods.
Usually it's called something like "Encapsulate property access". You then select the properties you wish setter/getter created and you are done, the idea puts the correct code in your class.

Nice, but, it could be simpler.
The drawback of all this is, that you fill up your class file with a lot of set/get code which you usually don't want to see.

Fortunally there is help for this as well.
Look at the project Lombook.

With this project you write your class like this:

01 import lombok.AccessLevel;
02 import lombok.Setter;
03 import lombok.Data;
04 import lombok.ToString;
05
06 @Data public class DataExample {
07   private final String name;
08   @Setter(AccessLevel.PACKAGEprivate int age;
09   private double score;
10   private String[] tags;
11   
12   @ToString(includeFieldNames=true)
13   @Data(staticConstructor="of")
14   public static class Exercise<T> {
15     private final String name;
16     private final T value;
17   }
18 }
 
These 18 lines of code do the same as these 118 lines of plain java coding:
001 import java.util.Arrays;

002
003 public class DataExample {
004   private final String name;
005   private int age;
006   private double score;
007   private String[] tags;
008   
009   public DataExample(String name) {
010     this.name = name;
011   }
012   
013   public String getName() {
014     return this.name;
015   }
016   
017   void setAge(int age) {
018     this.age = age;
019   }
020   
021   public int getAge() {
022     return this.age;
023   }
024   
025   public void setScore(double score) {
026     this.score = score;
027   }
028   
029   public double getScore() {
030     return this.score;
031   }
032   
033   public String[] getTags() {
034     return this.tags;
035   }
036   
037   public void setTags(String[] tags) {
038     this.tags = tags;
039   }
040   
041   @Override public String toString() {
042     return "DataExample(" this.getName() ", " this.getAge() ", " this.getScore() ", " + Arrays.deepToString(this.getTags()) ")";
043   }
044   
045   protected boolean canEqual(Object other) {
046     return other instanceof DataExample;
047   }
048   
049   @Override public boolean equals(Object o) {
050     if (o == thisreturn true;
051     if (!(instanceof DataExample)) return false;
052     DataExample other = (DataExampleo;
053     if (!other.canEqual((Object)this)) return false;
054     if (this.getName() == null ? other.getName() != null : !this.getName().equals(other.getName())) return false;
055     if (this.getAge() != other.getAge()) return false;
056     if (Double.compare(this.getScore(), other.getScore()) != 0return false;
057     if (!Arrays.deepEquals(this.getTags(), other.getTags())) return false;
058     return true;
059   }
060   
061   @Override public int hashCode() {
062     final int PRIME = 59;
063     int result = 1;
064     final long temp1 = Double.doubleToLongBits(this.getScore());
065     result = (result*PRIME(this.getName() == null this.getName().hashCode());
066     result = (result*PRIMEthis.getAge();
067     result = (result*PRIME(int)(temp1 ^ (temp1 >>> 32));
068     result = (result*PRIME+ Arrays.deepHashCode(this.getTags());
069     return result;
070   }
071   
072   public static class Exercise<T> {
073     private final String name;
074     private final T value;
075     
076     private Exercise(String name, T value) {
077       this.name = name;
078       this.value = value;
079     }
080     
081     public static <T> Exercise<T> of(String name, T value) {
082       return new Exercise<T>(name, value);
083     }
084     
085     public String getName() {
086       return this.name;
087     }
088     
089     public T getValue() {
090       return this.value;
091     }
092     
093     @Override public String toString() {
094       return "Exercise(name=" this.getName() ", value=" this.getValue() ")";
095     }
096     
097     protected boolean canEqual(Object other) {
098       return other instanceof Exercise;
099     }
100     
101     @Override public boolean equals(Object o) {
102       if (o == thisreturn true;
103       if (!(instanceof Exercise)) return false;
104       Exercise<?> other = (Exercise<?>o;
105       if (!other.canEqual((Object)this)) return false;
106       if (this.getName() == null ? other.getValue() != null : !this.getName().equals(other.getName())) return false;
107       if (this.getValue() == null ? other.getValue() != null : !this.getValue().equals(other.getValue())) return false;
108       return true;
109     }
110     
111     @Override public int hashCode() {
112       final int PRIME = 59;
113       int result = 1;
114       result = (result*PRIME(this.getName() == null this.getName().hashCode());
115       result = (result*PRIME(this.getValue() == null this.getValue().hashCode());
116       return result;
117     }
118   }
119 }
 
 
 
 
So with project lombok you can concentrate on the real code, and the annotations do expand on build to the boilerplate code.
There are many options in lombok to also generate other things for java classes, be sure to look at the documentation.

There is just one "bad" thing about it:
By definition annotations should not create java code, but in this case I think it is worth the "break" of rules.

Thursday, August 21, 2014

Using "related" fields/properties in vaadin tables with JPA containers

Vaadin is a Java framework for building modern web applications that look great, perform well and make you and your users happy.

It has a lot of features which help you in building data driven applications, without having to code everything yourself.
As with any powerfull frameworks you often come to a plcae where a "simple" thing isn't that simple to implement.
Perhaps the framework is just not prepared for that simple feature you wish to use, or you don't find the way to use it correctly.

Vaadin has containers which allow you do present data in forms and tables, without needing to code everything yourself.
There exist different types of containers, depending on your original data source, for example you can have SQL database, Bean objects and many others as a source.

With the JPA container, you can use to handle then whole data stuff with JPA.
So you could for example use hibernate or eclispelink to back your java objects in a sql database.

There is a whole chapter in the book of vaadin describing the JPA container.
When you have a object which has relations to other objects, then you can also specify the JPA container about such "related" properties or fields.
For example when you have a Person object, which has a relation to a country object, you can teel the JPA container about the additional fields available from the Country object.

// Have a persistent container
JPAContainer<Person> persons =
    JPAContainerFactory.make(Person.class, "book-examples");

// Add a nested property to a many-to-one property
persons.addNestedContainerProperty("country.name");
        
// Show the persons in a table, except the "country" column,
// which is an object - show the nested property instead
Table personTable = new Table("The Persistent People", persons);
personTable.setVisibleColumns(new String[]{"name","age",
                                           "country.name"});


// Have a nicer caption for the country.name column
personTable.setColumnHeader("country.name", "Nationality");


The Vaadin JPA container automagically knows to go via the object/database relation and retrieve the correct values.

When you use the filtering table add on available from the vaadin add ons, you can also implement filters on these additional fields.

For this you have to implement the FilterGenerator interface and then tell the table which properties are handled with this filter.
Of course your filter code must then generate the correct filter criterias.

@Override
public Container.Filter generateFilter(Object propertyId, Object value)
{
    if ("country.name".equals(propertyId))
    {
        if (value != null && value instanceof String)
        {
            return new Like("name", value.toString()+"%");
        }
    }
}


Thursday, January 23, 2014

Vaadin JPAContainer, Filterable table and related entities

In the web 2.0 framework Vaadin you have containers which provide data to be displayed in your application.
These containers are very flexible and can for example be a databasebackend, a JPA system or your own implementation.

In my past post I showed you how to implement filtering for related fields.

Vaadin also provides many data aware components, for example a table component.
The table component is very sophisticated and allows displaying huge amounts of data in the webbrowser. The table has a lazy loading system, so as only the visible parts of a table are retrieved from the backend and sent to the webbrowser.

There also exists a addon component which has built in filter and sort support.

When using the table with jpa container, then you have to use several tricks to allow filtering on related fields.

The first trick is to display the related fields in the table

For this the simplest way is to add these related fields to the main entity you display.
You don't have to store the property, it's enough for the JPA container when you have a getXXX() method.

That way it displays the additional properties in the table. You could also use this way to show calculated related fields in the table.

This could look like this:
public String getProjectInfos()
{
    return projectNr+" - "+name;
}


When you now display these in the table, you can also filter on that field.
But then, you will get a error message, telling you that the sql select did not find the field for the where condition.

For this to work, you have to use the second trick

You can build your own FilterGenerator which then builds the correct criterias for your tables and relations.

contentTable.setFilterGenerator(new MyFilterGenerator());

The filter generator has different methods, when you don't want to override them, you can just return NULL and then the default behaviour is done.

For us the interesting method is the generateFilter() method.
Here you can implement your own conditions.

If you for example wish to filter with the LIKE statement, then you can do it this way:

@Override
public Container.Filter generateFilter(Object propertyId, Object value)
{
    if ("contract".equals(propertyId))
    {
        if (value != null && value instanceof String)
        {
            return new Like("contractID", value.toString()+"%");
        }
    }
    return null;

}

To now filter on a related table, you can use the IN condition, which then builds the correct sql statements.

@Override
public Container.Filter generateFilter(Object propertyId, Object value)
{
       if ("contracts".equals(propertyId))
        {
            if (value != null && value instanceof String)
            {
                String  lsNr= (String) value;
                EntityManager em= ((MyVaadinUI)UI.getCurrent()).getEntityManager();
                TypedQuery<Shippings> tq= em.createNamedQuery("Shippings.findByLikeProjectNr", Shippings.class);
                tq.setParameter("projectNr", lsNr+"%");
                List<Shippings> rs= tq.getResultList();
                if (rs.isEmpty())
                {
                    return new IsNull("shipping");
                }
                else
                {
                    if (rs.size() > 2000)
                    {
                        Notification.show("To many entries", "\n\nMake more restrictions", Notification.Type.WARNING_MESSAGE);
                        return new IsNull("shipping");
                    }
                    else
                    {
                        return new In("shipping", rs);
                    }
                }
            }
        }

    return null;
}

So when the property contracts has some value, we filter the contracts by projectNr and use the resulting result set to specify as the IN criteria.

If you are still with me, then you probably have a compiler error when you try this code.
The reason is, that the default JPAContainer has no implementation of the IN criteria.
Unfortunally the design of the JPAContainer does not allow to expand the capabilities in that area.

Fortunally there exists a fork of the JPAContainer which just provides the required IN() clause.
You can download the sources and add them to your project.

The source can be found here: https://github.com/lelmarir/jpacontainer

You can learn how to show related / nested properties in this post.

Monday, March 4, 2013

Video encoding with Java

After some reasearch for a good performing java library to do different video conversions, we stumbled over many projects.

On closer inspection we found that most of them are old projects, with no one maintainig them any longer.

We finaly got to the jave project which did what we needed.
Unfortunally it is based on a old version of ffmpeg, which has some problems depending on the input/output formats used.

So we did upgrade the binaries, adapted the parsing code and added a few new features to the library.
After some time trying to contact the original author to push back the changes upstream, we descided to create a new project which is maintained by us.

We named this Jave2, to show the difference to the original jave probject.
The project is hosted on github, you can find it here.

Wednesday, November 2, 2011

Using FTPS with the commons jakarta net library

Using FTPS via the jakarta commons library is not that complicated.

But it has a few things to know:

The correct sequence is this:

FTPClient.connect("YourServer"); 
FTPClient.execPBSZ(0); 
FTPClient.execPROT("P"); 
FTPClient.login("YourUserName","YourPassword"); 

If you skip the execPBSZ or execPROT calls, then your ftps server will probably deny access to you.

If you receive strange errors like:

java.io.IOException: DerValue.getOctetString, not an Octet String 10 

or

Caused by: java.security.cert.CertificateParsingException: java.io.IOException:
DerValue.getOctetString, not an Octet String: 10
        at sun.security.x509.X509CertInfo.(Unknown Source)
        at sun.security.x509.X509CertImpl.parse(Unknown Source)
        at sun.security.x509.X509CertImpl.(Unknown Source)
        at sun.security.provider.X509Factory.engineGenerateCertificate(Unknown S
ource)
        at java.security.cert.CertificateFactory.generateCertificate(Unknown Sou
rce)
        ... 16 more
Caused by: java.io.IOException: DerValue.getOctetString, not an Octet String: 10

        at sun.security.util.DerValue.getOctetString(Unknown Source)
        at sun.security.x509.Extension.(Unknown Source)
        at sun.security.x509.CertificateExtensions.init(Unknown Source)
        at sun.security.x509.CertificateExtensions.(Unknown Source)
        at sun.security.x509.X509CertInfo.parse(Unknown Source)

Then you (or your pfts server) are probably behind a checkpoint firewall.
There is nothing you can do, just talk with the firewall admin and tell him to fix the "FTP Bounce" attack.
He will see this in the logs:

Attack Information: The packet was modified due to a potential Bounce Attack (Telnet Options)

Here is the option to disable this on a checkpoint firewall:

Wednesday, April 27, 2011

Java server side connector for CKEditor

We just published our first version of a java connector for CKEditor V3.x

You can find it here: http://sourceforge.net/projects/jckconnector/

It allows you to integrate file browsing/linking in your java server application.
Technically it uses vaadin the webinterface to the user.

You can provide your own version of the file store and link store.

It's the alpha release with probably some security holes "included"

Monday, January 17, 2011

Java application under windows 7

Developing java application fro windows 7 ?

Then look at this library, it might help integrate better with windows 7.
Not a "must have" but your users will appreciate it as a "nice to have".
And 50% of application functionality are "nice to have"

http://www.strixcode.com/j7goodies/

Tuesday, March 2, 2010

Storing binary stuff in database with tomcat

When storing binary data in a database, usually you do the following:

PreparedStatement pStmt= conn.prepareStatement("insert into FileData (FileData, BinData) values (?,?)");
pStmt.setString(1, fileDataID);
pStmt.setBinaryStream(2, fInfo.getInputStream(), fInfo.getSize());
pStmt.executeUpdate();


That works fine, but sometimes you can get this error when using it
with tomcat connection pooling:


java.lang.AbstractMethodError: org.apache.tomcat.dbcp.dbcp.DelegatingPreparedStatement.setBinaryStream(ILjava/io/InputStream;J)V


The reason for this is, that you passed the last parameter as a long, instead of a int.

This is the fix for it:
PreparedStatement pStmt= conn.prepareStatement("insert into FileData (FileData, BinData) values (?,?)");
pStmt.setString(1, fileDataID);
pStmt.setBinaryStream(2, fInfo.getInputStream(), (int)fInfo.getSize());
pStmt.executeUpdate();


Took half a day to figure out the problem.