KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opencms > setup > CmsUpdateThread


1 /*
2  * File : $Source: /usr/local/cvs/opencms/src/org/opencms/setup/CmsUpdateThread.java,v $
3  * Date : $Date: 2006/03/27 14:52:51 $
4  * Version: $Revision: 1.4 $
5  *
6  * This library is part of OpenCms -
7  * the Open Source Content Mananagement System
8  *
9  * Copyright (c) 2005 Alkacon Software GmbH (http://www.alkacon.com)
10  *
11  * This library is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public
13  * License as published by the Free Software Foundation; either
14  * version 2.1 of the License, or (at your option) any later version.
15  *
16  * This library is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  * Lesser General Public License for more details.
20  *
21  * For further information about Alkacon Software GmbH, please see the
22  * company website: http://www.alkacon.com
23  *
24  * For further information about OpenCms, please see the
25  * project website: http://www.opencms.org
26  *
27  * You should have received a copy of the GNU Lesser General Public
28  * License along with this library; if not, write to the Free Software
29  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30  */

31
32 package org.opencms.setup;
33
34 import org.opencms.main.CmsLog;
35 import org.opencms.main.CmsShell;
36
37 import java.io.File JavaDoc;
38 import java.io.FileInputStream JavaDoc;
39 import java.io.FileNotFoundException JavaDoc;
40 import java.io.PipedOutputStream JavaDoc;
41 import java.io.PrintStream JavaDoc;
42
43 /**
44  * Used for the OpenCms workplace update wizard.<p>
45  *
46  * @author Michael Moossen
47  *
48  * @version $Revision: 1.4 $
49  *
50  * @since 6.0.0
51  */

