Posted by: Ivko | June 21, 2013

FAQ, затова протестирам

Лесно е да се протестира без да знаеш отговора на въпроса “Защо”. По тази причина съм подготвил кратък списък с най-често задаваните въпроси и моите лични отговори на тях:

Против какво протестираш
Против наглостта. Против това, че тези, които дойдоха на власт, обещаха, че ще са коренно различни от предишните. А още в първите си дни доказаха, че ще са по-зле от тях и от всички останали. И тази наглост при по-малко от 30% реална подкрепа на изборите!

Добре, ако има избори сега, ще гласуваш ли
Естествено! Както го направих на последните избори и както ще го правя винаги. Смятам, че човек, за да протестира, трябва да е изявил позиция против тази, срещу която се обявява. Е, мен не ме е било срам никога от позицията, която съм заемал.

А не мислиш ли, че не е времето сега да се сваля правителство? Не трябва ли да се даде шанс на изпълнителната власт да работи по време на криза?
Не, не смятам! Не искам ‘кризата’ да се използва като плашило и като оправдание за всякакви гаври с мислещите хора. Не искам в името на нечии интереси да се примирявам с дебелащина и наглост от най-високо ниво. Писна ми от шутове, кукли на конци, марионетки, играещи си със страха на малцинствата, самозабравили се мутри, икони за един ден и прочие и прочие. Искам нормалността, на която е способна една нормална държава с много мислещи хора. Каквато, бъдете сигурни, е България!

Мислиш ли, че ще постигнете нещо с протестите?
Оооо, да! Досега властващите подценяваха гражданското общество и си мислеха, че, ако не вдигат цените на тока, ще държат всички у дома. Само че “хляб и зрелища” помагаха в една предишна епоха отпреди близо 2000 години. Спонтанната реакция след избора на Делян Пеевски за ръководител на ДАНС ще спре всеки следващ да изпада в катарзис през ден. И не, увеличената минимална работна заплата и майчинските не могат да ни хвърлят прах в очите.

…to be continued…

Posted by: Ivko | November 12, 2010

Unit testing services, part 2 – Embedded Glassfish

Вече техническите теми ги качвам само на nosoftskills.com :-)

http://nosoftskills.com/2010/11/unit-testing-services-embedded-glassfish/

Posted by: Ivko | November 4, 2010

За тебе пееме,…

И кво стана, ЦСКА най-сетне спечели така жадуваната победа в лига еуропа. Най-сетне намерихме сили да спечелим три точки в пролетарския турнир. Не бих казал, че го направихме с класа, но със сигурност показахме дух. Дух, който ни липсваше в доста мачове от началото на сезона. Да, трудно ставаха дори три паса поред, но пък нямаше нито една грешка отзад. Да, Рапид се представиха (а и са си) като някакви нискоразрядни пролетарски фудбалерчета, но пък армейците имаха нужда от победа срещу каквито и да било европейски противници. Да, Радуканов по-скоро гледаше мача отстрани и викаше при конкретни грешки вместо да води играта тактически, но пък отборът имаше настроение и хъс.

Сега най-опасното за червените футболисти ще бъде да решат, че са велики и че нищо не може да ги спре. А най-хубавото е, че ще си повярват, че могат и че отборчета като Пирин, Сливен и Локо Сф не бива да ги притесняват. И лека полека ще забравят за ужасното настроение, в което бяха изпаднали.

А сега малко (само)критика за агитката. Тази година като че ли феновете (тук включвам и себе си) се държат като опозиция на една власт, а не като дванадесети играч на отбора. Мачът във Виена беше буквално гонене на дивото след като пропуснахме да хванем питомното и да спечелим в София. И тази победа дойде напълно нормално срещу един жалък отбор като Рапид Виена. Още 73-тата минута на мача днес знаех, че те няма как да вкарат гол. И този умрял отбор ни победи в София. Според мен основната причина за това е единствено тоталната неподкрепа (забележете – не липсата на подкрепа, а тоталната неподкрепа) от трибуните. Футболистите съвсем не знаеха за кого играят оная вечер. А публиката съвсем се беше забравила. Мислехме, че викаме срещу “боклуците”, но нашето държание никак не беше чисто. Защото ние сме там само заради отбора, а отборът е това, което излиза на зеления терен. Надявам се тази победа да накара феновете да повярват в тима и след месец да направим онази атмосфера срещу Бешикаш, която нашият отбор заслужава. А те наистина го заслужават. Ако не вярвате, сетете се какво сподели Тошко Янчев след победата: “Поздравявам всички цесекари!”. Викайте заради Тошко.

А за себе си какво да кажа… Откровено ме е срам, че и аз бях сред боклуците, които викаха повече срещу Титан отколото за отбора ми. Но срещу Бешикташ ще съм от тези, които Тошко поздрави.

Posted by: Ivko | October 28, 2010

Unit testing services, part 1 – Spring

Introduction

