Thursday, January 25, 2007

年长者的失眠症

有一位年长者缺席了一堂课。我打电话去慰问她什么原因没来。她提到她有失眠问题,星期六早上觉得头晕,怕走路跌到,又不想麻烦女儿早起载她上学,所以便没来了。

我和她聊了很久,针对她的问题我给了我的意见。最后她反应道,“你的意见很新奇,我没有想过。”

因为聊了很多,她大概也记不了。所以我把重点写出来,印在纸上给她看。

问题: 白天不敢午睡,怕晚上会睡不着。常常小睡十五分钟就起身。
建议: 如果白天能够入睡,就应该敢敢睡久久。如果晚上睡不着了,就起身做事。反正都已经退休了,时间都是自己的,不用怕白天和晚上的生理时钟颠倒了。最重要的是睡的足够,睡到自然醒,睡足比药补好。

问题: 可是白天忙於做家务,不能午睡太久。
建议: 把家务事分开来。累时睡觉休息,醒了之后,就算在晚上也可以继续做家务。

问题: 晚上失眠,想看书看杂志,可是又不敢开灯,怕打扰到家人。
建议: 可以去客厅,去厨房,去厕所看书。

问题: 早上调闹钟,准时起来为家人准备早餐。
建议: 老娘今天不想早起做家务,你们自己吃面包。你不是女佣嘛,不需要做家务24小时。

问题: 已经习惯坐在书桌上看书,坐久会腰痛。躺下就会舒缓舒服。
建议: 做人不要让自己的习惯绑死,可以尝试洒脱的躺在床上看书。如果累了,书一丢,可以马上在床上睡着。

Saturday, January 13, 2007

OracleAS rewrite/redirect URL to internal machine hostname

Usually when we setup a machine, we’ll simply give a hostname for the machine. Then we install OracleAS on it.

When it goes live, we’ll assign a new Domain Name to point to this machine. Hence, this machine has 2 names. Internally it is machine_hostname.domain.com and externally it is host.internet.com.

From Internet when we access this machine, here comes a strange behaviour: when we login OracleAS EM (Enterprise Manager) or perform JSP/Struct/JSF Redirect in our web application, the internal machine hostname is shown up in browser. Hence the browser shows “Page cannot be displayed”.

Amazing. What’s wrong? What happens inside JSP/Struct/JSF Redirect?

Here is the culprit:

vi $ORACLE_HOME/Apache/Apache/conf/httpd.conf”.

You see the following information:


ServerName machine_hostname.domain.com


# UseCanonicalName: (new for 1.3) With this setting turned on, whenever
# Apache needs to construct a self-referencing URL (a URL that refers back
# to the server the response is coming from) it will use ServerName and
# Port to form a "canonical" name. With this setting off, Apache will
# use the hostname:port that the client supplied, when possible. This
# also affects SERVER_NAME and SERVER_PORT in CGI scripts.
UseCanonicalName On


Observation is,
1) ServerName is hardcoded with internal machine hostname.
2) Read the explanation of UseCanonicalName, Apache use ServerName to construct a self-referencing URL.

So, we need to set UseCanonicalName off, then Apache will use the hostname:port that the client supplied.

Problem solved. Mission accomplished.

What happens inside JSP/Struct/JSF Redirect?

Let’s use the following servlet to demo a redirect.
public class RedirectServlet extends HttpServlet
{
public void service(HttpServletRequest req,
HttpServletResponse resp)
{
String contextPath = req.getContextPath();
String redirectStr = contextPath +
"/nextpage.jsp?param1=value1";
// Use encodeRedirectURL so that session id can be
// included in browser which doesn't support cookies.
resp.sendRedirect(resp.encodeRedirectURL(redirectStr));
}
}
Then, we use Telnet to access this servlet:
% telnet machine_hostname 8080

Manually enter:
GET /servlet/RedirectServlet HTTP/1.0

You will get the reply:
HTTP1.1 302 Moved Temporarily
Location: http://machine_hostname:8080/webapp/nextpage.jsp?param1=value1
DATE: ...
Server: ...
Connection: ...


What to observe is that,
1) Redirect in fact is a 302 response from webserver.
2) The complete URL and Port are written out fully in the Location.

This is the reason why the internal machine hostname will be shown up in browser, when we login OracleAS EM (Enterprise Manager) or perform a JSP/Struct/JSF Redirect in our web application.

Please check how to let EM and Redirect return URL as expected-ly.

Run OHS at port 80 (HTTP) and port 443 (HTTPS)

By default, OracleAS HTTP Server runs at port 7777 for HTTP and port 4443 for HTTPS.

In Linux, only root user can use port less than 1024. So, in order to have OHS runs at port 80 and HTTPS runs at port 443, we need to do the following steps:

1. As oracle user,
"cd $ORACLE_HOME/Apache/Apache/conf"

2. Edit httpd.conf
"vi httpd.conf"

Search for 7777 by ":/7777"
You see:
# This port is used when starting without SSL
Port 7777
Listen 7777

Change them to:
# This port is used when starting without SSL
Port 80
Listen 80


3. Edit ssl.conf
"vi ssl.conf"

Replace all occurance of 4443 with 443.
":1,$ s/4443/443/g" or ":%s/4443/443/g"

4. As root user, "cd $ORACLE_HOME/Apache/Apache/bin". Execute:
# chmod 777 apachectl
# chown root .apachectl
# chmod 6750 .apachectl

You'll see the following:
# ll apachectl
-rwxrwxrwx 1 oracle dba 11909 Dec 8 15:11 apachectl
# ll .apachectl
-rwsr-s--- 1 root dba 1703684 Jan 16 2006 .apachectl

Ok, done.

As oracle user, you can startup OracleAS with "opmnctl startall"
Also, you can view the status with "opmnctl status -l"