I made a change in the blogger configuration to ease the later work when blogging. It is possible that older entries are not correctly formatted.

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

Thursday, 20 June 2013

emacs Malabar Mode

Emacs Malabar is an emacs mode for editing java with emacs. While browsing I found this stackoverflow entry. I still have to dig into it, but it seems to have very useful properties (according to the Stack overflow answer):

  • tight maven integration
  • groovy console for rapid prototyping
  • junit integration
  • import help
  • extra class, overide method and implement interface helpers
  • simplistic refactoring


An interesting extension to this work is to use also a mode for maven.

emacs JDE

Emacs JDE is an emacs mode for editing java with emacs. For a long time I wanted to dig into this again, because I find myself often annoyed by having to start eclipse for very simple java projects.


An interesting extension to this work is to use also a mode for maven.

Thursday, 13 June 2013

Http Only cookies

I just learned something about http-only cookies to prevent client code of the browser to temper with cookies. I found a little more information in this article:https://www.owasp.org/index.php/HttpOnly. To do this in Java for a cookie, write:


Cookie cookie = getMyCookie("myCookieName");
cookie.setHttpOnly(true);

In particular for the session cookie, you can use the following code:

SessionCookieConfig config = request.getServletContext().getSessionCookieConfig();
config.setHttpOnly(true);

Thursday, 3 November 2011

Performance in Java

For diverse purposes I wanted to take a look at performance benchmarks for diverse implementations of collections.

I came to the following free book: Java Performace Book

Moreover, for a project for which I am working we will be do performance measures in order to choose the best infrastructure
for our project. We will do some profiling using JETM or using JProfiler depending on which gives the best infrastructure for our goal.


Thursday, 22 April 2010

Flex - generating Flex on Java Project

I collected the following from The Flex On Java Book.

$workspace > mvn archetype:create -DarchetypeGroupId=org.foj -DarchetypeArtifactId=flex-mojos-archetype -DarchetypeVersion=1.0-SNAPSHOT -DgroupId=org.foj -DartifactId=flex-bugs-ria -DremoteRepositories=http://flexonjava.googlecode.com/svn/repository

However, I still need to solve some dependency problems. In particular I needed to install the following packages:

  • com.adobe.flex.compiler:asdoc:zip:template:3.2.0.3958
  • org.graniteds:granite-generator:jar:1.1.0
  • com.adobe.flex:compiler:pom:4.0.0.7219

But this is solved by using the correct repository by adding
<repositories>
   ...
    <repository>
       <id>flexcompiler-repo</id>
       <url>http://repository.sonatype.org/content/groups/flexgroup/</url>
    </repository>
</repositories>

However, after running:

$flex-bugs-ria >mvn package
it runs quite a while with an error. It seems it just could not start the flash player. So it may be all good ;-).

It returns successful and the generated swf can be displayed in the browser

$flex-bugs-ria >mvn package-Dmaven.test.skip=true

Next step: do a more complex flex example.

Saturday, 16 January 2010

URL Rewriting Code in Java

Some time ago, I had to take a look and have something work with URL rewriting. My colleague told me there was an URL rewriting tool called URLRewrite. I will later introduce it here: http://tuckey.org/urlrewrite/.

Thursday, 14 January 2010

Java scrapbook page

I discovered the java scrapbook page. Which allows you to try simple snippets of code very easily. I should try that. It is really cool.

Sunday, 2 November 2008

Java Workqueue API

As I explained in a previouse entry, since java 1.5 there are new classes and interfaces to help the implementation of parallel systems. These structures are found in the java.util.concurrent package. But how are these structures to be used. The package description recalls the main types of interfaces and classes in this package:
  • executors
  • queues
  • concurrent collections
  • synchronizers
  • atomic (in package: java.util.concurrent.atomic )
  • locks (in package: java.util.concurrent.locks )
Of course, the structures discussed in this package have somewhat similar to the one found in typical kernel discussions: work queues and semaphores... Let us now take a look at the different parts.

Executors

Executors are containers taking care of the execution of tasks, just as SwingUtilities used to do it. Different kinds of executors are imaginable:
  • DirectExecutor (performing the task but not asynchronously)
  • ThreadPerTaskExecutor (one thread is created for each task to be performed)
  • SerialExecutor (each task is performed sequentially)
  • ThreadPoolExecutor (executor performing using a pool of threads)
  • ScheduledThreadPoolExecutor (executor performing using a pool of threads at certain specified moments or periodically)
The implentations of the java.util.concurrent package implement the ExecutorService interface which provides a number of supplementary methods.

Queues

Another useful data structure for performing task in parallel and asynchronously are queues (also known as FIFO data structures). The java.util.concurrent package provides a number of data structures for this purpose too. One particular kind of FIFO are blocking queues, for which five different versions exist:
  • LinkedBlockingQueue
  • ArrayBlockingQueue
  • SynchronousQueue
  • PriorityBlockingQueue
  • DelayQueue