In today’s world the software industry is going towards continuous integration and test driven development. This brings requirement of testing each piece of code created in an organization and running this as often as possible. At the same time the applications are getting much more complex, relying on different containers to provide their collaborator objects and to care about their life cycle. Unit testing such managed objects becomes very hard and this demotivates even the toughest developers to write and maintain good unit tests. No matter how you call these special classes – services, session beans, data access objects, etc., the problem that needs to be solved is how you get hold of the infrastructure and the various collaborators that are usually provided by the container.

Of course you can always create complicated framework where you can run complex scenarios testing your service code. It’s a good thing to have such integration tests that run every night. But it would be great if during development (virtually on each save) you are able to test your code in as much isolation as possible. Which means to create tests closer to the unit test paradigm.

In this series of blog posts I will try to show you how you can use the test infrastructures provided by Spring framework, Embedded Glassfish and Arquillian in order to easily test complex managed objects like services, EJBs, DAOs, etc.

Let’s first start with our…

Usecase

Suppose you want to build a football statistics application. You want to keep track of all sorts of stuff around the matches played on our planet. One of the most important parts of your domain model is the team. So you want to know what is team’s name, which is its city, what is the name of its stadium and of course which is the country where it competes. In order to keep everything as simple as possible, let’s stop with the domain model right here. As a first step maybe we would like to provide a service that adds a new team to our database and also retrieves from there all the teams from a particular country.

I assume that you have basic knowledge about JPA as it will be the technology that I will use for describing and persisting the domain model. I also hope that you know Maven, because I will use that to setup, build and test our services from the command line. You can download the latest version of Maven from here.

I will use Eclipse 3.6 as development environment throughout the series. It can be downloaded from here. It doesn’t matter which of the packages you will choose. The example is simple enough and you can run it even with the most basic functionality. You will also need the m2eclipse plugin.

Setting the things up

In order to start, you need a project setup. You can both create the directory structure manually or use one of maven’s archetypes to do it for you. Let’s go to the second approach. Just run from the command line:

mvn archetype:create -DgroupId=com.foo -DartifactId=my-bar-spring -DarchetypeArtifactId=maven-archetype-webapp

This will create a default maven directory structure with pom.xml inside. The latter has to be tweaked a little bit. First change the compiler version to 1.6 (by default maven compiles to 1.4). As we are going to create JUnit4 unit tests, we’ll change the JUnit version in pom.xml as well:

<dependencies>
  <dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.8.1</version>
    <scope>test</scope>
  </dependency>
</dependencies>

<build>
  <plugins>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-compiler-plugin</artifactId>
      <inherited>true</inherited>
      <configuration>
        <source>1.6</source>
        <target>1.6</target>
      </configuration>
    </plugin>
  </plugins>
</build>

Now it’s time to import our project in Eclipse and start adding the source code.

First, let’s create with our domain class – Team. Under src/main create a directory called java and then create our sample package (com.foo). After that create the Team class with the following attributes:

@Entity
@NamedQuery(name = "findAllTeamsByCountry", query = "select t from Team t where t.country = :country")
public class Team {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    private String name;
    private String city;
    private String stadium;
    private String country;
...
// Getters and setters skipped for clarity
}

As you can see it is an annotated POJO that will result in the DB table called TEAM, with one column for each of the attributes and also with primary key column called ID, where the values will automatically be generated. We have also defined one named query, which will be used to return all the teams in a championship. I have omitted the getters and setters, which you can leave to your ID to create for you.

Of course in order for this to compile you will need to enter the following dependency in your pom.xml:

<dependency>
  <groupId>javax.persistence</groupId>
  <artifactId>persistence-api</artifactId>
  <version>1.0</version>
  <type>jar</type>
  <scope>compile</scope>
</dependency>

Creating the Spring service

The code above will be exactly the same in this and the next two posts. However, when it comes to the reusable service, Spring and pure Java EE have slightly different approaches. What you call a session EJB in Java EE is known as component in Spring framework. The component as the session bean is annotated with one of the available annotations, but is not transactional by default.

In Spring, if you want to create reusable service, then the annotation that you will usually use for decorating your component is @Service. In order to get hold of all the Spring framework specific libraries for your service code to compile, add this to your pom.xml:

<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-context</artifactId>
  <version>3.0.4.RELEASE</version>
  <type>jar</type>
  <scope>compile</scope>
</dependency>

<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-tx</artifactId>
  <version>3.0.4.RELEASE</version>
  <type>jar</type>
  <scope>compile</scope>
</dependency>

And here is the code of our Spring-driven team service:

@Service
public class TeamService {

    @PersistenceContext(unitName = "barPU")
    private EntityManager em;

    @Transactional
    public Team createTeam(Team team) {
        em.persist(team);
        return team;
    }

    @SuppressWarnings("unchecked")
    public List<Team> findAllTeamsFromCountry(String country) {
        Query query = em.createNamedQuery("findAllTeamsByCountry");
        query.setParameter("country", country);
        return query.getResultList();
    }
}

