Archive for the 'Operating Systems' Category

01
Mar
10

How to mount a remote ssh filesystem using sshfs

I came across this great article that shows how to mount a file system using sshfs. Just thought I would share.

How to mount a remote ssh filesystem using sshfs

It talks about the sshfs and fusermount commands that allow you to use a remote file system just like it was local. On  the  local  computer where  the  SSHFS  is mounted, the implementation makes use of the FUSE (Filesystem in Userspace) kernel module. I believe this to be better than the davfs2 because you don’t have to be root in order to mount the file system.

Requirements

  • Ubuntu
  • Server will files you want to access
  • Root access to local box

TO install

sudo apt-get install sshfs

To Mount

mkdir /home/[your username]/mnt

sshfs prod:/home/[your username] /home/[your username]/mnt

To Un-mount

fusermount -uz /home/[your username]/mnt

28
Feb
10

How to Grep Multiple Lines

This page describes how to find and replace patterns that span across multiple lines.

Once I was coding up some SQL DDL’s and came across a problem. The program I used to generate the DDL did not generate the foreign key relationships such that they could be read by mySQL. The create table looked like this…

CREATE TABLE pwd_category (
       category_i           SMALLINT NOT NULL,
       passwd_i             INTEGER NOT NULL,
       PRIMARY KEY (category_i, passwd_i), 
       FOREIGN KEY (passwd_i)
                             REFERENCES pwd_master, 
       FOREIGN KEY (category_i)
                             REFERENCES pwd_category_master
);

The problem seen above is that mySQL does not like the above format. Instead it wants the DDL to look like this…

CREATE TABLE pwd_category (
       category_i           SMALLINT NOT NULL,
       passwd_i             INTEGER NOT NULL,
       PRIMARY KEY (category_i, passwd_i), 
       FOREIGN KEY (passwd_i) REFERENCES pwd_master  (passwd_i),
       FOREIGN KEY (category_i) REFERENCES pwd_category_master (category_i)
);


Based on sed(1)’s man page you can execute a series of commands within the { } block.

One of the commands is N;
n N Read/append the next line of input into the pattern space.

Step 1

Detect the pattern “FOREIGN KEY” and read the next line.

/FOREIGN KEY/ – search for lines starting with FOREIGN KEY

sed -e '/FOREIGN KEY/{N;p}'

Step 2

{} – apply the enclosed when the preceeding match is made
N – append the next line to the pattern buffer
p – prints the buffer that was just read

The resulting conversion looks like this…

CREATE TABLE pwd_category (
       category_i           SMALLINT NOT NULL,
       passwd_i             INTEGER NOT NULL,
       PRIMARY KEY (category_i, passwd_i), 
       FOREIGN KEY (passwd_i)
                             REFERENCES pwd_master, 
       FOREIGN KEY (passwd_i)
                             REFERENCES pwd_master, 
       FOREIGN KEY (category_i)
                             REFERENCES pwd_category_master
       FOREIGN KEY (category_i)
                             REFERENCES pwd_category_master
);

As you can see that the lines that matched are duplicated…

Step 4

Next we apply a substution using the following pattern

s/FOREIGN KEY \((.*)\).*REFERENCES \(.*\)/FOREIGN KEY \1 REFERENCES \2 \1,/

sed -e '/FOREIGN KEY/{N;s/FOREIGN KEY \((.*)\).*REFERENCES \(.*\)/FOREIGN KEY \1 REFERENCES \2 \1,/p}'

This results in output like this…

CREATE TABLE pwd_category (
       category_i           SMALLINT NOT NULL,
       passwd_i             INTEGER NOT NULL,
       PRIMARY KEY (category_i, passwd_i), 
       FOREIGN KEY (passwd_i) REFERENCES pwd_master,  (passwd_i),
       FOREIGN KEY (passwd_i) REFERENCES pwd_master,  (passwd_i),
       FOREIGN KEY (category_i) REFERENCES pwd_category_master (category_i),
       FOREIGN KEY (category_i) REFERENCES pwd_category_master (category_i),
);

