среда, 4 декабря 2013 г.

Apache POI 3.9 DOCX Cookbook

Here is my cookbook recipes for working with Apache POI 3.9 and DOCX.

Api for DOCX in Apache POI is scarce.

1) It is difficult to grab the terminology at first. To understand the terminology you can look at the http://officeopenxml.com. This site contains the information about the structure of DOCX format.

2) Create paragraph and center it:
XWPFDocument document = new XWPFDocument();
XWPFParagraph paragraph = document.createParagraph();
paragraph.setAlignment(ParagraphAlignment.CENTER);

3) Fill the paragraph with bold text, size 16 and break after it:
XWPFRun paragraphRun = paragraph.createRun();
paragraphRun.setBold(true);
paragraphRun.setText("my text");
paragraphRun.setFontSize(16);
paragraphRun.addBreak();

4) Merge 2 cells in table (like colspan attribute in html):
XWPFTable table = document.createTable();
XWPFTableRow row = table.getRow(0);
row.getCell(0).setText("my cell);
// get a table cell properties element (tcPr)
CTTcPr tcpr = row.getCell(0).getCTTc().addNewTcPr();
// set gridspan
CTDecimalNumber gridSpan = tcpr.addNewGridSpan();
gridSpan.setVal(new BigInteger("2")); <-- the colspan of cell

5) Set background of cell:
// set fill color
 CTShd ctshd = tcpr.addNewShd();
 ctshd.setColor("auto");
 ctshd.setVal(STShd.CLEAR);
 ctshd.setFill("E3E7E9"); <--  RGB color

6) Set width of cell to 100%:
// setwidth
CTTblWidth cttblwidth = tcpr.addNewTcW();
cttblwidth.setW(BigInteger.valueOf(100));
cttblwidth.setType(STTblWidth.PCT);




пятница, 29 ноября 2013 г.

WebSphere Portal 7. Longrunning tasks in portlets.

Sometimes operations in portlets can take a long time to complete, like a report generating task. It is better not to execute such tasks in a synchronous way, because :
  • they block the web container thread, which slows down user satisfaction;
  • make the appearance of application to freeze;
  • the task can take up time of no more then request timeout;
  • you cannot cancel the task;
  • you don't see the progress of your task.

среда, 30 октября 2013 г.

Code complete. Complete.

Just have finished reading Code Complete by Steve McConnell. This book blew up my mind. It contains a lot information about programming as a collection of  activities. The most useful idea for me was that we program for people first, computers second. Some useful thoughts i wrote out in my recent post "Programming quotes".

The last chapter contains the book list that a developer should read. Here it is:

суббота, 26 октября 2013 г.

Java 7. Find yourself what's new?

During the reading of Code Complete 2e I've come across the grep example. This utility helps you to find files matching your search criteria.
If you have a javadoc and you want to find out what's new then you can use the following example:

duglas@duglas-ThinkStation /usr/share/doc/openjdk-7-doc/api $ grep -R -l --include="*.html"   "<dd>1.7</dd>" . > /home/duglas/j.txt



This command will find all files that have been changed or added to the JDK 1.7. Moreover it allows you not only to rely on the what's new file but to explore the changes to the JDK documentation by yourself.

пятница, 25 октября 2013 г.

Spring Portlet MVC 3 on WebSphere Portal 7 cookbook

