作者: Wei

19 篇文章

CentOS7 Minimal 虚拟机安装

自己折腾虚拟机一直以来都是用的Ubuntu, 小巧方便,用户群庞大,有什么问题也好解决。最近才看到有人推荐CentOS,还是RHEL系的。刚好这几天需要一个虚拟环境来学习storm、kafka,就决定用CentOS来搭建新环境, 趁机体验一下。搭建过程碰到些大大小小的问题,记录一下,以后少走弯路。

下载:

安装

VMPlayer相信大家都熟悉了,只单独提一下网络设置。一般大家都会选NAT模式或者Bridged模式吧

网络设置 – NAT

NAT设置最为简单,可以通过主机(host)连internet,可以用putty从主机登陆虚拟机,但是不能从其他局域网里的机器登陆。

vm_centos7_network

Continue reading →

Wildcards in Java Generics

Generics is one of my favorite feature in Java – strong type check, no need to type cast, parameterized type in class/method implementation. Despite using it for several years, I realized that I still don’t fully understand wildcards.

Wildcards in Generics

Refer to Oracle Tutorial on wildcards

In generic code, the question mark (?), called the wildcard, represents an unknown type. The wildcard can be used in a variety of situations: as the type of a parameter, field, or local variable; sometimes as a return type (though it is better programming practice to be more specific). The wildcard is never used as a type argument for a generic method invocation, a generic class instance creation, or a supertype.

By above definition, List<?> wildList should be read as List of unknown types. Because the type is unknown to compiler, the compiler will not allow adding anything to the list other than null (instead of accepting everything !)

List vs List<Object> vs List<?>

Declared type List rawList = … List<Object> objList = … List<?> unknownList = …
Checked? No, raw type Yes, checked type <Object> Yes, checked but type is unknown
Assignment
rawList = new ArrayList();
rawList = new ArrayList<String>();
rawList = new ArrayList(); //ok
//complication error
rawList = new ArrayList<String>();
rawList = new ArrayList(); //ok
rawList = new ArrayList<String>();//ok
list.add() Anything, all lines below are ok

rawList.add("testStr");
rawList.add(1);
rawList.add(Integer.class);
rawList.add(null);

 

Anything, all lines below are ok

objList.add("testStr");
objList.add(1);
objList.add(Integer.class);
objList.add(null);
NULL only

//complication error
objList.add("testStr");
//complication error
objList.add(1);
//complication error
objList.add(Integer.class);
objList.add(null);//ok
list.get() Object Object Object

Upper and Lower Bounded Wildcards

Sample Classes


    private static class A{
    }
    private static class B extends A{
    }
    private static class C extends B{
    }
Upper Bounded Type Lower Bounded Type
Expression List<? extends B> extendBList=… List<? super B> superBList=…
Read as A List of unknown type, the type could be B or subclass of B. in this  example, a List<B> or List<C> A List of unknown type, the type could be B or superclass of B. in this  example, a List<A> or List<B> or List<Object>
Assignment
extendsBList = new ArrayList<A>();//compilation error
extendsBList = new ArrayList<B>();
extendsBList = new ArrayList<C>();
superBList = new ArrayList<A>();
superBList = new ArrayList<B>();
superBList = new ArrayList<C>();//compilation error
superBList = new ArrayList<Object>();
list.add() Because the complier does not know the list is a List<B> or List<C>, to be  safe, it only allow adding instances of class C, which is also a B instance.

//compilation for all below
extendsBList.add(new Object());
extendsBList.add(new A());
extendsBList.add(new B());
extendsBList.add(new C());
Because the complier does not know the list is a List<A> or List<B> or List<Object>, to be  safe, it only allow adding instances of class A.

superBList.add(new Object());//compilation error
superBList.add(new A());//compilation error
superBList.add(new B());//ok
superBList.add(new C());//ok
list.get() B

//ok
for(B b:extendsBList){
/do something
}
//compilation error
for(C c:extendsBList){
//do something
}
Object