First we tell the Spring container that we want that class to be treated as a service and thus to be injectable in other code managed by that container. This is achieved through the @Service annotation. You can see that our service does not necessary implement an interface. However, it is good practice to work against interfaces and not with concrete implementations. As the service will perform basic data access activities, it would need a reference to the JPA entity manager to help it with that. We expect that the Spring container would manage it for us and inject it in our service. This is declared in Java EE standard way – through the @PersistenceContext annotation. The annotation itself is configured to look up the barPU persistence unit from the persistence.xml.

Creating a team should be done inside a transaction. As Spring beans are not transactional by default, the method is decorated with @Transactional. It is also good practice to make getter method transactional as well (even with read-only transactions), but I have skipped that in this example. In order to run the method in transactional context, Spring framework applies AOP techniques. If our team service implemented an interface, Spring would use the dynamic proxy mechanism from the JDK. However, here we need the Code Generation Library (a.k.a. cglib) for the proxying to work. So we add it in our pom.xml’s dependencies:

<dependency>
  <groupId>cglib</groupId>
  <artifactId>cglib</artifactId>
  <version>2.2</version>
  <type>jar</type>
  <scope>runtime</scope>
</dependency>

The rest of the service code is purely calling the entity manager interface to persist or query our objects to/from the database.

Wiring things up

Now we have to make sure that everything that we created so far is glued together. This is assured by two basic artifacts:

  • JPA persistence context configuration
  • Spring container configuration

JPA persistence context is configured in the persistence.xml. Its default location is under the META-INF directory of the jars we create, which in terms of Maven directory layout is under src/main/resources/META-INF. Here is our persistence.xml:

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0"
 xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
    <persistence-unit name="barPU">
        <class>com.foo.Team</class>
    </persistence-unit>
</persistence>

The only things that we declare here are the persistence unit name and the list of entities managed by this persistence unit. You noticed that the persistence unit name maps exactly to the name declared in the entity manager declaration’s annotation in our team service. Maybe you also saw that we have not declared here any database connectivity settings. We leave that to the Spring’s application context configuration. And here it comes:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:context="http://www.springframework.org/schema/context"
 xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">

    <context:annotation-config/>
    <context:component-scan base-package="com.foo" />
    <context:property-placeholder location="/META-INF/spring/jdbc.properties"/>

    <bean id="dataSource"
      class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="${jdbc.driverClassName}" />
        <property name="url" value="${jdbc.url}" />
        <property name="username" value="${jdbc.username}" />
        <property name="password" value="${jdbc.password}" />
    </bean>

    <bean id="jpaVendorAdapter"
      class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
        <property name="database" value="${database.type}"/>
        <property name="showSql" value="true"/>
    </bean>

    <bean id="entityManagerFactory"
      class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="persistenceUnitName" value="barPU"/>
        <property name="dataSource" ref="dataSource"/>
        <property name="jpaVendorAdapter" ref="jpaVendorAdapter"/>
        <property name="jpaProperties">
            <props>
                <prop key="hibernate.hbm2ddl.auto">create-drop</prop>
            </props>
        </property>
    </bean>

    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactory"/>
    </bean>

    <tx:annotation-driven transaction-manager="transactionManager"/>
</beans>

Here is a brief description of the above instructions to the spring container:

  • <context:annotation-config/> enables the injection of beans inside other beans just with annotating them with @Autowired. We’ll use them later in our unit test
  • <context:component-scan base-package=”com.foo”/> enables the discovery of all the beans under the com.foo package, that are annotated with @Component, @Service, @Repository or any other such annotation
  • <context:property-placeholder location=”/META-INF/spring/jdbc.properties”/> tells the container that this properties file contains some substituti0n values for macros defined in this application context (and not only)
  • The dataSource bean defines a Spring framework data source helper. It is initialized with DB driver, URL, username and password, which values are taken from the file declared in the above entry
  • The jpaVendorAdapter bean contains vendor-specific JPA settings
  • The entityManagerFactory bean is another Spring wrapper, this time around the JPA entity manager factory
  • The transactionManager bean declares Spring’s implementation used by the container to manage transactions whenever is needed
  • <tx:annotation-driven transaction-manager=”transactionManager”/> enables annotating transactional methods and classes (remember our createTeam method?)

And here is the jdbc.properties file, which was configured in the application context definition:

database.type=HSQL
jdbc.driverClassName=org.hsqldb.jdbcDriver
jdbc.url=jdbc:hsqldb:mem:foobar
jdbc.username=sa
jdbc.password=

As you can see, these database connectivity settings do not fit a production setup (using non-persistent database with default user and password). This is so, because the above configuration will only be used for our tests. That is why the above properties file is placed under the src/test/resources directory of our project.

Writing the test

Finally we reached our goal. We’ll start writing the test. I guess that the numerous TDD practitioners and evangelists would place this section in front of all the rest, but I decided to leave writing the test as a dessert.

So it will be placed in the com.foo package under the src/test/java project directory. Here is the code:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "/META-INF/spring/applicatonContext.xml" })
public class TeamServiceTest {

    @Autowired
    private TeamService teamService;

