Saturday, November 5, 2011

JBOSS AS 7 authentication under Openshift

There are many implementations for the authentication (for example LDAP, Database, Property-File, etc...):
https://docs.jboss.org/author/display/AS7/Security+subsystem+configuration

One of the simplest methods is using a property file for the users and an other for the roles;
Code: UsersRoles
Classname: org.jboss.security.auth.spi.UsersRolesLoginModule

File users.properties:
username0=password0
username1=password1

File roles.properties:
username0=role0,role1
username1=role1,role2

In your Jboss's config/standalone.xml look for this configuration:
<subsystem xmlns="urn:jboss:domain:security:1.0">
  <security-domains>
    <security-domain name="other" cache-type="default">
      <authentication>
        <login-module code="UsersRoles" flag="required">
          <module-option name="usersProperties" value="${OPENSHIFT_APP_DIR}/runtime/repo/users.properties" />
          <module-option name="rolesProperties" value="${OPENSHIFT_APP_DIR}/runtime/repo/roles.properties" />
        </login-module>
      </authentication>
   </security-domain>
 </security-domains>
</subsystem>

JBoss is now ready, remember to protect your web app adding this configuration in WEB-INF/web.xml:
<security-constraint>
 <web-resource-collection>
  <web-resource-name>Finance</web-resource-name>
  <url-pattern>/*</url-pattern>
 </web-resource-collection>
 <auth-constraint>
  <role-name>user</role-name>
  <role-name>admin</role-name>
 </auth-constraint>
</security-constraint>

<login-config>
 <auth-method>BASIC</auth-method>
 <realm-name>Megaris Finance</realm-name>
</login-config>
 
<security-role>
 <description>Role for simple users</description>
 <role-name>user</role-name>
</security-role>
<security-role>
 <description>Role for administrators</description>
 <role-name>admin</role-name>
</security-role> 

Friday, July 22, 2011

Automatically create a fixed symbolic link when a device is attached

My laptop had two USB devices that was mapped everytime to differents /dev/ttyUSB paths.
For example sometime:
[9.019817] usb 2-1.4: Qualcomm USB modem converter now attached to ttyUSB0
[9.053636] usb 2-1.6: pl2303 converter now attached to ttyUSB1
or sometime so:
[9.053636] usb 2-1.6: pl2303 converter now attached to ttyUSB0
[9.019817] usb 2-1.4: Qualcomm USB modem converter now attached to ttyUSB1

The "pl2303 converter" is a USB-serial adapter that I need for some special hardware.
Because of this inconsistent mapping I needed to change quite often the settings in my software.

To solve the problem I created a symbolic device link for the USB-serial adapter, as described below:

1 step: detect the "idVendor" and "idProduct" of your hardware
$ lsusb
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 002 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 001 Device 002: ID 8087:0020 Intel Corp. Integrated Rate Matching Hub
Bus 002 Device 002: ID 8087:0020 Intel Corp. Integrated Rate Matching Hub
Bus 001 Device 003: ID 147e:2016 Upek Biometric Touchchip/Touchstrip Fingerprint Sensor
Bus 001 Device 004: ID 0a5c:217f Broadcom Corp. Bluetooth Controller
Bus 001 Device 005: ID 17ef:1009 Lenovo 
Bus 001 Device 006: ID 17ef:480f Lenovo Integrated Webcam [R5U877]
Bus 002 Device 003: ID 05c6:9204 Qualcomm, Inc. 
Bus 002 Device 004: ID 067b:2303 Prolific Technology, Inc. PL2303 Serial Port
Bus 001 Device 007: ID 046d:c018 Logitech, Inc. Optical Wheel Mouse
Bus 001 Device 008: ID 046a:0023 Cherry GmbH CyMotion Master Linux Keyboard
Bus 003 Device 001: ID 1d6b:0003 Linux Foundation 3.0 root hub

In my case was the "Prolific Technology, Inc. PL2303 Serial Port". The "idVendor" is 067b while the "idProduct" is 2303

2 step: add an udev rule to create a symbolic device link everytime that the hardware is attached.
go to '/etc/udev/rules.d/' and create a file named for example '97-local.rules' with this content:
kernel=="ttyUSB*", SYSFS{idVendor}=="067b", SYSFS{idProduct}=="2303", SYMLINK="SerialUSB"

3 step: restart you system... you've done!
Give a look to your /dev directory. You shoud now see a device named /dev/SerialUSB

You can create udev rules also for other purposes, for example run a script when a new hardware is detected.

Thursday, June 30, 2011

Compare binary two jar files

Compare two jar files, included the binary content (it will be created an hex dump).

Third-party dependency: jar (openJdk), hexdump, meld
#!/bin/sh

export TEMP="/tmp"

print_usage() {
  echo "$0  " 
}

# Validate the args. Two arguments are expected.
validate_args() {
  if [[ $1 == "" ]]; then
    print_usage
    exit -1
  fi
  
  if [[ $2 == "" ]]; then
    print_usage
    exit -1
  fi
}

# Extract the jar to the $TEMP dir and do a hex dump for 
# the binary files (the original file is deleted).
#
# arg1: the path to the jar file
extract_jar() {
  basename1=`basename $1`
  dir1="$TEMP/$basename1.d"
  rm -fR $dir1
  mkdir -p $dir1
  cp $1 $dir1
  cd $dir1 && jar -xf "$dir1/$basename1" && cd -
  rm -f "$dir1/$basename1"
  
  find "$dir1" -type f | while read filename
   do
     # retrieve the mime type
     mime=`file -bi "$filename"`
     encoding=`echo $mime | cut -d "=" -f2`
     
     # hex dump for binary files
     if [[ $encoding == "binary" ]]; then
       hexdump -C "$filename" > "$filename.hex"
       rm -f "$filename"
     fi
   done
}
validate_args $1 $2
echo "Compare '$1' with '$2'"
extract_jar $1
extract_jar $2

#Compare with MELD
basename1=`basename $1`
dir1="$TEMP/$basename1.d"
basename2=`basename $2`
dir2="$TEMP/$basename2.d"

meld "$dir1" "$dir2"

Thursday, November 26, 2009

How to pack native libraries (JNI) into a jar.

Usually the native libraries are passed to the VM with the property java.library.path. This is not pratical when we want to provide a single jar, and we cannot pass an URL to System#load(String).
Therefore we adopt the strategy to copy the native library contained in the jar (or simply classpath) in the system temporary folder and than we
pass this filename to System#load(String).

As for the standard JDK method, this one should be invoked at classloading:

static {
  try {
   loadLibrary("my.package", "HelloWorld");
  } catch (IOException e) {
   // handle this exception
  }
 }

This is our implementation:
/**
 * Load native libraries packed in a jar.
 *
 * @param pkg
 *            The package where the shared library (for example .so or .ddl)
 *            is.
 * @param libname
 *            The system indipendent name (for example HelloWorld for
 *            libHelloWorld.so under linux or HelloWorld.dll under windows).
 * @throws IOException
 */
