Archive

Archive for the ‘Java’ Category

Presentia: Sarbarian’s incubator

May 26, 2010 Leave a comment

Hi all,

I would like to introduce Presentia. Presentia (www.presentia.com.br) is a Brazilian Consulting and Services company focused on IT. Presentia is our business incubator and through them we’ll be able to start developing software as a service (SAAS) and finally release some interesting software and components. Thanks Presentia for join us!

Davi

Olá a todos,

Eu gostaria de apresentar a Presentia. Presentia (www.presentia.com.br) é uma empresa Brasileira de consultoria e serviços em TI e a partir deles a Sarbarian poderá começar a desenvolver software como um serviço junto aos clientes da Presentia.  A Presentia estará atuando como uma incubadora para a Sarbarian e juntas oferecerão mais valor aos clientes de serviços.

Obrigado Presentia.

Um abraço,

Davi

Categories: About me, Java, JavaScript

RESTEasy and OSGi: Perfect match

March 7, 2010 1 comment

I used to develop Eclipse plugins which have been implemented since version 3.0 as OSGi bundles. Also, it is possible to run OSGi bundles inside a web container allowing these bundles to provide services and resources to a web application providing an outstanding modular platform.

As I’m a big fan of jBoss and of jBoss RESTEasy (JAX-RS implementation) I started looking for a solution to integrate these frameworks and build a web application based on REST services and bundles.

How things get together

First of all, you need to run an OSGi framework inside a web container. Today there are many possibilities however, I choose to use the Eclipse Equinox bridge servlet. You can find more about this implementation here.

Second, you need to convert the jBoss RESTEasy libraries and dependencies into OSGi bundles. You can easy create these bundles by creating Eclipse plugins from existent jar archives (Eclipse: File -> new -> Other, Plug-in form existent JAR archives) and follow the wizard to create a bundle adding the RESTEasy jar files.

Third, we need to use my plugin to start the RESTEasy framework inside the web container as an OSGi service.

And them, you start creating your JAX-RS resources as bundles.

Example

Create a new Eclipse Plug-in project (Tip: In the wizard select an OSGi framework as Standard). You can download an example plugin here.

Sample Activator class

package sample1;

import org.jboss.resteasy.plugins.server.osgi.IResteasyService;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.util.tracker.ServiceTracker;

public class Activator implements BundleActivator {

 private ServiceTracker serviceTracker;
 private IResteasyService service;

 public void start(BundleContext context) throws Exception {

    serviceTracker = new ServiceTracker(context, IResteasyService.SERVICE_NAME, null);
    serviceTracker.open();

    service = (IResteasyService) serviceTracker.getService();

    if(service != null) {
       System.out.println("Registering singleton");
       service.addSingletonResource(new SampleResource());
       System.out.println(SampleResource.class.getName() + " registered");
    }    
 }

 public void stop(BundleContext context) throws Exception {

    System.out.println("Removing singleton");
    service.removeSingletonResource(SampleResource.class);

    serviceTracker.close();
    serviceTracker = null;
    System.out.println("Done");
 }
}

Sample JAX-RS Resource class

package sample1;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.WebApplicationException;

@Path("/sample2")
public class SampleResource2 {

 @GET
 @Path("hello")
 public String sayHello() throws WebApplicationException {
    return "Hello from OSGi";
 }
}

If you start this bundle your will be able to access the url http://localhost:8080/osgi/rest/sample2/hello, and if you stop this bundle the path /sample2/hello will be no longer available for client requests. In this implementation the path /osgi is the application context and /rest is the RESTEasy mapping prefix from the web.xml file.

Download here a complete WAR file to deploy this solution and test yourself the power of RESTEasy and OSGi together.

Download here the experimental code develop to connect the RESTEasy and OSGi.

Categories: Java Tags: , , , , ,

JavaScript Dependency Injection

February 15, 2010 Leave a comment

Some time ago I had this idea: – How to create a JavaScript dependency injection container in Java? The beginning of this idea can be read here.

This idea is redundant if you think that now a days we have lots of JavaScript frameworks or even GWT which helps us to write JavaScript applications in Java. However, this is not I want. I want something complete different, something that help me to have the best of two worlds: Java and JavaScript.

How it works is pretty simple: Inject resources (variables, code, objects etc) into the JavaScript code before sending to the client (browser in this case). The draft version of this application allow us to put i18n in action into JavaScript using Apache Velocity as engine.

You can check the development at Google’s code website here.

I’ll update this blog with examples as soon as the development is ready for real things.

Davi

Categories: Java, JavaScript Tags: , , , ,

Changing jBoss DefaultDS

February 15, 2010 Leave a comment

jBoss Application Server 5.x comes shipped with HQSQL as default database backend  called DefaultDS. However, sometimes using HSQL isn’t a good approach when you are deploying the application server on production. The following steps will guide you to change DefaultDS to use another database backend such as PostgreSQL 8.x.

1. Create a PostgreSQL database as usual

This procedure is out-of-scope of this post.

2. Configure login-config.xml

Add the following lines to your running jBoss login-config.xml

PostgreSQLRealm">
<authentication>
<login-module
 code="org.jboss.resource.security.ConfiguredIdentityLoginModule"
 flag="required">
<module-option name="principal">postgres-user</module-option>
<module-option name="userName">postgres-user</module-option>
<module-option name="password">postgres-password</module-option>
<module-option name="managedConnectionFactoryName">
 jboss.jca:service=LocalTxCM,name=DefaultDS
</module-option>
</login-module>
</authentication>
</application-policy>

3. Configure JMS

  • Copy postgresql-persistence-service.xml from the jboss_instalation/docs/examples/jms/ to jboss_instalation/server/all/deploy/messaging
  • Remove or rename hsqldb-persistence-service.xml from jboss_instalation/server/all/deploy/messaging

4. Create a new DefaultDS file

  • Create a new file called postgresql-ds.xml on the deploy folder.

(Example file. You can change it based on your needs)

<?xml version="1.0" encoding="UTF-8"?>
<datasources>
<local-tx-datasource>
<jndi-name>DefaultDS</jndi-name>
<connection-url>
 jdbc:postgresql://hostname:5432/database
</connection-url>
<driver-class>org.postgresql.Driver</driver-class>
<min-pool-size>5</min-pool-size>
<max-pool-size>20</max-pool-size>
<idle-timeout-minutes>0</idle-timeout-minutes>
<track-statements/>
<security-domain>PostgreSQLRealm</security-domain>
<security-domain-and-application>
 PostgreSQLRealm
</security-domain-and-application>
<prepared-statement-cache-size>32</prepared-statement-cache-size>
<metadata>
<type-mapping>PostgreSQL</type-mapping>
</metadata>
</local-tx-datasource>
</datasources>
  • Remove or rename the hsqldb-ds.xml file on deploy folder.

5. Start jBoss and enjoy

Categories: Java