    @Test
    public void testTeamService() {
        Team testTeam = new Team();
        testTeam.setName("CSKA");
        testTeam.setCity("Sofia");
        testTeam.setCountry("Bulgaria");
        testTeam.setStadium("Bulgarska armia");

        teamService.createTeam(testTeam);

        assertEquals(1, teamService.findAllTeamsFromCountry("Bulgaria").size());
    }
}

It is a pure JUnit 4 unit test with some Spring framework spice in it:

  • JUnit will run this using SpringJUnit4ClassRunner
  • The Spring test context framework is told that the Spring application context is located under /META-INF/spring/applicationContext.xml in the application’s classpath
  • The class under test (TeamService) is injected by the Spring container

With the help of the highlighted lines we tell Spring framework to load the application context for us, initialize all the beans that we need and inject them inside our test. Thus we are able to proceed directly to testing our team service without bothering too much about stubbing or mocking the entity manager, which is not the most pleasant task.

The above class is actually more an integration than a pure unit test. It takes some time to load the Spring container. But this is done only once per test suite. So if you create several such test cases with numerous tests each, you will pay this penalty just once.

One final step is to add certain dependencies to pom.xml:

<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-test</artifactId>
  <version>3.0.4.RELEASE</version>
  <type>jar</type>
  <scope>test</scope>
</dependency>

<dependency>
  <groupId>org.hsqldb</groupId>
  <artifactId>hsqldb</artifactId>
  <version>2.0.0</version>
  <type>jar</type>
  <scope>test</scope>
</dependency>

<dependency>
  <groupId>org.hibernate</groupId>
  <artifactId>hibernate-entitymanager</artifactId>
  <version>3.4.0.GA</version>
  <type>jar</type>
  <scope>runtime</scope>
</dependency>

<dependency>
  <groupId>org.slf4j</groupId>
  <artifactId>slf4j-api</artifactId>
  <version>1.6.1</version>
  <type>jar</type>
  <scope>runtime</scope>
</dependency>

<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-beans</artifactId>
  <version>3.0.4.RELEASE</version>
  <type>jar</type>
  <scope>runtime</scope>
</dependency>

<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-orm</artifactId>
  <version>3.0.4.RELEASE</version>
  <type>jar</type>
  <scope>runtime</scope>
</dependency>

The test should work fine when you run it both from Eclipse and from the command line with Maven.

Conclusion

In this first part of the series we were able to create step by step an entity and a very simple data access object using Spring and JPA. Finally we created a lightweight integration test with the help of the Spring test context framework. It very much resembled a unit test as all the work for establishing the test environment and creating the collaborators of the class under test was left to the Spring container.

In the next part of the series you will see how to do the same thing but with Java EE session beans and the embedded Glassfish server.

Resources

You can download the source code of the above sample from here

Posted by: Ivko | October 8, 2010

Java2Days 2010 Day 2

So, this year’s Java2Days conference finished. The Bulgarian Java community got its JavaOne (with much smaller scale of course). We deserve this. The audience is still shy to actively participate in the presentations, some of the presenters were even shier and looked like students in front of a crowd of professors and the organizers had their small mistakes too. But as a whole at least my opinion is that the content was more important than all the small bugs!

Day two started very promising – a lot of coding and very few slides. Basically the people who were talking abstract yesterday were inside their IDE today. Arun Gupta showed one of his miles-to-go-like sessions. I must confess that I was very happy when I saw his name in the list of presenters last week. And this morning everyone watching had the same feeling. Basically everything that was just presented and slightly touched yesterday was put into play by Arun. We saw CDI, Servlets, EJBs, RESTful web services, NetBeans,.. and maybe I miss something. No ivory tower or Chinese slides. Just NetBeans, Glassfish and the developer! I hope that more and more people think now that Java and Java EE in particular is much better than the various strange combinations like RoR, PHP or some other awkward framework that promises developer heaven.

And the next session that I attended could be an answer to those of the Java haters that say that the language is too verbose. Yes, they are right, but they don’t quite realize that Java language is no the only thing that make the whole Java stack. We have the libraries and we have the Java platform (or JVM more precisely). And if you put on top languages like Scala, then everything becomes much prettier. Scala is a perfect hybrid of a statically typed, object oriented and function oriented language. It has most of the functionalities that the dynamic languages have and in the same time is type safe and has a compiler that guarantees that. The presenter Vassil Dichev proved that he is not only interested in Scala, but has very broad knowledge on what is going on in the whole industry. It was probably due to the lack of time, but I felt that the pace of the presentation was quicker than people could follow (especially when describing the language features). After the session I talked to Vasko about the Scala enthusiasm in Sofia. He agreed with me that there were not too much fans of the language and nearly zero developers using it productively. Maybe a Scala user group is a good idea for bootstrapping everything?

Next I went to the building lightweight SOA applications with Spring. It was actually all about AOP and cross cutting concerns and also a practical sequel to yesterday’s Spring Integration introduction. Oleg showed two of the areas where the Spring platform rocks and finally with nearly no effort he sent a live twitter message!