static public void loadLibrary(String pkg, String libname) throws IOException {
 String syslibname = System.mapLibraryName(libname);
  String tmpdir = System.getProperty("java.io.tmpdir");
 File file = new File(tmpdir + File.separator + syslibname);
 if (!file.exists()) {
  String resourcename = "/" + pkg.replace('.', '/') + "/" + syslibname;
  writeToFile(GtkVersion.class.getResourceAsStream(resourcename), file);
 }
  System.load(file.getAbsolutePath());
}
 static private void writeToFile(InputStream stream, File file) throws IOException {
 OutputStream os = null;
 try {
  os = new FileOutputStream(file);
  byte buffer[] = new byte[4096];
  int len = 0;
  while ((len = stream.read(buffer)) > 0) {
   os.write(buffer, 0, len);
  }
 } finally {
  if (os != null) {
   os.close();
  }
  stream.close();
 }
}

Monday, May 18, 2009

Generating Java Beans from XSD Schema

As you probably know, JAXB (Java Architecture for XML Binding) allows to map (marshal/unmarshal) Java Beans to XML representations.
You can also do the opposite: generating Java Beans from XSD Schema.
To do that, there the xjc utility in $JAVA_HOME/bin.
For example:
$ wget http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd
$ xjc -p eu.kostia.persistence persistence_1_0.xsd

will generate the classes ObjectFactory, Persistence and PersistenceUnitTransactionType in the package eu.kostia.persistence.

Other useful info here.

Wednesday, April 22, 2009

Best Gnome Theme for the GTK Look and Feel

The GTK Look and Feel was a little bit buggy under gnome, but a lot of improvements were done with the last JDK 1.6 Update 13. Anyway I had still some problems with comboboxes and tabbedpanes.
I used the default Fedora Gnome theme. Now I've switched to Glossy Theme and everything looks perfectly in my java applications.
See with your eyes: the second example uses the Glossy Theme and the combo are rendered correctly.





Also the universal db client SQuirrel looks very good!:


Tuesday, January 20, 2009

Video Plugins for Firefox under Fedora 10

The most important video player/library under linux are Gstreamer, Xine, Mplayer and VLC.
The first two are plug-in based, while the last two are "all-in-one" solutions.

Many of the packages that you need to see the most common proprietary video formats aren't in the standard Fedora repository, therefore you have to add RPMFusion

Of course to see videos in Firefox you need a plugin. To see the videos at http://tv.repubblica.it I've tried all these plugins:
  1. xine-plugin: It couldn't recognise the format and came the message "Format x-ms-asf unknown"
  2. mplayerplug-in: It isn't in RPMFusion but in ATrpms. These two repositories aren't compatible and it's better to not enable them together. Personally I prefer RPMFusion because there the packages are more stable. Anyway with this plugin I could see the video stream but in other sites firefox crashed. It was quite unstable.
  3. mozilla-vlc: I coudn't see anything, maybe I hadn't VLC complete installed.
  4. totem-mozplugin: Totem is the standard player for Gnome. At the beginning I coudn't see anything, but after installing mit yum gstreamer-plugins* and gstreamer-ffmpeg it worked perfectly. It appears also to be very stable and it's the plugin that I'm now currently using.
With the totem-mozplugin I tried then to see a video on http://www.rai.tv: after a short advertising clip the reproduction was interrupted. I clicked with the mouse right button on the video frame and copied the video source URL. I was able to reproduce it with mplayer but not with totem.