//ok
for(Object obj:superBList){
    //do something
}
//compilation error
for(B b:superBList){
    //do something
}

which means:
List<? extends B> is read-only, and only as type B
List<? super B> is kind of write-only as whatever is reading from the list, can get a Object only and does not know the actual type

So, we can only copy data from a List<? extends B> to List<? super B>, but not the other way around
This is pretty much where PECS (Producer Extends, Consumer Super) comes from.

References:
Guidelines for Wildcard Use
Stackoverflow: Difference between <? super T> and <? extends T> in Java

Maven: Add local dependencies

From time to time, we need to work with legacy codes.
Recently I got a chance to look at some old modules. They were developed many years ago, Ant is used as build tool. Each of these modules has its lib folder, which has all the dependent jars. And some of the Ant build scripts have its own “typedef” task and the the typedef task class comes from the module itself. They are left behind comparing to other stuff I work on in term of SDLC stack. I decided to move those modules to Maven first.
To keep it simple, I decided to keep the jars as they are, give it a try with “system” scope. It turns out it works perfectly.

Add local jars as Maven dependencies

<dependency>
    <groupId>io.wwei.sample</groupId>
    <artifactId>stmp</artifactId>
    <version>1.0.0</version>
    <scope>system</scope>
    <systemPath>${basedir}/lib/stmp.jar</systemPath>
</dependency>

Execute Java Classes as part of “package” step

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>exec-maven-plugin</artifactId>
    <version>1.4.0</version>
    <executions>
        <execution>
            <id>exec1</id>
            <phase>package</phase>
            <goals>
                <goal>java</goal>
            </goals>
            <configuration>
                <mainClass>io.wwei.sample.PostCompile</mainClass>
                <additionalClasspathElements>
                    <additionalClasspathElement>{basedir}/target/classes</additionalClasspathElement>
                </additionalClasspathElements>
                <classpathScope>system</classpathScope>
                <commandlineArgs>local</commandlineArgs>
            </configuration>
        </execution>
    </executions>
</plugin>

Include local jars to final assembly

“dependencySets” does not include local jars, so we have to copy them explicitily as below