The next session I attended was one that was again very scarcely using powerpoint. Sasa Slavnic, a Serbian developer, gave us a brief inside into the Java FX development. Java FX script looked great, but unfortunately Oracle decided to discontinue its support. The reason for that is missing tool support and hard times when debugging. In this direction Sasa showed us how the same thing would look like in Swing and I must admit that it wasn’t the shortest code I’ve read in my life. The coolest thing of Java FX is that the script is used just for declaring the UI and it leaves all the business logic and persistence handling to Java. Sasa’s advice was to try to migrate most of the things we do to Java code before it is known what the replacement for Java FX script will be. The good thing is that the community is not sleeping. There was immediate fork of the Java FX script project. It even got its name – project Visage. Let’s see how it will do.

The afternoon sessions started for me with Vlado Pavlov and Dimitar Giormov’s JRuby issues and the pill for it – SAP’s Eclipse memory analyzer tool. The worst thing in debugging OutOfMemmoryError’s occurring in JRuby applications comes from the fact that the stack traces in the heap dump generated by JRuby are too hard to understand. The situation becomes even more complicated when the out-of-memory is caused after a call from JRuby to Java method. Of course the memory analyzer steps in here with its predefined and easily extensible rules for analyzing traces. It was very impressive (again). The best thing is that the tool is free and hopefully it will soon support not only analyzing heap dumps, but thread dumps as well. With the help of the community.

The room was very small for the next session. Andrew Lombardi, the Wicket guy, showed us what is new in the not yet specified HTML 5. In an even more entertaining fashion than yesterday the keen developers got their first (or for some of them not) impression on the cool features of the markup language. The browser support was one of the things that cheered up the crowd.

Peter Peshev from SAP was the next presenter that I watched. He showed his demo from JavaOne about how a big bloated Java EE application can and should be transferred to an OSGi one. We all viewed the prerequisites, potential migration paths and possible errors. Peter had a lot of jokes, which I do not believe were fine for the JavaOne audience two weeks ago. But we Bulgarians are famous for our black and sometimes rude humor. ;-)

The final session Reza Rahman presented us the Java Community Process. This is basically the way how java specifications (or JSRs) are created. It is really an open process, true Java democracy in a sense. Everyone, even individual developers, is invited to participate at least as an observer. I was mostly interested in the Apache boycott of the process due to the strange ‘field of use’ issue that prevents them license their Java SE 6 implementation. And whether this boycott can hinder the release of the Java 7 SE spec (right now all the development goes under the title JDK 7, nobody talks about Java 7 due to lack of a JSR). Fortunately I got reassured by the people on board (all the present JSR Expert Group members) that this is not the issue and nobody can put a veto on releasing a specification just by a single No vote.

I will finish the Java2Days story with my final impression: the content was great, though the organization was not at last year’s merits. The small room on the first floor lacked microphones, there was not support for poor Andrew Lombardy when he tried to fix the beamer’s position and the pretty girls from the fashion agencies that were here last year were missing ;-)

My biggest not-too-hard-skill impression was that the Java EE people are quite self-confident after the release of version 6 of the spec. They are very often mentioning (not with the best of their spirit) Spring in their talks. At the same time, the minds of the Spring people seemed high in the sky (probably because of the clouds that appeared there after the VMware acquisition of SpringSource). I didn’t hear that passion which Rod Johnson had when explaining that Java EE is stopping innovation. I hope that they will surprise us soon. Or maybe they are too busy to follow VMware’s strategy?

Anyway, Java2Day conference and all the speakers proved that visiting such events is at the same time helpful and entertaining. So, see you next month at Devoxx. :-)

Posted by: Ivko | October 7, 2010

Java2Days 2010 Day 1

Last year I wrote (in Bulgarian) that the Java2Days conference was the best conference that I have been to. Well, this had been the only conference where I had gone at that time. This year the situations is nearly the same – I have not gone to any other conference yet (hopefully this will change for good in a month), but now I can say that I am impressed. The content this edition is great so far.

For those of my (ten) readers that were not there, there are three tiers going on in parallel. I must confess that for some of the slots I had hard times to decide where I would like to go, but sometimes life is harsh. :-)

So, the conference started with a half hour delay and an improvised key note from the Oracle sales director for Bulgaria, Serbia and Montenegro. The only thing that I liked about the guy was that he was speaking half Bulgarian half Serbian language, which is always great fun for me (I pretend that I speak Serbian too :-)).

The first real session that I attended was Alex Moussine Pouchkine’s “Why and how Java EE became popular”. I knew Alex from the last Java2Days, from his blog and of course from the Glassfish podcast. His presentation was perfect for a developer who was interested in what is new in Java EE 6. However, my colleague and friend Vlado Pavlov asked me: “did he explain why and how”? And I realized that even though the presentation was great for me and my colleagues, it was not following too much its title. Anyway, I think the Bulgarian community needed this introduction for many reasons. And it was perfect that the conference, or at least one of its tiers, started with it.

