13 June 2012

XML indenting/formatting

Bambitroll @ 14:56

Working with XML can be a pain, but it is even more so if the XML you get it not properly formatted or indented.

Editors support this more or less properly.

This simple command line will give you something nice:
$ xmllint --format original.xml > nice.xml

P.S: make sure you run this in order to have xmllint installed
apt-get install libxml2-utils

14 March 2012

Private repositories in Sonatype Nexus

Bambitroll @ 16:36
Sonatype Nexus is an artifact repository manager very much used when maven is your build tool for a java project. You can use it as a proxy for other repositories but also to host your own artifacts.

By default in Nexus, all repositories are reachable via the anonymous user.

Here is what to do to configure your Nexus server so that (at least) some of your repositories will require credentials to access them.

First you have to prevent the anonymous user to have access to all the repositories:
  1. Create a new Privilege that gives access to you public group (or individual repositories) NOTE: Assigning access to a group is equivalent to assigning these privileges to all of the repositories in the group.
    1. Login to nexus as an administrator.
    2. Click on Privileges in the left menu.
    3. Click Add.
    4. Use the following values:
      1. Name: M2 Public Repositories (Group)
      2. Description: Access to Public Repositories (Group)
      3. Repository: Public Repositories (Group)
      4. Repository Target: All (Maven2)
    5. Save.
  2. Repeat the previous step for all you public groups and/or respositories.
  3. Create a new Role and assign this new privilege to it.
    1. Click on Roles in the left menu.
    2. Click Add.
    3. User the following values:
      1. Role Id: repo-public-read
      2. Name: Repo: All Public Repositories (read)
      3. Description: Read only access to all public repositories.
      4. Session Timeout: 60
      5. Selected Roles / Privileges: M2 Public Repositories (Group) - (read)
      6. NOTE: Include all of the roles you created in the first step.
    4. Save.
  4. Remove the Grant Read all role from the Anonymous user and add the new role.
    1. Click on Users in the left menu.
    2. Click on the anonymous user.
    3. Remove the role 'Repo: All Repositories (read)'
    4. Add the role 'All Public Repositories (read)'
    5. Save.

Once you have done this, you need to create users to access all the non public repositories.
  1. You first create a privilege for your given repo (Security->Privileges then Add)
  2. Then a role (add the view and read part of the privilege created above)
  3. And finally a user and assign him the role just create
  4. Repeat this for as many times as you have repos. If you want to have several users for one repo, then just repeat 3.


The final step is to configure the client side with the proper settings in .m2/settings.xml and in the project pom.xml. like this:
settings.xml
<servers>
   <server>
       <id>myserver</id>
       <username>mrx</username>
       <password>pwd</password>
   </server>
<servers>
pom.xml
<repositories>
   <repository>
       <id>myserver</id>
       <name>myserver</name>
       <url>http://server:8081/nexus/content/repositories/your_repo</url>
   </repository>
</repositories>


Now your repository should be accessible with user and password only.


Some links:
Managing Security with Nexus
Depend on a password protected nexus repository
Can I make a repository private without disabling anonymous access?


02 August 2011

Hibernate/JPA Identity Generators

Bambitroll @ 16:05
While googling for some answer regarding JPA identity generators and how they behave according to which DB you use, I found this very nice blog post!

Very informative!

31 May 2011

SOAPUI and dynamic XML values

Bambitroll @ 17:44
I am currently using SOAPUI to load test an application via web services.
In order to do that, I need to be able to change the value of some XML elements on the fly.
Here is how you can do that:
- Create a Test Suite
- Create a Test Case (which will contains Test Steps)
- Your first test step will be a SOAP request called TestReq1
- In the Test Case Editor, you can create a Setup Script

Here is the Groovy script I use to set the value of one element dynamically (in this case the element attribute called Version):
def groovyUtils = new com.eviware.soapui.support.GroovyUtils(context)
def holder = groovyUtils.getXmlHolder("TestReq1#Request")
holder.setNodeValue("//@Version","7.2")
holder.updateProperty()