52 public class CmsUpdateThread extends Thread JavaDoc {
53
54     /** Gets the System.err stream so it can be restored. */
55     public static PrintStream JavaDoc m_tempErr;
56
57     /** Logging thread. */
58     private CmsSetupLoggingThread m_loggingThread;
59
60     /** System.out and System.err are redirected to this stream. */
61     private PipedOutputStream JavaDoc m_pipedOut;
62
63     /** The cms shell to import the workplace with. */
64     private CmsShell m_shell;
65
66     /** Gets the System.out stream so it can be restored. */
67     private PrintStream JavaDoc m_tempOut;
68
69     /** The additional shell commands, i.e. the setup bean. */
70     private CmsUpdateBean m_updateBean;
71
72     /**
73      * Constructor.<p>
74      *
75      * @param updateBean the initialized update bean
76      */

77     public CmsUpdateThread(CmsUpdateBean updateBean) {
78
79         super("OpenCms: Workplace update");
80
81         // store setup bean
82
m_updateBean = updateBean;
83         // init stream and logging thread
84
m_pipedOut = new PipedOutputStream JavaDoc();
85         m_loggingThread = new CmsSetupLoggingThread(m_pipedOut, m_updateBean.getLogName());
86     }
87
88     /**
89      * Returns the logging thread.<p>
90      *
91      * @return the logging thread
92      */

93     public CmsSetupLoggingThread getLoggingThread() {
94
95         return m_loggingThread;
96     }
97
98     /**
99      * Returns the status of the logging thread.<p>
100      *
101      * @return the status of the logging thread
102      */

103     public boolean isFinished() {
104
105         return m_loggingThread.isFinished();
106     }
107
108     /**
109      * Kills this Thread as well as the included logging Thread.<p>
110      */

111     public void kill() {
112
113         if (m_shell != null) {
114             m_shell.exit();
115         }
116         if (m_loggingThread != null) {
117             m_loggingThread.stopThread();
118         }
119         m_shell = null;
120         m_updateBean = null;
121     }
122
123     /**
124      * Write somthing to System.out during setup.<p>
125      *
126      * @param str the string to write
127      */

128     public void printToStdOut(String JavaDoc str) {
129
130         m_tempOut.println(str);
131     }
132
133     /**
134      * @see java.lang.Runnable#run()
135      */

136     public void run() {
137
138         // save the original out and err stream
139
m_tempOut = System.out;
140         m_tempErr = System.err;
141         try {
142             // redirect the streams
143
System.setOut(new PrintStream JavaDoc(m_pipedOut));
144             System.setErr(new PrintStream JavaDoc(m_pipedOut));
145
146             // start the logging thread
147
m_loggingThread.start();
148
149             // create a shell that will start importing the workplace
150
m_shell = new CmsShell(
151                 m_updateBean.getWebAppRfsPath() + "WEB-INF" + File.separator,
152                 m_updateBean.getServletMapping(),
153                 m_updateBean.getDefaultWebApplication(),
154                 "${user}@${project}>",
155                 m_updateBean);
156
157             try {
158                 try {
159                     if (CmsLog.INIT.isInfoEnabled()) {
160                         // log welcome message, the full package name is required because
161
// two different Message classes are used
162
CmsLog.INIT.info(org.opencms.main.Messages.get().getBundle().key(
163                             org.opencms.main.Messages.INIT_DOT_0));
164                         CmsLog.INIT.info(org.opencms.main.Messages.get().getBundle().key(
165                             org.opencms.main.Messages.INIT_DOT_0));
166                         CmsLog.INIT.info(org.opencms.main.Messages.get().getBundle().key(
167                             org.opencms.main.Messages.INIT_DOT_0));
168                         CmsLog.INIT.info(org.opencms.setup.Messages.get().getBundle().key(
169                             org.opencms.setup.Messages.INIT_WELCOME_UPDATE_0));
170                         CmsLog.INIT.info(org.opencms.setup.Messages.get().getBundle().key(
171                             org.opencms.setup.Messages.INIT_UPDATE_WORKPLACE_START_0));
172                         CmsLog.INIT.info(org.opencms.main.Messages.get().getBundle().key(
173                             org.opencms.main.Messages.INIT_DOT_0));
174                         CmsLog.INIT.info(org.opencms.main.Messages.get().getBundle().key(
175                             org.opencms.main.Messages.INIT_DOT_0));
176                         for (int i = 0; i < org.opencms.main.Messages.COPYRIGHT_BY_ALKACON.length; i++) {
177                             CmsLog.INIT.info(". " + org.opencms.main.Messages.COPYRIGHT_BY_ALKACON[i]);
178                         }
179                         CmsLog.INIT.info(org.opencms.main.Messages.get().getBundle().key(
180                             org.opencms.main.Messages.INIT_DOT_0));
181                         CmsLog.INIT.info(org.opencms.main.Messages.get().getBundle().key(
182                             org.opencms.main.Messages.INIT_DOT_0));
183                         CmsLog.INIT.info(org.opencms.main.Messages.get().getBundle().key(
184                             org.opencms.main.Messages.INIT_LINE_0));
185
186                     }
187                     m_shell.start(new FileInputStream JavaDoc(new File JavaDoc(m_updateBean.getWebAppRfsPath()
188                         + CmsUpdateBean.FOLDER_UPDATE
189                         + "cmsupdate.txt")));
190                     if (CmsLog.INIT.isInfoEnabled()) {
191                         CmsLog.INIT.info(org.opencms.setup.Messages.get().getBundle().key(
192                             org.opencms.setup.Messages.INIT_UPDATE_WORKPLACE_FINISHED_0));
193                     }
194                 } catch (FileNotFoundException JavaDoc e) {
195                     e.printStackTrace();
196                 }
197                 // stop the logging thread
198
kill();
199                 m_pipedOut.close();
200             } catch (Throwable JavaDoc t) {
201                 // ignore
202
}
203         } finally {
204             // restore to the old streams
205
System.setOut(m_tempOut);
206             System.setErr(m_tempErr);
207         }
208     }
209 }
Popular Tags