Next I half-visited Spring Summer session (didn’t get too much of it as I tuned in right before its end) and then visited Spring integration. I realized why I like Spring so much – it integrates very well with merely everything. It’s so easy. If you wish, you can keep your Java code independent of any technology/product specific packages and classes and use XML. Or otherwise you can code everything by yourself. With the help of the tooling (and even without it), it is a piece of cake to follow both approaches. I still don’t understand the Java EE evangelists that keep polluting the web with claims that Spring is all about XML. Disclaimer: Spring integration does not have anything to do with integrating Spring with anything. It helps you integrate your code with other systems through different channels.

The next session was the most impressive of the day. Not that the presenter was perfect as such, but it was the content – Pseudo Functional Domain Specific Languages in Java. The guy (BTW coming from Macedonia) presented his work (still in progress) on a Java library that uses static inserts, generics, dynamic proxies and other language and JDK features that can make writing Java seem like writing functional coding (think about Lisp and lambdas). This does not only save a lot of boilerplate, but also makes the code look more elegant. The presenter (Nikolce Mihajlovski is his name) was quite shy and said that he is not yet ready to publish his work, though he will do it soon.

After the lunch break I went to the Apache Wicket session. It was presented by Andrew Lombardi, who was here also last year. He is a great presenter, very entertaining speaker and even a developer (Wicket contributor). He had a very interesting approach to demonstrate Java code – everything was recorded in a video clip. From creating the classes, to writing their content, the tests, running and displaying the result in the browser. However, the guy was very ill disposed to JSF. Before JSF 2 I would agree with him, but now it is quite better. When you want to implement a JSF component, you can do it even easier than in Wicket (at least you don’t have to create a tone of anonymous inner classes). The xhtml code and all the special tags are not quite different from the HTML tags from the designer’s perspective. And Andrew mentioned that HTML should be written by designers. But anyway, it was a great presentation which I hope persuaded a lot of people to come to the bright side of the [web app development] world :-). And he also pointed wicket’s cons…

In the next slot my former colleague Vassil Popovski talked about developing RESTful web services in Java. This is a very important topic, because in my opinion the Java community should leave the WS-* bloat to Microsoft and other mastodons who use their SOAP implementations as apology for lacking integration with the REST of the world. My opinion is that REST is a very simple topic, which only sounds complex. As Vasko mentioned: the specifications sounds like an article in a popular blog rather than an ivory tower paper.

The last session in the agenda (and not in the day hopefully) that I attended was Reza Rahman’s Testing Java EE Applications. Testing and continuous integration is one of my topics of interest so I was wondering how it is done in the Java EE world, where the dependency on containers and the infrastructure they provide seems huge. I was even more intrigued because I am just reading a book on Java EE 6 and alongside all the simplifications there (even there is an EJB container which can be embedded in the client JVM) I felt very disappointed by the fact that it is not possible (or at least for me) to inject EJB’s inside JUnit tests. At the same time you can inject EJBs virtually in every class managed by a Java EE container. And on top of that my experience with Spring has taught me that you can inject everything everywhere. Well, Reza showed us that Java EE can also do it, but not through the specification. JBoss’s Arquillian library comes to help here. And not only here, but in all the cases where you want to test Java EE components (Servlets, JSF, EJBs, JPA, etc.).

Finally the toughest listeners watched Arun Gupta‘s session on bringing Java EE 6 to the cloud. He again scratched the surface of what is new in Java EE 6. The presented four different cloud solutions (Amazon amongst them, I did not keep notes, so I forgot the rest). And he showed how you can install database and several app server instance (Oracle Glassfish of course) on each one of them. He described the monitoring, management and deployment capabilities and finished with the pricing. One of the proposals from Arun’s presentation had a very appealing developer (free) edition, but I forgot which it was. Follow this blog, I promise to publish it tomorrow. Finally Arun promised that Java EE 7 will stress on making the Java enterprise platform more suitable for cloud computing. We’ll see. According to the presentation, we’ll wait until 2012 to find out.

So, this was the first day. Tomorrow I’ll surely visit Arun’s tools show (no ppt, just NetBeans and Glassfish), Vasko Dichev’s Scala session, Vlado Pavlov’s JRuby memory and thread issues (Ruby has also scalability issue, but this is another topic), JavaOne’s star OSGi Migration headaches and finally we’ll all take a look inside the JCP together with Reza Rahman :-)

Posted by: Ivko | July 12, 2010

ГБС, моля ви, побързайте

Краят на миналата седмица ни поднесе доста емоции. Някои приятни, други не чак толкова. Тук ще пиша за събитията от четвъртък и петък във варненския дворец на културата и спорта. Там мъжкия ни национален волейболен отбор трябваше да направи последната крачка за класирането ни на финалите на световната лига.

Предпоставките бяха прекрасни – осем поредни чисти (е, почти) победи срещу скромните Корея и Холандия, измъкната точка и добра игра в гостуването в Бразилия и пълна зала, която ни очакваше край морето. Хвърляхме едно око и в другата група, където се надявахме някой от двата отбора Италия и Сърбия да свърши нашата работа, спечелвайки двата мача помежду им.