At the moment the script is not so useful since it dynamically sets the element to the same value. But there are several strategies to use different values each time, from reading from a file or a DB to writing a little generator in Groovy.


Another thing which is useful with SOAPUI is to be able to create a mock-up response which is dynamically created according to the incoming data. Once you have created your mock-up service, here is how to fetch data from the request and paste it in the response. First the incoming XML and then the Groovy script changing the response (in this example both the request and the response have a Version attribute on an element and we simply return the value we get in):

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:abc="http://my.project.com/abc">
   <soapenv:Header/>
   <soapenv:Body>
      <abc:customerApp Version="${version}" />
   </soapenv:Body>
</soapenv:Envelope>

def holder = new com.eviware.soapui.support.XmlHolder(mockRequest.requestContent)
context.version = holder["//@Version"]

28 March 2011

Testing Databases with JUnit and Hibernate

Bambitroll @ 13:51
Here a 3 part blog post showing how to use JUnit and Hibernate (and Hypersonic) to test your database layer. Often using an ORM makes this superfluous but in some cases it can be an interesting to do.

Part 1
Part 2
Part 3

23 March 2011

Socket connection error when using remote JBoss AS

Bambitroll @ 10:05
I spent most if the afternoon on this problem yesterday and I thought I
could just share the solution since this might be a good tip for everyone...

I have Arquillian tests which used to run flawlessly and which stopped
working out of the blue! The problem was, according to the JUnit logs, a
socket connection error when using remote JBoss AS.

After a lot of searching and great help from Aslak Knutsen on
#jbosstesting, I found out that the problem was in /etc/hosts!


More info and the solution from JBoss community.

28 February 2011

Improving your laptop battery life

Bambitroll @ 08:14
-
According to this page, you should avoid charging your laptop if its battery is nearly full. Indeed, it destroys your battery capacity in the long run.

Running Ubuntu on a Thinkpad (mine is a T510), you can set up the thresholds for when to start and stop charging yourself.

Here is how:
# aptitude install tp-smapi-dkms
# modprobe tp_smapi
# echo 40 > /sys/devices/platform/smapi/BAT0/start_charge_thresh
# echo 70 > /sys/devices/platform/smapi/BAT0/stop_charge_thresh
# cat /sys/devices/platform/smapi/BAT0/*_charge_thresh

Then to have the settings loaded at each startup:
# apt-get install sysfsutils
And add the following in /etc/sysfs.conf
# For a LiIon battery in a Thinkpad
devices/platform/smapi/BAT0/start_charge_thresh = 50
devices/platform/smapi/BAT0/stop_charge_thresh =85

All the info is here.

Once you are done are reboot the machine, you will need to run the "modprobe tp_smapi" command to be able to access /sys/devices/platform/smapi
-

23 February 2011

Arquillian testing - JBoss 5.1.0 - EJB3

Bambitroll @ 11:17
I started using Arquillian to test my business logic code directly in my running JBoss 5.1 instance, and after some fighting I got it working pretty nicely!

So here is what I had to do:
1. Add a profile for running the Arquillian Integration tests in Maven

<profiles>
   <profile>
      <id>it</id>
      <activation>
        <activeByDefault>false</activeByDefault>
      </activation>
      <build>
        <defaultGoal>verify</defaultGoal>
        <plugins>
          <plugin>
            <artifactId>maven-failsafe-plugin</artifactId>
            <version>2.6</version>
            <executions>
              <execution>
                <goals>
                  <goal>integration-test</goal>
                </goals>
              </execution>
            </executions>
          </plugin>
        </plugins>
        <testResources>
            <testResource>
                <directory>src/test/resources</directory>
            </testResource>
        </testResources>
      </build>
      <dependencies>
        <dependency>
            <groupId>org.jboss.arquillian.container</groupId>
            <artifactId>arquillian-jbossas-remote-5.1</artifactId>
            <version>1.0.0.Alpha4</version>
            <scope>test</scope>
          </dependency>
        <dependency>
          <groupId>org.jboss.jbossas</groupId>
          <artifactId>jboss-as-client</artifactId>
          <version>5.1.0.GA</version>
          <type>pom</type>
        </dependency>
      </dependencies>
    </profile>  
