I'm dedicated agile security architect/system architect/developer with specialty of open source framework.
December 23, 2012
Configure Subclipse to Use SVNKit
Getting Started Guide for Eclipse with Maven (m2eclipse) and Subversion (Subclipse) Plugin
First, if you haven not yet done it, install Java SDK. Here I will use a linux client, but the same yields for a Windows client environment, except installing program is more comprehensive in Linux.
On a Internet connected Ubuntu 12.10 Quantal client, simply open a terminal and print.
$ sudo apt-get install openjdk-7-jdk openjdk-7-source openjdk-7-doc
Then you need Maven. Here I will use Maven 3, which is the recommended version for building new Java project. For Ubuntu client simply continue typing in your terminal:
$ sudo apt-get install maven
Now you need to install Eclipse. I recommend NOT to use the latest version, since it is remarkably slower then the previous version of Eclipse Indigo (3.7). Simple open the below link in you web browser and download the right version version for your operation system and architecture. Then unzip the compressed file in you programming directory.
http://www.eclipse.org/downloads/packages/release/indigo/sr2
Now check that eclipse points to your default java home directory, where source code is also stored.
Now you need to install two plugins, Maven (m2eclipse) and Subversion (Subclipse) plugin. Installing extra plugin in Eclipse is quite simple. Open Window → Install New Software.... Now enter the below URL and follow the Eclipse guide.
http://download.eclipse.org/technology/m2e/releases
http://subclipse.tigris.org/update_1.8.x
References:
- m2eclipse - http://www.eclipse.org/m2e/download/
- Subclipse - http://subclipse.tigris.org/
- Configure Subclipse - http://magnus-k-karlsson.blogspot.se/2012/12/configure-subclipse-to-use-svnkit.html
November 15, 2012
A Better Maven POM for Wicket Archetypes
If you are new or just wants to try Apache Wicket there is a good Wicket Maven Archetype for that. You can find instruction for that at http://wicket.apache.org/start/quickstart.html.
But the generated pom.xml leafs room for improvement. A better pom would be
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>se.msc.examples</groupId>
<artifactId>example-wicket</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>Example Wicket</name>
<organization>
<name>MSC</name>
<url>www.msc.se</url>
</organization>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.build.outputEncoding>UTF-8</project.build.outputEncoding>
<wicket.version>6.2.0</wicket.version>
<jetty.version>7.5.0.v20110901</jetty.version>
</properties>
<dependencies>
<!-- WICKET DEPENDENCIES -->
<dependency>
<groupId>org.apache.wicket</groupId>
<artifactId>wicket-core</artifactId>
<version>${wicket.version}</version>
</dependency>
<dependency>
<groupId>org.apache.wicket</groupId>
<artifactId>wicket-extensions</artifactId>
<version>${wicket.version}</version>
</dependency>
<dependency>
<groupId>org.apache.wicket</groupId>
<artifactId>wicket-datetime</artifactId>
<version>${wicket.version}</version>
</dependency>
<!-- LOGGING DEPENDENCIES - LOG4J -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.6.4</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.16</version>
</dependency>
<!-- JUNIT DEPENDENCY FOR TESTING -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
<scope>test</scope>
</dependency>
<!-- JETTY DEPENDENCIES FOR TESTING -->
<dependency>
<groupId>org.eclipse.jetty.aggregate</groupId>
<artifactId>jetty-all-server</artifactId>
<version>${jetty.version}</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<resources>
<resource>
<filtering>false</filtering>
<directory>src/main/resources</directory>
</resource>
<resource>
<filtering>false</filtering>
<directory>src/main/java</directory>
<includes>
<include>**</include>
</includes>
<excludes>
<exclude>**/*.java</exclude>
</excludes>
</resource>
</resources>
<testResources>
<testResource>
<filtering>false</filtering>
<directory>src/test/resources</directory>
</testResource>
<testResource>
<filtering>false</filtering>
<directory>src/test/java</directory>
<includes>
<include>**</include>
</includes>
<excludes>
<exclude>**/*.java</exclude>
</excludes>
</testResource>
</testResources>
<plugins>
<plugin>
<inherited>true</inherited>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.5.1</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
<encoding>UTF-8</encoding>
<showWarnings>true</showWarnings>
<showDeprecation>true</showDeprecation>
</configuration>
</plugin>
<plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>${jetty.version}</version>
<configuration>
<scanIntervalSeconds>1</scanIntervalSeconds>
<useTestClasspath>true</useTestClasspath>
<connectors>
<connector implementation="org.eclipse.jetty.server.nio.SelectChannelConnector">
<port>8080</port>
<maxIdleTime>3600000</maxIdleTime>
</connector>
</connectors>
</configuration>
</plugin>
</plugins>
</build>
</project>
The differences I made are the following.
1. Downgrade the Jetty Maven Plugin to 7.5.0.v20110901
<jetty.version>7.5.0.v20110901</jetty.version>
The new jetty plugin are much slower than the old one and you want miss anything important.
2. Add Wicket Datetime Dependency
<dependency>
<groupid>org.apache.wicket</groupId>
<artifactid>wicket-datetime</artifactId>
<version>${wicket.version}</version>
</dependency>
Wicket provides a good datetime picker and I recommend that you use this.
3. Configure Jetty Maven Plugin for Autodeploy
<scanintervalseconds>1</scanIntervalSeconds>
The jetty plugin can automatically be configure to pick up any changes made within Eclipse and automatically redeploy your application. This is a great way to develop and I recommend it.
<usetestclasspath>true</useTestClasspath>
You also might want to mock any possible server facade class, which is located in your test directory. To enable that add your web test classes to jetty classpath.
November 13, 2012
Where is Additional Driver in Ubuntu 12.10?
Broadcom Wireless Network Card not working after updating Ubuntu 12.10
Yesterday I updated my HP ProBook 6550 with Broadcom 4313 with my fresh installed Ubuntu 12.10 and suddenly my wireless network card stopped working. I google and found the following article http://www.howopensource.com/2012/10/install-broadcom-b43-legacy-wireless-driver-in-ubuntu-12-10-12-04/. But that did not helped, until I also installed
$ sudo apt-get install broadcom-sta-common
System Development Environment on Ubunut 12.10
With the latest Ubuntu release most of the tool a Java developer needs comes packaged in official release packages.
Open JDK 7
The open JDK 7 is mature and ready for production. This since the latter version of Open JDK 6. The reason for this is that now Oracle really develop the next version of Oracle JDK version is done in the Open JDK version. Previously when Sun owned the Java SDK they merely dumped source code in the Open JDK repository, but Open JDK is really a upstream project.
$ sudo apt-get install openjdk-7-jdk
Maven 3
$ sudo apt-get install maven
Subversion 1.7
Please note that there is a big difference between svn 1.6 and 1.7, where 1.6 was the official package version in Ubuntu 12.04.$ sudo apt-get install subversion
Subversion Client
If you have previously used RabidSVN, you should consider switching to KDESVN$ sudo apt-get install kdesvn
Best Practice Apache Wicket
1. And maybe the most important is always use Wicket Models.
First Wicket is a component based framework, which in itself implies you should always use Wicket Models.Secondly wicket is also a framework that out of the box handles the back button dilemma, which is what should happen when the user press the back button in the web browser? Wicket handles this by versioning every page. Which means that if a user have in entered some data in a HTML form, then submitted and thereafter presses back button, Wicket will then out of the box present the values the way it was before pressing submit AND keeping you server Model in synch. You can get a good feeling of the above by playing around with the form example at http://www.wicket-library.com/wicket-examples/forminput/.
To achieve this Wicket uses the HTTP Session to store the previously pages states. To not now blow the RAM space on the server, you should almost always use the LoadableDetachableModel.
Be sure you have completely have understood the antipatterns at https://cwiki.apache.org/WICKET/best-practices-and-gotchas.html#BestPracticesandGotchas-Antipatterns.
The second and maybe finally most important Model is the CompoundPropertyModel. And remember that you can wrap Model. Example of that is:
class PersonList extends ListView<Person> {
private static final long serialVersionUID = 1L;
public PersonList(final String id, final IModel<List<Person>> model) {
super(id, model);
}
@Override
protected void populateItem(final ListItem<Person> item) {
item.setModel(new CompoundPropertyModel<Person>(item.getModel()));
item.add(new Label("id"));
item.add(new Label("name"));
// ...
}
}
2. To keep wicket code more readable, avoid using anonymous inner classes.
Wicket as well as Swing code can blow up in your face when it comes to anonymous inner classes. Instead write separate classes in the same WebPage class, like the above example. Which result in in a much more cleaner code.public class PersonPage extends WebPage {
private static final long serialVersionUID = 1L;
public PersonPage(final PageParameters parameters) {
super(parameters);
IModel<List<Person>> persons = ...;
add(new PersonList("personList", persons));
}
}
3. Try to only use the PageParameters constructor
Example of that you can see in the above PersonPage example. The reason for that is now you page can always be bookmarkable. Which the default constructor also can be, but it is more generic to directly use the PageParameters constructor, in case you later need to use a page parameters.4. Do not try to reinvent the wheel when it comes to graphical components.
Wicket comes with a rich set of ready to use graphical components. Be sure to import them in your pom file.<dependency>
<groupid>org.apache.wicket</groupId>
<artifactid>wicket-core</artifactId>
<version>${wicket.version}</version>
</dependency>
<dependency>
<groupid>org.apache.wicket</groupId>
<artifactid>wicket-extensions</artifactId>
<version>${wicket.version}</version>
</dependency>
<dependency>
<groupid>org.apache.wicket</groupId>
<artifactid>wicket-datetime</artifactId>
<version>${wicket.version}</version>
</dependency>
If you need more have a look at http://wicket.visural.net/examples/, which have a nice WYSIWYG editor and a spinner.
Also since Wicket 6 is jQuery the backend library for Ajax. Look for example at the jQuery modal window at http://www.wicket-library.com/wicket-examples/ajax/modal-window.
5. Use Wicket XHTML namespace
See https://cwiki.apache.org/WICKET/wickets-xhtml-tags.html6. Be sure you have configured Maven Jetty Plugin to restart whenever you have made code changes
<plugin>
<groupid>org.mortbay.jetty</groupId>
<artifactid>jetty-maven-plugin</artifactId>
<version>${jetty.version}</version>
<configuration>
<scanintervalseconds>1</scanIntervalSeconds>
<usetestclasspath>true</useTestClasspath>
<connectors>
<connector implementation="org.eclipse.jetty.server.nio.SelectChannelConnector">
<port>8080</port>
<maxidletime>3600000</maxIdleTime>
</connector>
</connectors>
</configuration>
</plugin>
7. And remember you can always debug your Wicket code with a JUnit.
Wicket has a great support for module testing support. But these test cases can also be used for debugging. When generating a Wicket project with the archetype you get a single JUnit test case with that you can start Jetty and then simply add breakpoints and you can debug your code.
References:
- https://cwiki.apache.org/WICKET/best-practices-and-gotchas.html
- Apache Wicket in Action - http://www.manning.com/dashorst/
- Apache Wicket Cookbook - http://www.packtpub.com/apache-wicket-cookbook/book
September 19, 2012
How to add Bookmark/Shortcut to Left Sidebar of Nautilus Manager in Ubuntu 12.04
2. Then press Ctrl+D. Now will a new bookmark be created in the upper left corner of nautilus manager.
September 2, 2012
Configure DatabaseServerLoginModule with Hashed Password on JBoss 7 AS
Before we begin our tour we need to first install a database driver. In my previous blog I showed you how to install MySQL driver, please see http://magnus-k-karlsson.blogspot.se/2012/08/how-to-install-mysql-datasource-on.html. And in this blog I will continue to use MySQL for my data source. We will also use JBoss in standalone mode, since we are dealing with a single node installation. Remember to look at the new JBoss module capabilities if you are facing a multi node installation and you want to share the same configurations.
After you have installed your data source you can check your configuration by starting JBoss and you should receive something like in your JBoss server log.
Bound data source [java:jboss/datasources/MySQLDS]
Now continue by creating database schema and tables:
CREATE TABLE Users(username VARCHAR(255), passwd VARCHAR(255), PRIMARY KEY (username));
CREATE TABLE UserRoles(username VARCHAR(255), userRoles VARCHAR(255));
Now we continue with add a new security domain, i.e. actually configure our database login module
<security-domain name="StaticUserPwd" cache-type="default">
<authentication>
<login-module code="org.jboss.security.auth.spi.DatabaseServerLoginModule" flag="required">
<module-option name="dsJndiName" value="java:jboss/datasources/MySQLDS"/>
<module-option name="principalsQuery" value="select passwd from Users where username=?"/>
<module-option name="rolesQuery" value="select userRoles, 'Roles' from UserRoles where username=?"/>
<module-option name="hashAlgorithm" value="SHA1"/>
<module-option name="hashEncoding" value="BASE64"/>
<module-option name="hashCharset" value="UTF-8"/>
<module-option name="hashUserPassword" value="true"/>
<module-option name="hashStorePassword" value="false"/>
</login-module>
</authentication>
</security-domain>
Now we need a web application. I did not bother to build a maven application for that so I simply created a new folders manually
$ mkdir -p $JBOSS_HOME/standalone/deployments/msc-secure-webapp.war/WEB-INF
A simple Index.jsp page
$ touch $JBOSS_HOME/standalone/deployments/msc-secure-webapp.war/Index.jsp
<html>
<head>
</head>
<body>
<h2>Hello <%= request.getRemoteUser() %></h2>
</body>
</html>
And the standard web application deployment descriptor:
$ touch $JBOSS_HOME/standalone/deployments/msc-secure-webapp.war/WEB-INF/web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
<display-name>MSC Secure Webapp</display-name>
<welcome-file-list>
<welcome-file>./Index.jsp</welcome-file>
</welcome-file-list>
<security-constraint>
<web-resource-collection>
<web-resource-name>Secure Webapp</web-resource-name>
<url-pattern>/*</url-pattern>
<http-method>GET</http-method>
<http-method>POST</http-method>
<http-method>PUT</http-method>
<http-method>DELETE</http-method>
<http-method>HEAD</http-method>
</web-resource-collection>
<auth-constraint>
<description>These are the roles who have access.</description>
<role-name>*</role-name>
</auth-constraint>
<user-data-constraint>
<description>This is how the user data must be transmitted.</description>
<transport-guarantee>NONE</transport-guarantee>
</user-data-constraint>
</security-constraint>
<login-config>
<auth-method>BASIC</auth-method>
<realm-name>StaticUserPwd</realm-name>
</login-config>
<security-role>
<description>These are the roles who have access.</description>
<role-name>*</role-name>
</security-role>
</web-app>
And the corresponding JBoss application deployment descriptor:
$ touch $JBOSS_HOME/standalone/deployments/msc-secure-webapp.war/WEB-INF/jboss-web.xml
<?xml version="1.0" encoding="UTF-8"?>
<jboss-web>
<security-domain>java:/jaas/StaticUserPwd</security-domain>
</jboss-web>
Before we can fire up JBoss we need to tell the JBoss container to deploy the exploded web app.
$ touch $JBOSS_HOME/standalone/deployments/msc-secure-webapp.war.dodeploy
Now we can start JBoss and look for the deployment info log,
Deployed "msc-secure-webapp.war"
Now when we try to access the web application at http://localhost:8080/msc-secure-webapp we are meet by a username and password login window. Since we do not have any user in our database we will not be able to login yet.
Lets create some user. But how do we do that when the password are suppose to be stored in a hased format? Either you can write a small Java program to get the hashed password a easier way is to use openssl. To create a hashed password for admin simple enter
$ echo -n "admin" | openssl dgst -sha1 -binary | openssl base64
And with that hashed password we can create a new user with the followin sql insert.
Insert into Users values('admin','0DPiKuNIrrVmD8IUCuw1hQxNqZc=');
Insert into UserRoles values('admin','GRP_ADMIN');
Now you can open your web application http://localhost:8080/msc-secure-webapp/ and log in with username "admin" and password "admin".
August 16, 2012
Eclipse Keyboard Shortcut Does not Work on Ubuntu 12.04
In the example below I simple removed the Ubuntu keyboard shortcut for Zoom in and Zoom out, since I don't need them.
August 15, 2012
How to Install Maven 3 on Ubuntu
In short the installation follow:
- Download the apache maven binaries from the offical website http://maven.apache.org.
- Decompress the binaries. I use /home/magnus/bin/apache-maven-3.0.4 on my local laptop, but for a server I suggest /opt/apache-maven-3.0.4.
- Modify binary path and environment which I previously described here http://magnus-k-karlsson.blogspot.se/2012/04/how-to-set-javahome-environment.html.
$ cat /etc/environment
PATH="/home/magnus/bin/apache-maven-3.0.4/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games"
JAVA_HOME=/usr/lib/jvm/jdk1.6.0_31
M2_HOME=/home/magnus/bin/apache-maven-3.0.4
August 13, 2012
How to install MySQL DataSource on JBoss EAP 6 and AS 7.
- The changed classloading to a more OSGI like architecture.
- The simplification of have only one configuration file.
You find module in $JBOSS_HOME/modules folder. To create a new module you need to do three things:
- Create a folder hierarchy for files.
- Copy module jar files.
- Create module configuration file – module.xml.
$ mkdir -p $JBOSS_HOME/modules/com/mysql/main
$ cp mysql-connector-java-5.1.19.jar $JBOSS_HOME/modules/com/mysql/mainmysql-connector-java-5.1.19.jar
$ touch $JBOSS_HOME/modules/com/mysql/main/module.xml
Now edit the module.xml file:
<?xml version="1.0" encoding="UTF-8"?>
<module xmlns="urn:jboss:module:1.1" name="com.mysql">
<resources>
<resource-root path="mysql-connector-java-5.1.19.jar"/>
</resources>
<dependencies>
<module name="javax.api"/>
</dependencies>
</module>
Now you are ready to use configure your DataSource. Here I will use the JBoss Standalone and not the Domain configuration. In short the main differences between Module and Standalone JBoss configuration is if you want to reuse your configuration through several JBoss instance you shoud use the Domain configuration approach. In this blog I will use the Standalone configuration, but the differences of doing the other way around is not big.
The configuration of the JBoss can be done in several way: 1. CLI 2. Web interface 3. or manually through editing xml file. Which way you choose is up to you, here I will show the result.
$JBOSS_HOME/standalone/configuration/standalone.xml:
<subsystem xmlns="urn:jboss:domain:datasources:1.1">
<datasources>
...
<datasource jndi-name="java:jboss/datasources/YourProjectDS" pool-name="YourProjectDS" enabled="true" use-java-context="true">
<connection-url>jdbc:mysql://localhost:3306/YourProjectDB</connection-url>
<driver>mysql</driver>
<security>
<user-name>uid</user-name>
<password>pwd</password>
</security>
</datasource>
<drivers>
...
<driver name="mysql" module="com.mysql"/>
</drivers>
</datasources>
</subsystem>
August 1, 2012
Search JAR Files after Specific Class
Ones in a while I need to search in a lot of jar files after a specific class class and that is what this command:
$ find . -name "*.jar" -a -exec bash -c "unzip -l {} | grep Foo.class" \; -print
July 28, 2012
Java EE 6 Annotations - Summary
- Java EE 6 Annotations - JSR 299: Contexts and Dependency Injection - Part 1
- Java EE 6 Annotations - JSR 299: Contexts and Dependency Injection - Part 2
- Java EE 6 Annotations - JSR 250: Common Annotations
Contexts and Dependency Injection, CDI
JavaServer Faces, JSF
Resource Injection
EJB Injection
EJB Types
Schedule
Transaction
- javax.ejb.TransactionAttribute
- javax.ejb.AfterBegin
- javax.ejb.BeforeCompletion
- javax.ejb.AfterCompletion
Lifecycle
- javax.annotation.PostConstruct (POJO/EJB)
- javax.annotation.PreDestroy (POJO/EJB)
- javax.ejb.PostActivate (EJB)
- javax.ejb.PrePassivate (EJB)
- javax.ejb.Remove (EJB)
Interceptors
Security
Java Persistence API, JPA
July 27, 2012
Java EE 6 Annotations - JSR 250: Common Annotations
Resource Injection:
Lifecycle:
Managed Bean:
javax.annotation.Resource and javax.annotation.Resources
The @Resource and @Resources are shortcut for doing jndi lookup, e.g. new new InitialContext().lookup(name).javax.annotation.PostConstruct and javax.annotation.PreDestroy
The @PostConstruct and @PreDestroy are neat annotation to do initialization and clean up in a standardized way. I would recommend to use these annotation even if you are not deploying to an enterprise environment. The above annotation can be used on any bean, such EJB, MDB, Managed Bean, but if you annotate a POJO, you need to fetch that POJO with @Inject to make the PostConstruct and PreDestroy be called.javax.annotation.ManagedBean
This annotation should NOT be mixed up with javax.faces.bean.ManagedBean or JMX Managed Bean. It is a lightweight component that enables resource injection, lifecycle callbacks and interceptors. I my self is not totally sure where this component fits in, but it's there.Java EE 6 Annotations - JSR 299: Contexts and Dependency Injection - Part 2
- JBoss Arquillian, http://www.jboss.org/arquillian.html
- Apache OpenEJB, http://openejb.apache.org/
In this blog I will show how to do that with JBoss Arquillian, but Apache OpenEJB is also a good solution.
There is a rather good getting started guide at http://arquillian.org/guides/getting_started/, which I encourage you to read and here I will give you a shorthand version, but also give some more clarification about the Arquillian Maven dependency.
First add JBoss Maven User repository, which is described here, https://community.jboss.org/wiki/MavenGettingStarted-Users.
Then create an empty maven project. Modify your pom.xml accordingly
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>se.msc.examples</groupId>
<artifactId>cdi-tutorial</artifactId>
<version>1.0.0-SNAPSHOT</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.build.outputEncoding>UTF-8</project.build.outputEncoding>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.jboss.arquillian</groupId>
<artifactId>arquillian-bom</artifactId>
<version>1.0.1.Final</version>
<scope>import</scope>
<type>pom</type>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.jboss.spec</groupId>
<artifactId>jboss-javaee-6.0</artifactId>
<version>1.0.0.Final</version>
<type>pom</type>
<scope>provided</scope>
</dependency>
<!-- Test Support -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.1</version>
<scope>test</scope>
</dependency>
<!-- Arquillian JUnit Integration -->
<dependency>
<groupId>org.jboss.arquillian.junit</groupId>
<artifactId>arquillian-junit-container</artifactId>
<scope>test</scope>
</dependency>
<!-- Arquillian container adapter for the target container -->
<dependency>
<groupId>org.jboss.arquillian.container</groupId>
<artifactId>arquillian-weld-ee-embedded-1.1</artifactId>
<version>1.0.0.CR3</version>
<scope>test</scope>
</dependency>
<!-- Target Container - Weld EE Container -->
<dependency>
<groupId>org.jboss.weld</groupId>
<artifactId>weld-core</artifactId>
<version>1.1.5.Final</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.6.4</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>cdi-tutorial</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.12</version>
</plugin>
</plugins>
</build>
</project>
There are two importing thing to stress here. First we need to understand one imported design goal with Arquillian and that is it was meant to be conainer neutral, i.e. the Arquillian test should not be affected if you decided to run your EE test on a different EE Container. And this design goal is reflected in the Arquillian Maven dependency module.
So with that in mind, the first dependency arquillian-weld-ee-embedded-1.1 is only an adapter and does not contain any real CDI implementation code, which is in this case the Weld implementation code.
The second imported thing to understand is that Weld does not support EJB or any other similar EE modules, it only supports CDI.
If you want more container support look at the following pom, https://raw.github.com/arquillian/arquillian-examples/master/arquillian-tutorial/pom-no-container-profiles.xml.
Java EE 6 Annotations - JSR 299: Contexts and Dependency Injection - Part 1
Introduction
One of the maybe biggest new feature in EE 6 is the Dependency Injection, DI, support. Many of you are already familiar with the this concept after been using Spring, Guice or other DI framework. But now these feature are standardized in the EE 6 specification via the following Annotations:Overview
Core DI package [http://docs.oracle.com/javaee/6/api/javax/inject/package-summary.html]- javax.inject.Inject
- javax.inject.Named
- javax.inject.Qualifier
- javax.inject.Singleton
- javax.enterprise.context.ApplicationScoped
- javax.enterprise.context.SessionScoped
- javax.enterprise.context.ConversationScoped
- javax.enterprise.context.RequestScoped
- javax.enterprise.context.Dependent
- javax.enterprise.inject.Alternative
- javax.enterprise.inject.New
- javax.enterprise.inject.Any
- javax.enterprise.inject.Produces
- javax.enterprise.inject.Disposes
javax.inject.Named
The @Named annotation is used for alternative lookup id. Example@Named("cart") public class ShoppingCart {
public String getHello() { return "Hello"; }
}
And the JSF/JSP page:
<h:outputText value="#{cart.total}" />
javax.inject.Qualifier
The @Qualifier is used to qualify injection if there are multiple candidate in the classpath. Example a bean @Inject an interface but there are several implementation of that interface in the classpath.javax.enterprise.context
You might think the above scooping Annotation are only used with JSF, but in fact these Annotation can be used on any Bean, for example try to annotate a POJO with @ApplicationScope and then grab it with the @Inject annotation in a Bean.javax.enterprise.inject.Alternative
The @Alternative annotation is used for annotating a class to be ignored for @Inject lookup, and to enable must it be declared in META-INF/beans.xml. Example:public interface IFoo {
public String getHello();
}
public class FooImpl implements IFoo {
@Override public String getHello() { return "Hello"; }
}
@javax.enterprise.inject.Alternative
public class FooAlternativeImpl implements IFoo {
@Override public String getHello() { return "Alternative Hello"; }
}
META-INF/beans.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/beans_1_0.xsd">
<alternatives>
<class>se.msc.example.cdi.alternative.FooAlternativeImpl</class>
</alternatives>
</beans>
And now can @Alternative be used with @Inject
public class IFooService {
@Inject IFoo iFoo;
}
javax.enterprise.inject.New
The @New annotation creates a new inject instance of Object, good example is shown in EE API, http://docs.oracle.com/javaee/6/api/javax/enterprise/inject/New.html.javax.enterprise.inject.Any
“The @Any qualifier allows an injection point to refer to all beans or all events of a certain bean type.” [http://docs.oracle.com/javaee/6/api/javax/enterprise/inject/Any.html]javax.enterprise.inject.Produces
The @Produces annotation can be seen as a factory. Good example can be seen here http://code.google.com/p/jee6-cdi/wiki/DependencyInjectionAnIntroductoryTutorial_Part1#Using_simple_@Produces.javax.enterprise.inject.Disposes
And the @Disposes is the cleanup method for factory @Produces.Now I presume that you are eager to get started testing these and annotation and that I will describe in my next blog http://magnus-k-karlsson.blogspot.se/2012/07/java-ee-6-annotations-jsr-299-contexts_27.html.
June 29, 2012
JUDCon and JBoss 2012 Boston: OSGi is coming back
App Server | Deploy Bundle | WAB Support* | EJB/JPA |
Glassfish | Yes | Yes | Prototype |
Webspere | Yes | Yes | Prototype |
WebLogic | No | No | No |
JBoss AS | Yes | Yes | No |
This is all interesting you might think, but what impact will that have on the Java EE architecture? I would say a rather a lot. Lets start with the deployment. All Java EE deployable are monolithic, think of web and ear. They all package everything in on single file. How does that go with the OSGi moduling thinking? Not at all. The OSGi concept is programming modules and make them interchangeable, deployable as separate module and with own versions lifecycle. So this has to change if one want to bring the OSGi thinking to the Java EE arena.
If you want to play with OSGi I would recommend Eclipse and install the bnd tool (http://bndtools.org/).
And if you want to read more about OSGI OSGi in Action by Manning is a great book.
http://www.amazon.com/OSGi-Action-Creating-Modular-Applications/dp/1933988916/ref=sr_1_1?s=books&ie=UTF8&qid=1340977056&sr=1-1&keywords=osgi+in+action
And at more architecture level I would recommend Java Application Architecture
http://www.amazon.com/Java-Application-Architecture-Modularity-Patterns/dp/0321247132/ref=sr_1_1?s=books&ie=UTF8&qid=1340977092&sr=1-1&keywords=Java+Application+Architecture
JUDCon and JBoss 2012 Boston: Red Hat released the JBoss EAP 6 at june 21 2012
Red Hat keeps the pattern of changing the JBoss server drastically with every major release version and that is the case with JBoss EAP 6 as well.
One of the biggest differences are:
- Is starting to aim at a more OSGi oriented architecture, the full OSGi support is not there yet, but will be fully supported in JBoss EAP 6.1
- The configuration has changed dramatically, basically they have moved all configuration to one file instead of having them sprayed out the entire JBoss file directories. And then start using more default which leads to a great reduction of the configuration file.
- The startup and running lifecyle has changed and comes down to two ways domain and standalone, which is quite similar to Oracle BEA, if you are familiar with that.
But there were also more releases announced at the same day and maybe from a developer point of view, the most interesting was the the improved JBoss best practice documentation sites:
The JBoss Way
http://www.jboss.org/developer
JBoss Developer Framework
http://www.jboss.org/jdf/
Another big and important announcement is that the project Wolf has finally delivered. The project Wolf is about providing a centralized maven repository for the JBoss EAP development. And this repository is also shipped with JBoss EAP.
And all the presentation will be made available at http://www.youtube.com/redhat.
June 28, 2012
Improve your performance on Ubuntu 12.04 with Jupiter
$ sudo add-apt-repository ppa:webupd8team/jupiter
$ sudo apt-get update
$ sudo apt-get install jupiter
For details see http://www.webupd8.org/2010/07/jupiter-ubuntu-ppa-hardware-and-power.html.
And if you like to change the default Icon simply replace the /usr/share/pixmaps/bolt1-4.png files. For details see http://askubuntu.com/questions/127449/how-can-i-change-the-icon-for-jupiter.
June 1, 2012
How to install install and configure SSL on Tomcat 7
$CATALINA_HOME/conf/server.xml:
<Connector port="8443" protocol="HTTP/1.1" SSLEnabled="true"
maxThreads="150" scheme="https" secure="true"
clientAuth="false" sslProtocol="TLS"
keystoreFile="${catalina.base}/conf/server.keystore"
keystorePass="changeit" />
Then reuse the previous test application and open your browser. Here you will get a warning about a self-signed certificate. Accept it and you will see your application.
Performance Tuning Web Pages
Before:
<html>
<head>
<script src="some_url" type="text/javascript"></script>
<script type="text/javascript">
// maybe call some script
</script>
</head>
<body>
</body>
</html>
After:
<html>
<head>
</head>
<body>
<script src="some_url" type="text/javascript"></script>
<script type="text/javascript">
// maybe call some script
</script>
</body>
</html>
May 31, 2012
How to install install and configure SSL on JBoss 5.1
The first you need is a Server Certificate. You can get a valid Certificate from Certification Authority (CA) like Thwate, Verisign and many more. But here I will use a self-signed certificate but the process is quite similar for the ones from a Certification Authority (CA).
$ keytool -genkey -alias rsatest -keyalg RSA -keystore server.keystore -keysize 2048 -validity 60
...
Is CN=localhost, OU=MSC, O=MSC, L=Stockholm, ST=Stockholm, C=SE correct?
[no]: yes
Remember that the first and last name should be either your domain address or machine name.
And to view your self-signed certificate:
$ keytool -list -v -keystore server.keystore
Now you need configure your HTTPS Connector.
$JBOSS_HOME/server/$JBOSS_CONF/deploy/jbossweb.sar/server.xml:
<Connector protocol="HTTP/1.1" SSLEnabled="true"
port="8443" address="${jboss.bind.address}"
scheme="https" secure="true" clientAuth="false"
keystoreFile="${jboss.server.home.dir}/conf/server.keystore"
keystorePass="changeit" sslProtocol="TLS" />
There are several more configuration for the Connector which you can read more about here for JBoss 5.1:http://docs.jboss.org/jbossweb/2.1.x/config/http.html
And the latest documentation is found here:
http://docs.jboss.org/jbossweb/latest/config/http.html
The last thing we need is a test application I will leave that as a practice, but I will show one interesting part and that is the web.xml
<web-app>
...
<security-constraint>
<web-resource-collection>
<web-resource-name>Security Demo</web-resource-name>
<url-pattern>/*</url-pattern>
<!-- Do not specify http-method, since then only specified
http-method will be authenticated, not e.g. JUNK (attack) -->
</web-resource-collection>
<auth-constraint>
<description>These are the roles who have access.</description>
<role-name>ROLE_FOO</role-name>
</auth-constraint>
<user-data-constraint>
<description>This is how the user data must be transmitted.</description>
<transport-guarantee>CONFIDENTIAL</transport-guarantee>
</user-data-constraint>
</security-constraint>
<login-config>
<auth-method>BASIC</auth-method>
<realm-name>foo-realm</realm-name>
</login-config>
<security-role>
<role-name>ROLE_FOO</role-name>
</security-role>
<session-config>
<!-- Session timeout after X MINUTES after no user interaction. -->
<session-timeout>15</session-timeout>
<cookie-config>
<!-- XSS attack: make sure that cookie cannot be accessed via
client side scripts -->
<http-only>true</http-only>
<!-- CSRF attack, session hijack attack: require cookie can
only be used for SSL communication. -->
<secure>true</secure>
</cookie-config>
<!-- Do not use URL, since then it can be stored in numerous places:
browser history, proxy server log, referrer logs, web logs, etc. -->
<tracking-mode>COOKIE</tracking-mode>
</session-config>
...
</web-app>
Now open you web browser and test your application. To do the same for Tomcat is quite similiar, read more about it in my next blog - http://magnus-k-karlsson.blogspot.se/2012/06/how-to-install-install-and-configure.html.
May 19, 2012
How to find out which version of Ubuntu
$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 12.04 LTS
Release: 12.04
Codename: precise
And to find out if you are running a 32 or 64-bit OS.
$ uname -a
Linux magnus-HP-ProBook-6550b 3.2.0-24-generic #37-Ubuntu SMP Wed Apr 25 08:43:22 UTC 2012 x86_64 x86_64 x86_64 GNU/Linux
For 64-bit you will see: x86_64
and for 32-bit: i686
Top Things After Installing Ubuntu 12.04 Practical Precise
1. First update your system.
$ sudo apt-get update && sudo apt-get upgrade
2. Enable Restricted drivers available
Open Additional Drivers and select Recommended driver.3. Remove overlay bar:
$ sudo apt-get remove overlay-scrollbar liboverlay-scrollbar3-0.2-0 liboverlay-scrollbar-0.2-0
4. Add Medibuntu Repository
http://medibuntu.org/repository.php
5. Install Common Codecs
$ sudo apt-get install non-free-codecs libxine1-ffmpeg gxine mencoder totem-mozilla icedax tagtool easytag id3tool lame nautilus-script-audio-convert libmad0 mpg321
6. To play encrypted DVDs
$ sudo apt-get install libdvdcss2
$ sudo /usr/share/doc/libdvdread4/./install-css.sh
7. Install Non Open Source Programs
$ sudo apt-get install ubuntu-restricted-extras
8. Install Video Player
$ sudo apt-get install vlc
9. Video Editor
There are several easy to use video editors out there: Here are two:- Avidemux
- Openshot
10. Torrents client - qBittorrent
A Simple to use Bittorrent client is qBittorrent. You install it easily from Ubuntu Software Center.11. Manage Unity - MyUnity
If you are not happy with the dock you can easily change it's appearance with MyUnity. You install from Ubuntu Software Center.12. Oracle Java 6
If think the best way to install Oracle Java SDK is to do it manually. An easy to use guide is here https://help.ubuntu.com/community/Java.13. Set $JAVA_HOME
I have previously describe how to set the Java environment home variable here: http://magnus-k-karlsson.blogspot.se/2012/04/how-to-set-javahome-environment.html14.Keyboard Shortcut
15. New Launcher (Shortcut) in Dock
I have previously described it here:http://magnus-k-karlsson.blogspot.se/2012/01/how-to-make-launcher-shortcut-in-new_31.html
16. LibreOffice
If you need to install more language for spelling checking, first download the wantedhttp://extensions.libreoffice.org/extension-center?getCategories=Dictionary&getCompatibility=any&sort_on=positive_ratings&path=%2FLibreOffice-Extensions-and-Templates%2Fextension-center&portal_type=PSCProject&SearchableText=swedish
How to install:
http://wiki.documentfoundation.org/Documentation/HowTo/install_extension
Restart.
Check installation under Tools -> Options -> Language Settings -> Writing Aids and Edit Module.