Аз не съм волейболен специалист. Така че няма да коментирам защо загубихме така нелепо двете срещи. Нито горя от желание да пиша за фарса, който сътвориха бившият и несбъднатият олимпийски шампиони в Белград. На този свят всичко се връща рано или късно.

Тук искам да коментирам един аспект, на който не се обръща достатъчно внимание. А имено тези шест хиляди зрители в залата във Варна по време на двата мача с Бразилия. Забележете – зрители. Не публика и в никакъв случай фенове. Нищо против тях, те стават за прекрасни наблюдатели на световното първенство по скокове на батут или спортна аеробика. Навярно са много възпитани и са посещавали много повече театрални и оперни представления от мен. Но за публика на решаващ мач от световната лига просто не стават.

На всекиго е лесно да вика и скача за отбора си при 2:0 срещу Корея и при геймовете спечелени на момиче срещу Холандия. Но когато нещата не се развиват по този начин и срещу нас застана колос, който ни най малко не мисли да се церемони с любимците ни, нещата за мое съжаление се промениха.

Тези шест хиляди гърла сякаш изведнъж изчезваха при всяка поредица от две или повече точки за бразилците. Понякога освиркваха техния сервиз, но при по-тежките серии атмосферата в залата ставаше толкова тягостна, че нашите момчета се чудеха дали играят в една от най-големите спортни зали у нас или в някой физкултурен салон в дълбоката китайска провинция.

Съжалявам дами и господа, при цялото ми уважение към вас, вие показахте, че Варна не става за домакин точно на този род срещи. Въпреки прекрасните условия, въпреки всички усилия на федерацията и организаторите, зрителите бяха там по-скоро за пълнеж, отколкото за подкрепа в тежките моменти. Да си призная, в изблик на ярост на няколко пъти се чудех не може ли мачовете догодина да се играт в значително по-малката Арена Самоков? Побира по-малко зрители, но пък е по-близо до София. От където при една добра организация могат да се изсипят ядрата на агитките на двата ни футболни гранда. Едните от тях доказаха последните две години, че могат да подкрепят волейболните си любимци при всякакви ситуации и при всякакво развитие на срещата. Да, знам, че звучи прекрасно само на теория и че у нас любовта към клубния отбор е доста по-силна от тази към националния. А и тази любов понякога е доста по-силна от общоприетата и избива в някои крайности, носещи само наказания и голби. И въпреки всички тези доводи, все си мисля, че софийските фенове са наистина фенове. Или поне по-големи от варненските.

Така че, уважаеми господа от Главболгарстрой, продължавайте в същия дух и същото темпо. За да може скоро залата на четвърти километър в София да приема мачовете от Световната лига (а защо не и някой ден и финала й). А за двореца на спорта в морската ни столица да останат други интересни събития като балканската купа по баскетбол жени, европейското първенство по художествена гимнастика за девойки и републиканските финали по самбо.

от Лазар Борисов

500 гр пилешки гърди
3 с.л. зехтин
1 с.л. мед
1 чаша бяло вино
2 ск. чесън
Сол, черен пипер

В купичка забъркваме меда с виното и черния пипер до добиване на еднородна консистенция.

Нарязваме на дължина пилешките гърди на големи филета, поръзваме ги със сол и ги запържваме в зехтин със счукан чесън до порозовяване.

Местим ги заедно със соса в тавичка за печене или в огнеупорна (йенска) стъклена тенжера с капак, заливаме с винено-медената смес, слагаме капака (за стъклена тенджера) или покриваме с алуминиево фолио (за тавичка) и печем на 180 градуса 30 минути. Фурната да не е предватително загрята!

Posted by: Ivko | April 25, 2010

Choice of the week – 16/2010

Considering data stores

This article is not from this week, but anyway, it is very interesting and profound, so I cannot help sharing it with you.

What is a data store? Well, this is a repository where you can store data. Author’s idea is to provide a quick overview and benchmark results of a wide range of data stores.

There are certainly several types of data stores – relational databases, object databases, document oriented stores, etc. For each of these types there are different kinds of libraries and even technologies that help the developer work with the data storage. Each of the data stores and the concrete implementations has its strengths and weaknesses. In the beginning the author tries to stress that there is no silver bullet that solves the data storage issues. However, most people (99,99999% according to him) go for the relational database solution and most of those (there is no percentage mentioned) use JPA as the layer between JDBC and the application code. The author calls this no-thought solution SOD – same old data store.

I must admit that I am in the SOD camp – I don’t usually think much when I have to develop a simple application. I go directly to relational DB + JPA. However, at a bigger project where I participated, we had to judge between several persistence representations and technologies, so I had the opportunity to get acquainted with some of them (even a colleague of mine keeps insisting that JCR is the best solution, even though we chose JAXB and XML :-)).

Anyway, the bottom line is that it is good that people like Joe write such reviews so that the next time we’ll take a well-thought decision and not go directly to the SOD.

Groovy closure in pure Java

