|          
在OpenCms的安装过程中有一个文件导入的过程。由于工作原因,本人对这个导入过程做了点研究。有点收获写出来和大家共享。
 在系统中安装过程是通过run_import.jsp文件(该文件位于sourcpath1\opencms_src_5.0.1\opencms\web\ocsetup)触发的。在该页中她使用了
 
 <frameset rows="100%,*">
 
 <frame src="display_import.jsp" name="display">
 
 <frame src="about:blank" name="data">
 
 </frameset>
 
 这样的结构来完成导入和显示的功能。看到name为display很容易猜想他是用来向用户显示处理结果的,name 为data 的frame肯定是用来处理文件导入的了。这只是猜想,具体如何还有待证实。下面我们就去看看display_import.jsp这个文件。
 
 在display_import.jsp服务端基本没有什么处理,客户端的处理倒是不少。这一句<body <% if(setupOk){ out.print("onload='enable();'");} %>>倒是很重要。我们先来看看enable()这个函数。
 
 /* indicates if the document has been loaded */
 
 function enable()   {
 
 enabled = true;
 
 parent.data.location.href="data_import.jsp";
 
 document.forms[0].info.value = message;
 
 }
 
 在这个函数中有parent.data.location.href="data_import.jsp";这一句。他把name 为data 的frame的location.href设为"data_import.jsp"。从文件名来看应该是由该文件处理文件的导入工作。到底是不是,我们进去看看。
 
 在data_import.jsp页面中除了有个com.opencms.boot.CmsSetup的session范围的JavaBean外还有一个com.opencms.boot.CmsSetupThread的session范围的JavaBean。那么这个CmsSetupThread到底是什么玩意。去看源码吧!
 
 
 
 package com.opencms.boot;
 
 
 
 import java.io.File;
 
 import java.io.IOException;
 
 import java.io.PipedOutputStream;
 
 import java.io.PrintStream;
 
 
 
 // CmsSetupThread是一个线程。
 
 public class CmsSetupThread extends Thread {
 
 
 
 public void run() {
 
 
 
 /* save the original out and err stream */
 
 m_tempOut = System.out;
 
 m_tempErr = System.err;
 
 
 
 /* redirect the streams */
 
 System.setOut(new PrintStream(m_pipedOut));
 
 System.setErr(new PrintStream(m_pipedOut));
 
 
 
 /* start the logging thread */
 
 m_lt.start();
 
 
 
 /* start importing the workplace */
 
 CmsMain.startSetup(basePath + "WEB-INF/ocsetup/cmssetup.txt", basePath + "WEB-INF/");
 
 
 
 /* stop the logging thread */
 
 try {
 
 sleep(1000);
 
 m_lt.stopThread();
 
 m_pipedOut.close();
 
 }
 
 catch (InterruptedException e)  {
 
 m_lt.stopThread();
 
 e.printStackTrace(m_tempErr);
 
 }
 
 catch (IOException e)  {
 
 m_lt.stopThread();
 
 e.printStackTrace(m_tempErr);
 
 }
 
 
 
 /* restore to the old streams */
 
 System.setOut(m_tempOut);
 
 System.setErr(m_tempErr);
 
 
 
 }
 
 … …… ……
 
 }
 
 从上面的代码中的public void run() 方法中可以看出系统的确用该类来处理文件导入的工作。主要分为一下几个步骤来完成。这里就验证了我们前面name为display的frame向用户显示处理结果的,name 为data 的frame来处理文件导入的猜想。。
 
 1、 备份系统的标准输出和系统的错误输出
 
 m_tempOut = System.out;
 
 m_tempErr = System.err;
 
 2、 将系统输出重新定向到一个PipedOutputStream实例(instance)
 
 System.setOut(new PrintStream(m_pipedOut));
 
 System.setErr(new PrintStream(m_pipedOut));
 
 这样一来说有的系统输出都会定向到m_pipedOut到这个对象上来了。要显示处理信息只需要处理是用System.out.println()之类方法打印出来。在前面读取m_pipedOut就可以了。是不是这样实现还有待证实。
 
 3、 启动 CmsSetupLoggingThread  线程;
 
 4、 开始导入工作。有代码为证
 
 /* start importing the workplace */
 
 CmsMain.startSetup(basePath + "WEB-INF/ocsetup/cmssetup.txt", basePath + "WEB-INF/");
 
 5、 停止CmsSetupLoggingThread 线程。
 
 6、 还原系统的标准输出和系统的错误输出。
 
 下面就是还不清楚CmsSetupLoggingThread到底是什么玩意。通过查看源码可以发现他就是用来收集CmsSetupThread线程的处理信息的。并且就是通过PipedOutputStream来完成这两个线程的通信。这一点可以从他的构造函数知道。
 
 public CmsSetupLoggingThread(PipedOutputStream pipedOut)  {
 
 messages = new Vector();
 
 m_stopThread = false;
 
 try {
 
 m_pipedIn = new PipedInputStream();
 
 m_pipedIn.connect(pipedOut);
 
 m_LineReader = new LineNumberReader(new BufferedReader(new InputStreamReader(m_pipedIn)));
 
 }
 
 catch (Exception e) {
 
 messages.addElement(e.toString());
 
 }
 
 }
 
 
 
 为了方便jsp页面方便的得到这些信息CmsSetupLoggingThread还特地提供了一个private static Vector messages;和静态的getMessages()方法来处理他。这一点在data_import.jsp中的
 
 messages = com.netmarch.infopub.pubServletCon.LogginThread.getMessages();语句可以得到验证。最后messages通过
 
 <script language="Javascript">
 
 var output = new Array();
 
 <%         for(int i = 0; i < (size-offset) ;i++)    {
 
 out.println("output[" + i + "] = \"" + messages.elementAt(i+offset).toString() + "\";");
 
 }
 
 
 
 %>
 
 function send()     {
 
 parent.display.start(output);
 
 }
 
 
 
 </script>传到客户端。完成了处理信息的显示。
 
 我写的是我的知道的,我还很菜。欢迎高手指出我的错误。我的E-mail是jetgeng@hotmail.com 谢谢。
 
 
 
 注 1、cms的源码在http://www.opencms.org/opencms/en/download/opencms.html处下载。Sourcpath是你下载OpenCms 5.0.1 Source Distribution解压后的目录。
 |