2 * Copyright (C) 2001, 2002 The Mir-coders group
\r
4 * This file is part of Mir.
\r
6 * Mir is free software; you can redistribute it and/or modify
\r
7 * it under the terms of the GNU General Public License as published by
\r
8 * the Free Software Foundation; either version 2 of the License, or
\r
9 * (at your option) any later version.
\r
11 * Mir is distributed in the hope that it will be useful,
\r
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
14 * GNU General Public License for more details.
\r
16 * You should have received a copy of the GNU General Public License
\r
17 * along with Mir; if not, write to the Free Software
\r
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
\r
20 * In addition, as a special exception, The Mir-coders gives permission to link
\r
21 * the code of this program with any library licensed under the Apache Software License,
\r
22 * The Sun (tm) Java Advanced Imaging library (JAI), The Sun JIMI library
\r
23 * (or with modified versions of the above that use the same license as the above),
\r
24 * and distribute linked combinations including the two. You must obey the
\r
25 * GNU General Public License in all respects for all of the code used other than
\r
26 * the above mentioned libraries. If you modify this file, you may extend this
\r
27 * exception to your version of the file, but you are not obligated to do so.
\r
28 * If you do not wish to do so, delete this exception statement from your version.
\r
31 package mircoders.global;
\r
35 import mir.config.MirPropertiesConfiguration;
\r
36 import mir.config.MirPropertiesConfiguration.PropertiesConfigExc;
\r
37 import mir.misc.ConfigException;
\r
38 import mircoders.localizer.MirCachingLocalizerDecorator;
\r
39 import mircoders.localizer.*;
\r
40 import mircoders.accesscontrol.*;
\r
41 import mircoders.entity.*;
\r
42 import mir.entity.adapter.*;
\r
44 public class MirGlobal {
\r
45 static private MirPropertiesConfiguration configuration;
\r
46 static private MirLocalizer localizer;
\r
47 static private ProducerEngine producerEngine;
\r
48 static private Abuse abuse;
\r
49 static private MRUCache mruCache;
\r
50 static private AccessControl accessControl;
\r
51 static private Map articleOperations;
\r
52 static private Map commentOperations;
\r
53 static private Map loggedInUsers = new HashMap();
\r
55 public synchronized static MirLocalizer localizer() {
\r
56 String localizerClassName;
\r
57 Class localizerClass;
\r
59 if (localizer == null ) {
\r
60 localizerClassName = config().getString("Mir.Localizer", "mirlocal.localizer.basic.MirBasicLocalizer");
\r
63 localizerClass = Class.forName(localizerClassName);
\r
65 catch (Throwable t) {
\r
66 throw new ConfigException("localizer class '" + localizerClassName + "' not found: " + t.toString());
\r
69 if (!(MirLocalizer.class.isAssignableFrom(localizerClass)))
\r
70 throw new ConfigException("localizer class '" + localizerClassName + "' is not assignable from MirLocalizer");
\r
73 localizer = new MirCachingLocalizerDecorator((MirLocalizer) localizerClass.newInstance());
\r
75 catch (Throwable t) {
\r
76 throw new ConfigException("localizer class '" + localizerClassName + "' cannot be instantiated: " + t.toString());
\r
83 public static Abuse abuse() {
\r
85 synchronized(MirGlobal.class) {
\r
87 abuse = new Abuse();
\r
94 public static MirPropertiesConfiguration config() {
\r
96 return MirPropertiesConfiguration.instance();
\r
98 catch (PropertiesConfigExc e) {
\r
99 throw new RuntimeException(e.getMessage());
\r
103 public static ProducerEngine producerEngine() {
\r
104 if (producerEngine == null) {
\r
105 producerEngine = new ProducerEngine();
\r
108 return producerEngine;
\r
111 public static MRUCache mruCache() {
\r
112 synchronized(MirGlobal.class) {
\r
113 if (mruCache == null) {
\r
114 mruCache = new MRUCache();
\r
120 public static synchronized AccessControl accessControl() {
\r
121 if (accessControl == null) {
\r
122 accessControl=new AccessControl();
\r
125 return accessControl;
\r
128 public static void performArticleOperation(EntityUsers aUser, EntityContent anArticle, String anOperation) {
\r
129 MirAdminInterfaceLocalizer.MirSimpleEntityOperation operation = getArticleOperationForName(anOperation);
\r
132 if (operation!=null)
\r
134 localizer().dataModel().adapterModel().makeEntityAdapter("user", aUser),
\r
135 localizer().dataModel().adapterModel().makeEntityAdapter("content", anArticle));
\r
137 catch (Throwable t) {
\r
138 throw new RuntimeException(t.toString());
\r
142 public static void performCommentOperation(EntityUsers aUser, EntityComment aComment, String anOperation) {
\r
143 MirAdminInterfaceLocalizer.MirSimpleEntityOperation operation = getCommentOperationForName(anOperation);
\r
146 if (operation!=null)
\r
148 localizer().dataModel().adapterModel().makeEntityAdapter("user", aUser),
\r
149 localizer().dataModel().adapterModel().makeEntityAdapter("comment", aComment));
\r
151 catch (Throwable t) {
\r
152 throw new RuntimeException(t.toString());
\r
156 private synchronized static MirAdminInterfaceLocalizer.MirSimpleEntityOperation getArticleOperationForName(String aName) {
\r
158 if (articleOperations == null) {
\r
159 articleOperations = new HashMap();
\r
160 Iterator i = localizer().adminInterface().simpleArticleOperations().iterator();
\r
161 while (i.hasNext()) {
\r
162 MirAdminInterfaceLocalizer.MirSimpleEntityOperation operation = (MirAdminInterfaceLocalizer.MirSimpleEntityOperation) i.next();
\r
163 articleOperations.put(operation.getName(), operation);
\r
167 return (MirAdminInterfaceLocalizer.MirSimpleEntityOperation) articleOperations.get(aName);
\r
169 catch (Throwable t) {
\r
170 throw new RuntimeException(t.toString());
\r
174 private synchronized static MirAdminInterfaceLocalizer.MirSimpleEntityOperation getCommentOperationForName(String aName) {
\r
176 if (commentOperations == null) {
\r
177 commentOperations = new HashMap();
\r
178 Iterator i = localizer().adminInterface().simpleCommentOperations().iterator();
\r
179 while (i.hasNext()) {
\r
180 MirAdminInterfaceLocalizer.MirSimpleEntityOperation operation = (MirAdminInterfaceLocalizer.MirSimpleEntityOperation) i.next();
\r
181 commentOperations.put(operation.getName(), operation);
\r
185 return (MirAdminInterfaceLocalizer.MirSimpleEntityOperation) commentOperations.get(aName);
\r
187 catch (Throwable t) {
\r
188 throw new RuntimeException(t.toString());
\r
193 public static List getLoggedInUsers() {
\r
194 List result = new Vector();
\r
196 synchronized (loggedInUsers) {
\r
197 Iterator i = loggedInUsers.entrySet().iterator();
\r
199 while (i.hasNext()) {
\r
200 Map.Entry entry = (Map.Entry) i.next();
\r
202 Map item = new HashMap();
\r
203 item.put("name", entry.getKey());
\r
204 item.put("count", entry.getValue());
\r
212 public static void registerLogin(String aName) {
\r
213 modifyLoggedInCount(aName, 1);
\r
216 public static void registerLogout(String aName) {
\r
217 modifyLoggedInCount(aName, -1);
\r
220 private static void modifyLoggedInCount(String aName, int aModifier) {
\r
221 synchronized (loggedInUsers) {
\r
222 Integer count = (Integer) loggedInUsers.get(aName);
\r
224 count = new Integer(0);
\r
226 if (count.intValue()+aModifier<=0) {
\r
227 loggedInUsers.remove(aName);
\r
230 loggedInUsers.put(aName, new Integer(count.intValue() + aModifier));
\r