Here are my personal experience of Spring Portlet MVC 3 on WebSphere Portal 7.
  1. Spring profiles feature is not working in portlet environment.
  2. If your portlet use session for anonymous user, then you can enable it with container-runtime-option feature in portlet.xml like this:
         <container-runtime-option>
            <name>com.ibm.portal.public.session</name>
            <value>true</value>
        </container-runtime-option>
  3. Using client side aggregation and file upload doesn't work together. Switch you page to server side aggregation mode.
  4. When you first time add the event processing feature to portlets and deploy them to portal then you better switch the client side aggregation to server side aggregation. If you don't do this, then you cannot setup wires between your portlets.
  5. If you want your spring portlet to work in client side aggregation, then in your "form:form" tag set attribute htmlEscape="false".
  6. If you use Spring JDBC on WebSphere Portal  then  set connection extractor on your jdbc template like this:
    <bean id="nativeJdbcExtractor" class="org.springframework.jdbc.support.nativejdbc.WebSphereNativeJdbcExtractor" />. This is especially useful if you need to work with special database types and\or stored procedures.
  7. Spring JDBC. Use jndi to create the datasource like this in application context:
    <jee:jndi-lookup id="myDataSource" jndi-name="jdbc/myds" cache="true" resource-ref="true" lookup-on-startup="false" proxy-interface="javax.sql.DataSource" />
  8. If you use JNDI resources in portlet then declare them in ibm-web-bnd.xml file like this:
    <?xml version="1.0" encoding="UTF-8"?>
    <web-bnd
        xmlns="http://websphere.ibm.com/xml/ns/javaee"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://websphere.ibm.com/xml/ns/javaee http://websphere.ibm.com/xml/ns/javaee/ibm-web-bnd_1_0.xsd"
        version="1.0">
        <virtual-host name="default_host" />
        <resource-ref name="jdbc/mydb" binding-name="jdbc/mydb" />
    </web-bnd>
    Also don't forget to declare it in web.xml:
     <resource-ref>
        <res-ref-name>jdbc/mydb</res-ref-name>
        <res-type>javax.sql.DataSource</res-type>
        <res-auth>Container</res-auth>
        <res-sharing-scope>Shareable</res-sharing-scope>
        <mapped-name>jdbc/mydb</mapped-name>
      </resource-ref>
  9. When declare portlet.xml use the following declaration:
    <portlet-app xmlns="http://java.sun.com/xml/ns/portlet/portlet-app_2_0.xsd"
        version="2.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://java.sun.com/xml/ns/portlet/portlet-app_2_0.xsd
            http://java.sun.com/xml/ns/portlet/portlet-app_2_0.xsd"
        id="myportlet.war">.
    Take a look at id attribute. This is how WebSphere Portal distinguishes your war files on portal.
  10. When work with AJAX use @ResourceMapping annotation extensively.
  11. Use spring portlet mvc mock objects for unit testing your controllers.
  12. Use @ExceptionHandler annotation to process exceptions in controller.
  13. Some other interesting information can be found http://www.ibm.com/developerworks/websphere/techjournal/0609_alcott/0609_alcott.html .
Update 24.11.2013:
When using @ResourceMapping  method you can return string, that will map to the jsp, so you can conveniently serve jsp content.

суббота, 12 октября 2013 г.

Programming quotes

Minimizing complexity is a key to writing high-quality code.

If you treat modifications as opportunities to tighten up the original design of the program, the quality improves.

A long parameter list is a warning that the abstraction of the routine interface has not been well thought out.

Program changes are a fact of life both during initial development and after initial release.

Ideas to manage your manager:
Plant ideas for what  you want to do, and then wait for your manager to have a brainstorm (your idea) about doing what you want to do.

Educate your manager about the right way to do things. This is an ongoing job because managers are often promoted, transfered, or fired.

Focus on your manager's interests, doing what he or she really wants you to do, and don't distract you manager with unnecessary implementation details. (Think of it as "encapsulation" of your job.)

Refuse to do what your manager tells you, and insist on doing your job the right way.

Find another job.

Code as if whoever maintains your program is a violent psychopath who knows where you live. -- Anonymous.

One key to effective  programming is learning to make mistakes quickly, learning from them each time. Making a mistake is no sin. Failing to learn from a mistake is. 

Code complete.2e.

суббота, 31 августа 2013 г.

jQuery Tree plugin

Very often in web development UI you need a tree component. Some time ago I've written about jsTree and lazy loading. Now I've found the promising alternative called zTree. It is a jQuery plugin that supports JSON, AJAX, checkbox and other things you can imagine. Also it has good documentation and examples. Moreover it is easy to use. 

среда, 21 августа 2013 г.

jQueryUI position utility

In web-development you often need to work with html, css, javascript.
The most tedious part of this work is to position elements among each other.

What can help you to speed up this work?

I have discovered the position method in jQueryUI library.
This method easily allows you to position given element against selector, element, jquery or event. The last option means that you can also position elements against your cursor.

Hope this help someone.

среда, 17 июля 2013 г.

Apache Ivy. My experience.

I've been using Apache Ant for a long time. It is easy to use. But the most feature it lacks is a project dependency management. Moreover, I need not just resolve the open source projects, but to resolve my own projects.

In my development I have 2 projects that share common classes, which are in a third project. I also have a continues integration server. All projects are build on it and later deployed.