<fileSets>
    <fileSet>
        <directory>lib</directory>
        <outputDirectory>lib</outputDirectory>
        <includes>
            <include>**/*</include>
        </includes>
    </fileSet>
</fileSets>
Asus N66U简单开苞

从巴厘岛回来之后就一直忙着project, 没顾得上折腾..
9月头的时候家里的Singtel 10Mb ADSL突然巨慢无比, 10Mb的网络在SpeedTest居然只有20KB, 连网页也打不开, 更不要提下载和看视频了. Singtel的客服还是很不错的, 很快就有technician下来换了个新的MIO box – PACE A5520, 速度稍微好了点但远达不到10Mb, 还多了一个新问题 – 一开Xbox, 不管是网线还是插网线, 只要是联网, modem就挂了, 家里所有的设备就都上不了网了. 网上也没有看到类似的问题. 不爽.
和Singtel沟通了一下, 决定换到Fiber去, 一是心水Fiber很久了, 100mb的网络和10mb的ADSL一样钱, 速度要快好多,新加坡本地100mb, 国际路线15mb+. 二是根据Singtel Technician的说法, Pace A5520本来是给Fiber用的, 用在ADSL上固件可能不是很好.
安装还算顺利,前后大约三个星期.OpenNet负责TP, Singtel负责ONT (ZTE F620G) + Mio Box (还是A5520). Fibre果然不是吹的, 下个动画片电影立等可取..郁闷的是依然不兼容Xbox. 刚好吊起了我折腾的劲头.
Continue reading →

巴厘岛归来

不知不觉都注册结婚一周年. 结婚以来忙着买房换工作, 都一年了也没有出去来个蜜月旅行,也没有拍婚纱照, 亏欠老婆太多了 T_T, 还好老婆深明大义. 这次去Bali主要就是拍婚纱照.
机票去年就定了,  7天6夜, AirAsia促销的时候抢的, 两个人265SGD. 现在机票真是白菜价啊, 趁着没小孩这几年要带着我家大猫把东南亚都转一遍.
Bali岛的介绍/游记网上随处可见, 就不重复了. 切入正题
Continue reading →

开苞几个小东西

几天不败点小玩意就手痒 T_T
耳机真是个消耗品啊. Desire HD自带的耳机用了两次一个耳朵就没声音了. 之后一直在用老婆iphone的耳机, 时间久了耳机头边上的塑封部分都脱落了. 看了下Amazon的review, JVC HA-FX1X算是便宜又好用. 到手一试效果还真不错, 大红色, 造型也够酷, 就是线材塑料感强了点. 希望这个能用的久点



Continue reading →

The Avengers @ IMAX 3D

下午去新开的JCube看了人生第一次IMAX 3D – The Avengers.


22块一张票,贵了点,不过太值了. 屏幕和效果一流, 东西就在眼前, 很有身临其境的感觉.
电影开场前有MIB3的宣传片, 有个画面是Will smith从楼上跳下去, 感觉就跟自己在跳似的, 吓得老子心狂跳.
扯远了, 还是说The Avengers, 结论就是强烈推荐.
Ironman还是一如既往的贱. 那套suite太帅气了, 希望有生之年科技能发展到那种地步,自己也能整一套, 哈哈~.
绿巨人同学这次大出风头, 除了能搞破坏之外, 还干掉了好多大怪小怪, 打得”大神”洛基满地找牙, 偶尔还负责搞个笑.
Captain America这次真的当了captain,把一群牛哄哄的heroes团结在了一起, 表现也很出彩.
他的盾就是绝对防御啊, 不管是thor的铁锤, 洛基的手杖,还是手雷炸弹, 就没有什么防不住的.
最后稍微剧透一下, Avenger系列里除了众heroes,就属某人最出彩了, 贯穿了各个hero的故事, 没想到在这一集领了便当..

Buffalo Linkstation Pro Duo 入手

家里人都喜欢聚在客厅看PPS, 本来一直用老婆的笔记本在放. 不过这样一来老婆就没电脑用了.
纠结了很久,月初的时候,装了台HTPC, 打算用来看PPS和7 x 24下载. 装好了才发现这东西耗电啊 T_T, 用的还是集成显卡..
再次纠结..下决心再整个NAS.
以前用过WD my book world edition blue ring, 用来下载还不错, 就是本地传输速度巨慢. 2MB左右吧, 传个文件等半天, 看个1080p卡的要死
所以决定这次要买个稍微好点的. 目前最平价的就是dlink dns-320和buffalo linkstation pro duo (不是linkstation duo). buffalo的速度稍微好点,  评测看>>>这里<<<

1. 开包



2.安装 NAS Navigator并升级到最新的固件1.57
buffalo的固件升级还是很频繁的, 我装的时候才1.57, 写这个篇blog的时候已经1.58了..
固件升级很简单, 下载固件到本地电脑一步一步来就行了. 不过我一开始从无线升级, 很悲催的碰到acp_state_failure error. 用网线连接NAS和电脑再刷,解决问题..hoho~

3. 获取root

注意引号,应该是英文的
方法一

java -jar acp_commander.jar -t 192.168.1.120 -ip 192.168.1.120 -pw password -c “sed -i ‘/PermitRootLogin/s/no/yes/’ /etc/sshd_config”
java -jar acp_commander.jar -t 192.168.1.120 -ip 192.168.1.120 -pw password -c “sed -i ‘/PermitEmptyPasswords/s/^#//’ /etc/sshd_config”
java -jar acp_commander.jar -t 192.168.1.120 -ip 192.168.1.120 -pw password -c “sed -i ‘/PermitEmptyPasswords/s/no/yes/’ /etc/sshd_config”
java -jar acp_commander.jar -t 192.168.1.120 -ip 192.168.1.120 -pw password -c “usermod -p \”\” root”
java -jar acp_commander.jar -t 192.168.1.120 -ip 192.168.1.120 -pw password -c “/etc/init.d/sshd.sh restart”

用putty登陆, 用户名root,密码空. 还没研究出来怎么改密码. 试过几种方法总是access denied

方法二

java -jar acp_commander.jar -t 192.168.1.110 -ip 192.168.1.110 -pw password -c “ls /”
java -jar acp_commander.jar -t 192.168.1.110 -ip 192.168.1.110 -pw password -c “(echo 123456;echo 123456)|passwd”
java -jar acp_commander.jar -t 192.168.1.110 -ip 192.168.1.110 -pw password -c “sed -i ‘s/PermitRootLogin no/PermitRootLogin yes/g’ /etc/sshd_config”
java -jar acp_commander.jar -t 192.168.1.110 -ip 192.168.1.110 -pw password -c “sed -i ‘s/UsePAM yes/UsePAM no/g’ /etc/sshd_config”
java -jar acp_commander.jar -t 192.168.1.110 -ip 192.168.1.110 -pw password -c “/etc/init.d/sshd.sh restart”

用putty登陆,用户名root, 密码123456, 登陆进去之后可以用passwd改密码. 出自这里

4. 安装Optware/Transmission

参考 <buffalo 固件1.57破解 安装transmission及mldonkey > 和 <Transmission>
cd /tmp
wget http://ipkg.nslu2-linux.org/feeds/optware/cs08q1armel/cross/stable/ls-mvkw-bootstrap_1.2-7_arm.xsh
sh ./ls-mvkw-bootstrap_1.2-7_arm.xsh
mv /etc/init.d/optware /etc/init.d/rc.optware
ipkg update
ipkg install transmission

创建脚本
echo ‘/opt/bin/transmission-daemon -g /root/.config/transmission-daemon’ >/opt/etc/init.d/S50transmission
chmod 755 /opt/etc/init.d/S50transmission
echo ‘killall /opt/bin/transmission-daemon’ >/etc/rc.d/extensions.d/K05transmission
chmod 755 /etc/rc.d/extensions.d/K05transmission

修改配置文件
“download-dir”: “/mnt/disk1/share/downloads”,
“incomplete-dir”: “/mnt/disk1/share/incomplete”,
“incomplete-dir-enabled”: true,
“peer-port”: 45678,
“rpc-authentication-required”: true,
“rpc-password”: “password”,
“rpc-username”: “Transmission”,
“rpc-whitelist”: “*.*.*.*”,
“rpc-whitelist-enabled”: true,
“umask”: 0,

5. 安装Aria2

Transmission可以PT,支持magnet和BT. Aria2支持http/ftp/BT/magnet
最新版1.6.3 :http://nas1.cn/thread-26697-1-1.html
YAAW复制到/www/buffalo/www/static

6. 完工

测试环境
电脑 : Sandy Bridge G620 + ECS H67H2-i + 8 Gb RAM
路由: E3000 + Tomato Shibby 85v

千兆有线网络下能达到43.9MB/s, 无线就看信号了, 台式机离得比较远, 在4-8MB之间浮动.
T430比较好, 看1080p毫无压力, 可以随意快进后退. 和以前的WD blue ring一个天上一个地下啊

7. 参考

2013 Feb 17
+抽空把LS-WVL的firmware升级到了1.64, 以上的步骤都还可以用
+添加Aria2

 

Yup, I’m back…Again

Buying host for the first time

Initially tried to setup a HTPC for watching movie, downloading stuff and host wordpress.

Unfortunately it has quite high power consumption, around 50w, and no fixed IP for ADSL 🙁

Two year plan @ 50USD – unlimited space / database/domain, worth the money.