Thursday, October 24, 2013

receive argument on expect shell script

I just wanted to scp easily. I just simply made a expect shell script which receive one argument.
I just excute one script like below

cp.sh file

and copy finished
#!/usr/bin/expect -f

set filename [lindex $argv 0];

spawn scp -v /temp/$filename root@192.168.1.1:/tmp/root/
expect "Enter passphrase"
send "\r"
...

Thursday, August 8, 2013

Manipulate Access Control List (ACL) on mysql

I had to limit of access to mysql server due to security improvement task.
We like to allow only few host to the mysql. It was very easy

mysql>use mysql
mysql> select host, user from user;
----------------------------+
host  user
----------------------------+
%  dmitry
host : % means all (It is security breach)
host side should be ip address of web server
or
ip address of admin pc.
example)
-- Replace unlimited access rule to only allow 192.168.0.5 host rule
mysql>update user set host='192.168.0.5' where host = '%' and user in ('dmitry');
mysql>commit;
mysql>flush privileges;
Query OK, 0 rows affected (0.01 sec)


-- Remove unlimited access rule

mysql> delete from user where host = '%';
mysql> commit;
mysql> flush privileges;


you can insert more hosts if you like to.

Monday, July 29, 2013

Android Json file read instead of property file read


While I am building an Android application. I wanted to read a property file and wanted to act my app depends on the property file configuration. But, property file can't handle array unless I add dependency Apache Common Configuration. As I am building an mobile application, it should be compact. for the reason, I just used Android JSONObject to resolve this.

Create "country.json" file and locate it Android device "/" directory

{
    "country": [
        {
            "name": "germany",
            "code": "DE",
            "continent": "Europe",
            "eat": "sausage"
        },    
        {
            "name": "france",
            "code": "FR",
            "continent": "Europe",
            "eat": "croissant"
        },
        {
            "name": "korea",
            "code": "KR",
            "continent": "ASIA",
            "eat": "rice"
        },
        {
            "name": "japan",
            "code": "JP",
            "continent": "ASIA",
            "eat": "fish"
        }
    ]
}


Create 2 method and use "getCountryList()" on your need.
private List<Country> getCountryList() throws Exception {

 File dirSDCard = Environment.getExternalStorageDirectory();
 File yourFile = new File(dirSDCard, "country.json");
 InputStream jsonStream = new FileInputStream(yourFile);
 JSONObject jsonObject = new JSONObject(InputStreamToString(jsonStream));
 JSONArray jsonArray = jsonObject.getJSONArray("country");
 List<Country> countryList = new ArrayList<Country>();

 for (int i = 0; i < jsonArray.length(); i++) {

  JSONObject jsonCountry = jsonArray.getJSONObject(i);
  Country country = new Country();
  country.setName(jsoncountry.getString("name"));
  country.setCode(jsoncountry.getString("code"));
  country.setContinent(jsoncountry.getString("continent"));
  country.setEat(jsoncountry.getString("eat"));

  countryList.add(country);
 }

 return countryList;
}

private String InputStreamToString(InputStream is) {

 BufferedReader r = new BufferedReader(new InputStreamReader(is));
 StringBuilder total = new StringBuilder();
 String line;
 try {
  while ((line = r.readLine()) != null) {
   total.append(line);
  }
 } catch (IOException e) {
  e.printStackTrace();
 }
 return total.toString();

}

Monday, June 17, 2013

mybatis various datasource sample jndi, hsql, oracle, mysql, sqlite configuration setup with Springframework

While doing various projects, I had to use different DBs. I just summarized datasource for Mybatis samples which is after testing. You can simply comment out to for your prefer datasource.
If you like to know detail to implement this sample with Spring framework, please follow mybatis-integration-with-spring.

<?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:p="http://www.springframework.org/schema/p"
 xmlns:context="http://www.springframework.org/schema/context"
 xmlns:jdbc="http://www.springframework.org/schema/jdbc"
 xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.0.xsd
    http://www.springframework.org/schema/jdbc
        http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd">