</profiles>

2. Make sure you are using the correct version of the javassist library in your business logic project. It has to be version 3.10.0.GA. The one coming with JBoss AS 5.1.0 GA is older (3.9.0) and will not work properly!

3. Write your test classes and run them using "mvn -Pit"!

4. Here is an example of a simple class just calling a EJB3 SLSB (test-persistence.xml has to be in src/test/resources together with jndi.properties):
import javax.naming.Context;
import javax.naming.InitialContext;

import junit.framework.Assert;
import myclasses.ejb.actions.interfaces.AuthenticatorLocal;
import myclasses.ejb.valueobjects.RequestorInfo;

import org.jboss.arquillian.api.Deployment;
import org.jboss.arquillian.junit.Arquillian;
import org.jboss.shrinkwrap.api.ArchivePaths;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.spec.JavaArchive;
import org.junit.Test;
import org.junit.runner.RunWith;

@RunWith(Arquillian.class)
public class AuthenticatorBeanIT {
 @Deployment
 public static JavaArchive createDeployment() {
  return ShrinkWrap.create(JavaArchive.class, "testAuthenticatorBean.jar")
  .addClasses(AuthenticatorBean.class, AuthenticatorLocal.class, RequestorInfo.class)
  .addManifestResource("test-persistence.xml", ArchivePaths.create("persistence.xml"));
 }

 @Test
 public void isAuthenticatedTest() throws Exception {
  Context ctx = new InitialContext();
  AuthenticatorLocal auth;

  Object obj = ctx.lookup("AuthenticatorBean/local");

  auth = (AuthenticatorLocal) obj;
  RequestorInfo req = new RequestorInfo();

  // Trying with a valid user (according to import.sql!)
  req.setRequestorId("0123456789");
  req.setPassword("secret");
  Assert.assertTrue(auth.isAuthenticated(req));
  
  // Trying with a invalid user
  req.setRequestorId("0123456789");
  req.setPassword("incorrect");  
  Assert.assertFalse(auth.isAuthenticated(req));
 }
}