Step 5

Next we get rid of the dups by appending a:
s/\n.*// – delete everything following the first \n.

sed -e '/FOREIGN KEY/{N;s/FOREIGN KEY \((.*)\).*REFERENCES \(.*\)/FOREIGN KEY \1 REFERENCES \2 \1,/;s/\n.*//p}'

This results in output like this…

CREATE TABLE pwd_category (
       category_i           SMALLINT NOT NULL,
       passwd_i             INTEGER NOT NULL,
       PRIMARY KEY (category_i, passwd_i), 
       FOREIGN KEY (passwd_i) REFERENCES pwd_master,  (passwd_i),
       FOREIGN KEY (category_i) REFERENCES pwd_category_master (category_i),
);

Step 6

Next we need to apply some additional clean-up patterns.

sed -e 's/,.*(/ (/' |
sed -e 's/^);/) ENGINE=InnoDB;/'

This results in…

CREATE TABLE pwd_category (
       category_i           SMALLINT NOT NULL,
       passwd_i             INTEGER NOT NULL,
       PRIMARY KEY (category_i, passwd_i), 
       FOREIGN KEY (passwd_i) REFERENCES pwd_master (passwd_i),
       FOREIGN KEY (category_i) REFERENCES pwd_category_master (category_i),
) ENGINE=InnoDB;

The only thing remaining is to cleanup the extra comma. Ohh well. I will fix this some other time…

24
Jan
10

Setting Environment Variables in Ubuntu

This page describes the process of setting up environment variables in Ubuntu.

Session-wide environment variables

In order to set environment variables in a way that affects a user’s entire desktop session, one may place commands to set their values in one of the “hidden” script files in the user’s home directory. The more common such files are outlined below.

  • ~/.profile – This is probably the best file for placing environment variable assignments in, since it gets executed automatically by the Display Manager during the startup process desktop session as well as by the login shell when one logs-in from the textual console.
  • ~/.bash_profile or ~./bash_login – If one of these file exist, bash executes it rather then “~/.profile” when it is started as a login shell. (Bash will prefer “~/.bash_profile” to “~/.bash_login”). However, these files won’t influence a graphical session by default.
  • ~/.bashrc – Because of the way Ubuntu currently sets up the various script files by default, this may be the easiest place to set variables in. The default configuration nearly guarantees that this file will be executed in each and every invocation of bash as well as while logging in to the graphical environment. However, performance-wise this may not be the best thing to do since it will cause values to be unnecessarily set many times.

System-wide environment variables

Environment variable settings that affect the system as a whole (rather then just a particular user’s desktop session) can be placed in any of the many system-level scripts that get executed when the system or the desktop session are loaded. Ubuntu defines several locations dedicated to placing such settings:

  • /etc/profile – This file gets executed whenever a bash login shell is entered (e.g. when logging in from the console or over ssh), as well well as by the DisplayManager when the desktop session loads. This is probably the file you will get referred to when asking veteran UNIX system administrators about environment variables. In Ubuntu, however, this file does little more then invoke the /etc/bash.bashrc file.
  • /etc/bash.bashrc – This is is the system-wide version of the ~/.bashrc file. Ubuntu is configured by default to execute this file whenever a user enters a shell or the desktop environment.
  • /etc/environment – This file is specifically meant for system-wide environment variable settings. It is not a script file, but rather consists of assignment expressions, one per line. Specifically, this file stores the system-wide locale and path settings.

The above was taken from the following URL: https://help.ubuntu.com/community/EnvironmentVariables

My thoughts on the above

If you want to change system wide environment variables then /etc/environment is your best bet.

The environment file contains variables that are made effective globally. The following output shows my current environment file.

vi /etc/environment

PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games"

If you want to change session wide then ~/.bashrc or ~/.pam_environment would be your best bet. See the next code block or the comments below.

vi ~/.bashrc

PATH=$PATH:/path_to_where_java_is/bin
export PATH