The first two are queue data structure backed by a linked list, respectively an array. The synchronous queue is special in the way that the queue accept elements only if at the same times elements are removed from the queue on the other end (though I must admit I still need to look at how this actually performed). The priority blocking queue is a FIFO with priority to use the same ordering as a PriorityQueue. Finally, the delay queue is a blocking queue only providing elements when a certain delay as passed for the element to be retrieved.

Synchronizers

A number of possible techniques can be used to synchronize threads. Among these are semaphores which we discussed in the linux kernel context. Other types provided by the java.util.concurrent package, such as the CountDownLatch, used to block some actions until a number of events or elements have been counted, the CyclicBarrier, which is a resettable multiway synchronization mechanism, an exchanger to allow threads to exchange objects at some definite point and the already mentioned locks.

Concurrent Collections

The java.util Collection Framework already contained a number of snychronized or synchronizable classes. However, the java.util.concurrent package introduces new structures useful in a multithreaded context:
  • ConcurrentHashMap,
  • ConcurrentSkipListMap,
  • ConcurrentSkipListSet,
  • CopyOnWriteArrayList, and
  • CopyOnWriteArraySet
The synchronization approach of the usual synchonized classes came with a drawback for the scalability, because they imposed a strong check on the serializability of the action performed on the data structure. The Concurrent implementations are on contrary not as restrcited. It remains in the job of the developper to know when to prefer which implementation.

Timing Units

The java.util.concurrent package also introduces a new class TimingUnit to provide different kind of granularity as to which unit of interval for time measurements should be used. Here again, it would be useful to take a look at the implementation of the kernel and compare.

Atomic and Locks

Since atomic variables and locks are in other packages, I will describe them in other entries. However, it is again interesting to note that the same topics were already discussed in other entries of this blog.

Tuesday, 16 September 2008

Java AWT bug on linux with XCB

When starting my java application on linux, I have the following traceback:

Locking assertion failure. Backtrace:
#0 /usr/lib/libxcb-xlib.so.0 [0xc3e767]
#1 /usr/lib/libxcb-xlib.so.0(xcb_xlib_unlock+0x31) [0xc3e831]
#2 /usr/lib/libX11.so.6(_XReply+0x244) [0xc89f64]
#3 /usr/java/jre1.6.0_03/lib/i386/xawt/libmawt.so [0xb534064e]
#4 /usr/java/jre1.6.0_03/lib/i386/xawt/libmawt.so [0xb531ef97]
#5 /usr/java/jre1.6.0_03/lib/i386/xawt/libmawt.so [0xb531f248]
#6 /usr/java/jre1.6.0_03/lib/i386/xawt/libmawt.so(Java_sun_awt_X11GraphicsEnvironment_initD

It has already been discussed in a number of forums and bug reports:

http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6532373, or https://bugs.launchpad.net/ubuntu/+source/libxcb/+bug/87947

One possible work around seems to be to add the following variable setting when starting the application:

export LIBXCB_ALLOW_SLOPPY_LOCK=1

Friday, 12 September 2008

Java invokeLater

A number of month ago, I took a look at the new features of Java 1.5 and 1.6. And I fell on the new java.util.concurrent package.

Whoever programmed GUI interfaces in Java is certainly aware of the importance of using thread to run in the background in order to enable the user to perform other tasks and not just wait in front of a screen which is not refreshing. Using nice runnable threads, you could have a much more responsive GUI. A typical example was things of the sort:

Thread t = new Thread(){
  public void run(){
   // the task to perform which requires a certain amount of time
  }
};

SwingUtilities.invokeLater(t);

This technique is really fundamental to a well programmed graphical interface.

But since Java 1.5, there are a number of supplementary structures which can be used to perform tasks in parallel. And these are found in the package java.util.concurrent which will be the topic of a future entry.

Friday, 14 March 2008

java problem with browser

