For one of my recent projects I needed to implement X.509 certificate validation library that validates a certificate across given set of trusted root certificated and a set of intermediate certificate. Initially I thought this is a problem that has already out-of-the-box solution in BouncyCastle but the CRL verification was found to be unpleasant to implement and not available out-of-the-box.
The task was formulated as follows: given a X.509 certificate and a set of trusted root certificates and a set of intermediate certificates to build a certification chain (if possible) and to extract the CRL distribution point from the certificate (if available) and to check whether the certificate is not revoked. It was required to support HTTP, HTPS, FTP and LDAP based distribution points.
I will not get into more details because I don’t heve too much time but I think the code is clear enough, so most developers will be able to read, understand and use it:
I hope the above coude could be useful to anybody trying to build and validate X.509 certificate chain and check the CRL revocation status.
Posted by nakov as java, blog at 12:10 AM EET
Comments Off
My open-source book “Introduction to Programming with Java” has new Web site: www.introprogramming.info. The book is written by a team of 30 authors managed by me and is an excellent tutorial for beginners. It is a book about the fundamentals of computer programming, data strustures, algorithms and logical thinking, not just a Java book. The only weakness (or respectively a plus) is that it is entirely in Bulgarian (sorry, no transaltions are available).
Ето няколко полезни линка, свързани с книгата:
Posted by nakov as news, java, blog at 4:45 AM EEST
Comments Off
All Java and Java EE developers are invited to the unique for the Balkans and Eastern Europe conference on Java technologies called Java2Days. At the conference distinguished speakers will talk in Sofia about Java, Java EE 6, JBoss, EJB 3.1, Spring Framework, JPA, OSGi, GWT, JSF, jBPM, Wicket, JRockit, cloud computing and other hot technologies. Some of the speakers:
- Reza Rahman - independent Java EE consultant, co-author of the book “EJB 3 in Action”
- Mircea Markus - core developer and trainer at JBoss
- Bruno Bossola - agile coach at Vodafone Global and Java champion
- John Willis - CEO at Zabovo Corp., cloud computing expert
- Josh Long - enterprise architect, speaker, consultant, and author
For more information visit the conference official Web site: http://java2days.com/.
Posted by nakov as news, java, blog at 7:14 PM EEST
Comments Off
In a recent Java project I needed to develop and provide to external clients a RESTful Web Services interface to an internal system. After some research I found that using JAX-RS and its open-source implementation Jersey with Spring and Hibernate as back-end will be great technologies stack for this project. Seems easy but unfortunately I found that JAX-RS does not support optional path parameters.
Customer requested each service to have an optional path parameter called “format” that specifies the output format. All services were required to support multiple output formats: XML, plain text, CSV, JSON, PDF, etc. For example if I request this URL: http://myserver.com/services/location/3/format/xml, the output should be XML, but if I request just http://myserver.com/services/location/3 without “format” parameter, the result should be plain text.
Mandatory Path Parameters
Using a path pattern like this:
makes the parameter “format” mandatory. If we skip it, the request will not match the path.
Optional @Path Parameters in JAX-RS
Using regular expressions and a simple hack can overcome this limitation in JAX-RS. The following example defines two optional path parameters “format” and “encoding”:
Requesting http://localhost:8080/services/user/3, will return “No format specified. No encoding specified”.
Requesting http://localhost:8080/services/user/3/format/pdf/encoding/utf8, will return “Format=pdf Encoding=utf8″.
Requesting http://localhost:8080/services/user/3/encoding/utf8, will return “No format specified. Encoding=utf8″.
Flexible @Path Parameters in JAX-RS
If we need more flexibility, we can match the entire path ending in the REST request and map it in key-value style (HashMap< String, String >):
Requesting http://localhost:8080/services/location/3, will return “Location: id=3″.
Requesting http://localhost:8080/services/location/3/format/json, will return “{ ‘location’ : { ‘id’ : ‘3′ } }”.
Enjoy!
Posted by nakov as java, blog at 2:47 AM EEST
Comments Off