FreeBSD

To change environment variables globally just change the /etc/login.conf file. and run the command it lists on the top for the changes to take. Also if there are any global variables in your local configuration files. ex. /etc/cshrc.conf or ~/.cshrc then you need to make sure that the files dont clobber any variables from login.conf.

23
Jan
10

Enabling Digest Authentication in Apache

This page describes the process of getting Apache to protect a directory using digest authentication.

Digest Authentication

Using digest authentication, your password is never sent across the network in the clear, but is always transmitted as an MD5 digest of the user’s password. In this way, the password cannot be determined by sniffing network traffic. This is good for when your web clients are not using SSL.

Modules

Digest authentication is implemented by the module mod_auth_digest. Please have that loaded in the httpd.conf

Protecting a Location or Directory

Put the following in the Location or Directory tag…

    AuthType Digest
    AuthName Protected

    # You can use the htdigest program to create the password database:
    # the -c parameter overwrites a file if it exists. Remove it to append.
    #   htdigest -c "/usr/local/user.passwd" Protected myUser
    AuthUserFile "/usr/local/user.passwd"
    AuthDigestProvider file
    require user myUser myUser2 myUser3

Note: You must protect the user file from unauthorized users.

17
Dec
09

Querying a Database using Spring JdbcTemplate

This page describes the process of querying data from a database using The Spring Framework’s JdbcTemplate and SimpleJdbcTemplate.

One of the first things that catches our eye when we start using the spring framework is the JdbcTemplate.

The following is the DDL for the table we will be using to run Queries. It holds movie Genre’s. Its a simple table with 2 columns.

CREATE TABLE IF NOT EXISTS `GENRE` (
  `GNR_I` int(11) NOT NULL AUTO_INCREMENT,
  `GNR_X` varchar(40) DEFAULT NULL,
  PRIMARY KEY (`GNR_I`)
)

JdbcTemplate

(legacy) This class is considered legacy. If you want to take advantages of the Java 5 features then use SimpleJdbcTemplate in the next section.

The following code queries the Genre table.

package test;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Required;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Component;

@Component("genreDataManager")
public class GenreDataManagerImpl implements GenreDataManager {
	private JdbcTemplate jdbcTemplate;

	public JdbcTemplate getJdbcTemplate() {
		return jdbcTemplate;
	}

	@Autowired
	@Required
	public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
		this.jdbcTemplate = jdbcTemplate;
	}

	public List<Genre> getAllGenre() {
		List genreList = this.jdbcTemplate.query(
			    "select gnr_i, gnr_x from GENRE",
			    new RowMapper() {
			        public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
			            Genre genre = new Genre();
			            genre.setId(rs.getInt(1));
			            genre.setName(rs.getString(2));
			            return genre;
			        }
			    });
		return genreList;
	}
}

SimpleJdbcTemplate

This class takes advantage of varargs and autoboxing, and exposing only the most commonly required operations in order to simplify JdbcTemplate usage. Use this class instead of the plain old JdbcTemplate.

The following is the same thing but using SimpleJdbcTemplate

package test;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Required;
import org.springframework.jdbc.core.simple.ParameterizedRowMapper;
import org.springframework.jdbc.core.simple.SimpleJdbcTemplate;
import org.springframework.stereotype.Component;

@Component("genreDataManager")
public class GenreDataManagerImpl implements GenreDataManager {
	private SimpleJdbcTemplate jdbcTemplate;

	public SimpleJdbcTemplate getJdbcTemplate() {
		return jdbcTemplate;
	}

	@Autowired
	@Required
	public void setJdbcTemplate(SimpleJdbcTemplate jdbcTemplate) {
		this.jdbcTemplate = jdbcTemplate;
	}

	public List<Genre> getAllGenre() {
		List<Genre> genreList = this.jdbcTemplate.<Genre>query(
			    "select gnr_i, gnr_x from GENRE",
			    new ParameterizedRowMapper<Genre>() {
			        public Genre mapRow(ResultSet rs, int rowNum) throws SQLException {
			            Genre genre = new Genre();
			            genre.setId(rs.getInt(1));
			            genre.setName(rs.getString(2));
			            return genre;
			        }
			    });
		return genreList;
	}
}