<!-- JNDI datasource -->
<!-- 
<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
 <property name="jndiName" value="jdbc/oraclePool" />
 <property name="resourceRef" value="true" />
</bean>
-->

<!-- Hsql datasource -->
<!-- 
<jdbc:embedded-database id="dataSource">
 <jdbc:script location="classpath:hsql/schema.sql" />
 <jdbc:script location="classpath:hsql/data.sql" />
</jdbc:embedded-database>
-->

<!-- Oracle JDBC datasource -->
<!-- 
<bean id="dataSource"
 class="org.springframework.jdbc.datasource.SimpleDriverDataSource">
 <property name="driverClass" value="oracle.jdbc.OracleDriver" />
 <property name="url" value="jdbc:oracle:thin:@HOST:1111:ORA" />
 <property name="username" value="ID" />
 <property name="password" value="PASSWORD" />
</bean>
-->

<!-- mysql JDBC datasource -->
<!-- 
<bean id="dataSource"
 class="org.springframework.jdbc.datasource.SimpleDriverDataSource">
 <property name="driverClass" value="com.mysql.jdbc.Driver" />
 <property name="url" value="jdbc:mysql://localhost:3306/mydata" />
 <property name="username" value="user123" />
 <property name="password" value="12345678" />
</bean>
 -->

<!-- sqlite JDBC datasource -->
<!-- 
<bean id="dataSource"
 class="org.springframework.jdbc.datasource.SimpleDriverDataSource">
 <property name="driverClass" value="org.sqlite.JDBC" />
 <property name="url" value="jdbc:sqlite:C:/yourdb.sqlite" />
</bean>
 -->

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
 <property name="dataSource" ref="dataSource" />
 <property name="typeAliasesPackage" value="com.devtrigger.model" />
 <property name="mapperLocations" value="classpath*:dao/**/*.xml" />
</bean>

<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
 <constructor-arg index="0" ref="sqlSessionFactory" />
</bean>

<!-- scan for mapper interface files and let them be autowired -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
 <property name="basePackage" value="com.devtrigger.dao" />
</bean>

<bean id="transactionManager"
 class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
 <property name="dataSource" ref="dataSource" />
</bean>

</beans>

Thursday, March 14, 2013

Useful SVN adminitrator command

Export svn
- When I don't know SVN credential. but I have access to repository server
$svn export --force file:///home/svn/myrepos ./myrepos


Check User access control
$cat /home/svn/myrepos/conf/authz
$cat /home/svn/myrepos/conf/passwd
$cat /home/svn/myrepos/conf/svnserve.conf


Create version 1.6 compatible Repository
$svnadmin create myrepos --pre-1.6-compatible


Dump svn repository
$svnadmin dump /home/svn/myrepos > /home/backup/svn/myrepos.dump


Incremental dump svn repository
- SVN incremental Backup
$ svnadmin dump myrepos --revision 0:1000 > dumpfile1
$ svnadmin dump myrepos --revision 1001:2000 --incremental > dumpfile2
$ svnadmin dump myrepos --revision 2001:3000 --incremental > dumpfile3
$ svnadmin dump <repos> -r 58:HEAD --deltify > <file2> 


Import dump file
$ cd /home/svn
$ svnadmin load --bypass-prop-validation myrepos < /home/backup/svn/myrepos.dump


Incremental import dump file
- SVN recovery 
* incremental :
$ svnadmin load < ~/repos-0-1000.svn_dump
$ svnadmin load < ~/repos-1000-2000.svn_dump
$ svnadmin load < ~/repos-2000-3000.svn_dump


Kill SVN daemon on Solaris
$ pkill -KILL svnserve


Start SVN daemon
$ svnserve -d -r /home/svn/


Pack svn
- SVN revision pack per 1000 revision
$ svnadmin pack /home/svn/sfc