java/ruby unzip

http://www.java2s.com/Tutorial/Java/0180__File/UnzipusingtheZipInputStream.htm

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

public class MainClass {

public static void main(String[] args) throws IOException {

for (int i = 0; i < args.length; i++) { FileInputStream fin = new FileInputStream(args[i]); ZipInputStream zin = new ZipInputStream(fin); ZipEntry ze = null; while ((ze = zin.getNextEntry()) != null) { System.out.println("Unzipping " + ze.getName()); FileOutputStream fout = new FileOutputStream(ze.getName()); for (int c = zin.read(); c != -1; c = zin.read()) { fout.write(c); } zin.closeEntry(); fout.close(); } zin.close(); } } } http://www.kodejava.org/examples/334.html package org.kodejava.example.util.zip; import java.io.*; import java.util.zip.ZipInputStream; import java.util.zip.ZipEntry; public class UnzipDemo { public static void main(String[] args) { String zipname = "data.zip"; try { FileInputStream fis = new FileInputStream(zipname); ZipInputStream zis = new ZipInputStream(new BufferedInputStream(fis)); ZipEntry entry; // // Read each entry from the ZipInputStream until no more entry found // indicated by a null return value of the getNextEntry() method. // while ((entry = zis.getNextEntry()) != null) { System.out.println("Unzipping: " + entry.getName()); int size; byte[] buffer = new byte[2048]; FileOutputStream fos = new FileOutputStream(entry.getName()); BufferedOutputStream bos = new BufferedOutputStream(fos, buffer.length); while ((size = zis.read(buffer, 0, buffer.length)) != -1) { bos.write(buffer, 0, size); } bos.flush(); bos.close(); } zis.close(); fis.close(); } catch (IOException e) { e.printStackTrace(); } } } http://www.markhneedham.com/blog/2008/10/02/ruby-unzipping-a-file-using-rubyzip/ require 'rubygems' require 'zip/zip' def unzip_file (file, destination) Zip::ZipFile.open(file) { |zip_file| zip_file.each { |f| f_path=File.join(destination, f.name) FileUtils.mkdir_p(File.dirname(f_path)) zip_file.extract(f, f_path) unless File.exist?(f_path) } } http://stackoverflow.com/questions/856891/unzip-zip-tar-tag-gz-files-with-ruby http://www.anyang-window.com.cn/tag/ava-zip/page/3/ http://www.iteye.com/topic/626705

update-alternatives bug?

这两天学习SCRUM之后,感觉还真是不错,有的时候自己看书啊,自己想策略啊什么的是不如这个方式快。
好久没碰代码,今天心血来潮,试试Play 2.0,自从用了Play 1.x,2.0出来很久了都没有碰过。2.0改成了Scala是比较感兴趣的。

但是create hello后run的时候发生了一个很奇怪的错误。
参见:java.lang.ExceptionInInitializerError when trying to run Play Application

第一感觉是path不对?不能啊,以前没改过java的设置。
想起以前用过update-alternatives配置过java,因为另一个用java的SweetHome3D在Linux 64bit上经常闪退,切换过OpenJDK和Oracle JDK,还有1.6/1.7的切换。

验证之下
java 1.7
javac 1.6

用update-alternatives切换成7,javac还是1.6
ls /usr/bin/javac发现它是链接在/etc/alternatives/javac上的,手工改成7的就好了。

当然,问题继续存在,参考了stackover的答案。
见:JNotify Linux support</>
到Linux的root用户下修改

echo 32000 > /proc/sys/fs/inotify/max_user_watches

一切OK。

第一次Java,第一次反射

对Java不熟悉,如果要用PHP/Python之类来做,下面代码用不到这么复杂吧。过则不美,用了后悔。

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package others.commons.play;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.List;

import play.db.jpa.GenericModel.JPAQuery;

/**
 *
 * @author Administrator
 */
public class Paginator {
    public List data;
    public int page;
    public int pageSize = 10;
    public int pageCount;
    public String css;

    public Paginator(Class<? extends Model> aClass, long page, long pageSize, String css){
        this.page = (int)page;
        this.pageSize = (int)pageSize;
        this.css = css;
        this.pageCount = (int)count(aClass);
        this.data = getData(aClass, (int)page, (int)pageSize);
    }

    public String indicator() {
        return "html here";
    }

    private static List getData(Class<? extends Model> typeToken, int p1, int p2) {
        Method method;
        JPAQuery handle;
        try {
            method = typeToken.getMethod("all");
            handle = (JPAQuery) method.invoke(typeToken);
        } catch (IllegalArgumentException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e);
        } catch (SecurityException e) {
            throw new RuntimeException(e);
        } catch (NoSuchMethodException e) {
            throw new RuntimeException(e);
        }

        //

        return handle.fetch(p1, p2);
    }
    private static long count(Class<? extends Model> typeToken) {
        Method method;
        Long count = new Long(0);
        try {
            method = typeToken.getMethod("count");     // 实在想不明白直接调用一下有什么问题
            count = (Long) method.invoke(typeToken);   // 这里需要invoke个鸟~~~~
        } catch (IllegalArgumentException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e);
        } catch (SecurityException e) {
            throw new RuntimeException(e);
        } catch (NoSuchMethodException e) {
            throw new RuntimeException(e);
        }
        return count.longValue();

    }

}

免费工具介绍:Power*Architect

Power*Architect

Data Modeling & Profiling Tool

The Power*Architect is a user-friendly data modeling tool created by data warehouse designers, and has many unique features geared specifically for the data warehouse architect. It allows users to reverse-engineer existing databases, perform data profiling on source databases, and auto-generate ETL metadata.