ApplicationContext.xml

The following file defines the jdbcTemplate and datasource. With these minimal settings you can successfully query a database.

applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<!-- The following is used for the @Autowired and @Required, @Resource etc... java.sql.DriverManager -->
	<context:annotation-config/>
	<context:component-scan base-package="test"/>
	<!-- This is a quick and dirty way to setup a datasource -->
	<bean name="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
		<property name="driverClassName" value="org.gjt.mm.mysql.Driver"/>
		<property name="url" value="dburl"/>
		<property name="username" value="dbusername"/>
		<property name="password" value="password"/>
	</bean>

	<bean name="jdbcTemplate" class="org.springframework.jdbc.core.simple.SimpleJdbcTemplate">
		<constructor-arg><ref bean="dataSource"/></constructor-arg>
	</bean>
</beans>

12
Dec
09

Converting RPM Packages To DEB

This page describes the process of converting an RPM package into a DEB package. DEB packages are used by Ubuntu package manager for installing applications. RPM is used by Red Hat Distribution. The binaries are compatible but the package are not.

Requirements

  • Ubuntu Linux
  • An RPM package to convert

sudo apt-get install alien dpkg-dev debhelper build-essential
sudo alien --scripts pkg-name.rpm
sudo dpkg -i pkg-name.deb

27
Oct
09

Ibatis 2.3 spring 2.5 Hello World

This page describes a very simple HelloWorld type application being created so that it can be used as a base for more complex projects.

We start out with adding this to an already created pom.xml file.

		<dependency>
			<groupId>org.apache.ibatis</groupId>
			<artifactId>ibatis-sqlmap</artifactId>
			<version>2.3.4.726</version>
		</dependency>

If you have not done so already add the jdbc drivers for your database. In my case I am using mySQL.

		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<version>5.1.9</version>
		</dependency>

regenerate the eclipse project:
mvn eclipse:clean eclipse:eclipse

REturn back to eclipse and refresh the project.

IBatis Main Configuration File

We first start with defining the main configuration file for the Ibatis framework. If you are working with a web application this is typically your WEB-INF folder. If you were using ibatis as a standalone framework you would have needed to put additional configuration entries in this file. However since we are using this with the state of-the-art dependency injection functionality provided by the spring framework.

MappersConfig.xml currently contains the locations of all the xml configuration files of all the individual sqlMap files.

MapperConfig.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE sqlMapConfig
        PUBLIC "-//ibatis.apache.org//DTD SQL Map Config 2.0//EN"
        "http://ibatis.apache.org/dtd/sql-map-config-2.dtd">
<sqlMapConfig>
    <sqlMap resource="com/vermatech/electronics/repository/ItemDataManager.xml"/>
</sqlMapConfig>

SQL Maps configuration files

Each SQL MAp resource specifies a set of sql statements that may be run against an entity. Inserts, updates, deletes etc are specified within XML syntax. In addition to SQL Statements you may specify additional attributes like how long you want the statements to cache. Aliases may be defined for class names. REsults map for returned results and parameter map for parameters.

Parameters Maps and Result Maps

Here is an example that uses parameters map and results maps explicitly

<typeAlias alias=”product” type=”com.ibatis.example.Product” />
<parameterMap id=”productParam” class=”product”>
         <parameter property=”id”/>
</parameterMap>
<resultMap id=”productResult” class=”product”>
         <result property=”id” column=”PRD_ID”/>
         <result property=”description” column=”PRD_DESCRIPTION”/>
</resultMap>
<select id=”getProduct” parameterMap=”productParam”
         resultMap=”productResult” cacheModel=”product-cache”>
    select * from PRODUCT where PRD_ID = ?
</select>