I found that Apache Ivy best suites my need. The reasons for this are:
  • it does integrate with Ant. 
  • it takes minimum intervention into the project to use Ivy. 
  • it has tutorials and good documentation. 

What I like is that in default configuration Ivy offers you a local repository. All you have to do is write an ivy.xml file, which describes your project and dependencies and modify build.xml.

In my situation I write the ivy.xml, that describes the shared project like this:
<ivy-module version="2.0">
    <info organisation="my" module="common"/>
    <publications>
      <artifact name="common" type="jar" />
    </publications>
</ivy-module>

And modified build.xml to publish project like this:
<!-- =================================
          target: publish-local
         ================================= -->
<target name="publish-local" depends="jar" description="--> publish this project in the local ivy repository">
        <tstamp>
            <format property="now" pattern="yyyyMMddHHmmss" />
        </tstamp>
        <ivy:publish artifactspattern="${dist.dir}/[artifact].[ext]" resolver="local" pubrevision="${version}" pubdate="${now}" forcedeliver="true" />
        <echo message="project ${ant.project.name} published locally with version ${version}" />
        <ivy:cleancache />
    </target>

In my other projects which depends on a "common.jar" I write the following ivy.xml:
<ivy-module version="2.0">
    <info organisation="my" module="booking"/>
    <dependencies>
        <dependency name="common" rev="latest.integration" />
    </dependencies>
</ivy-module>

And modified the build.xml like this:
<!-- =================================
          target: resolve             
         ================================= -->
    <target name="resolve" depends="clean, clean-cache">
        <ivy:retrieve />
    </target>

That is all.

One more thing. You can use Ivy in Eclipse. For this one can download plugin IvyDE.

среда, 3 июля 2013 г.

Book review. Java EE 6 Cookbook for Securing, Tuning, and Extending Enterprise Applications

Hi. Have just finished reading "Java EE 6 Cookbook for Securing, Tuning, and Extending Enterprise Applications".

A good book to scratch the surface from the J2EE development. It gives you an overview of J2EE 6 and gives you direction to further investigation. The cons of this book are a tutorial style which describes how to do something in different containers. In my opinion this information can be obtained from the documentation to that containers.

четверг, 27 июня 2013 г.

MySQL table size statistics

If you need to monitor and analyze your MySQL databases size over time you can automate this process.
1) create the database
CREATE DATABASE stat   DEFAULT CHARACTER SET utf8 DEFAULT COLLATE utf8_general_ci;

2) create the table
CREATE  TABLE `stat`.`sizeTable` (
  `idsizeTable` INT NOT NULL AUTO_INCREMENT ,
  `insertdate` DATE NOT NULL ,
  `databaseName` VARCHAR(45) NOT NULL ,
  `sizeMB` DOUBLE NOT NULL ,
  PRIMARY KEY (`idsizeTable`) ,
  INDEX `dateIndex` (`insertdate` ASC) )
ENGINE = InnoDB;

3) schedule the sql to collect statistics:
create event stat_db_evt
on schedule
every 24 hour
do
insert into stat.sizeTable (insertdate,databaseName, sizeMB) SELECT NOW() as insertdate, table_schema databaseName, sum( data_length + index_length ) / 1024 / 1024 sizeMB  FROM information_schema.TABLES GROUP BY table_schema;

Thar is all. This script will run every 24 hours and collect the statistics into the table, which you can analyze later.

пятница, 14 июня 2013 г.

OCPJP 7 Passed!

Yesterday I have passed 1z0-804 and earned the OCPJP status! Yeah!
So, what did I use for my preparation:
  1. Book Oracle Certified Professional Java SE 7 Programmer Exams by Ganesh and Sharma.
  2. Tests from http://enthuware.com.
It took me 3 months for preparation.

пятница, 3 мая 2013 г.

Java 7 Concurrency Cookbook

Have read Java 7 Concurrency Cookbook.
A very good book about concurrency in Java.
Every feature is accompanied with an example.
Most important thing is that examples are clear and resemble some real problems.
When working with an examples one should be very careful, because of the typos in the text. So it's better to consult errata on the publisher's site.
Also you can get 2 free chapters and source code on the publisher's site.

вторник, 9 апреля 2013 г.

Spring WS 2.1.0. Expose WSDL with external schema.

It is a good practice to keep schema external to your wsdl.
In Spring WS documentation it is not clearly mentioned how to publish wsdl with external schema.
After some time searching google i managed to found the solution:
<!-- WSDL -->
  <sws:static-wsdl id="BookingFacadeService" location="/WEB-INF/wsdl/BookingFacadeService.wsdl"  />
 
