在MainServlet启动Struts*控制器之后第一件事情就是去处理启动事件:
- protected void processStartupEvents() throws Exception {
- StartupAction startupAction = new StartupAction();
- startupAction.run(null);
- }
它会去调用StartupAction中的run方法,进而调用doRun()方法,这里又做了很多事情:
(1)它在控制台打印出产品版本信息:
- // Print release information
- System.out.println("Starting " + ReleaseInfo.getReleaseInfo());
这个版本信息,可以从ReleaseInfo类中得到:
- public static final String getReleaseInfo() {
- return releaseInfo;
- }
- static String releaseInfo =
- releaseInfoPrefix + name + " " + versionDisplayName + " (" + codeName +
- " / Build " + build + " / " + date + ")" + releaseInfoSuffix;
这些常量都可以在ReleaseInfo类找到:所以控制台中显示:
- Starting Liferay Portal Community Edition 6.1.0 CE (Paton / Build 6100 / January 6, 2012)
(2)清空锁信息:
- try {
- LockLocalServiceUtil.clear();
- }
它会调用LockLocalServiceUtil中的clear()方法:
- public static void clear()
- throws com.liferay.portal.kernel.exception.SystemException {
- getService().clear();
- }
进而调用LockLocalServiceImpl类中的clear()方法:
- public void clear() throws SystemException {
- lockPersistence.removeByLtExpirationDate(new Date());
- }
进而调用LocalPersistenceImpl类的removeByLtExpirationDate方法:
- /**
- * Removes all the locks where expirationDate < ? from the database.
- *
- * @param expirationDate the expiration date
- * @throws SystemException if a system exception occurred
- */
- public void removeByLtExpirationDate(Date expirationDate)
- throws SystemException {
- for (Lock lock : findByLtExpirationDate(expirationDate)) {
- remove(lock);
- }
- }
它会从数据库中移除所有到期的锁,以当前日期为标准,下面就不展开了。
(3)关闭所有的hook:
它让当前应用的运行时去关闭所有的Hook:
- Runtime runtime = Runtime.getRuntime();
- runtime.addShutdownHook(new Thread(new ShutdownHook()));
每个Hook都开启一个新的Thread来关闭,它实现Runnable接口,仅仅做一些打印工作:
- public class ShutdownHook implements Runnable {
- public void run() {
- if (GetterUtil.getBoolean(
- System.getProperty("shutdown.hook.print.full.thread.dump"))) {
- printFullThreadDump();
- }
- }
- ...
而这些hook都会添加到Liferay应用的运行时中,运行时会先检查是否有shutdown hook的权限,然后再做shutdown hook的事情:
- public void addShutdownHook(Thread hook) {
- SecurityManager sm = System.getSecurityManager();
- if (sm != null) {
- sm.checkPermission(new RuntimePermission("shutdownHooks"));
- }
- ApplicationShutdownHooks.add(hook);
- }
递归找下去,会发现这个permission定义在%LIFERAY_HOME%/tomcat-7.0.23/conf/catalina.policy中,所以我们拥有这个权限:
- ...
- permission java.io.FilePermission
- "${catalina.base}${file.separator}logs${file.separator}*", "read, write";
- permission java.lang.RuntimePermission "shutdownHooks";
- permission java.lang.RuntimePermission "getClassLoader";
- permission java.lang.RuntimePermission "setContextClassLoader";
- ...
之后,就把hook添加到ApplicationShutdownHooks的hashmap中来关闭:
- static {
- Shutdown.add(1 /* shutdown hook invocation order */,
- new Runnable() {
- public void run() {
- runHooks();
- }
- });
- }
(4)配置Security Manager
- // Security manager
- String portalSecurityManagerStrategy =
- PropsValues.PORTAL_SECURITY_MANAGER_STRATEGY;
- if (portalSecurityManagerStrategy.equals("smart")) {
- if (ServerDetector.isWebSphere()) {
- portalSecurityManagerStrategy = "none";
- }
- else {
- portalSecurityManagerStrategy = "default";
- }
- }
- if (portalSecurityManagerStrategy.equals("liferay")) {
- if (System.getSecurityManager() == null) {
- System.setSecurityManager(new PortalSecurityManager());
- }
- }
- else if (portalSecurityManagerStrategy.equals("none")) {
- System.setSecurityManager(null);
- }
它会先检查SecurityManager的策略:
- public static final String PORTAL_SECURITY_MANAGER_STRATEGY = PropsUtil.get(PropsKeys.PORTAL_SECURITY_MANAGER_STRATEGY);
它的key来自PropsKeys接口:
- public static final String PORTAL_SECURITY_MANAGER_STRATEGY = "portal.security.manager.strategy";
最终我们去访问portal.properties可以看到:
- ##
- ## Security Manager
- ##
- #
- # Set this property to "default" to use the default security manager
- # configured by the application server. A security manager will not be used
- # if the application server did not configure one.
- #
- # Set this property to "liferay" to use Liferay's security manager if the
- # application server has not configured a security manager.
- #
- # Set this property to "none" to explicitly remove the security manager
- # regardless of whether one is configured. This ensures that the portal is
- # run in a JVM without a security manager.
- #
- # Set this property to "smart" to allow the portal decide which strategy to
- # use based on which application server it is on.
- #
- portal.security.manager.strategy=smart
所以,它用的是"smart"策略,所以执行下面代码:
- if (portalSecurityManagerStrategy.equals("smart")) {
- if (ServerDetector.isWebSphere()) {
- portalSecurityManagerStrategy = "none";
- }
- else {
- portalSecurityManagerStrategy = "default";
- }
- }
因为我们的Server用的是tomcat,而不是websphere,所以我们执行else代码段,也就是我们的portalSecurityManagerStrategy用的是"default",这种情况下使用tomcat应用服务器的安全策略。