‘Closures in Java’ is a long discussed topic in the community. And it seems that we are going to have them in JDK 7.

But first, what is a closure? Well, this is can be a very broad and complex area, but the easiest answer is – a function pointer. Hmm, it does not seem quite clear, right? OK, imagine that you can define a block of code and you can pass that as a parameter to normal functions. For example you can define a generic iterate() method over a collection, which receives the algorithm that handles the collection data.

Anyway, we should not wait for JDK 7 to come to start using closures. Most of the dynamically typed languages have them and some of them are built on top of the JVM, which means that they “compile” to Java byte code.

The article described hereby is an example of how you can implement a Groovy method that takes a closure parameter and then create and pass this parameter from withing pure Java code. Short but useful hint! :-)

The ABC of JDBC, part 1

JDBC was discussed in an earlier article of this blog. So if you liked it, but you are still not very familiar with it, then DZone’s series is just for you.

In the first interview-like installment Daniel Rubio explains the very basic terms of JDBC: how it works, what is a connection and how do you create it, what is a statement, how are connections pooled, what is a DB driver, etc.

5 things you didn’t know about… the Java Collections API, part 1

IBM developerWorks continues Ted Neward’s series “5 things you didn’t know about…”. Now it is time for the Java Collections API. As this is a very vast topic, there will be several parts devoted to it.

In this first part the author starts with the most obvious things – how you can use the API for common stuff: converting an array into a collection, iteration, the new for-each loop and its usage with collections, new and handy collection algorithms and extending a collection.

I personally thought that using the plain old array is better in terms of optimization. Well, not exactly. Think about all the tedious code that you have to write for a single operation with the array. For example a simple dumping to the console requires at least several lines of code. Not to mention extending it with one or several elements. Well, this is handled by the collections API for you. Not only that, but the API designers have overcome all the traps that you can fall into if you decide to work with the array by yourself – concurrency for example.

So, my advice is: use collections as much as possible. It’s easy, it’s fast and your code will be clean and easy to understand and maintain.

JavaScript in Java with Mozilla Rhino

Last but not least, Steven Haines, the editor in chief of the InformIT Java guide created a quick series this week on running JavaScript code inside Java programs using the Rhino library coming from Mozilla.

The usecase presented here is that a certain web application needed same validation to be run on the server, as well as on the client side. The easiest solution would be to develop the validation logic twice – the first time in a language suitable for the browser (e.g. JavaScript) and the second time in Java – our server side language of choice. After a while you can realize that this is not a good idea: developing something twice is one of the worst practices, which we always try to avoid (though I must admit I’m still doing it :-().

So the solution is simple: write the validation code just once and call this same code on both locations. It’s easy to run JavaScript on the client side – all the modern browsers understand it. But how do you do it in Java? Well, Mozilla Rhine does it all and InformIT knows it all.

Don’t forget after reading the first part to go on with the next ones by clicking the Next links at the bottom right of the articles. Thus you will find out how you can combine the result of different functions, how you can build a JavaScript entity using JSON and how you can convert it to a JavaBean with GSON, and finally how you can organize your validation code.

Posted by: Ivko | April 21, 2010

Пепел

С този кратък постинг бих искал да си посипя главата с пепел. За радост на един-двама от десетината (верно станаха десет) читатели.

Преди няколко месеца исках с пълна сила да махнем един треньор от отбора на ЦСКА. Както се казва в далекоизточната мъдрост “Внимавай какво си пожелаваш, защото може да се сбъдне”. Е, сбъдна се. Любо Пенев го махнаха, ЦСКА обаче пропадна още толкова. И на всички, включително и на мен, стана ясно, че всъщност вината май не е в Любо Пенев. Не той направи великата селекция през зимата, в която ЦСКА купи футболисти за постове, където имаше две-три алтернативи, но не взе нито един разиграващ халф да речем. И това при положение, че продаде Иван Стоянов. Безидейността, която си мислех, че демонстрираме в края на есента, заприлича на добре смазана машина в сравнение с това, което гледаме днес. Доскоро не бях от хората, които на 100% предпочитат да гледат по Диема Хъл Сити – Фулъм пред ЦСКА – Сливен по Канал 1. Но последната мъка в Надежда срещу Миньор ме разубеди напълно. Играещият по същото време опашкар Уигън правеше по-точни и идейни пасове срещу Арсенал, отколкото великия ЦСКА срещу перншкия тим. Както и да е, нека не си разваляме настроението след победате над Литекс.

В заключение бих искал да споделя какво провокира това излияние. Най-великият немски отбор Байерн Мюнхен изпитваше особени трудности първия полусезон. Всички брояха дните до изгонването на Луис ван Гаал. Но за разлика от българските титани, баварците оставиха човека да работи. И в момента за всички е ясно, че Гуардиола не може да бие Специалния, но ван Гаал може. Според мен Любо Пенев можеше да бие Левски и втория полусезон. Нооооо, това е една друга култура, за нас е важно да бием манекенките по дискотеките…

Older Posts »

Categories

Follow

Get every new post delivered to your Inbox.