KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * File : $Source: /usr/local/cvs/opencms/src/org/opencms/setup/CmsSetupWorkplaceImportThread.java,v $
3  * Date : $Date: 2006/03/27 14:52:51 $
4  * Version: $Revision: 1.19 $
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 workplace setup in the OpenCms setup wizard.<p>
45  *
46  * @author Alexander Kandzior
47  *
48  * @version $Revision: 1.19 $
49  *
50  * @since 6.0.0
51  */

52 public class CmsSetupWorkplaceImportThread 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 additional shell commands, i.e. the setup bean. */
64     private CmsSetupBean m_setupBean;
65
66     /** The cms shell to import the workplace with. */
67     private CmsShell m_shell;
68
69     /** Gets the System.out stream so it can be restored. */
70     private PrintStream JavaDoc m_tempOut;
71
72     /** Flag to signalize if a workplace import is needed or not. */
73     private boolean m_workplaceImportNeeded;
74
75     /**
76      * Constructor.<p>
77      *
78      * @param setupBean the initialized setup bean
79      */

80     public CmsSetupWorkplaceImportThread(CmsSetupBean setupBean) {
81
82         super("OpenCms: Setup workplace import");
83
84         // store setup bean
85
m_setupBean = setupBean;
86         // init stream and logging thread
87
m_pipedOut = new PipedOutputStream JavaDoc();
88         m_loggingThread = new CmsSetupLoggingThread(m_pipedOut, m_setupBean.getLogName());
89         m_workplaceImportNeeded = !setupBean.getModulesToInstall().isEmpty();
90     }
91
92     /**
93      * Returns the logging thread.<p>
94      *
95      * @return the logging thread
96      */

97     public CmsSetupLoggingThread getLoggingThread() {
98
99         return m_loggingThread;
100     }
101
102     /**
103      * Returns the status of the logging thread.<p>
104      *
105      * @return the status of the logging thread
106      */

107     public boolean isFinished() {
108
109         return m_loggingThread.isFinished();
110     }
111
112     /**
113      * Kills this Thread as well as the included logging Thread.<p>
114      */

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

132     public void printToStdOut(String JavaDoc str) {
133
134         m_tempOut.println(str);
135     }
136
137     /**
138      * @see java.lang.Runnable#run()
139      */

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