If you want to keep the xml configuration simple you can have Ibatis make educated guesses on how to map a Bean to Parameter and from Results back to beans. There is a slight performance consequence in that this approach requires accessing the ResultSetMetaData. This limitation can be overcome by using an explicit resultMap. Result maps are described in more detail later in this document. This uses the auto mapping feature of the framework which could be slower than if you specified the mappings yourself.

Here is an example that allows the sqlMap to make educated guesses about the mappings

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE sqlMap
  PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN"
  "http://ibatis.apache.org/dtd/sql-map-2.dtd">
<sqlMap namespace=”Product”>
   <select id=”getProduct” parameterClass=” com.ibatis.example.Product”
                              resultClass=”com.ibatis.example.Product”>
         select
            PRD_ID as id,
            PRD_DESCRIPTION as description
         from PRODUCT
         where PRD_ID = #id#
   </select>
</sqlMap>

Since the developer is specifying SQL witin XML you need the ability to escape characters that would appear in xml. An example is the < and > signs.

<select id="getPersonsByAge" parameterClass=”int” resultClass="examples.domain.Person">
         SELECT *
         FROM PERSON
         WHERE AGE <![CDATA[ > ]]> #value#
</select>

SQL Fragments

In order to reduce SQL Code duplication you can use things like Fragments but keep in mind that it does increase the amount of testing effort if something in the common fragment changes.

Here is an example:

<sql id="selectItem_fragment">
         FROM items
         WHERE parentid = 6
</sql>
<select id="selectItemCount" resultClass="int">
         SELECT COUNT(*) AS total
         <include refid="selectItem_fragment"/>
</select>
<select id="selectItems" resultClass="Item">
         SELECT id, name
         <include refid="selectItem_fragment"/>
</select>

Auto generated keys

Supported by using the following XML code

<!—Oracle SEQUENCE Example -->
<insert id="insertProduct-ORACLE" parameterClass="com.domain.Product">
    <selectKey resultClass="int" >
         SELECT STOCKIDSEQUENCE.NEXTVAL AS ID FROM DUAL
    </selectKey>
    insert into PRODUCT (PRD_ID,PRD_DESCRIPTION)
    values (#id#,#description#)
</insert>
<!— Microsoft SQL Server IDENTITY Column Example -->
<insert id="insertProduct-MS-SQL" parameterClass="com.domain.Product">
    insert into PRODUCT (PRD_DESCRIPTION)
    values (#description#)
    <selectKey resultClass="int" >
         SELECT @@IDENTITY AS ID
    </selectKey>
</insert>

Calling Stored Procedures

<parameterMap id="swapParameters" class="map" >
  <parameter property="email1" jdbcType="VARCHAR" javaType="java.lang.String" mode="INOUT"/>
  <parameter property="email2" jdbcType="VARCHAR" javaType="java.lang.String" mode="INOUT"/>
</parameterMap>
<procedure id="swapEmailAddresses" parameterMap="swapParameters" >
  {call swap_email_address (?, ?)}
</procedure>

The next steps are to start coding your CRUD operations on your objects. I talk about this in more detail at the following page

30
Mar
09

Getting Sound to work in Linux Ubuntu

I have a p6T motherboard. This Comes with an Intel ALC1200 Surround Sound card.

I had to put the following into

/etc/modprobe.d/alsa-base.conf

options snd-hda-intel model=intel probe_mask=1

Also had to change to use Pulse Audio instead of ALSA for virtual box after upgrade to 9.10.

Power Saving
By default the operating system enables power saving. This helps in two ways.

  1. Saves Energy (obviously)
  2. Reduces the hum that sound cards make due to EM interference in the computer.

The Disadvantage of this is that when you start playing music you hear a loud pop or click. Sometime this is more annoying than the hum.

To fix this all you need to do is modify the alsa-base.conf and comment out the the following line:

# Power down HDA controllers after 10 idle seconds
#options snd-hda-intel power_save=10 power_save_controller=N




Follow

Get every new post delivered to your Inbox.

Join 34 other followers