5. Here is an example of a class persisting something to the DB and cleaning up after itself (Note: you have to lookup the EntityManager and EntityTransaction yourself. They can not be injected since you are not in an EJB(:
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Query;

import junit.framework.Assert;
import myclasses.ejb.actions.interfaces.MyClassLocal;
import myclasses.ejb.valueobjects.Info;

import org.jboss.arquillian.api.Deployment;
import org.jboss.arquillian.junit.Arquillian;
import org.jboss.shrinkwrap.api.ArchivePaths;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.spec.JavaArchive;
import org.junit.Test;
import org.junit.runner.RunWith;

@RunWith(Arquillian.class)
public class BarCodeBoardingPassBeanIT {
 @Deployment
 public static JavaArchive createDeployment() {
  return ShrinkWrap.create(JavaArchive.class, "testBarCodeBoardingPassBean.jar")
  .addClasses(MyClassLocal.class)
  .addManifestResource("test-persistence.xml", ArchivePaths.create("persistence.xml"));
 }

 @Test
 public void persistBoardingPassInfoTest() throws Exception {
  Context ctx = new InitialContext();
  MyClassLocal mcl;

  Object obj = ctx.lookup("MyClasssBean/local");

  mcl = (MyClassLocal) obj;
  
  Info info = new Info("XXX");
  mcl.persistInfo(info);
  
  
  // Now checking that the data is properly persisted in the DB
  EntityManagerFactory emFactory = (EntityManagerFactory) ctx.lookup("java:/myTestEntityManagerFactory");
  EntityManager em = emFactory.createEntityManager();
  EntityTransaction tx = em.getTransaction();
  tx.begin();

  Query query = em.createQuery("From Info where text=:info");
  query.setParameter("info", "XXX");
  runQuery(query);
  
  // Cleaning up
  query = em.createQuery("delete from Info where text=:info");
  query.setParameter("info", "XXX");
  query.executeUpdate();
  
  tx.commit();
 }
 
 private void runQuery(Query query) {
  try {
   query.getSingleResult();
   Assert.assertTrue(true);
  } catch (Exception e) {
   Assert.assertTrue(false);
  }
 }
}

6. For the previous test class to work you need a persistence.xml which looks like this:
<?xml version="1.0" encoding="UTF-8"?>
<persistence 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"
    version="1.0">

     <persistence-unit name="myTestPU" transaction-type="RESOURCE_LOCAL" >
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <non-jta-data-source>java:/myAppDS</non-jta-data-source>
        <properties>
            <property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect"/>
            <property name="hibernate.current_session_context_class" value="jta"/>
            <property name="hibernate.show_sql" value="true"/>
            <property name="hibernate.format_sql" value="true"/>
            <property name="hibernate.default_schema" value="public"/>
            <property name="hibernate.hbm2ddl.auto" value="validate" />
            <property name="jboss.entity.manager.factory.jndi.name" value="java:/myTestEntityManagerFactory"/>
            
        </properties>
    </persistence-unit>

</persistence>

05 November 2010

Test syntax and xml publishing

Bambitroll @ 15:40
Use this little page to format the XML code for you


#! /bin/env python
def python_funct():
   a = a + b
   print "Hello Highlighted code"

class Foo(Bar):
   pass


<Location /svn>

  # Uncomment this to enable the repository
  DAV svn

  # The following three lines allow anonymous read, but make
  # committers authenticate themselves.  It requires the 'authz_user'
  # module (enable it with 'a2enmod').
  #<LimitExcept GET PROPFIND OPTIONS REPORT>
  Require valid-user
  #</LimitExcept> 

</Location>

23 August 2010

How to update Smooks in JBoss ESB

Bambitroll @ 10:51
The latest JBoss SOA platform (5.0) uses JBoss ESB 4.7 (https://www.jboss.com/products/platforms/soa/components/) which itself uses Smooks 1.2.3 (http://docs.jboss.org/jbossesb/docs/4.7/manuals/html/ReleaseNotes.html)
The latest and greatest Smooks is 1.3.1 and contains certain new features.
So if you absolutely need them in your JBoss Soa Platform 5.0, you will have to update Smooks manually. To do so, download the zip file from http://downloads.sourceforge.net/smooks/smooks-1.3.1.zip, unpack it and replace all the milyn-* from $JBOSS_ESB_HOME/server/default/deployers/esb.deployer/lib/ with the ones from the lib folder in the zip file.

Warning: This configuration is most likely not supported in case you need support from Red Hat.

14 June 2010

Agilo for Scrum

Bambitroll @ 15:29
I found a tool called Agilo (built on of Trac) which looks promising as a Scrum tool. It supports most of the Scrum features and is actually quite nice to use.

Here is a description regarding how to install Agilo

I used a lot of information from the following pages (mostly for Hardy but can be reused):
 http://trac.edgewall.org/wiki/0.11/TracOnUbuntu
 http://trac.edgewall.org/wiki/0.10.4/TracOnUbuntuHardy
 http://trac.edgewall.org/wiki/TracUbuntuMultipleProjects
 http://anantgarg.com/2009/03/25/subversion-trac-multiple-projects/
 http://www.agile42.com/cms/pages/download-install/
 http://specialbrands.net/2010/01/12/agilo-a-scrum-tool-agile-scrum/

Now for the step by step install (all commands are run as root!):

We start from a fresh Ubuntu 10.04 Lucid Lynx server

First we add Apache and some tools

apt-get install apache2 libapache2-mod-python libapache2-svn python-setuptools subversion python-subversion python-simplejson

Then we install trac

apt-get install trac

Then we configure Trac

mkdir /var/lib/trac
chown www-data:www-data /var/lib/trac
ln -s /var/lib/trac/ /trac
vi /etc/apache2/sites-available/trac and add this:

<VirtualHost *>
ServerAdmin user@email.com
ServerName you.servername.com
DocumentRoot /var/www
ErrorLog /var/log/apache2/error.trac.log
CustomLog /var/log/apache2/access.trac.log combined

<location /trac>
SetHandler mod_python
PythonInterpreter main_interpreter
PythonHandler trac.web.modpython_frontend
PythonOption TracEnvParentDir /var/lib/trac
PythonOption TracUriRoot /trac
PythonOption PYTHON_EGG_CACHE /tmp
</location>
# use the following for one authorization for all projects 
# (names containing "-" are not detected):
<LocationMatch "/trac/[[:alnum:]]+/login">
AuthType Basic
AuthName "trac"
AuthUserFile /etc/apache2/dav_svn.passwd
Require valid-user
</LocationMatch>
</VirtualHost *>

a2dissite default
a2ensite trac
/etc/init.d/apache2 reload

Then we configure SVN:

mkdir /var/lib/svn
svnadmin create /var/lib/svn/theproject
chown -R www-data /var/lib/svn


Then we create the Trac site for the project:

trac-admin /var/lib/trac/theproject initenv
chown -R www-data /var/lib/trac
vi /etc/apache2/mods-available/dav_svn.conf and add this:

<location /svn>
# Uncomment this to enable the repository
DAV svn

# Set this to the path to your repository
#SVNPath /var/lib/svn
# Alternatively, use SVNParentPath if you have multiple repositories under
# under a single directory (/var/lib/svn/repo1, /var/lib/svn/repo2, ...).
# You need either SVNPath and SVNParentPath, but not both.
SVNParentPath /var/lib/svn
#Provides directory listing of repositories
SVNListParentPath On

# Basic Authentication is repository-wide.  It is not secure unless
# you are using https.  See the 'htpasswd' command to create and
# manage the password file - and the documentation for the
# 'auth_basic' and 'authn_file' modules, which you will need for this
# (enable them with 'a2enmod').
AuthType Basic
AuthName "Subversion Repository"
AuthUserFile /etc/apache2/dav_svn.passwd
AuthzSVNAccessFile /etc/apache2/authz_svn.access

Require valid-user
</location>

/etc/init.d/apache2 reload


Now Agilo

Download and unpack Agilo Source Code and then run:
python setup.py install
vi /trac/theproject/conf/trac.ini and make sure that you have these lines

[components]
agilo.* = enabled
webadmin.* = enabled

Then run:
trac-admin /var/lib/trac/theproject upgrade
/etc/init.d/apache2 reload

Add the AccountManager plugin  http://trac-hacks.org/wiki/AccountManagerPlugin

Then you have to configure the access control for Trac (we use the same users for SVN and Trac):
htpasswd -cm /etc/apache2/dav_svn.passwd - NB: the -c creates the file so use it only once
vi /etc/apache2/authz_svn.access and have something like this:

[theproject:/]
theuser = rw


trac-admin /trac/theproject permission remove anonymous BROWSER_VIEW CHANGESET_VIEW FILE_VIEW LOG_VIEW MILESTONE_VIEW REPORT_SQL_VIEW REPORT_VIEW ROADMAP_VIEW SEARCH_VIEW TICKET_CREATE TICKET_MODIFY TICKET_VIEW TIMELINE_VIEW WIKI_CREATE WIKI_MODIFY WIKI_VIEW
trac-admin /trac/theproject permission add TRAC_ADMIN theadminuser
/etc/init.d/apache2 restart

YOU ARE DONE!

How to add new users (as root):
htpasswd -m /etc/apache2/dav_svn.passwd theuser (use -c for the first time to create the file)
vi /etc/apache2/authz_svn.access and add the new user there with rw
/etc/init.d/apache2 reload
trac-admin /trac/theproject permission add theuser the_perm with THE_PERM being TEAM_MEMBER, PRODUCT_OWNER, SCRUM_MASTER or TRAC_ADMIN

How to increase the default attachment size for documents the wiki
Change max_size value in the [attachment] section of the trac.ini of your site
More info on the online doc which comes with every new Trac site!

24 March 2010

JBoss and web-console

Bambitroll @ 11:06
I have been fighting with my JBoss 4.3 for a good hour now just because I want to access the web console located at htpp://localhost:8080/web-console/.
The problem is that no matter what I set for the user in the config file, it is not taken into account.
So here is how I got it to work:
- Go to $JBOSS_HOME/server/default/conf/props
- Add the files web-console-users.properties and web-console-roles.properties
- Edit  $JBOSS_HOME/server/default/conf/login-config.xml and make sure that the path to the 2 files above looks like props/web-console-*.properties.

For some fishy reasons, if you have the files right under conf/ and login-config.xml properly referring to them, it will not work. You have to have them in conf/props/ and update the reference in login-config.xml.

16 March 2010

Seamgen to Maven

Bambitroll @ 17:51
As a follow-up of my previous post, I generated a Seam 2.1.2 project via seamgen and tried to make it work in the maven structure I made earlier.
The biggest problem was with the config files and have richfaces properly working. But I managed and the tar of the project is here.

The files I had to change where persistence.xml, pages.xml, web/pom.xml, web.xml and components.xml.

Now download it, unpack, copy your Java classes and webapp generated by seamgen where they belong, run "mvn clean install package" and you should be able to deploy the .ear file without any problems (given that you have the corresponding *ds.xml file)

10 March 2010

Seam 2.0 and Maven

Bambitroll @ 16:54
Since I am playing with Maven and Seam, I had to manage to create a Seam 2.0 project which can be built with Maven (Seam 3.0 will be fully mavenized).

So here is what I found...
All you need to know is here:
http://seamframework.org/Documentation/IsThereAMaven2ProjectTemplateForSeamApplications

The first link (http://kasper.eobjects.dk/2009/04/seam-ejbs-and-ear-packaging-in-maven.html) works out of the box and uses sub-projects for the different modules (and Eclipse is ok with that!) but I am a bit unsure regarding the test part. It should work fine from the command line though.

The second link (http://www.smies.com/blog/?p=30) seems to have the test part built in but I have not tested it yet. It also uses a flat structure for the project which is a bit less nice...

So after a bit of work, I improved Kasper's setup so that the project now has a persistence unit defined (remember to add a proper *ds.xml file in your deploy folder) and is also able to do some sort of hot-deploy. You can get the tar file here.


For the hot-deploy part, I used something described here (http://relation.to/Bloggers/FastAndIncrementalPackagingInJBossTools) which copies files automatically upon changes from your workspace to your deploy folder (or any other folder of your choosing) according to some rules so you can filter the files you do not want.
This JBoss Tools might be interesting in other cases as well...

So to have the hot-deploy working, I configured the Project archives like this:
- one exploded archive for the ear
- one exploded archive for the jar, exploded inside the ear above
- one exploded archive for the war, exploded inside the ear above, and with 2 file sets (one taking all from web/src/main/webapp (aka the xhtml files) and the other taking all from web/target/JMSeamExample-web-1.0 minus **/*.*html (basically only the libs)).
An explanatory picture can be found here.

If one changes a Java class, the best is to right click on the project in Project archives and choose "Build full" to force a redeploy. Otherwise for xhtml files it happens by itself.
I know it is not has seamless as with the out of the box seam-gen integration but it is not too bad.