Features

Accesses source databases via JDBC
Connects to multiple source databases concurrently
Compares data models & database structures and identifies discrepancies
Drag-and-drop source tables & columns into the playpen
Remembers the origin of each column
Generates source-to-target visual Mapping Reports
Forward/reverse engineers PostgreSQL, Oracle, MS SQL Server & more
Saves source data structure snapshots in the project, so you can work remotely
All project data is stored in an easy-to-parse XML format
OLAP schema modeling: Cubes, Measures, Dimensions, Hierarchies & Levels
GPL (version 3) licensed

Data Architects, DBA’s, Analysts and Designers rely on Data Modeling tools to facilitate and simplify their data Modeling efforts, while maximizing the use of their resources. The Power*Architect allows these busy highly technical resources to perform this most intricate part of their job in a fraction of the time.

Plus, the Power*Architect has the ability to take snapshots of database structures, allowing users to design DW data models while working offline.

Whether you are building a Data Warehouse or using data models to communicate business rules, the Power*Architect will facilitate and automate your data modeling efforts.

设计数据库原来用DBDesigner4。Windows的易用性是没说的,下载了安装就可以了。不过在Ubuntu上着实费了点事。先是DBDesigner4用Kylix3开发,需要下载支持库。后来,后来发现我在用amd64版本,在32位程序兼容方面,库之间的调用关系和设置还是没有太了解。所以发动gooooogle,现在打算用这个了。Power*Architect,听着象PowerDesigner,不过照着PowerDesigner还是差了不是一星半点啊。

有兴趣用D版或者wine一下的同学可以试一下:在linux 下运行PowerDesigner 15 Beta 2

Ubuntu安装Power*Architect也很容易

下载Architect-generic-jdbc-x.x.x.tar.gz(x.x.x为版本号)

tar -zxvf Architect-generic-jdbc-x.x.x.tar.gz

cd architect-x.x.x

java -jar architect.jar

就可以使用了。

Tomcat – java.security.AccessControlException: access denied (logging.properties read)

After upgraded to Tomcat version 5.5.25, it hit a lot errors in security policy path.

Caused by: java.security.AccessControlException: access denied (java.io.FilePermission /usr/share/tomcat5.5-webapps/jsp-examples/WEB-INF/classes/logging.properties read)

Above error is cause by policy file, Tomcat 5.5.25 made a lot modified at policy file, we need to modify policy file(03catalina.policy) file to fix it.

vi 03catalina.policy which usually located at policy.d folder

mkyong@mkyong-desktop:/etc/tomcat5.5/policy.d$ vi 03catalina.policy
find below
[sourcecode language=”java”]
grant codeBase “file:${catalina.home}/bin/tomcat-juli.jar” {
permission java.util.PropertyPermission “java.util.logging.config.class”, “read”;
permission java.util.PropertyPermission “java.util.logging.config.file”, “read”;
permission java.lang.RuntimePermission “shutdownHooks”;
permission java.io.FilePermission “${catalina.base}${file.separator}conf${file.separator}logging.properties”, “read”;
permission java.util.PropertyPermission “catalina.base”, “read”;
permission java.util.logging.LoggingPermission “control”;
permission java.io.FilePermission “${catalina.base}${file.separator}logs”, “read, write”;
permission java.io.FilePermission “${catalina.base}${file.separator}logs${file.separator}*”, “read, write”;
permission java.lang.RuntimePermission “getClassLoader”;
// To enable per context logging configuration, permit read access to the appropriate file.
// Be sure that the logging configuration is secure before enabling such access
// eg for the examples web application:
//permission java.io.FilePermission “${catalina.base}${file.separator}webapps${file.separator}examples${file.separator}WEB-INF${file.separator}classes${file.separator}logging.properties”, “read”;
};
CHANGED TO below to allow all permission like before

grant codeBase “file:${catalina.home}/bin/tomcat-juli.jar” {
permission java.security.AllPermission;
};
OR enable permission explicitly to your web app path

permission java.io.FilePermission “${catalina.base}${file.separator}webapps${file.separator}YOUR_PATH_HERE
${file.separator}WEB-INF${file.separator}classes${file.separator}logging.properties”, “read”;

full command is

grant codeBase “file:${catalina.home}/bin/tomcat-juli.jar” {
permission java.util.PropertyPermission “java.util.logging.config.class”, “read”;
permission java.util.PropertyPermission “java.util.logging.config.file”, “read”;
permission java.lang.RuntimePermission “shutdownHooks”;
permission java.io.FilePermission “${catalina.base}${file.separator}conf${file.separator}logging.properties”, “read”;
permission java.util.PropertyPermission “catalina.base”, “read”;
permission java.util.logging.LoggingPermission “control”;
permission java.io.FilePermission “${catalina.base}${file.separator}logs”, “read, write”;
permission java.io.FilePermission “${catalina.base}${file.separator}logs${file.separator}*”, “read, write”;
permission java.lang.RuntimePermission “getClassLoader”;
// To enable per context logging configuration, permit read access to the appropriate file.
// Be sure that the logging configuration is secure before enabling such access
// eg for the examples web application:
permission java.io.FilePermission “${catalina.base}${file.separator}webapps${file.separator}YOUR_PATH_HERE${file.separator}WEB-INF${file.separator}classes${file.separator}logging.properties”, “read”;
};
[/sourcecode]

Done, restart Tomcat.
sudo /etc/init.d/tomcat5.5 restart

来源:Programmer Life

经过测试http://127.0.0.1:8180/,Tomcat不再抛出500错误。