<!-- Schema -->
  <bean
        id="BookingFacadeService_schema"
        class="org.springframework.xml.xsd.SimpleXsdSchema">
        <property
            name="xsd"
            value="/WEB-INF/wsdl/BookingFacadeService_schema.xsd">
        </property>
    </bean>

BookingFacadeService.wsdl is my wsdl which include the external schema BookingFacadeService_schema.xsd.

Manipulate XML with Ant

Sometimes it is necessary to manipulate xml configs with Ant.
This can be done with XMLTask. A great tutorial about this ant task can be found here.

Software in Government

Did you know that http://dorogi.mos.ru is made with:
1) Spring security
2) Apache Wicket
3) Oracle Application Server 11.

четверг, 28 марта 2013 г.

Tricky Swagger

Hello.
I was faced with a task to create a documentation for our new developing REST service. Search revealed two technologies at hand: JSON-WSP and Swagger.
I liked more Swagger,  because:
1) I can dynamically define documentation with annotations in java
2) relatively simple syntax allows to define the documentation by hand
3) there is a good application Swagger UI which allows to explore the service documentation and more important play with your api.

So here are some tricky moments I was faced during the development of specification and using Swagger UI:
1) If you use Swagger UI with documents other than English then you have to insert into Swagger UI index file the meta 'Content-Type' tag.
2) For your convenience set the 'discoveryUrl' in Swagger UI to your resource listing file.
3) When developing the documentation by hand check your json with validators like http://jsonlint.com/. Because errors in Swagger UI are not displayed.
4) When defining parameters in spec you can use path, query, body, or header AND form. The latter allows you to send json into multipart request. The 'form' argument is not mentioned  in https://github.com/wordnik/swagger-core/wiki/Parameters.
5) When defining you api you should point out the 'responseClass'. If you want your method to return an array of objects you can use the following construction: responseClass: 'List[Item]', where Item is your domain model. This moment is not well documented in documentation.

That's all for now.

среда, 13 марта 2013 г.

OCAJP 7 Passed!

Today I successfully passed Oracle 1Z0-803 and earned OCA level. I got 95%. Good result.
For preparation I used tests from http://enthuware.com/. It is a good buy for your money. If you can pass enthuware test, then you can pass the real exam.

понедельник, 4 марта 2013 г.

Setup new version of xalan and xerces libs on WebSphere Portal 6.0

1) login as root
2) mkdir /opt/endorsed.
3) copy
xalan-2.7.1.jar
serializer-2.7.1.jar
xml-resolver-1.1.jar
xercesImpl-2.8.1.jar
xml-apis-1.3.04.jar
to /opt/endorsed.
4) open /opt/IBM/WebSphere/AppServer/bin/startServer.sh.
5) find #Common args...
D_ARGS="-Dws.ext.dirs="$WAS_EXT_DIRS" $DELIM -Dwas.install.root="$WAS_HOME" $DELIM -Djava.util.logging.manager=com.ibm.ws.bootstrap.WsLogManager $DELIM -Djava.util.logging.configureByServer=true"
6) replace with
D_ARGS="-Djava.endorsed.dirs=/opt/endorsed -Dws.ext.dirs="$WAS_EXT_DIRS" $DELIM -Dwas.install.root="$WAS_HOME" $DELIM -Djava.util.logging.manager=com.ibm.ws.bootstrap.WsLogManager $DELIM -Djava.util.logging.configureByServer=true"

7) restart portal.
Now identity transformation will work as expected.

вторник, 19 февраля 2013 г.

Apache CXF integration testing

Sometimes it is necessary to make the test of web-service client and service.
If you have an Apache CXF service and client then you can try to use local transport for your test.
So, here is an example.

The client you define like this:
 <jaxws:client id="client" serviceClass="IIdentificationService"  address="local://test"/>.

And here is how you test the integration:

IIdentificationServiceImpl impl = new IIdentificationServiceImpl();
Endpoint ep = Endpoint.publish("local://test", impl);
IIdentificationService client = (IIdentificationService) context.getBean("client");
Result res = client.call("123");
ep.stop();


What is important here is the address of endpoint and client. Both uses the prefix "local". This means that messages will transport inside the JVM.
For reference see http://cxf.apache.org/docs/local-transport.html.