I noticed again that the java applet were not working correctly in firefox. I looked for the solution since I knew I once had solved the problem. But I could not find it easily again. After I tried to start the ControlPanel from the jre, an error occured:
Exception in thread "main" java.lang.UnsatisfiedLinkError: /usr/java/jre1.6.0_03/lib/i386/libdeploy.so: libstdc++.so.5: cannot open shared object file: No such file or directory
I installed the compatibility libraries ( after calling yum provides libstdc++.so.5. Once installed, java applets were working just fine.

Thursday, 10 January 2008

Java NIO - Channels

Channels are the other most important part of the NIO API.

A channel is a communication channel, that is a pipe of data.

The Channel interface has two methods: isOpen() and close() ( the latter may throws an IOException). Most Channels are specified using Interfaces. For example, there is the InterruptibleChannel interface which is a marker interface specifying how the channel behaves when a thread accessing it is interrupted.

There are different types of Channels.

  • ReadableByteChannel and WritableByteChannel (Interfaces which define how to read from or write in a channel using a ByteBuffer)
  • GatheringByteChannel and ScatteringByteChannel (interfaces defining methods to read from or write to a channel using an array of ByteBuffers)
  • InterruptibleChannel ( interface defining the behaviour of the channel when the thread using it is interrupted)
  • FileChannel (class for the interaction with a Channel based on a File)
  • SocketChannel and ServerSocketChannel, and DatagramChannel ( classes for the communication based on sockets)
Channels cannot be opened directly. The method used to create a Channel object depends on the type of Channels. For example FileChannel are opened using Streams or RandomAccessFiles.

ChannelsUtilityClass There is a Utility Class called java.nio.channels.Channels, which has a number of utility methods to convert between the old and the new API. There are two Channel creating methods: newChannel(InputStream) and newChannel(OutputStream) which return a ReadableByteChannel and a WritableByteChannel respectively, and there are the newInputStream(ReadableByteChannel ch) and newOutputStream(WriteableByteChannel ch) which return InputStream and OutputStream respectively. The same kind of methods exist also for Readers and Writers: newReader and newWriter methods return the appropriate classes.

FileChannel FileChannel have different facets. Using the utility class Channels we can open FileChannel instances. Note that Channels opened from InputStreams are only readable while Channels from OutputStreams are only writable.

A number of methods are important for FileChannels. The force() methods writes to the disk the data which is actually cached ( for performance reasons). The truncate() methods gives a way to cut off all the data which is further than a given data. The methods position() and positions(long positions) are methods to decide at which part of the file the Channel should go.

Pipe Another important class is the Pipe class in general it is a conduit through which data can only go in one direction. Each pipe has a SourceChannel and a SinkChannel. The information written in the SinkChannel (a WritableChannel) is then transmitted to the SourceChannel (a ReadableChannel).

MemoryMappedFiles This type of Buffer and Channels are a far reaching topic so we treat them separately. SocketChannels SocketChannels are also a separate topic so we treat them also separately. Locks The topics of locks will also be covered in its own entry.

Monday, 7 January 2008

Google Web Toolkit

The Google Web Toolkit (GWT) is a framework used to develop Webapplications. It works by transforming the Java in Javascript. Moreover, GWT has the following additional features:
  1. Interfaces for RPC Calls
  2. Integration with JUnit
  3. Lots of Widgets for GUI Design (similar to Swing in the way of its use
It seems that GWT uses mainly Java to develop the application and theat therefore the typical Java development tools can be reused for the purpose

Sunday, 6 January 2008

JAXB

Another interesting Java technology pushed by Sun is JAXB, it stands for "Java Architecture XML Binding". Most of the information found here comes from Sun's JAXB tutorial. It is a means of coding Java Object into XML using the descriptive power of the XML Schemas. It is possible to generate Java Object from XML descriptions using a Schema. To simplify the process, it is also possible to use JAXB annotations of Java classe in order to generate the schemas needed for such schemas. So the important steps to consider when using this is:
  1. write the classes of the objects which should be stored as XML files, or DOM nodes, etc...
  2. annotate them so that the corresonding schema can be generated
  3. generate content according to schema
  4. unmarshal the content (i.e load into content tree in Java)
  5. validate if required
  6. marshall, i.e export again as XML
This page gives a list of (XML)Schema-Java correspondance tags. As specified, the binding can be generated either using annotations inside the Java files, or it is also possible to use external configuration files outside these files.

Tuesday, 11 December 2007

Learning Groovy or Groovy Learning

Well I decided to learn Groovy.

It seems to have the most interesting properties, something like python or perl for java, except without the things I dislike from perl and python (about the former, it's not readible, about the latter I dislike the formatting constraint).

I haven't tested the compilation issue or the speed but this should not be a big problem. We'll see.

I made myself a summary from the groovy syntax and features from the tutorial.

I really like: (see: the differences from Java )

  • closures
  • the simple list and map syntax... What a pain it is in Java.
  • groovy markup as a nice way to work with XML, HTML, ANT...
  • easy use of regular expressions !!!!!! YEAH!
  • less problems with quotation marks and multi line strings
  • Gstring ( something to use variables for strings )
  • lots of supplementary useful methods (see the groovy jdk )
  • easy iteration ( though 1.5 did make things a lot easier)
  • easy setter/getter mechanism for beans
  • using closure for listeners

I'll have to try GRAILS, because I tried Rails And I found it not bad really.

It would also be interesting to see whether Groovy can be combined with JSF which I really liked.

But I think I am just starting feeling groovy.