KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opencms > main > CmsContextInfo


1 /*
2  * File : $Source: /usr/local/cvs/opencms/src/org/opencms/main/CmsContextInfo.java,v $
3  * Date : $Date: 2005/07/13 10:06:02 $
4  * Version: $Revision: 1.12 $
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.main;
33
34 import org.opencms.file.CmsProject;
35 import org.opencms.file.CmsRequestContext;
36 import org.opencms.file.CmsUser;
37 import org.opencms.i18n.CmsEncoder;
38 import org.opencms.i18n.CmsLocaleManager;
39
40 import java.util.Locale JavaDoc;
41
42 /**
43  * Contains user information for automated creation of a
44  * {@link org.opencms.file.CmsRequestContext} during system runtime.<p>
45  *
46  * @version $Revision: 1.12 $
47  *
48  * @since 6.0.0
49  */

50 public class CmsContextInfo {
51
52     /** Localhost ip used in fallback cases. */
53     public static final String JavaDoc LOCALHOST = "127.0.0.1";
54
55     /** The encoding to create the context with. */
56     private String JavaDoc m_encoding;
57
58     /** Indicates if the configuration if this context info can still be changed or not. */
59     private boolean m_frozen;
60
61     /** The locale to create the context with. */
62     private Locale JavaDoc m_locale;
63
64     /** The locale name to create the context with. */
65     private String JavaDoc m_localeName;
66
67     /** The project to create the context with. */
68     private CmsProject m_project;
69
70     /** The user name to create the context with. */
71     private String JavaDoc m_projectName;
72
73     /** The remote ip address to create the context with. */
74     private String JavaDoc m_remoteAddr;
75
76     /** The request URI to create the context with. */
77     private String JavaDoc m_requestedUri;
78
79     /** The site root to create the context with. */
80     private String JavaDoc m_siteRoot;
81
82     /** The user to create the context with. */
83     private CmsUser m_user;
84
85     /** The user name to create the context with. */
86     private String JavaDoc m_userName;
87
88     /**
89      * Creates a new instance, initializing the variables with some reasonable default values.<p>
90      *
91      * The default values are:<dl>
92      * <dt>User name</dt><dd>(configured default guest user)</dd>
93      * <dt>Project name</dt><dd>Online</dd>
94      * <dt>Requested URI</dt><dd>/</dd>
95      * <dt>Site root</dt><dd>/</dd>
96      * <dt>Locale name</dt><dd>(configured default locale name)</dd>
97      * <dt>Encoding</dt><dd>(configured default system encoding)</dd>
98      * <dt>Remote address</dt><dd>127.0.0.1</dd>
99      * </dl><p>
100      */

101     public CmsContextInfo() {
102
103         setUserName(OpenCms.getDefaultUsers().getUserGuest());
104         setProjectName(CmsProject.ONLINE_PROJECT_NAME);
105         setRequestedUri("/");
106         setSiteRoot("/");
107         setLocaleName(CmsLocaleManager.getDefaultLocale().toString());
108         setEncoding(OpenCms.getSystemInfo().getDefaultEncoding());
109         setRemoteAddr(CmsContextInfo.LOCALHOST);
110     }
111
112     /**
113      * Creates a new instance with all context variables initialized from the given request context.<p>
114      *
115      * @param requestContext the request context to initialize this context info with
116      */

117     public CmsContextInfo(CmsRequestContext requestContext) {
118
119         setUserName(requestContext.currentUser().getName());
120         setProjectName(requestContext.currentProject().getName());
121         setRequestedUri(requestContext.getUri());
122         setSiteRoot(requestContext.getSiteRoot());
123         setLocale(requestContext.getLocale());
124         setEncoding(requestContext.getEncoding());
125         setRemoteAddr(requestContext.getRemoteAddress());
126     }
127
128     /**
129      * Creates a new instance with all context variables initialized.<p>
130      *
131      * @param user the user to create the context with
132      * @param project the project to create the context with
133      * @param requestedUri the request URI to create the context with
134      * @param siteRoot the site root to create the context with
135      * @param locale the locale to create the context with
136      * @param encoding the encoding to create the context with
137      * @param remoteAddr the remote ip address to create the context with
138      */

139     public CmsContextInfo(
140         CmsUser user,
141         CmsProject project,
142         String JavaDoc requestedUri,
143         String JavaDoc siteRoot,
144         Locale JavaDoc locale,
145         String JavaDoc encoding,
146         String JavaDoc remoteAddr) {
147
148         m_user = user;
149         setUserName(m_user.getName());
150         m_project = project;
151         setProjectName(m_project.getName());
152         setRequestedUri(requestedUri);
153         setSiteRoot(siteRoot);
154         setLocale(locale);
155         setEncoding(encoding);
156         setRemoteAddr(remoteAddr);
157     }
158
159     /**
160      * Creates a new instance, initializing the user name as provided and
161      * all other vaiables with the same default values as in {@link #CmsContextInfo()}.<p>
162      *
163      * @param userName the user name to create the context with
164      *
165      * @see #CmsContextInfo()
166      */

167     public CmsContextInfo(String JavaDoc userName) {
168
169         this();
170         setUserName(userName);
171     }
172
173     /**
174      * Creates a new instance with all context variables initialized.<p>
175      *
176      * @param userName the user name to create the context with
177      * @param projectName the project name to create the context with
178      * @param requestedUri the request URI to create the context with
179      * @param siteRoot the site root to create the context with
180      * @param localeName the locale name to create the context with
181      * @param encoding the encoding to create the context with
182      * @param remoteAddr the remote ip address to create the context with
183      */

184     public CmsContextInfo(
185         String JavaDoc userName,
186         String JavaDoc projectName,
187         String JavaDoc requestedUri,
188         String JavaDoc siteRoot,
189         String JavaDoc localeName,
190         String JavaDoc encoding,
191         String JavaDoc remoteAddr) {
192
193         super();
194         setUserName(userName);
195         setProjectName(projectName);
196         setRequestedUri(requestedUri);
197         setSiteRoot(siteRoot);
198         setLocaleName(localeName);
199         setEncoding(encoding);
200         setRemoteAddr(remoteAddr);
201     }
202
203     /**
204      * Creates a clone of this context info object.<p>
205      *
206      * @see java.lang.Object#clone()
207      */

208     public Object JavaDoc clone() {
209
210         CmsContextInfo result = new CmsContextInfo();
211         result.m_encoding = m_encoding;
212         result.m_frozen = false;
213         result.m_locale = m_locale;
214         result.m_localeName = m_localeName;
215         result.m_project = m_project;
216         result.m_projectName = m_projectName;
217         result.m_remoteAddr = m_remoteAddr;
218         result.m_requestedUri = m_requestedUri;
219         result.m_siteRoot = m_siteRoot;
220         result.m_user = m_user;
221         result.m_userName = m_userName;
222         return result;
223     }
224
225     /**
226      * Finalizes (freezes) the configuration of this context information.<p>
227      *
228      * After this entry has been frozen, any attempt to change the
229      * configuration of this context info with one of the "set..." methods
230      * will lead to a <code>RuntimeException</code>.<p>
231      */

232     public void freeze() {
233
234         m_frozen = true;
235     }
236
237     /**
238      * Returns the encoding.<p>
239      *
240      * @return the encoding
241      */

242     public String JavaDoc getEncoding() {
243
244         return m_encoding;
245     }
246
247     /**
248      * Returns the locale.<p>
249      *
250      * @return the locale
251      */

252     public Locale JavaDoc getLocale() {
253
254         return m_locale;
255     }
256
257     /**
258      * Returns the locale name.<p>
259      *
260      * @return the locale name
261      */

262     public String JavaDoc getLocaleName() {
263
264         return m_localeName;
265     }
266
267     /**
268      * Returns the project, or <code>null</code> if the project
269      * has not been configured.<p>
270      *
271      * If the project has not been configured, at last the
272      * project name will be available.<p>
273      *
274      * @return the project
275      *
276      * @see #getProjectName()
277      */

278     public CmsProject getProject() {
279
280         return m_project;
281     }
282
283     /**
284      * Returns the project name.<p>
285      *
286      * @return the project name
287      *
288      * @see #getProject()
289      */

290     public String JavaDoc getProjectName() {
291
292         return m_projectName;
293     }
294
295     /**
296      * Returns the remote ip address.<p>
297      *
298      * @return the remote ip address
299      */

300     public String JavaDoc getRemoteAddr() {
301
302         return m_remoteAddr;
303     }
304
305     /**
306      * Returns the requested uri.<p>
307      *
308      * @return the requested uri
309      */

310     public String JavaDoc getRequestedUri() {
311
312         return m_requestedUri;
313     }
314
315     /**
316      * Returns the siteroot.<p>
317      *
318      * @return the siteroot
319      */

320     public String JavaDoc getSiteRoot() {
321
322         return m_siteRoot;
323     }
324
325     /**
326      * Returns the user, or <code>null</code> if the user
327      * has not been configured.<p>
328      *
329      * If the user has not been configured, at last the
330      * user name will be available.<p>
331      *
332      * @return the user
333      *
334      * @see #getUserName()
335      */

336     public CmsUser getUser() {
337
338         return m_user;
339     }
340
341     /**
342      * Returns the username.<p>
343      *
344      * @return the username
345      *
346      * @see #getUser()
347      */

348     public String JavaDoc getUserName() {
349
350         return m_userName;
351     }
352
353     /**
354      * Sets the encoding.<p>
355      *
356      * @param encoding the encoding to set
357      */

358     public void setEncoding(String JavaDoc encoding) {
359
360         checkFrozen();
361         m_encoding = CmsEncoder.lookupEncoding(encoding, OpenCms.getSystemInfo().getDefaultEncoding());
362     }
363
364     /**
365      * Sets the locale.<p>
366      *
367      * Setting the locale name will override the currently selected locale
368      * and vice-versa. The locale name and the locale will always match.<p>
369      *
370      * @param locale the locale to set
371      *
372      * @see #setLocaleName(String)
373      */

374     public void setLocale(Locale JavaDoc locale) {
375
376         checkFrozen();
377         m_locale = locale;
378         m_localeName = m_locale.toString();
379     }
380
381     /**
382      * Sets the locale name.<p>
383      *
384      * Setting the locale name will override the currently selected locale
385      * and vice-versa. The locale name and the locale will always match.<p>
386      *
387      * @param localeName the locale name to set
388      *
389      * @see #setLocale(Locale)
390      */

391     public void setLocaleName(String JavaDoc localeName) {
392
393         checkFrozen();
394         m_localeName = localeName;
395         m_locale = CmsLocaleManager.getLocale(localeName);
396     }
397
398     /**
399      * Sets the project name.<p>
400      *
401      * @param projectName the project name to set
402      */

403     public void setProjectName(String JavaDoc projectName) {
404
405         checkFrozen();
406         m_projectName = projectName;
407     }
408
409     /**
410      * Sets the remote ip address.<p>
411      *
412      * @param remoteAddr the remote ip address
413      */

414     public void setRemoteAddr(String JavaDoc remoteAddr) {
415
416         checkFrozen();
417         m_remoteAddr = remoteAddr;
418     }
419
420     /**
421      * Sets the requested uri.<p>
422      *
423      * @param requestedUri the requested uri to set
424      */

425     public void setRequestedUri(String JavaDoc requestedUri) {
426
427         checkFrozen();
428         m_requestedUri = requestedUri;
429     }
430
431     /**
432      * Sets the siteroot.<p>
433      *
434      * @param siteRoot the siteroot to set
435      */

436     public void setSiteRoot(String JavaDoc siteRoot) {
437
438         checkFrozen();
439         m_siteRoot = siteRoot;
440     }
441
442     /**
443      * Sets the username.<p>
444      *
445      * @param userName the username to set
446      */

447     public void setUserName(String JavaDoc userName) {
448
449         checkFrozen();
450         m_userName = userName;
451     }
452
453     /**
454      * Checks if this context info configuration is frozen.<p>
455      *
456      * @throws CmsRuntimeException in case the configuration is already frozen
457      */

458     protected void checkFrozen() throws CmsRuntimeException {
459
460         if (m_frozen) {
461             throw new CmsRuntimeException(Messages.get().container(Messages.ERR_CONTEXT_INFO_FROZEN_0));
462         }
463     }
464 }
Popular Tags