Recently my team of 20 authors and few editors working on creating a contemporary book about the fundamentals of computer programming, data structures and algorithms is ready with the public official release of the book: http://introjavabook.googlecode.com/files/Introduction-to-Programing-with-Java-Book-v1.01.pdf.
The book “Introduction to Programming with Java” focuses on the fundamentals of computer programming, logical, algorithmical thinking, problem solving, data structures and algorithms. It uses a contemporary object-oriented approach and language but is valuable for any non-Java programmer in the begginning of his career. The book is free and open-source.
The contents of the book:
- Chapter 0. Preface
- Chapter 1. Introduction to Programming
- Chapter 2. Primitive Types and Variables
- Chapter 3. Operators and Expressions
- Chapter 4. Console Input and Output
- Chapter 5. Conditional Statements
- Chapter 6. Loops
- Chapter 7. Arrays
- Chapter 8. Numeral Systems
- Chapter 9. Methods
- Chapter 10. Recursion
- Chapter 11. Creating and Using Objects
- Chapter 12. Exceptions Handling
- Chapter 13. Strings
- Chapter 14. Defining Classes
- Chapter 15. Text Files
- Chapter 16. Linear Data Structures
- Chapter 17. Trees and Graphs
- Chapter 18. Dictionaries, Hash Tables and Sets
- Chapter 19. Data Structures: Comparison and Best Practices
- Chapter 20. Object-Oriented Programming Principles
- Chapter 21. High-Quality Programming Code
- Chapter 22. How to Solve Programming Tasks?
- Chapter 23. Example Topic from Exam in NASD – 30.09.2005
- Chapter 24. Example Topic from Exam in NASD – 8.04.2006
- Chapter 25. Example Topic from Exam in NASD – 11.12.2005
|
More information is available on the book’s official Web site: http://www.introprogramming.info/.
Posted by nakov as news, java, blog at 3:36 PM EET
Comments Off
I use SWING for long time but I am amazed how slowly this GUI framework evolve and how far is it comparing to Windows Forms, WPF and Flex. One of the small missing things in Swing is that there is not standard way to display an error message with Exception stacktrace. So I created such dialog and I want to share it with everybody using SWING. The result look like this (shrinked form):

When the dialog is expanded, it shows the exception:

It seems like creating such a dialog is trivial task but unfortunately you need to solve a number of problems related to correct positioning, scrolling issues, etc. When the exception is large, it needs a scroller. When the error description is logn it needs good layout and scroller. This makes the things a little bit challenging. See the code below (the ErrorDilaog class):
The ImageUtils class provides a simplified API for loading images from a file:
The class ImagePanel shows image in a JPanel:
Download the entire example source code (Eclipse project): errordialog.zip.
I hope all this would be helpful to anyone. Enjoy!
Posted by nakov as java, blog at 11:31 PM EET
Comments Off
Today I needed to optimize the file size for a commercial Java applet (Web form signer) that my team is developing in the last month. We use few external libraries (Bouncy Castle Crypto API and few others) and these libraries are above 2 MB in several JAR files. If we were developing server side application, it would not be a problem, but when we are building an applet, the size of the applet and all its JARs matters.
I needed to remove all unused classes from the JARs that my applet was including a part of itself. For example the Bouncy Castle JARs were about 1,6 MB but the applet used only a small part of all algorithms and standards implemented by these JARs.
Extracting All Classes Loaded in the JVM
My final goal was not only to remove all unused classes from all JAR files but also merge these JARs along with the applet classes into a single JAR file that has the smallest possible size. I came with the idea to run the applet, to go through all its functionality and to get a list of all classes currently loaded into the JVM executing the applet. At this moment all classes required by the applet for its normal work will be loaded in the JVM and all classes that was never used by the applet will not be loaded in the JVM. If I package all these classes into a new JARs, it will contain the minimal set of classes nedded by the applet along with the applet classes.
As fas as I know how the JVM and the class loaders behave, this should be correct - we can expect all classes required by the applet to be loaded in the JVM after its entire functionality is accessed at least once.
I had a serious problem: how to get a list of all classes loaded in the JVM.
List All Classes Loaded in the JVM
Geting a list of all classes that are loaded in the JVM at some moment is not easy job. We can write Java agent through the java.lang.instrument API but I needed to do this at runtime (just to add few lines to the applet). I found in Google a very nice class for accessing all classes loaded in the JVM written by Vladimir Roubtsov and published in Java World (http://www.javaworld.com/javaworld/javaqa/2003-07/01-qa-0711-classsrc.html). With few modifications it successfully listed all classes loaded in my applet.
Create a Single JAR with All Classes Loaded in the JVM
The next step was to create a single JAR with all classes loaded in the JVM. This was not complex. I created a class with few methods for copying all currently loaded classes into some directory specified as parameter. Here is the source code:
It is not a rocket science. I go through all classes loaded by the current class loader and by the system class loader, get their fully qualified name (e.g. org.bouncycastle.cms.CMSSignedData) and their source URL location (e.g. jar:file:/C:/PROJECTS/GeneratePKCS7andVerify/lib/bcmail-jdk15-140.zip!/org/bouncycastle/cms/CMSSignedData.class) and I copy their binary contents (from the URL) to the destination folder (into a .class file). In the mean time I recreate the package structure (following the full class name with all its packages). Finally I get a directory containing all class files loaded in the JVM at the time of caling my method and I can manually package them in a JAR (removing beforehand all system Java classes). That’s all. I use slightly modified version of ClassScope.java.
You can download a fully functional example here (Eclipse project): ExtractAllClassesFromJVMIntoJAR.zip.
Posted by nakov as java, blog at 11:32 PM EEST
Comments Off
The next meeting of the Bulgarian Java Users Group (BGJUG) will be held on 28 August 2008 in the Chemistry Faculty, Hall 210 in Sofia, starting at 18:30.
The topicwill be the Java FX technology. Speaker will be Naiden Gochev.
Posted by nakov as news, java at 2:08 PM EEST
Comments Off
In one of my last projects I was working on Java applet that needed to digitally sign data forms at the client side in a Web browser. I with my team developed the applet and it did the job well. Unfortunately our customer decided to put multiple instances of the
applet in a single Web page. This resulted to unexpected behaviour: sometimes (from time to time) the browser window hangs. You can achieve this by refreshing the browser window continuously very fast pressing [F5] key many times). Strange problem, right?
JSObject is not thread-safe
Initially I thought that this is some kind of threading issue because:
- it happens only when we have multiple instances of the applet
- it happens sometimes, not regularly
After experimenting with lots of examples I found that the problem is related to JSObject. I created simple applet and it was able to run with multiple instances. After that I added access to the browser DOM by using JSObject.getWindow(this) and the “hang” problem was introduced.
It was clear that the JSObject class is not thread-safe and this causes the unregular hangs of the browser window. When we have several instances of the applet running in different browser windows we don’t have this issue. It appears only when we have several applets on a single Web page. The reason is simple: when multiple applets are running in the same browser window, all of them share the same instance of the JVM (Java Virtual Machine). In the same time all these applets share the same browser window and thus the same browser DOM tree. This means that JSObject.getWindow(this) could be called by multiple threads in the same time.
JSObject is not thread-safe
To overcome this problem we synchronize all code accessing JSObject by JSObject.class. Why JSObject.class? In a single virtual machine this class should be loaded only in the JVM so synchronizing by it will block multiple threads runngin in a single JVM to access JSObject at the same time.
Below is a sample applet that illustrates how to perform such synchronization:
If you remove the synchronization of the code above, and put the applet several times in a single form, it will randomly hang the Web browser.
Posted by nakov as java, blog at 5:29 PM EEST
Comments Off
Today I needed to debug Java applet running in Internet Explorer from Eclipse. I was not straightforward. Let me share the experience how to do this:
1) First enable remote debugging in the Java Plugin:
a) Start –> Settings –> Control Panel –> Java –> Java Applet Runtime Settings –> View
b) Add the following to the “Java Runtime Parameters”:
-Djava.compiler=NONE -Xnoagent -Xdebug -Xrunjdwp:transport=dt_socket,address=8000,server=y,suspend=n

This will enable remote debuging on TCP port 8000 for all applications running inside the Java Plugin (in Internet Explorer). Make sure this port is not used by other applications.
3) Close all Web browser Windows
4) Put breakpoints in your source code (you need to have an Eclipse project containing the applet source code)
4) In Eclipse create a remote debugging configuration:
a) Run –> Open Debug Dialog –> Remote Java Application –> New
b) Assign host = localhost and port = 8000

Press the “Debug” button and enjoy debugging.
Note: Don’t forget to build your applet with debug information (javac -g). I am buidling my applet with ant so I just added <javac debug=”true” … />.
Posted by nakov as java at 6:53 PM EEST
Comments Off