четверг, 14 февраля 2013 г.

Challenges in WS development


1) Work with large and user base.
Solution:
Audit & monitoring.
Multifactor authentication.

2) Long life cycle.
Solution:
Choose solution based on industry standards.

3) Robustness.
Solution:
Architect the solution that will work even if some of it components will go down.

4) Manageability.
Solution:
Take care at monitoring, backup, upgrade.

 5) Integration with legacy applications.
Solution:
Look for interface of other applications.

WS stack technologies review

Recently I started to invest time in learning of web services.
I have learnt the basics of WSDL 1.1.
Now I'm trying to understand other aspects of WS. Where are a lot of WS-* technologies. Some of them are obsolete, some are not supported. So the aim of this post is to understand the widely adopted specifications and the possible use cases for them.

воскресенье, 10 февраля 2013 г.

Ajax and ActiveMQ

Sometimes you need to notify users (browsers) about some events occurred on the server side. For example someone has added the row into the table and you need to be notified about it.
Also the client could be not just the browser but any program component. And this components can be dynamically connected and disconnected.
The solution could be the message queue and publish subscribe pattern.
 After some research I managed to work the solution based of ActiveMQ 5.7.0 and Apache Tomcat 6.0.35. Here is how I did it.

пятница, 8 февраля 2013 г.

JStree AJAX lazy loading and submitting of selected nodes to server

Sometimes your need to work with trees.
After some time of research I could find 2 implementations of javascript trees: jsTree (http://www.jstree.com/) and Dynatree (http://code.google.com/p/dynatree/).

I could only try jsTree because of limited time.
So, here are my requirements:
1) lazy loading of selected nodes. It's because tree size can be quite big. And store all the tree is unnecessary.
2) one can choose any number of nodes and submit selected nodes of the tree to server.

четверг, 24 января 2013 г.

Thoughts after reading "Practical TDD and Acceptance TDD for developers"

TDD for web applications

After reading the book «Practical TDD and Acceptance TDD fot developers» I decided to write some thoughts about testing.

TDD Steps

  1. Write the Test.
  2. Write the code that will pass the test.
  3. Refactor the test and the code. Watch carefully what dependencies can be eliminated or restructed.
The real question is how to write code that can be tested? Not how to test the written code. If code can't be tested — nobody needs such code.

Therefore when writing the code one should rely on the technical solutions that allow you to test the code.

Testing servlets

There is no universal solution to testing the servlets.

The main obstacle — how to make servlet that it could support dependency injection.
Following choices are available:
  • use the interface HttpRequestHandler from Spring library,
  • use methods get and set for setting the dependencies.

Unit testing database access code

How to test jdbc? One can test the code that use the services to access the data or the code that directly work with database.
One can use spring jdbctemplate or hibernate. Along with it you can use EasyMock for testing the code, that access the database.

Hibernate allows you to offload from controlling the differencies in SQL among the databases. Also it looks for opening and closing the resources

Testing database access code in memory database

Testing in HSQLDB. Hibernate settings:
hibernate.dialect=org.hibernate.dialect.HSQLDialect
hibernate.connection.driver_class=org.hsqldb.jdbcDriver
hibernate.connection.url=jdbc:hsqldb:mem:testdb
hibernate.connection.username=sa
hibernate.connection.password=
hibernate.show_sql=true
hibernate.hbm2ddl.auto=create-drop

Automating database schema configuration

Solutions: http://dbdeploy.com/ , http://www.liquibase.org/.
Testing Database access code: http://dbunit.sf.net .

Testing code working with files

For working with files it is better to use http://commons.apache.org/vfs/.
One can use RAM filesystem for unit testing.

Testing code dealing with time

One of the solutions is to create your onw interface and substitute different implementations on the demand. Also you can take a look at joda-time library. Also watch for the strategy pattern.

четверг, 17 января 2013 г.

Axis 2 basic authentication on client

Some time ago I need to connect to web-service which use HTTP basic authentication. I had an Axis 2 client. So, here what I did:
HttpTransportProperties.Authenticator auth = new HttpTransportProperties.Authenticator();
auth.setPreemptiveAuthentication(true);
auth.setUsername(login);
auth.setPassword(password);
stub._getServiceClient().getOptions().setProperty(HTTPConstants.AUTHENTICATE,auth); 

Now you can connect to protected services.

вторник, 1 января 2013 г.